Skip to content

Commit 5d72fec

Browse files
committed
WebGPURenderer: Completed clear interface.
1 parent 33079a0 commit 5d72fec

File tree

2 files changed

+89
-13
lines changed

2 files changed

+89
-13
lines changed

examples/jsm/renderers/webgpu/WebGPUBackground.js

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,100 @@
11
import { GPULoadOp } from './constants.js';
22
import { Color } from '../../../../build/three.module.js';
33

4+
let _clearAlpha;
5+
const _clearColor = new Color();
6+
47
class WebGPUBackground {
58

69
constructor( renderer ) {
710

811
this.renderer = renderer;
912

10-
this.clearAlpha = 1;
11-
this.clearColor = new Color( 0x000000 );
13+
this.forceClear = false;
14+
15+
}
16+
17+
clear() {
18+
19+
this.forceClear = true;
1220

1321
}
1422

1523
render( scene ) {
1624

1725
const renderer = this.renderer;
1826
const background = ( scene.isScene === true ) ? scene.background : null;
19-
const clearColor = this.clearColor;
20-
let clearAlpha = this.clearAlpha;
21-
22-
let forceClear = false;
27+
let forceClear = this.forceClear;
2328

2429
if ( background === null ) {
2530

2631
// no background settings, use clear color configuration from the renderer
2732

28-
this.clearColor.copy( renderer._clearColor );
29-
this.clearAlpha = renderer._clearAlpha;
33+
_clearColor.copy( renderer._clearColor );
34+
_clearAlpha = renderer._clearAlpha;
3035

3136
} else if ( background !== null && background.isColor === true ) {
3237

3338
// background is an opaque color
3439

35-
clearColor.copy( background );
36-
clearAlpha = 1;
40+
_clearColor.copy( background );
41+
_clearAlpha = 1;
3742
forceClear = true;
3843

44+
} else {
45+
46+
console.error( 'WebGPURenderer: Unsupported background configuration.', background );
47+
3948
}
4049

4150
// configure render pass descriptor
4251

4352
const renderPassDescriptor = renderer._renderPassDescriptor;
4453
const colorAttachment = renderPassDescriptor.colorAttachments[ 0 ];
54+
const depthStencilAttachment = renderPassDescriptor.depthStencilAttachment;
4555

4656
if ( renderer.autoClear === true || forceClear === true ) {
4757

48-
colorAttachment.loadValue = { r: clearColor.r, g: clearColor.g, b: clearColor.b, a: clearAlpha };
58+
if ( renderer.autoClearColor === true ) {
59+
60+
colorAttachment.loadValue = { r: _clearColor.r, g: _clearColor.g, b: _clearColor.b, a: _clearAlpha };
61+
62+
} else {
63+
64+
colorAttachment.loadValue = GPULoadOp.Load;
65+
66+
}
67+
68+
if ( renderer.autoClearDepth === true ) {
69+
70+
depthStencilAttachment.depthLoadValue = renderer._clearDepth;
71+
72+
} else {
73+
74+
depthStencilAttachment.depthLoadValue = GPULoadOp.Load;
75+
76+
}
77+
78+
if ( renderer.autoClearStencil === true ) {
79+
80+
depthStencilAttachment.stencilLoadValue = renderer._clearDepth;
81+
82+
} else {
83+
84+
depthStencilAttachment.stencilLoadValue = GPULoadOp.Load;
85+
86+
}
4987

5088
} else {
5189

5290
colorAttachment.loadValue = GPULoadOp.Load;
91+
depthStencilAttachment.depthLoadValue = GPULoadOp.Load;
92+
depthStencilAttachment.stencilLoadValue = GPULoadOp.Load;
5393

5494
}
5595

96+
this.forceClear = false;
97+
5698
}
5799

58100
}

examples/jsm/renderers/webgpu/WebGPURenderer.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ class WebGPURenderer {
2626
this.parameters = parameters;
2727

2828
this.autoClear = true;
29+
this.autoClearColor = true;
30+
this.autoClearDepth = true;
31+
this.autoClearStencil = true;
32+
2933
this.sortObjects = true;
3034

3135
// internals
@@ -62,6 +66,8 @@ class WebGPURenderer {
6266

6367
this._clearAlpha = 1;
6468
this._clearColor = new Color( 0x000000 );
69+
this._clearDepth = 1;
70+
this._clearStencil = 0;
6571

6672
}
6773

@@ -281,6 +287,36 @@ class WebGPURenderer {
281287

282288
}
283289

290+
getClearDepth() {
291+
292+
return this._clearDepth;
293+
294+
}
295+
296+
setClearDepth( depth ) {
297+
298+
this._clearDepth = depth;
299+
300+
}
301+
302+
getClearStencil() {
303+
304+
return this._clearStencil;
305+
306+
}
307+
308+
setClearStencil( stencil ) {
309+
310+
this._clearStencil = stencil;
311+
312+
}
313+
314+
clear() {
315+
316+
this._background.clear();
317+
318+
}
319+
284320
dispose() {
285321

286322
this._objects.dispose();
@@ -617,9 +653,7 @@ async function initWebGPU( scope ) {
617653
} ],
618654
depthStencilAttachment: {
619655
attachment: null,
620-
depthLoadValue: 1,
621656
depthStoreOp: GPUStoreOp.Store,
622-
stencilLoadValue: 0,
623657
stencilStoreOp: GPUStoreOp.Store
624658
}
625659
};

0 commit comments

Comments
 (0)