浏览代码

Support scene background and environment texture in ObjectLoader and toJSON (#18834)

* scene background, environment serialize,deserialize

* formatting fixes

* revisions from PR

* Removed modified build files from PR

* Update ObjectLoader.js

Clean up.

* Update ObjectLoader.js

More clean up.

* Update ObjectLoader.js

More clean up.

* Update ObjectLoader.js

Change name to uuid in getTexture().

Co-authored-by: Michael Herzog <[email protected]>
Meli Harvey 4 年之前
父节点
当前提交
e9fb8d48cb
共有 3 个文件被更改,包括 37 次插入5 次删除
  1. 16 0
      src/core/Object3D.js
  2. 21 3
      src/loaders/ObjectLoader.js
  3. 0 2
      src/scenes/Scene.js

+ 16 - 0
src/core/Object3D.js

@@ -699,6 +699,22 @@ class Object3D extends EventDispatcher {
 
 		}
 
+		if ( this.isScene ) {
+
+			if ( this.background && this.background.isColor ) {
+
+				object.background = this.background.toJSON();
+
+			} else if ( this.background && this.background.isTexture ) {
+
+				object.background = this.background.toJSON( meta ).uuid;
+
+			}
+
+			if ( this.environment && this.environment.isTexture ) object.environment = this.environment.toJSON( meta ).uuid;
+
+		}
+
 		if ( this.isMesh || this.isLine || this.isPoints ) {
 
 			object.geometry = serialize( meta.geometries, this.geometry );

+ 21 - 3
src/loaders/ObjectLoader.js

@@ -128,7 +128,7 @@ class ObjectLoader extends Loader {
 		const textures = this.parseTextures( json.textures, images );
 		const materials = this.parseMaterials( json.materials, textures );
 
-		const object = this.parseObject( json.object, geometries, materials, animations );
+		const object = this.parseObject( json.object, geometries, materials, animations, textures );
 		const skeletons = this.parseSkeletons( json.skeletons, object );
 
 		this.bindSkeletons( object, skeletons );
@@ -784,7 +784,7 @@ class ObjectLoader extends Loader {
 
 	}
 
-	parseObject( data, geometries, materials, animations ) {
+	parseObject( data, geometries, materials, animations, textures ) {
 
 		let object;
 
@@ -836,6 +836,18 @@ class ObjectLoader extends Loader {
 
 		}
 
+		function getTexture( uuid ) {
+
+			if ( textures[ uuid ] === undefined ) {
+
+				console.warn( 'THREE.ObjectLoader: Undefined texture', uuid );
+
+			}
+
+			return textures[ uuid ];
+
+		}
+
 		let geometry, material;
 
 		switch ( data.type ) {
@@ -850,10 +862,16 @@ class ObjectLoader extends Loader {
 
 						object.background = new Color( data.background );
 
+					} else {
+
+						object.background = getTexture( data.background );
+
 					}
 
 				}
 
+				if ( data.environment !== undefined ) object.environment = getTexture( data.environment );
+
 				if ( data.fog !== undefined ) {
 
 					if ( data.fog.type === 'Fog' ) {
@@ -1069,7 +1087,7 @@ class ObjectLoader extends Loader {
 
 			for ( let i = 0; i < children.length; i ++ ) {
 
-				object.add( this.parseObject( children[ i ], geometries, materials, animations ) );
+				object.add( this.parseObject( children[ i ], geometries, materials, animations, textures ) );
 
 			}
 

+ 0 - 2
src/scenes/Scene.js

@@ -45,8 +45,6 @@ class Scene extends Object3D {
 
 		const data = super.toJSON( meta );
 
-		if ( this.background !== null ) data.object.background = this.background.toJSON( meta );
-		if ( this.environment !== null ) data.object.environment = this.environment.toJSON( meta );
 		if ( this.fog !== null ) data.object.fog = this.fog.toJSON();
 
 		return data;