@@ -38,22 +38,21 @@ function WebGLMorphtargets( gl, capabilities, textures ) {
3838
3939 // instead of using attributes, the WebGL 2 code path encodes morph targets
4040 // into an array of data textures. Each layer represents a single morph target.
41- // Note: This texture is created once per geoemtry. If the set of morph targets
42- // changes over time (e.g. by adding additional morph targets), it's necessary
43- // to call dispose() on the geometry and then create a new one.
4441
45- let texture = morphTextures . get ( geometry ) ;
42+ const numberOfMorphTargets = geometry . morphAttributes . position . length ;
4643
47- if ( texture === undefined ) {
44+ let entry = morphTextures . get ( geometry ) ;
45+
46+ if ( entry === undefined || entry . count !== numberOfMorphTargets ) {
47+
48+ if ( entry !== undefined ) entry . texture . dispose ( ) ;
4849
4950 const hasMorphNormals = geometry . morphAttributes . normal !== undefined ;
5051
5152 const morphTargets = geometry . morphAttributes . position ;
5253 const morphNormals = geometry . morphAttributes . normal || [ ] ;
5354
54- const numberOfMorphTargets = morphTargets . length ;
5555 const numberOfVertices = geometry . attributes . position . count ;
56-
5756 const numberOfVertexData = ( hasMorphNormals === true ) ? 2 : 1 ; // (v,n) vs. (v)
5857
5958 let size = Math . sqrt ( numberOfVertices * numberOfVertexData ) ;
@@ -62,7 +61,7 @@ function WebGLMorphtargets( gl, capabilities, textures ) {
6261
6362 const buffer = new Float32Array ( size * size * 3 * numberOfMorphTargets ) ;
6463
65- texture = new DataTexture2DArray ( buffer , size , size , numberOfMorphTargets ) ;
64+ const texture = new DataTexture2DArray ( buffer , size , size , numberOfMorphTargets ) ;
6665 texture . format = RGBFormat ;
6766 texture . type = FloatType ;
6867
@@ -101,7 +100,12 @@ function WebGLMorphtargets( gl, capabilities, textures ) {
101100
102101 }
103102
104- morphTextures . set ( geometry , texture ) ;
103+ entry = {
104+ count : numberOfMorphTargets ,
105+ texture : texture
106+ } ;
107+
108+ morphTextures . set ( geometry , entry ) ;
105109
106110 }
107111
@@ -120,8 +124,8 @@ function WebGLMorphtargets( gl, capabilities, textures ) {
120124 program . getUniforms ( ) . setValue ( gl , 'morphTargetBaseInfluence' , morphBaseInfluence ) ;
121125 program . getUniforms ( ) . setValue ( gl , 'morphTargetInfluences' , objectInfluences ) ;
122126
123- program . getUniforms ( ) . setValue ( gl , 'morphTargets' , texture , textures ) ;
124- program . getUniforms ( ) . setValue ( gl , 'morphTargetsSize' , texture . image . width ) ;
127+ program . getUniforms ( ) . setValue ( gl , 'morphTargets' , entry . texture , textures ) ;
128+ program . getUniforms ( ) . setValue ( gl , 'morphTargetsSize' , entry . texture . image . width ) ;
125129
126130
127131 } else {
0 commit comments