Skip to content

Commit fe65eaf

Browse files
committed
WebGPURenderer: Add initial version of WebGPUBackground.
1 parent 9021c89 commit fe65eaf

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { GPULoadOp } from './constants.js';
2+
import { Color } from '../../../../build/three.module.js';
3+
4+
class WebGPUBackground {
5+
6+
constructor() {
7+
8+
this.clearAlpha = 0;
9+
this.clearColor = new Color( 0x000000 );
10+
11+
}
12+
13+
render( scene, renderPassDescriptor, autoClear ) {
14+
15+
const background = ( scene.isScene === true ) ? scene.background : null;
16+
const clearColor = this.clearColor;
17+
let clearAlpha = this.clearAlpha;
18+
19+
let forceClear = false;
20+
21+
if ( background !== null && background.isColor === true ) {
22+
23+
clearColor.copy( background );
24+
clearAlpha = 1;
25+
forceClear = true;
26+
27+
}
28+
29+
const colorAttachment = renderPassDescriptor.colorAttachments[ 0 ];
30+
31+
if ( autoClear === true || forceClear === true ) {
32+
33+
colorAttachment.loadValue = { r: clearColor.r, g: clearColor.g, b: clearColor.b, a: clearAlpha };
34+
35+
} else {
36+
37+
colorAttachment.loadValue = GPULoadOp.Load;
38+
39+
}
40+
41+
}
42+
43+
}
44+
45+
export default WebGPUBackground;

examples/jsm/renderers/webgpu/WebGPURenderer.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import WebGPURenderPipelines from './WebGPURenderPipelines.js';
88
import WebGPUBindings from './WebGPUBindings.js';
99
import WebGPURenderLists from './WebGPURenderLists.js';
1010
import WebGPUTextures from './WebGPUTextures.js';
11+
import WebGPUBackground from './WebGPUBackground.js';
1112

1213
import { Frustum, Matrix4, Vector3 } from '../../../../build/three.module.js';
1314

@@ -24,6 +25,7 @@ class WebGPURenderer {
2425
this.domElement = ( parameters.canvas !== undefined ) ? parameters.canvas : document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' );
2526
this.parameters = parameters;
2627

28+
this.autoClear = true;
2729
this.sortObjects = true;
2830

2931
// internals
@@ -50,6 +52,7 @@ class WebGPURenderer {
5052
this._renderPipelines = null;
5153
this._renderLists = null;
5254
this._textures = null;
55+
this._background = null;
5356

5457
this._renderPassDescriptor = null;
5558

@@ -91,11 +94,12 @@ class WebGPURenderer {
9194

9295
const colorAttachment = this._renderPassDescriptor.colorAttachments[ 0 ];
9396
colorAttachment.attachment = this._swapChain.getCurrentTexture().createView();
94-
colorAttachment.loadValue = GPULoadOp.Load;
9597

9698
const depthStencilAttachment = this._renderPassDescriptor.depthStencilAttachment;
9799
depthStencilAttachment.attachment = this._depthBuffer.createView();
98100

101+
this._background.render( scene, this._renderPassDescriptor, this.autoClear );
102+
99103
const opaqueObjects = this._currentRenderList.opaque;
100104
const transparentObjects = this._currentRenderList.transparent;
101105

@@ -575,6 +579,7 @@ async function initWebGPU( scope ) {
575579
scope._objects = new WebGPUObjects( scope._geometries, scope._info );
576580
scope._renderPipelines = new WebGPURenderPipelines( device, compiler, scope._bindings );
577581
scope._renderLists = new WebGPURenderLists();
582+
scope._background = new WebGPUBackground();
578583

579584
//
580585

examples/webgpu_sandbox.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
camera.position.z = 4;
2929

3030
scene = new THREE.Scene();
31+
scene.background = new THREE.Color( 0xffffff );
3132

3233
// textured mesh
3334

0 commit comments

Comments
 (0)