|
1 | 1 | import { GPULoadOp } from './constants.js'; |
2 | 2 | import { Color } from '../../../../build/three.module.js'; |
3 | 3 |
|
| 4 | +let _clearAlpha; |
| 5 | +const _clearColor = new Color(); |
| 6 | + |
4 | 7 | class WebGPUBackground { |
5 | 8 |
|
6 | 9 | constructor( renderer ) { |
7 | 10 |
|
8 | 11 | this.renderer = renderer; |
9 | 12 |
|
10 | | - this.clearAlpha = 1; |
11 | | - this.clearColor = new Color( 0x000000 ); |
| 13 | + this.forceClear = false; |
| 14 | + |
| 15 | + } |
| 16 | + |
| 17 | + clear() { |
| 18 | + |
| 19 | + this.forceClear = true; |
12 | 20 |
|
13 | 21 | } |
14 | 22 |
|
15 | 23 | render( scene ) { |
16 | 24 |
|
17 | 25 | const renderer = this.renderer; |
18 | 26 | 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; |
23 | 28 |
|
24 | 29 | if ( background === null ) { |
25 | 30 |
|
26 | 31 | // no background settings, use clear color configuration from the renderer |
27 | 32 |
|
28 | | - this.clearColor.copy( renderer._clearColor ); |
29 | | - this.clearAlpha = renderer._clearAlpha; |
| 33 | + _clearColor.copy( renderer._clearColor ); |
| 34 | + _clearAlpha = renderer._clearAlpha; |
30 | 35 |
|
31 | 36 | } else if ( background !== null && background.isColor === true ) { |
32 | 37 |
|
33 | 38 | // background is an opaque color |
34 | 39 |
|
35 | | - clearColor.copy( background ); |
36 | | - clearAlpha = 1; |
| 40 | + _clearColor.copy( background ); |
| 41 | + _clearAlpha = 1; |
37 | 42 | forceClear = true; |
38 | 43 |
|
| 44 | + } else { |
| 45 | + |
| 46 | + console.error( 'WebGPURenderer: Unsupported background configuration.', background ); |
| 47 | + |
39 | 48 | } |
40 | 49 |
|
41 | 50 | // configure render pass descriptor |
42 | 51 |
|
43 | 52 | const renderPassDescriptor = renderer._renderPassDescriptor; |
44 | 53 | const colorAttachment = renderPassDescriptor.colorAttachments[ 0 ]; |
| 54 | + const depthStencilAttachment = renderPassDescriptor.depthStencilAttachment; |
45 | 55 |
|
46 | 56 | if ( renderer.autoClear === true || forceClear === true ) { |
47 | 57 |
|
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 | + } |
49 | 87 |
|
50 | 88 | } else { |
51 | 89 |
|
52 | 90 | colorAttachment.loadValue = GPULoadOp.Load; |
| 91 | + depthStencilAttachment.depthLoadValue = GPULoadOp.Load; |
| 92 | + depthStencilAttachment.stencilLoadValue = GPULoadOp.Load; |
53 | 93 |
|
54 | 94 | } |
55 | 95 |
|
| 96 | + this.forceClear = false; |
| 97 | + |
56 | 98 | } |
57 | 99 |
|
58 | 100 | } |
|
0 commit comments