WebGLMorphtargets.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. import { FloatType, RGBAFormat } from '../../constants.js';
  2. import { DataTexture2DArray } from '../../textures/DataTexture2DArray.js';
  3. import { Vector3 } from '../../math/Vector3.js';
  4. import { Vector2 } from '../../math/Vector2.js';
  5. function numericalSort( a, b ) {
  6. return a[ 0 ] - b[ 0 ];
  7. }
  8. function absNumericalSort( a, b ) {
  9. return Math.abs( b[ 1 ] ) - Math.abs( a[ 1 ] );
  10. }
  11. function denormalize( morph, attribute ) {
  12. let denominator = 1;
  13. const array = attribute.isInterleavedBufferAttribute ? attribute.data.array : attribute.array;
  14. if ( array instanceof Int8Array ) denominator = 127;
  15. else if ( array instanceof Int16Array ) denominator = 32767;
  16. else if ( array instanceof Int32Array ) denominator = 2147483647;
  17. else console.error( 'THREE.WebGLMorphtargets: Unsupported morph attribute data type: ', array );
  18. morph.divideScalar( denominator );
  19. }
  20. function WebGLMorphtargets( gl, capabilities, textures ) {
  21. const influencesList = {};
  22. const morphInfluences = new Float32Array( 8 );
  23. const morphTextures = new WeakMap();
  24. const morph = new Vector3();
  25. const workInfluences = [];
  26. for ( let i = 0; i < 8; i ++ ) {
  27. workInfluences[ i ] = [ i, 0 ];
  28. }
  29. function update( object, geometry, material, program ) {
  30. const objectInfluences = object.morphTargetInfluences;
  31. if ( capabilities.isWebGL2 === true ) {
  32. // instead of using attributes, the WebGL 2 code path encodes morph targets
  33. // into an array of data textures. Each layer represents a single morph target.
  34. const numberOfMorphTargets = geometry.morphAttributes.position.length;
  35. let entry = morphTextures.get( geometry );
  36. if ( entry === undefined || entry.count !== numberOfMorphTargets ) {
  37. if ( entry !== undefined ) entry.texture.dispose();
  38. const hasMorphNormals = geometry.morphAttributes.normal !== undefined;
  39. const morphTargets = geometry.morphAttributes.position;
  40. const morphNormals = geometry.morphAttributes.normal || [];
  41. const numberOfVertices = geometry.attributes.position.count;
  42. const numberOfVertexData = ( hasMorphNormals === true ) ? 2 : 1; // (v,n) vs. (v)
  43. let width = numberOfVertices * numberOfVertexData;
  44. let height = 1;
  45. if ( width > capabilities.maxTextureSize ) {
  46. height = Math.ceil( width / capabilities.maxTextureSize );
  47. width = capabilities.maxTextureSize;
  48. }
  49. const buffer = new Float32Array( width * height * 4 * numberOfMorphTargets );
  50. const texture = new DataTexture2DArray( buffer, width, height, numberOfMorphTargets );
  51. texture.format = RGBAFormat; // using RGBA since RGB might be emulated (and is thus slower)
  52. texture.type = FloatType;
  53. // fill buffer
  54. const vertexDataStride = numberOfVertexData * 4;
  55. for ( let i = 0; i < numberOfMorphTargets; i ++ ) {
  56. const morphTarget = morphTargets[ i ];
  57. const morphNormal = morphNormals[ i ];
  58. const offset = width * height * 4 * i;
  59. for ( let j = 0; j < morphTarget.count; j ++ ) {
  60. morph.fromBufferAttribute( morphTarget, j );
  61. if ( morphTarget.normalized === true ) denormalize( morph, morphTarget );
  62. const stride = j * vertexDataStride;
  63. buffer[ offset + stride + 0 ] = morph.x;
  64. buffer[ offset + stride + 1 ] = morph.y;
  65. buffer[ offset + stride + 2 ] = morph.z;
  66. buffer[ offset + stride + 3 ] = 0;
  67. if ( hasMorphNormals === true ) {
  68. morph.fromBufferAttribute( morphNormal, j );
  69. if ( morphNormal.normalized === true ) denormalize( morph, morphNormal );
  70. buffer[ offset + stride + 4 ] = morph.x;
  71. buffer[ offset + stride + 5 ] = morph.y;
  72. buffer[ offset + stride + 6 ] = morph.z;
  73. buffer[ offset + stride + 7 ] = 0;
  74. }
  75. }
  76. }
  77. entry = {
  78. count: numberOfMorphTargets,
  79. texture: texture,
  80. size: new Vector2( width, height )
  81. };
  82. morphTextures.set( geometry, entry );
  83. }
  84. //
  85. let morphInfluencesSum = 0;
  86. for ( let i = 0; i < objectInfluences.length; i ++ ) {
  87. morphInfluencesSum += objectInfluences[ i ];
  88. }
  89. const morphBaseInfluence = geometry.morphTargetsRelative ? 1 : 1 - morphInfluencesSum;
  90. program.getUniforms().setValue( gl, 'morphTargetBaseInfluence', morphBaseInfluence );
  91. program.getUniforms().setValue( gl, 'morphTargetInfluences', objectInfluences );
  92. program.getUniforms().setValue( gl, 'morphTargetsTexture', entry.texture, textures );
  93. program.getUniforms().setValue( gl, 'morphTargetsTextureSize', entry.size );
  94. } else {
  95. // When object doesn't have morph target influences defined, we treat it as a 0-length array
  96. // This is important to make sure we set up morphTargetBaseInfluence / morphTargetInfluences
  97. const length = objectInfluences === undefined ? 0 : objectInfluences.length;
  98. let influences = influencesList[ geometry.id ];
  99. if ( influences === undefined || influences.length !== length ) {
  100. // initialise list
  101. influences = [];
  102. for ( let i = 0; i < length; i ++ ) {
  103. influences[ i ] = [ i, 0 ];
  104. }
  105. influencesList[ geometry.id ] = influences;
  106. }
  107. // Collect influences
  108. for ( let i = 0; i < length; i ++ ) {
  109. const influence = influences[ i ];
  110. influence[ 0 ] = i;
  111. influence[ 1 ] = objectInfluences[ i ];
  112. }
  113. influences.sort( absNumericalSort );
  114. for ( let i = 0; i < 8; i ++ ) {
  115. if ( i < length && influences[ i ][ 1 ] ) {
  116. workInfluences[ i ][ 0 ] = influences[ i ][ 0 ];
  117. workInfluences[ i ][ 1 ] = influences[ i ][ 1 ];
  118. } else {
  119. workInfluences[ i ][ 0 ] = Number.MAX_SAFE_INTEGER;
  120. workInfluences[ i ][ 1 ] = 0;
  121. }
  122. }
  123. workInfluences.sort( numericalSort );
  124. const morphTargets = geometry.morphAttributes.position;
  125. const morphNormals = geometry.morphAttributes.normal;
  126. let morphInfluencesSum = 0;
  127. for ( let i = 0; i < 8; i ++ ) {
  128. const influence = workInfluences[ i ];
  129. const index = influence[ 0 ];
  130. const value = influence[ 1 ];
  131. if ( index !== Number.MAX_SAFE_INTEGER && value ) {
  132. if ( morphTargets && geometry.getAttribute( 'morphTarget' + i ) !== morphTargets[ index ] ) {
  133. geometry.setAttribute( 'morphTarget' + i, morphTargets[ index ] );
  134. }
  135. if ( morphNormals && geometry.getAttribute( 'morphNormal' + i ) !== morphNormals[ index ] ) {
  136. geometry.setAttribute( 'morphNormal' + i, morphNormals[ index ] );
  137. }
  138. morphInfluences[ i ] = value;
  139. morphInfluencesSum += value;
  140. } else {
  141. if ( morphTargets && geometry.hasAttribute( 'morphTarget' + i ) === true ) {
  142. geometry.deleteAttribute( 'morphTarget' + i );
  143. }
  144. if ( morphNormals && geometry.hasAttribute( 'morphNormal' + i ) === true ) {
  145. geometry.deleteAttribute( 'morphNormal' + i );
  146. }
  147. morphInfluences[ i ] = 0;
  148. }
  149. }
  150. // GLSL shader uses formula baseinfluence * base + sum(target * influence)
  151. // This allows us to switch between absolute morphs and relative morphs without changing shader code
  152. // When baseinfluence = 1 - sum(influence), the above is equivalent to sum((target - base) * influence)
  153. const morphBaseInfluence = geometry.morphTargetsRelative ? 1 : 1 - morphInfluencesSum;
  154. program.getUniforms().setValue( gl, 'morphTargetBaseInfluence', morphBaseInfluence );
  155. program.getUniforms().setValue( gl, 'morphTargetInfluences', morphInfluences );
  156. }
  157. }
  158. return {
  159. update: update
  160. };
  161. }
  162. export { WebGLMorphtargets };