Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions docs/components/background.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ frustum culling issues when `a-sky` is further than the far plane of the
camera. There are no unexpected occlusions either with far objects that might
be behind of the sphere geometry of `a-sky`.

The background component can also generate a default environment cube map for all
materials, if you find GLB models end up too dark or reflective materials don't
reflect the environment this will provide a default reflective environment.

## Example

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

## Properties

| Property | Description | Default Value |
|-------------|-----------------------------------------------------------|---------------|
| color | Color of the scene background. | black |
| transparent | Background is transparent. The color property is ignored. | false |
| Property | Description | Default Value |
|---------------------|-----------------------------------------------------------|---------------|
| color | Color of the scene background. | black |
| transparent | Background is transparent. The color property is ignored. | false |
| generateEnvironment | Whether to generate a default environment | true |
37 changes: 35 additions & 2 deletions src/components/scene/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,46 @@ var COMPONENTS = require('../../core/component').components;
module.exports.Component = register('background', {
schema: {
color: {type: 'color', default: 'black'},
transparent: {default: false}
transparent: {default: false},
generateEnvironment: {default: true}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want the default to be true?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's pretty safe and not a huge performance hit as it only renders the once. It will even fix materials unexpectedly not looking right because they expect an environment map. So it's a nice feature, that you only need to type 'background` to get.

I'd even be tempted to take it a step further and make background="transparent:true" a default component on <a-scene> or at least add background to the hello-world boilerplate just to get the better behaviour by default.

},
init: function () {
this.cubeRenderTarget = new THREE.WebGLCubeRenderTarget(128, { format: THREE.RGBFormat, generateMipmaps: true, minFilter: THREE.LinearMipmapLinearFilter });
this.cubeCamera = new THREE.CubeCamera(1, 100000, this.cubeRenderTarget);
this.needsEnvironmentUpdate = true;
},
update: function () {
var scene = this.el.sceneEl.object3D;
var data = this.data;
var object3D = this.el.object3D;
if (data.transparent) {
object3D.background = null;
} else {
object3D.background = new THREE.Color(data.color);
}

if (scene.environment && scene.environment !== this.cubeRenderTarget.texture) {
console.warn('Background will not overide predefined environment maps');
return;
}
object3D.background = new THREE.Color(data.color);

if (data.generateEnvironment) {
scene.environment = this.cubeRenderTarget.texture;
} else {
scene.environment = null;
}
},

tick: function () {
if (!this.needsEnvironmentUpdate) return;
var scene = this.el.object3D;
var renderer = this.el.renderer;
var camera = this.el.camera;

this.el.object3D.add(this.cubeCamera);
this.cubeCamera.position.copy(camera.position);
this.cubeCamera.update(renderer, scene);
this.needsEnvironmentUpdate = false;
},

remove: function () {
Expand All @@ -24,6 +54,9 @@ module.exports.Component = register('background', {
object3D.background = null;
return;
}
if (object3D.environment === this.cubeRenderTarget.texture) {
object3D.environment = null;
}
object3D.background = COMPONENTS[this.name].schema.color.default;
}
});