Browse Source

Merge pull request #14100 from takahirox/GLTFLoaderLessMaterialCloning

GLTFLoader: Optimization, Less clone materials
Mr.doob 7 years ago
parent
commit
0ec348971d
1 changed files with 21 additions and 27 deletions
  1. 21 27
      examples/js/loaders/GLTFLoader.js

+ 21 - 27
examples/js/loaders/GLTFLoader.js

@@ -2620,43 +2620,37 @@ THREE.GLTFLoader = ( function () {
 
 						}
 
-						// If the material will be modified later on, clone it now.
+						// Clone the material if it will be modified
 						if ( useVertexColors || useFlatShading || useSkinning || useMorphTargets ) {
 
-							material = material.isGLTFSpecularGlossinessMaterial
-									? extensions[ EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ].cloneMaterial( material )
-									: material.clone();
+							var cacheKey = 'ClonedMaterial:' + material.uuid + ':';
 
-						}
-
-						if ( useSkinning ) {
+							if ( material.isGLTFSpecularGlossinessMaterial ) cacheKey += 'specular-glossiness:';
+							if ( useSkinning ) cacheKey += 'skinning:';
+							if ( useVertexColors ) cacheKey += 'vertex-colors:';
+							if ( useFlatShading ) cacheKey += 'flat-shading:';
+							if ( useMorphTargets ) cacheKey += 'morph-targets:';
+							if ( useMorphNormals ) cacheKey += 'morph-normals:';
 
-							material.skinning = true;
+							var cachedMaterial = scope.cache.get( cacheKey );
 
-						}
+							if ( ! cachedMaterial ) {
 
-						if ( useVertexColors ) {
+								cachedMaterial = material.isGLTFSpecularGlossinessMaterial
+										? extensions[ EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ].cloneMaterial( material )
+										: material.clone();
 
-							material.vertexColors = THREE.VertexColors;
-							material.needsUpdate = true;
+								if ( useSkinning ) cachedMaterial.skinning = true;
+								if ( useVertexColors ) cachedMaterial.vertexColors = THREE.VertexColors;
+								if ( useFlatShading ) cachedMaterial.flatShading = true;
+								if ( useMorphTargets ) cachedMaterial.morphTargets = true;
+								if ( useMorphNormals ) cachedMaterial.morphNormals = true;
 
-						}
+								scope.cache.add( cacheKey, cachedMaterial );
 
-						if ( useFlatShading ) {
-
-							material.flatShading = true;
-
-						}
-
-						if ( useMorphTargets ) {
-
-							material.morphTargets = true;
-
-						}
-
-						if ( useMorphNormals ) {
+							}
 
-							material.morphNormals = true;
+							material = cachedMaterial;
 
 						}