Selaa lähdekoodia

CmdAddObject, CmdRemoveObject now serialize Textures and Images

Daniel 10 vuotta sitten
vanhempi
commit
a94fd88cba
2 muutettua tiedostoa jossa 52 lisäystä ja 12 poistoa
  1. 25 6
      editor/js/CmdAddObject.js
  2. 27 6
      editor/js/CmdRemoveObject.js

+ 25 - 6
editor/js/CmdAddObject.js

@@ -19,19 +19,38 @@ CmdAddObject = function ( object ) {
 			textures: {},
 			images: {}
 		};
-		this.objectJSON = object.toJSON( meta );
+		var json = object.toJSON( meta );
 
-		if ( object.geometry !== undefined ) {
+		var geometries = extractFromCache( meta.geometries );
+		var materials = extractFromCache( meta.materials );
+		var textures = extractFromCache( meta.textures );
+		var images = extractFromCache( meta.images );
 
-			this.objectJSON.geometries = [ object.geometry.toJSON( meta ) ];
+		if ( geometries.length > 0 ) json.geometries = geometries;
+		if ( materials.length > 0 ) json.materials = materials;
+		if ( textures.length > 0 ) json.textures = textures;
+		if ( images.length > 0 ) json.images = images;
 
-		}
+		this.objectJSON = json;
+
+	}
+
+	// Note: The function 'extractFromCache' is copied from Object3D.toJSON()
+
+	// extract data from the cache hash
+	// remove metadata on each item
+	// and return as array
+	function extractFromCache ( cache ) {
 
-		if ( object.material !== undefined ) {
+		var values = [];
+		for ( var key in cache ) {
 
-			this.objectJSON.materials = [ object.material.toJSON( meta ) ];
+			var data = cache[ key ];
+			delete data.metadata;
+			values.push( data );
 
 		}
+		return values;
 
 	}
 

+ 27 - 6
editor/js/CmdRemoveObject.js

@@ -14,25 +14,46 @@ CmdRemoveObject = function ( object ) {
 
 	if ( object !== undefined ) {
 
+		object.updateMatrixWorld( true );
+
 		meta = {
 			geometries: {},
 			materials: {},
 			textures: {},
 			images: {}
 		};
-		this.objectJSON = object.toJSON( meta );
+		var json = object.toJSON( meta );
 
-		if ( object.geometry !== undefined ) {
+		var geometries = extractFromCache( meta.geometries );
+		var materials = extractFromCache( meta.materials );
+		var textures = extractFromCache( meta.textures );
+		var images = extractFromCache( meta.images );
 
-			this.objectJSON.geometries = [ object.geometry.toJSON( meta ) ];
+		if ( geometries.length > 0 ) json.geometries = geometries;
+		if ( materials.length > 0 ) json.materials = materials;
+		if ( textures.length > 0 ) json.textures = textures;
+		if ( images.length > 0 ) json.images = images;
 
-		}
+		this.objectJSON = json;
+
+	}
+
+	// Note: The function 'extractFromCache' is copied from Object3D.toJSON()
+
+	// extract data from the cache hash
+	// remove metadata on each item
+	// and return as array
+	function extractFromCache ( cache ) {
 
-		if ( object.material !== undefined ) {
+		var values = [];
+		for ( var key in cache ) {
 
-			this.objectJSON.materials = [ object.material.toJSON( meta ) ];
+			var data = cache[ key ];
+			delete data.metadata;
+			values.push( data );
 
 		}
+		return values;
 
 	}