Skip to content

Commit 5a3ae78

Browse files
Use background to set the default environment map (#4797)
* Use background to set the default environment map * pr feedback * move object generation into init * don't try to override existing environment maps * clean up init
1 parent 9dab7c0 commit 5a3ae78

File tree

2 files changed

+44
-6
lines changed

2 files changed

+44
-6
lines changed

docs/components/background.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ frustum culling issues when `a-sky` is further than the far plane of the
1313
camera. There are no unexpected occlusions either with far objects that might
1414
be behind of the sphere geometry of `a-sky`.
1515

16+
The background component can also generate a default environment cube map for all
17+
materials, if you find GLB models end up too dark or reflective materials don't
18+
reflect the environment this will provide a default reflective environment.
19+
1620
## Example
1721

1822
The example below sets the background color to red.
@@ -23,7 +27,8 @@ The example below sets the background color to red.
2327

2428
## Properties
2529

26-
| Property | Description | Default Value |
27-
|-------------|-----------------------------------------------------------|---------------|
28-
| color | Color of the scene background. | black |
29-
| transparent | Background is transparent. The color property is ignored. | false |
30+
| Property | Description | Default Value |
31+
|---------------------|-----------------------------------------------------------|---------------|
32+
| color | Color of the scene background. | black |
33+
| transparent | Background is transparent. The color property is ignored. | false |
34+
| generateEnvironment | Whether to generate a default environment | true |

src/components/scene/background.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,46 @@ var COMPONENTS = require('../../core/component').components;
55
module.exports.Component = register('background', {
66
schema: {
77
color: {type: 'color', default: 'black'},
8-
transparent: {default: false}
8+
transparent: {default: false},
9+
generateEnvironment: {default: true}
10+
},
11+
init: function () {
12+
this.cubeRenderTarget = new THREE.WebGLCubeRenderTarget(128, { format: THREE.RGBFormat, generateMipmaps: true, minFilter: THREE.LinearMipmapLinearFilter });
13+
this.cubeCamera = new THREE.CubeCamera(1, 100000, this.cubeRenderTarget);
14+
this.needsEnvironmentUpdate = true;
915
},
1016
update: function () {
17+
var scene = this.el.sceneEl.object3D;
1118
var data = this.data;
1219
var object3D = this.el.object3D;
1320
if (data.transparent) {
1421
object3D.background = null;
22+
} else {
23+
object3D.background = new THREE.Color(data.color);
24+
}
25+
26+
if (scene.environment && scene.environment !== this.cubeRenderTarget.texture) {
27+
console.warn('Background will not overide predefined environment maps');
1528
return;
1629
}
17-
object3D.background = new THREE.Color(data.color);
30+
31+
if (data.generateEnvironment) {
32+
scene.environment = this.cubeRenderTarget.texture;
33+
} else {
34+
scene.environment = null;
35+
}
36+
},
37+
38+
tick: function () {
39+
if (!this.needsEnvironmentUpdate) return;
40+
var scene = this.el.object3D;
41+
var renderer = this.el.renderer;
42+
var camera = this.el.camera;
43+
44+
this.el.object3D.add(this.cubeCamera);
45+
this.cubeCamera.position.copy(camera.position);
46+
this.cubeCamera.update(renderer, scene);
47+
this.needsEnvironmentUpdate = false;
1848
},
1949

2050
remove: function () {
@@ -24,6 +54,9 @@ module.exports.Component = register('background', {
2454
object3D.background = null;
2555
return;
2656
}
57+
if (object3D.environment === this.cubeRenderTarget.texture) {
58+
object3D.environment = null;
59+
}
2760
object3D.background = COMPONENTS[this.name].schema.color.default;
2861
}
2962
});

0 commit comments

Comments
 (0)