11import Node , { addNodeClass } from '../core/Node.js' ;
22import { NodeUpdateType } from '../core/constants.js' ;
3- import { ShaderNode , nodeProxy } from '../shadernode/ShaderNode.js' ;
3+ import { nodeProxy } from '../shadernode/ShaderNode.js' ;
44import { attribute } from '../core/AttributeNode.js' ;
55import { uniform } from '../core/UniformNode.js' ;
66import { add } from '../math/OperatorNode.js' ;
@@ -9,91 +9,72 @@ import { normalLocal } from './NormalNode.js';
99import { positionLocal } from './PositionNode.js' ;
1010import { tangentLocal } from './TangentNode.js' ;
1111
12- const Skinning = new ShaderNode ( ( inputs , { } , builder ) => {
12+ class SkinningNode extends Node {
1313
14- const { index , weight , bindMatrix , bindMatrixInverse , boneMatrices } = inputs ;
14+ constructor ( skinnedMesh ) {
1515
16- const boneMatX = boneMatrices . element ( index . x ) ;
17- const boneMatY = boneMatrices . element ( index . y ) ;
18- const boneMatZ = boneMatrices . element ( index . z ) ;
19- const boneMatW = boneMatrices . element ( index . w ) ;
16+ super ( 'void' ) ;
2017
21- // POSITION
18+ this . skinnedMesh = skinnedMesh ;
2219
23- const skinVertex = bindMatrix . mul ( positionLocal ) ;
20+ this . updateType = NodeUpdateType . OBJECT ;
2421
25- const skinned = add (
26- boneMatX . mul ( weight . x ) . mul ( skinVertex ) ,
27- boneMatY . mul ( weight . y ) . mul ( skinVertex ) ,
28- boneMatZ . mul ( weight . z ) . mul ( skinVertex ) ,
29- boneMatW . mul ( weight . w ) . mul ( skinVertex )
30- ) ;
22+ //
3123
32- const skinPosition = bindMatrixInverse . mul ( skinned ) . xyz ;
24+ this . skinIndexNode = attribute ( 'skinIndex' , 'uvec4' ) ;
25+ this . skinWeightNode = attribute ( 'skinWeight' , 'vec4' ) ;
3326
34- // NORMAL
27+ this . bindMatrixNode = uniform ( skinnedMesh . bindMatrix , 'mat4' ) ;
28+ this . bindMatrixInverseNode = uniform ( skinnedMesh . bindMatrixInverse , 'mat4' ) ;
29+ this . boneMatricesNode = buffer ( skinnedMesh . skeleton . boneMatrices , 'mat4' , skinnedMesh . skeleton . bones . length ) ;
3530
36- let skinMatrix = add (
37- weight . x . mul ( boneMatX ) ,
38- weight . y . mul ( boneMatY ) ,
39- weight . z . mul ( boneMatZ ) ,
40- weight . w . mul ( boneMatW )
41- ) ;
31+ }
4232
43- skinMatrix = bindMatrixInverse . mul ( skinMatrix ) . mul ( bindMatrix ) ;
33+ construct ( builder ) {
4434
45- const skinNormal = skinMatrix . transformDirection ( normalLocal ) . xyz ;
35+ const { skinIndexNode , skinWeightNode , bindMatrixNode , bindMatrixInverseNode , boneMatricesNode } = this ;
4636
47- // ASSIGNS
37+ const boneMatX = boneMatricesNode . element ( skinIndexNode . x ) ;
38+ const boneMatY = boneMatricesNode . element ( skinIndexNode . y ) ;
39+ const boneMatZ = boneMatricesNode . element ( skinIndexNode . z ) ;
40+ const boneMatW = boneMatricesNode . element ( skinIndexNode . w ) ;
4841
49- positionLocal . assign ( skinPosition ) . build ( builder ) ; // @TODO : For some reason this doesn't work as stack.assign( positionLocal, skinPosition )?
50- normalLocal . assign ( skinNormal ) . build ( builder ) ;
42+ // POSITION
5143
52- if ( builder . hasGeometryAttribute ( 'tangent' ) ) {
44+ const skinVertex = bindMatrixNode . mul ( positionLocal ) ;
5345
54- tangentLocal . assign ( skinNormal ) . build ( builder ) ;
46+ const skinned = add (
47+ boneMatX . mul ( skinWeightNode . x ) . mul ( skinVertex ) ,
48+ boneMatY . mul ( skinWeightNode . y ) . mul ( skinVertex ) ,
49+ boneMatZ . mul ( skinWeightNode . z ) . mul ( skinVertex ) ,
50+ boneMatW . mul ( skinWeightNode . w ) . mul ( skinVertex )
51+ ) ;
5552
56- }
53+ const skinPosition = bindMatrixInverseNode . mul ( skinned ) . xyz ;
5754
58- } ) ;
55+ // NORMAL
5956
60- class SkinningNode extends Node {
57+ let skinMatrix = add (
58+ skinWeightNode . x . mul ( boneMatX ) ,
59+ skinWeightNode . y . mul ( boneMatY ) ,
60+ skinWeightNode . z . mul ( boneMatZ ) ,
61+ skinWeightNode . w . mul ( boneMatW )
62+ ) ;
6163
62- constructor ( skinnedMesh ) {
64+ skinMatrix = bindMatrixInverseNode . mul ( skinMatrix ) . mul ( bindMatrixNode ) ;
6365
64- super ( 'void' ) ;
66+ const skinNormal = skinMatrix . transformDirection ( normalLocal ) . xyz ;
6567
66- this . skinnedMesh = skinnedMesh ;
68+ // ASSIGNS
6769
68- this . updateType = NodeUpdateType . OBJECT ;
70+ builder . stack . assign ( positionLocal , skinPosition ) ;
71+ builder . stack . assign ( normalLocal , skinNormal ) ;
6972
70- //
73+ if ( builder . hasGeometryAttribute ( 'tangent' ) ) {
7174
72- this . skinIndexNode = attribute ( 'skinIndex' , 'uvec4' ) ;
73- this . skinWeightNode = attribute ( 'skinWeight' , 'vec4' ) ;
74-
75- this . bindMatrixNode = uniform ( skinnedMesh . bindMatrix , 'mat4' ) ;
76- this . bindMatrixInverseNode = uniform ( skinnedMesh . bindMatrixInverse , 'mat4' ) ;
77- this . boneMatricesNode = buffer ( skinnedMesh . skeleton . boneMatrices , 'mat4' , skinnedMesh . skeleton . bones . length ) ;
78-
79- }
75+ builder . stack . assign ( tangentLocal , skinNormal ) ;
8076
81- generate ( builder ) {
82-
83- /*return new ShaderNode( ( {}, stack, builder ) => Skinning.call( {
84- index: this.skinIndexNode,
85- weight: this.skinWeightNode,
86- bindMatrix: this.bindMatrixNode,
87- bindMatrixInverse: this.bindMatrixInverseNode,
88- boneMatrices: this.boneMatricesNode
89- }, stack, builder ) ).build( builder );*/
90- Skinning . call ( {
91- index : this . skinIndexNode ,
92- weight : this . skinWeightNode ,
93- bindMatrix : this . bindMatrixNode ,
94- bindMatrixInverse : this . bindMatrixInverseNode ,
95- boneMatrices : this . boneMatricesNode
96- } , { } , builder ) ;
77+ }
9778
9879 }
9980
0 commit comments