MorphNode.js 6.5 KB

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