Browse Source

Fix loading multiple materials with the same uuid

It fixes the problem when json contains multiple MultiMaterials containing the same material. Something like

```
MultiMaterialA = [X, Y]
MultiMaterialB = [X, Z]
```

then X will be created twice even though it is exactly the same.

I know MultiMaterial is deprecated, but the problem still stands for us as we are using io_three, which gives us MultiMaterials.
Michael Turkeev 6 years ago
parent
commit
3b6c7f4c76
1 changed files with 12 additions and 6 deletions
  1. 12 6
      src/loaders/ObjectLoader.js

+ 12 - 6
src/loaders/ObjectLoader.js

@@ -451,9 +451,10 @@ Object.assign( ObjectLoader.prototype, {
 
 	},
 
-	parseMaterials: function ( json, textures ) {
+    	parseMaterials: function ( json, textures ) {
 
 		var materials = {};
+		var cache = {};
 
 		if ( json !== undefined ) {
 
@@ -466,21 +467,26 @@ Object.assign( ObjectLoader.prototype, {
 
 				if ( data.type === 'MultiMaterial' ) {
 
-					// Deprecated
-
+                    			// Deprecated
 					var array = [];
-
 					for ( var j = 0; j < data.materials.length; j ++ ) {
-
-						array.push( loader.parse( data.materials[ j ] ) );
+						if (cache[data.materials[ j ].uuid]) {
+							array.push( cache[data.materials[ j ].uuid] );
+						} else {
+							var mat = loader.parse( data.materials[ j ] );
+							cache[ data.materials[ j ].uuid ] = mat;
+							array.push( mat );
+						}
 
 					}
 
 					materials[ data.uuid ] = array;
+					cache[ data.uuid ] = array;
 
 				} else {
 
 					materials[ data.uuid ] = loader.parse( data );
+					cache[ data.uuid ] = loader.parse( data );
 
 				}