WebGLMorphtargets.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. import { FloatType } from '../../constants.js';
  2. import { DataArrayTexture } from '../../textures/DataArrayTexture.js';
  3. import { Vector4 } from '../../math/Vector4.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 WebGLMorphtargets( gl, capabilities, textures ) {
  12. const influencesList = {};
  13. const morphInfluences = new Float32Array( 8 );
  14. const morphTextures = new WeakMap();
  15. const morph = new Vector4();
  16. const workInfluences = [];
  17. for ( let i = 0; i < 8; i ++ ) {
  18. workInfluences[ i ] = [ i, 0 ];
  19. }
  20. function update( object, geometry, material, program ) {
  21. const objectInfluences = object.morphTargetInfluences;
  22. if ( capabilities.isWebGL2 === true ) {
  23. // instead of using attributes, the WebGL 2 code path encodes morph targets
  24. // into an array of data textures. Each layer represents a single morph target.
  25. const morphAttribute = geometry.morphAttributes.position || geometry.morphAttributes.normal || geometry.morphAttributes.color;
  26. const morphTargetsCount = ( morphAttribute !== undefined ) ? morphAttribute.length : 0;
  27. let entry = morphTextures.get( geometry );
  28. if ( entry === undefined || entry.count !== morphTargetsCount ) {
  29. if ( entry !== undefined ) entry.texture.dispose();
  30. const hasMorphPosition = geometry.morphAttributes.position !== undefined;
  31. const hasMorphNormals = geometry.morphAttributes.normal !== undefined;
  32. const hasMorphColors = geometry.morphAttributes.color !== undefined;
  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. if ( width > capabilities.maxTextureSize ) {
  43. height = Math.ceil( width / capabilities.maxTextureSize );
  44. width = capabilities.maxTextureSize;
  45. }
  46. const buffer = new Float32Array( width * height * 4 * morphTargetsCount );
  47. const texture = new DataArrayTexture( buffer, width, height, morphTargetsCount );
  48. texture.type = FloatType;
  49. texture.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. morph.fromBufferAttribute( morphTarget, j );
  61. buffer[ offset + stride + 0 ] = morph.x;
  62. buffer[ offset + stride + 1 ] = morph.y;
  63. buffer[ offset + stride + 2 ] = morph.z;
  64. buffer[ offset + stride + 3 ] = 0;
  65. }
  66. if ( hasMorphNormals === true ) {
  67. morph.fromBufferAttribute( morphNormal, j );
  68. buffer[ offset + stride + 4 ] = morph.x;
  69. buffer[ offset + stride + 5 ] = morph.y;
  70. buffer[ offset + stride + 6 ] = morph.z;
  71. buffer[ offset + stride + 7 ] = 0;
  72. }
  73. if ( hasMorphColors === true ) {
  74. morph.fromBufferAttribute( morphColor, j );
  75. buffer[ offset + stride + 8 ] = morph.x;
  76. buffer[ offset + stride + 9 ] = morph.y;
  77. buffer[ offset + stride + 10 ] = morph.z;
  78. buffer[ offset + stride + 11 ] = ( morphColor.itemSize === 4 ) ? morph.w : 1;
  79. }
  80. }
  81. }
  82. entry = {
  83. count: morphTargetsCount,
  84. texture: texture,
  85. size: new Vector2( width, height )
  86. };
  87. morphTextures.set( geometry, entry );
  88. function disposeTexture() {
  89. texture.dispose();
  90. morphTextures.delete( geometry );
  91. geometry.removeEventListener( 'dispose', disposeTexture );
  92. }
  93. geometry.addEventListener( 'dispose', disposeTexture );
  94. }
  95. //
  96. let morphInfluencesSum = 0;
  97. for ( let i = 0; i < objectInfluences.length; i ++ ) {
  98. morphInfluencesSum += objectInfluences[ i ];
  99. }
  100. const morphBaseInfluence = geometry.morphTargetsRelative ? 1 : 1 - morphInfluencesSum;
  101. program.getUniforms().setValue( gl, 'morphTargetBaseInfluence', morphBaseInfluence );
  102. program.getUniforms().setValue( gl, 'morphTargetInfluences', objectInfluences );
  103. program.getUniforms().setValue( gl, 'morphTargetsTexture', entry.texture, textures );
  104. program.getUniforms().setValue( gl, 'morphTargetsTextureSize', entry.size );
  105. } else {
  106. // When object doesn't have morph target influences defined, we treat it as a 0-length array
  107. // This is important to make sure we set up morphTargetBaseInfluence / morphTargetInfluences
  108. const length = objectInfluences === undefined ? 0 : objectInfluences.length;
  109. let influences = influencesList[ geometry.id ];
  110. if ( influences === undefined || influences.length !== length ) {
  111. // initialise list
  112. influences = [];
  113. for ( let i = 0; i < length; i ++ ) {
  114. influences[ i ] = [ i, 0 ];
  115. }
  116. influencesList[ geometry.id ] = influences;
  117. }
  118. // Collect influences
  119. for ( let i = 0; i < length; i ++ ) {
  120. const influence = influences[ i ];
  121. influence[ 0 ] = i;
  122. influence[ 1 ] = objectInfluences[ i ];
  123. }
  124. influences.sort( absNumericalSort );
  125. for ( let i = 0; i < 8; i ++ ) {
  126. if ( i < length && influences[ i ][ 1 ] ) {
  127. workInfluences[ i ][ 0 ] = influences[ i ][ 0 ];
  128. workInfluences[ i ][ 1 ] = influences[ i ][ 1 ];
  129. } else {
  130. workInfluences[ i ][ 0 ] = Number.MAX_SAFE_INTEGER;
  131. workInfluences[ i ][ 1 ] = 0;
  132. }
  133. }
  134. workInfluences.sort( numericalSort );
  135. const morphTargets = geometry.morphAttributes.position;
  136. const morphNormals = geometry.morphAttributes.normal;
  137. let morphInfluencesSum = 0;
  138. for ( let i = 0; i < 8; i ++ ) {
  139. const influence = workInfluences[ i ];
  140. const index = influence[ 0 ];
  141. const value = influence[ 1 ];
  142. if ( index !== Number.MAX_SAFE_INTEGER && value ) {
  143. if ( morphTargets && geometry.getAttribute( 'morphTarget' + i ) !== morphTargets[ index ] ) {
  144. geometry.setAttribute( 'morphTarget' + i, morphTargets[ index ] );
  145. }
  146. if ( morphNormals && geometry.getAttribute( 'morphNormal' + i ) !== morphNormals[ index ] ) {
  147. geometry.setAttribute( 'morphNormal' + i, morphNormals[ index ] );
  148. }
  149. morphInfluences[ i ] = value;
  150. morphInfluencesSum += value;
  151. } else {
  152. if ( morphTargets && geometry.hasAttribute( 'morphTarget' + i ) === true ) {
  153. geometry.deleteAttribute( 'morphTarget' + i );
  154. }
  155. if ( morphNormals && geometry.hasAttribute( 'morphNormal' + i ) === true ) {
  156. geometry.deleteAttribute( 'morphNormal' + i );
  157. }
  158. morphInfluences[ i ] = 0;
  159. }
  160. }
  161. // GLSL shader uses formula baseinfluence * base + sum(target * influence)
  162. // This allows us to switch between absolute morphs and relative morphs without changing shader code
  163. // When baseinfluence = 1 - sum(influence), the above is equivalent to sum((target - base) * influence)
  164. const morphBaseInfluence = geometry.morphTargetsRelative ? 1 : 1 - morphInfluencesSum;
  165. program.getUniforms().setValue( gl, 'morphTargetBaseInfluence', morphBaseInfluence );
  166. program.getUniforms().setValue( gl, 'morphTargetInfluences', morphInfluences );
  167. }
  168. }
  169. return {
  170. update: update
  171. };
  172. }
  173. export { WebGLMorphtargets };
粤ICP备19079148号