Skip to content

Commit

Permalink
Support scene background and environment texture in ObjectLoader and …
Browse files Browse the repository at this point in the history
…toJSON (mrdoob#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]>
  • Loading branch information
meliharvey and Mugen87 authored Jun 19, 2021
1 parent e0d858e commit e9fb8d4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
16 changes: 16 additions & 0 deletions src/core/Object3D.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
24 changes: 21 additions & 3 deletions src/loaders/ObjectLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -784,7 +784,7 @@ class ObjectLoader extends Loader {

}

parseObject( data, geometries, materials, animations ) {
parseObject( data, geometries, materials, animations, textures ) {

let object;

Expand Down Expand Up @@ -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 ) {
Expand All @@ -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' ) {
Expand Down Expand Up @@ -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 ) );

}

Expand Down
2 changes: 0 additions & 2 deletions src/scenes/Scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit e9fb8d4

Please sign in to comment.