@@ -5,16 +5,46 @@ var COMPONENTS = require('../../core/component').components;
55module . 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