Skip to content

Commit e9fb8d4

Browse files
meliharveyMugen87
andauthored
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]>
1 parent e0d858e commit e9fb8d4

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

src/core/Object3D.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,22 @@ class Object3D extends EventDispatcher {
699699

700700
}
701701

702+
if ( this.isScene ) {
703+
704+
if ( this.background && this.background.isColor ) {
705+
706+
object.background = this.background.toJSON();
707+
708+
} else if ( this.background && this.background.isTexture ) {
709+
710+
object.background = this.background.toJSON( meta ).uuid;
711+
712+
}
713+
714+
if ( this.environment && this.environment.isTexture ) object.environment = this.environment.toJSON( meta ).uuid;
715+
716+
}
717+
702718
if ( this.isMesh || this.isLine || this.isPoints ) {
703719

704720
object.geometry = serialize( meta.geometries, this.geometry );

src/loaders/ObjectLoader.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class ObjectLoader extends Loader {
128128
const textures = this.parseTextures( json.textures, images );
129129
const materials = this.parseMaterials( json.materials, textures );
130130

131-
const object = this.parseObject( json.object, geometries, materials, animations );
131+
const object = this.parseObject( json.object, geometries, materials, animations, textures );
132132
const skeletons = this.parseSkeletons( json.skeletons, object );
133133

134134
this.bindSkeletons( object, skeletons );
@@ -784,7 +784,7 @@ class ObjectLoader extends Loader {
784784

785785
}
786786

787-
parseObject( data, geometries, materials, animations ) {
787+
parseObject( data, geometries, materials, animations, textures ) {
788788

789789
let object;
790790

@@ -836,6 +836,18 @@ class ObjectLoader extends Loader {
836836

837837
}
838838

839+
function getTexture( uuid ) {
840+
841+
if ( textures[ uuid ] === undefined ) {
842+
843+
console.warn( 'THREE.ObjectLoader: Undefined texture', uuid );
844+
845+
}
846+
847+
return textures[ uuid ];
848+
849+
}
850+
839851
let geometry, material;
840852

841853
switch ( data.type ) {
@@ -850,10 +862,16 @@ class ObjectLoader extends Loader {
850862

851863
object.background = new Color( data.background );
852864

865+
} else {
866+
867+
object.background = getTexture( data.background );
868+
853869
}
854870

855871
}
856872

873+
if ( data.environment !== undefined ) object.environment = getTexture( data.environment );
874+
857875
if ( data.fog !== undefined ) {
858876

859877
if ( data.fog.type === 'Fog' ) {
@@ -1069,7 +1087,7 @@ class ObjectLoader extends Loader {
10691087

10701088
for ( let i = 0; i < children.length; i ++ ) {
10711089

1072-
object.add( this.parseObject( children[ i ], geometries, materials, animations ) );
1090+
object.add( this.parseObject( children[ i ], geometries, materials, animations, textures ) );
10731091

10741092
}
10751093

src/scenes/Scene.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ class Scene extends Object3D {
4545

4646
const data = super.toJSON( meta );
4747

48-
if ( this.background !== null ) data.object.background = this.background.toJSON( meta );
49-
if ( this.environment !== null ) data.object.environment = this.environment.toJSON( meta );
5048
if ( this.fog !== null ) data.object.fog = this.fog.toJSON();
5149

5250
return data;

0 commit comments

Comments
 (0)