Browse Source

GLTFExporter: Prevents duplicate in the export the same image and material

Fernando Serrano 8 years ago
parent
commit
b74d1ca9e9
1 changed files with 26 additions and 2 deletions
  1. 26 2
      examples/js/exporters/GLTFExporter.js

+ 26 - 2
examples/js/exporters/GLTFExporter.js

@@ -78,6 +78,12 @@ THREE.GLTFExporter.prototype = {
 
 		var byteOffset = 0;
 		var dataViews = [];
+		var cachedData = {
+
+			images: {},
+			materials: {}
+
+		};
 
 		/**
 		 * Compare two arrays
@@ -318,6 +324,12 @@ THREE.GLTFExporter.prototype = {
 		 */
 		function processImage ( map ) {
 
+			if ( cachedData.images[ map.uuid ] ) {
+
+				return cachedData.images[ map.uuid ];
+
+			}
+
 			if ( !outputJSON.images ) {
 
 				outputJSON.images = [];
@@ -339,7 +351,10 @@ THREE.GLTFExporter.prototype = {
 
 			outputJSON.images.push( gltfImage );
 
-			return outputJSON.images.length - 1;
+			var index = outputJSON.images.length - 1;
+			cachedData.images[ map.uuid ] = index;
+
+			return index;
 
 		}
 
@@ -404,6 +419,12 @@ THREE.GLTFExporter.prototype = {
 		 */
 		function processMaterial ( material ) {
 
+			if ( cachedData.materials[ material.uuid ] ) {
+
+				return cachedData.materials[ material.uuid ];
+
+			}
+
 			if ( !outputJSON.materials ) {
 
 				outputJSON.materials = [];
@@ -539,7 +560,10 @@ THREE.GLTFExporter.prototype = {
 
 			outputJSON.materials.push( gltfMaterial );
 
-			return outputJSON.materials.length - 1;
+			var index = outputJSON.materials.length - 1;
+			cachedData.materials[ material.uuid ] = index;
+
+			return index;
 
 		}