MorphNode.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import Node, { addNodeClass } from '../core/Node.js';
  2. import { NodeUpdateType } from '../core/constants.js';
  3. import { nodeProxy, tslFn } from '../shadernode/ShaderNode.js';
  4. import { uniform } from '../core/UniformNode.js';
  5. import { referenceIndex } from './ReferenceNode.js';
  6. import { positionLocal } from './PositionNode.js';
  7. import { normalLocal } from './NormalNode.js';
  8. import { textureLoad } from './TextureNode.js';
  9. import { vertexIndex } from '../core/IndexNode.js';
  10. import { ivec2, int } from '../shadernode/ShaderNode.js';
  11. import { DataArrayTexture, Vector2, Vector4, FloatType } from 'three';
  12. const morphTextures = new WeakMap();
  13. const morphVec4 = new Vector4();
  14. const getMorph = tslFn( ( { bufferMap, influence, stride, width, depth, offset } ) => {
  15. const texelIndex = int( vertexIndex ).mul( stride ).add( offset );
  16. const y = texelIndex.div( width );
  17. const x = texelIndex.sub( y.mul( width ) );
  18. const bufferAttrib = textureLoad( bufferMap, ivec2( x, y ) ).depth( depth );
  19. return bufferAttrib.mul( influence );
  20. } );
  21. function getEntry( geometry ) {
  22. const hasMorphPosition = geometry.morphAttributes.position !== undefined;
  23. const hasMorphNormals = geometry.morphAttributes.normal !== undefined;
  24. const hasMorphColors = geometry.morphAttributes.color !== undefined;
  25. // instead of using attributes, the WebGL 2 code path encodes morph targets
  26. // into an array of data textures. Each layer represents a single morph target.
  27. const morphAttribute = geometry.morphAttributes.position || geometry.morphAttributes.normal || geometry.morphAttributes.color;
  28. const morphTargetsCount = ( morphAttribute !== undefined ) ? morphAttribute.length : 0;
  29. let entry = morphTextures.get( geometry );
  30. if ( entry === undefined || entry.count !== morphTargetsCount ) {
  31. if ( entry !== undefined ) entry.texture.dispose();
  32. const morphTargets = geometry.morphAttributes.position || [];
  33. const morphNormals = geometry.morphAttributes.normal || [];
  34. const morphColors = geometry.morphAttributes.color || [];
  35. let vertexDataCount = 0;
  36. if ( hasMorphPosition === true ) vertexDataCount = 1;
  37. if ( hasMorphNormals === true ) vertexDataCount = 2;
  38. if ( hasMorphColors === true ) vertexDataCount = 3;
  39. let width = geometry.attributes.position.count * vertexDataCount;
  40. let height = 1;
  41. const maxTextureSize = 4096; // @TODO: Use 'capabilities.maxTextureSize'
  42. if ( width > maxTextureSize ) {
  43. height = Math.ceil( width / maxTextureSize );
  44. width = maxTextureSize;
  45. }
  46. const buffer = new Float32Array( width * height * 4 * morphTargetsCount );
  47. const bufferTexture = new DataArrayTexture( buffer, width, height, morphTargetsCount );
  48. bufferTexture.type = FloatType;
  49. bufferTexture.needsUpdate = true;
  50. // fill buffer
  51. const vertexDataStride = vertexDataCount * 4;
  52. for ( let i = 0; i < morphTargetsCount; i ++ ) {
  53. const morphTarget = morphTargets[ i ];
  54. const morphNormal = morphNormals[ i ];
  55. const morphColor = morphColors[ i ];
  56. const offset = width * height * 4 * i;
  57. for ( let j = 0; j < morphTarget.count; j ++ ) {
  58. const stride = j * vertexDataStride;
  59. if ( hasMorphPosition === true ) {
  60. morphVec4.fromBufferAttribute( morphTarget, j );
  61. buffer[ offset + stride + 0 ] = morphVec4.x;
  62. buffer[ offset + stride + 1 ] = morphVec4.y;
  63. buffer[ offset + stride + 2 ] = morphVec4.z;
  64. buffer[ offset + stride + 3 ] = 0;
  65. }
  66. if ( hasMorphNormals === true ) {
  67. morphVec4.fromBufferAttribute( morphNormal, j );
  68. buffer[ offset + stride + 4 ] = morphVec4.x;
  69. buffer[ offset + stride + 5 ] = morphVec4.y;
  70. buffer[ offset + stride + 6 ] = morphVec4.z;
  71. buffer[ offset + stride + 7 ] = 0;
  72. }
  73. if ( hasMorphColors === true ) {
  74. morphVec4.fromBufferAttribute( morphColor, j );
  75. buffer[ offset + stride + 8 ] = morphVec4.x;
  76. buffer[ offset + stride + 9 ] = morphVec4.y;
  77. buffer[ offset + stride + 10 ] = morphVec4.z;
  78. buffer[ offset + stride + 11 ] = ( morphColor.itemSize === 4 ) ? morphVec4.w : 1;
  79. }
  80. }
  81. }
  82. entry = {
  83. count: morphTargetsCount,
  84. texture: bufferTexture,
  85. stride: vertexDataCount,
  86. size: new Vector2( width, height )
  87. };
  88. morphTextures.set( geometry, entry );
  89. function disposeTexture() {
  90. bufferTexture.dispose();
  91. morphTextures.delete( geometry );
  92. geometry.removeEventListener( 'dispose', disposeTexture );
  93. }
  94. geometry.addEventListener( 'dispose', disposeTexture );
  95. }
  96. return entry;
  97. }
  98. class MorphNode extends Node {
  99. constructor( mesh ) {
  100. super( 'void' );
  101. this.mesh = mesh;
  102. this.morphBaseInfluence = uniform( 1 );
  103. this.updateType = NodeUpdateType.OBJECT;
  104. }
  105. setup( builder ) {
  106. const { geometry } = builder;
  107. const hasMorphPosition = geometry.morphAttributes.position !== undefined;
  108. const hasMorphNormals = geometry.morphAttributes.normal !== undefined;
  109. const morphAttribute = geometry.morphAttributes.position || geometry.morphAttributes.normal || geometry.morphAttributes.color;
  110. const morphTargetsCount = ( morphAttribute !== undefined ) ? morphAttribute.length : 0;
  111. // nodes
  112. const { texture: bufferMap, stride, size } = getEntry( geometry );
  113. if ( hasMorphPosition === true ) positionLocal.mulAssign( this.morphBaseInfluence );
  114. if ( hasMorphNormals === true ) normalLocal.mulAssign( this.morphBaseInfluence );
  115. const width = int( size.width );
  116. for ( let i = 0; i < morphTargetsCount; i ++ ) {
  117. const influence = referenceIndex( 'morphTargetInfluences', i, 'float' );
  118. const depth = int( i );
  119. if ( hasMorphPosition === true ) {
  120. positionLocal.addAssign( getMorph( {
  121. bufferMap,
  122. influence,
  123. stride,
  124. width,
  125. depth,
  126. offset: int( 0 )
  127. } ) );
  128. }
  129. if ( hasMorphNormals === true ) {
  130. normalLocal.addAssign( getMorph( {
  131. bufferMap,
  132. influence,
  133. stride,
  134. width,
  135. depth,
  136. offset: int( 1 )
  137. } ) );
  138. }
  139. }
  140. }
  141. update() {
  142. const morphBaseInfluence = this.morphBaseInfluence;
  143. if ( this.mesh.geometry.morphTargetsRelative ) {
  144. morphBaseInfluence.value = 1;
  145. } else {
  146. morphBaseInfluence.value = 1 - this.mesh.morphTargetInfluences.reduce( ( a, b ) => a + b, 0 );
  147. }
  148. }
  149. }
  150. export default MorphNode;
  151. export const morph = nodeProxy( MorphNode );
  152. addNodeClass( 'MorphNode', MorphNode );