Skip to content

Commit 9f0b854

Browse files
authored
WebGPURenderer: Add setOutputRenderTarget() (#30459)
* Renderer: Add `setOutputTarget()` * renames * cleanup * Update Renderer.js
1 parent 91f9770 commit 9f0b854

File tree

4 files changed

+82
-20
lines changed

4 files changed

+82
-20
lines changed

src/nodes/display/PassNode.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -560,13 +560,34 @@ class PassNode extends TempNode {
560560
updateBefore( frame ) {
561561

562562
const { renderer } = frame;
563-
const { scene, camera } = this;
563+
const { scene } = this;
564564

565-
this._pixelRatio = renderer.getPixelRatio();
565+
let camera;
566+
let pixelRatio;
566567

567-
const size = renderer.getSize( _size );
568+
const outputRenderTarget = renderer.getOutputRenderTarget();
568569

569-
this.setSize( size.width, size.height );
570+
if ( outputRenderTarget && outputRenderTarget.isXRRenderTarget === true ) {
571+
572+
pixelRatio = 1;
573+
camera = renderer.xr.getCamera();
574+
575+
renderer.xr.updateCamera( camera );
576+
577+
_size.set( outputRenderTarget.width, outputRenderTarget.height );
578+
579+
} else {
580+
581+
camera = this.camera;
582+
pixelRatio = renderer.getPixelRatio();
583+
584+
renderer.getSize( _size );
585+
586+
}
587+
588+
this._pixelRatio = pixelRatio;
589+
590+
this.setSize( _size.width, _size.height );
570591

571592
const currentRenderTarget = renderer.getRenderTarget();
572593
const currentMRT = renderer.getMRT();

src/renderers/common/PostProcessing.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,13 @@ class PostProcessing {
101101

102102
//
103103

104+
const currentXR = renderer.xr.enabled;
105+
renderer.xr.enabled = false;
106+
104107
this._quadMesh.render( renderer );
105108

109+
renderer.xr.enabled = currentXR;
110+
106111
//
107112

108113
renderer.toneMapping = toneMapping;
@@ -164,8 +169,13 @@ class PostProcessing {
164169

165170
//
166171

172+
const currentXR = renderer.xr.enabled;
173+
renderer.xr.enabled = false;
174+
167175
await this._quadMesh.renderAsync( renderer );
168176

177+
renderer.xr.enabled = currentXR;
178+
169179
//
170180

171181
renderer.toneMapping = toneMapping;

src/renderers/common/Renderer.js

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,15 @@ class Renderer {
509509
*/
510510
this._activeMipmapLevel = 0;
511511

512+
/**
513+
* The current output render target.
514+
*
515+
* @private
516+
* @type {?RenderTarget}
517+
* @default null
518+
*/
519+
this._outputRenderTarget = null;
520+
512521
/**
513522
* The MRT setting.
514523
*
@@ -1206,7 +1215,7 @@ class Renderer {
12061215

12071216
const sceneRef = ( scene.isScene === true ) ? scene : _scene;
12081217

1209-
const outputRenderTarget = this._renderTarget;
1218+
const outputRenderTarget = this._renderTarget || this._outputRenderTarget;
12101219

12111220
const activeCubeFace = this._activeCubeFace;
12121221
const activeMipmapLevel = this._activeMipmapLevel;
@@ -2019,7 +2028,7 @@ class Renderer {
20192028
*/
20202029
get currentToneMapping() {
20212030

2022-
return this._renderTarget !== null ? NoToneMapping : this.toneMapping;
2031+
return this.isOutputTarget ? this.toneMapping : NoToneMapping;
20232032

20242033
}
20252034

@@ -2031,7 +2040,18 @@ class Renderer {
20312040
*/
20322041
get currentColorSpace() {
20332042

2034-
return this._renderTarget !== null ? LinearSRGBColorSpace : this.outputColorSpace;
2043+
return this.isOutputTarget ? this.outputColorSpace : LinearSRGBColorSpace;
2044+
2045+
}
2046+
2047+
/**
2048+
* Returns `true` if the rendering settings are set to screen output.
2049+
*
2050+
* @returns {boolean} True if the current render target is the same of output render target or `null`, otherwise false.
2051+
*/
2052+
get isOutputTarget() {
2053+
2054+
return this._renderTarget === this._outputRenderTarget;
20352055

20362056
}
20372057

@@ -2094,6 +2114,28 @@ class Renderer {
20942114

20952115
}
20962116

2117+
/**
2118+
* Sets the output render target for the renderer.
2119+
*
2120+
* @param {Object} renderTarget - The render target to set as the output target.
2121+
*/
2122+
setOutputRenderTarget( renderTarget ) {
2123+
2124+
this._outputRenderTarget = renderTarget;
2125+
2126+
}
2127+
2128+
/**
2129+
* Returns the current output target.
2130+
*
2131+
* @return {?RenderTarget} The current output render target. Returns `null` if no output target is set.
2132+
*/
2133+
getOutputRenderTarget() {
2134+
2135+
return this._outputRenderTarget;
2136+
2137+
}
2138+
20972139
/**
20982140
* Callback for {@link Renderer#setRenderObjectFunction}.
20992141
*

src/renderers/common/XRManager.js

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,6 @@ class XRManager extends EventDispatcher {
136136
*/
137137
this._controllerInputSources = [];
138138

139-
/**
140-
* The current render target of the renderer.
141-
*
142-
* @private
143-
* @type {?RenderTarget}
144-
* @default null
145-
*/
146-
this._currentRenderTarget = null;
147-
148139
/**
149140
* The XR render target that represents the rendering destination
150141
* during an active XR session.
@@ -567,8 +558,6 @@ class XRManager extends EventDispatcher {
567558

568559
if ( backend.isWebGPUBackend === true ) throw new Error( 'THREE.XRManager: XR is currently not supported with a WebGPU backend. Use WebGL by passing "{ forceWebGL: true }" to the constructor of the renderer.' );
569560

570-
this._currentRenderTarget = renderer.getRenderTarget();
571-
572561
session.addEventListener( 'select', this._onSessionEvent );
573562
session.addEventListener( 'selectstart', this._onSessionEvent );
574563
session.addEventListener( 'selectend', this._onSessionEvent );
@@ -972,7 +961,7 @@ function onSessionEnd() {
972961
// restore framebuffer/rendering state
973962

974963
renderer.backend.setXRTarget( null );
975-
renderer.setRenderTarget( this._currentRenderTarget );
964+
renderer.setOutputRenderTarget( null );
976965

977966
this._session = null;
978967
this._xrRenderTarget = null;
@@ -1159,7 +1148,7 @@ function onAnimationFrame( time, frame ) {
11591148

11601149
}
11611150

1162-
renderer.setRenderTarget( this._xrRenderTarget );
1151+
renderer.setOutputRenderTarget( this._xrRenderTarget );
11631152

11641153
}
11651154

0 commit comments

Comments
 (0)