Skip to content

Commit 2baec79

Browse files
committed
Scene: Added background property.
1 parent 851b868 commit 2baec79

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/renderers/WebGLRenderer.js

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,29 @@ THREE.WebGLRenderer = function ( parameters ) {
261261

262262
//
263263

264+
var backgroundCamera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 );
265+
var backgroundCamera2 = new THREE.PerspectiveCamera();
266+
var backgroundPlaneMesh = new THREE.Mesh(
267+
new THREE.PlaneBufferGeometry( 2, 2 ),
268+
new THREE.MeshBasicMaterial( { depthTest: false, depthWrite: false } )
269+
);
270+
var backgroundBoxShader = THREE.ShaderLib[ 'cube' ];
271+
var backgroundBoxMesh = new THREE.Mesh(
272+
new THREE.BoxBufferGeometry( 5, 5, 5 ),
273+
new THREE.ShaderMaterial( {
274+
uniforms: backgroundBoxShader.uniforms,
275+
vertexShader: backgroundBoxShader.vertexShader,
276+
fragmentShader: backgroundBoxShader.fragmentShader,
277+
depthTest: false,
278+
depthWrite: false,
279+
side: THREE.BackSide
280+
} )
281+
);
282+
objects.update( backgroundPlaneMesh );
283+
objects.update( backgroundBoxMesh );
284+
285+
//
286+
264287
function getTargetPixelRatio() {
265288

266289
return _currentRenderTarget === null ? _pixelRatio : 1;
@@ -1137,7 +1160,46 @@ THREE.WebGLRenderer = function ( parameters ) {
11371160

11381161
this.setRenderTarget( renderTarget );
11391162

1140-
if ( this.autoClear || forceClear ) {
1163+
//
1164+
1165+
var needsClear = this.autoClear || forceClear;
1166+
var background = scene.background;
1167+
1168+
if ( background === null ) {
1169+
1170+
glClearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
1171+
1172+
} else if ( background instanceof THREE.CubeTexture ) {
1173+
1174+
backgroundCamera2.projectionMatrix = camera.projectionMatrix;
1175+
1176+
backgroundCamera2.matrixWorld.extractRotation( camera.matrixWorld );
1177+
backgroundCamera2.matrixWorldInverse.getInverse( backgroundCamera2.matrixWorld );
1178+
1179+
backgroundBoxMesh.material.uniforms[ "tCube" ].value = background;
1180+
backgroundBoxMesh.modelViewMatrix.multiplyMatrices( backgroundCamera2.matrixWorldInverse, backgroundBoxMesh.matrixWorld );
1181+
1182+
_this.renderBufferDirect( backgroundCamera2, null, backgroundBoxMesh.geometry, backgroundBoxMesh.material, backgroundBoxMesh, null );
1183+
1184+
needsClear = false;
1185+
1186+
} else if ( background instanceof THREE.Texture ) {
1187+
1188+
backgroundPlaneMesh.material.map = background;
1189+
1190+
_this.renderBufferDirect( backgroundCamera, null, backgroundPlaneMesh.geometry, backgroundPlaneMesh.material, backgroundPlaneMesh, null );
1191+
1192+
needsClear = false;
1193+
1194+
} else if ( background instanceof THREE.Color ) {
1195+
1196+
glClearColor( background.r, background.g, background.b, 1 );
1197+
1198+
needsClear = true;
1199+
1200+
}
1201+
1202+
if ( needsClear ) {
11411203

11421204
this.clear( this.autoClearColor, this.autoClearDepth, this.autoClearStencil );
11431205

src/scenes/Scene.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ THREE.Scene = function () {
88

99
this.type = 'Scene';
1010

11+
this.background = null;
1112
this.fog = null;
1213
this.overrideMaterial = null;
1314

@@ -22,6 +23,7 @@ THREE.Scene.prototype.copy = function ( source, recursive ) {
2223

2324
THREE.Object3D.prototype.copy.call( this, source, recursive );
2425

26+
if ( source.background !== null ) this.background = source.background.clone();
2527
if ( source.fog !== null ) this.fog = source.fog.clone();
2628
if ( source.overrideMaterial !== null ) this.overrideMaterial = source.overrideMaterial.clone();
2729

0 commit comments

Comments
 (0)