浏览代码

Multi Material serialising and deserialising. See #10931

Mr.doob 8 年之前
父节点
当前提交
7de1d68cab
共有 2 个文件被更改,包括 46 次插入8 次删除
  1. 24 8
      src/core/Object3D.js
  2. 22 0
      src/loaders/ObjectLoader.js

+ 24 - 8
src/core/Object3D.js

@@ -611,27 +611,43 @@ Object.assign( Object3D.prototype, EventDispatcher.prototype, {
 
 		//
 
-		if ( this.geometry !== undefined ) {
+		function serialise( library, element ) {
 
-			if ( meta.geometries[ this.geometry.uuid ] === undefined ) {
+			if ( library[ element.uuid ] === undefined ) {
 
-				meta.geometries[ this.geometry.uuid ] = this.geometry.toJSON( meta );
+				library[ element.uuid ] = element.toJSON( meta );
 
 			}
 
-			object.geometry = this.geometry.uuid;
+			return element.uuid;
+
+		}
+
+		if ( this.geometry !== undefined ) {
+
+			object.geometry = serialise( meta.geometries, this.geometry );
 
 		}
 
 		if ( this.material !== undefined ) {
 
-			if ( meta.materials[ this.material.uuid ] === undefined ) {
+			if ( Array.isArray( this.material ) ) {
 
-				meta.materials[ this.material.uuid ] = this.material.toJSON( meta );
+				var uuids = [];
 
-			}
+				for ( var i = 0, l = this.material.length; i < l; i ++ ) {
 
-			object.material = this.material.uuid;
+					uuids.push( serialise( meta.materials, this.material[ i ] ) );
+
+				}
+
+				object.material = uuids;
+
+			} else {
+
+				object.material = serialise( meta.materials, this.material );
+
+			}
 
 		}
 

+ 22 - 0
src/loaders/ObjectLoader.js

@@ -549,6 +549,28 @@ Object.assign( ObjectLoader.prototype, {
 
 				if ( name === undefined ) return undefined;
 
+				if ( Array.isArray( name ) ) {
+
+					var array = [];
+
+					for ( var i = 0, l = name.length; i < l; i ++ ) {
+
+						var uuid = name[ i ];
+
+						if ( materials[ uuid ] === undefined ) {
+
+							console.warn( 'THREE.ObjectLoader: Undefined material', uuid );
+
+						}
+
+						array.push( materials[ uuid ] );
+
+					}
+
+					return array;
+
+				}
+
 				if ( materials[ name ] === undefined ) {
 
 					console.warn( 'THREE.ObjectLoader: Undefined material', name );