SkinningNode.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import Node, { addNodeClass } from '../core/Node.js';
  2. import { NodeUpdateType } from '../core/constants.js';
  3. import { nodeProxy } from '../shadernode/ShaderNode.js';
  4. import { attribute } from '../core/AttributeNode.js';
  5. import { uniform } from '../core/UniformNode.js';
  6. import { add } from '../math/OperatorNode.js';
  7. import { buffer } from './BufferNode.js';
  8. import { normalLocal } from './NormalNode.js';
  9. import { positionLocal } from './PositionNode.js';
  10. import { tangentLocal } from './TangentNode.js';
  11. class SkinningNode extends Node {
  12. constructor( skinnedMesh ) {
  13. super( 'void' );
  14. this.skinnedMesh = skinnedMesh;
  15. this.updateType = NodeUpdateType.OBJECT;
  16. //
  17. this.skinIndexNode = attribute( 'skinIndex', 'uvec4' );
  18. this.skinWeightNode = attribute( 'skinWeight', 'vec4' );
  19. this.bindMatrixNode = uniform( skinnedMesh.bindMatrix, 'mat4' );
  20. this.bindMatrixInverseNode = uniform( skinnedMesh.bindMatrixInverse, 'mat4' );
  21. this.boneMatricesNode = buffer( skinnedMesh.skeleton.boneMatrices, 'mat4', skinnedMesh.skeleton.bones.length );
  22. }
  23. setup( builder ) {
  24. const { skinIndexNode, skinWeightNode, bindMatrixNode, bindMatrixInverseNode, boneMatricesNode } = this;
  25. const boneMatX = boneMatricesNode.element( skinIndexNode.x );
  26. const boneMatY = boneMatricesNode.element( skinIndexNode.y );
  27. const boneMatZ = boneMatricesNode.element( skinIndexNode.z );
  28. const boneMatW = boneMatricesNode.element( skinIndexNode.w );
  29. // POSITION
  30. const skinVertex = bindMatrixNode.mul( positionLocal );
  31. const skinned = add(
  32. boneMatX.mul( skinWeightNode.x ).mul( skinVertex ),
  33. boneMatY.mul( skinWeightNode.y ).mul( skinVertex ),
  34. boneMatZ.mul( skinWeightNode.z ).mul( skinVertex ),
  35. boneMatW.mul( skinWeightNode.w ).mul( skinVertex )
  36. );
  37. const skinPosition = bindMatrixInverseNode.mul( skinned ).xyz;
  38. // NORMAL
  39. let skinMatrix = add(
  40. skinWeightNode.x.mul( boneMatX ),
  41. skinWeightNode.y.mul( boneMatY ),
  42. skinWeightNode.z.mul( boneMatZ ),
  43. skinWeightNode.w.mul( boneMatW )
  44. );
  45. skinMatrix = bindMatrixInverseNode.mul( skinMatrix ).mul( bindMatrixNode );
  46. const skinNormal = skinMatrix.transformDirection( normalLocal ).xyz;
  47. // ASSIGNS
  48. positionLocal.assign( skinPosition );
  49. normalLocal.assign( skinNormal );
  50. if ( builder.hasGeometryAttribute( 'tangent' ) ) {
  51. tangentLocal.assign( skinNormal );
  52. }
  53. }
  54. generate( builder, output ) {
  55. if ( output !== 'void' ) {
  56. return positionLocal.build( builder, output );
  57. }
  58. }
  59. update() {
  60. this.skinnedMesh.skeleton.update();
  61. }
  62. }
  63. export default SkinningNode;
  64. export const skinning = nodeProxy( SkinningNode );
  65. addNodeClass( 'SkinningNode', SkinningNode );