Skip to content

Commit 4749f99

Browse files
authored
PMREMGenerator: Add optional renderTarget in fromScene() (#29959)
* Add optional `renderTarget` in `fromScene()` * Update PMREMGenerator.js * cleanup * cleanup
1 parent add7f9b commit 4749f99

File tree

1 file changed

+40
-18
lines changed

1 file changed

+40
-18
lines changed

src/renderers/common/extras/PMREMGenerator.js

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -129,23 +129,27 @@ class PMREMGenerator {
129129
* and far planes ensure the scene is rendered in its entirety (the cubeCamera
130130
* is placed at the origin).
131131
*/
132-
fromScene( scene, sigma = 0, near = 0.1, far = 100 ) {
132+
fromScene( scene, sigma = 0, near = 0.1, far = 100, renderTarget = null ) {
133+
134+
this._setSize( 256 );
133135

134136
if ( this._hasInitialized === false ) {
135137

136138
console.warn( 'THREE.PMREMGenerator: .fromScene() called before the backend is initialized. Try using .fromSceneAsync() instead.' );
137139

138-
return this.fromSceneAsync( scene, sigma, near, far );
140+
const cubeUVRenderTarget = renderTarget || this._allocateTargets();
141+
142+
this.fromSceneAsync( scene, sigma, near, far, cubeUVRenderTarget );
143+
144+
return cubeUVRenderTarget;
139145

140146
}
141147

142148
_oldTarget = this._renderer.getRenderTarget();
143149
_oldActiveCubeFace = this._renderer.getActiveCubeFace();
144150
_oldActiveMipmapLevel = this._renderer.getActiveMipmapLevel();
145151

146-
this._setSize( 256 );
147-
148-
const cubeUVRenderTarget = this._allocateTargets();
152+
const cubeUVRenderTarget = renderTarget || this._allocateTargets();
149153
cubeUVRenderTarget.depthBuffer = true;
150154

151155
this._sceneToCubeUV( scene, near, far, cubeUVRenderTarget );
@@ -164,11 +168,11 @@ class PMREMGenerator {
164168

165169
}
166170

167-
async fromSceneAsync( scene, sigma = 0, near = 0.1, far = 100 ) {
171+
async fromSceneAsync( scene, sigma = 0, near = 0.1, far = 100, renderTarget = null ) {
168172

169173
if ( this._hasInitialized === false ) await this._renderer.init();
170174

171-
return this.fromScene( scene, sigma, near, far );
175+
return this.fromScene( scene, sigma, near, far, renderTarget );
172176

173177
}
174178

@@ -183,7 +187,13 @@ class PMREMGenerator {
183187

184188
console.warn( 'THREE.PMREMGenerator: .fromEquirectangular() called before the backend is initialized. Try using .fromEquirectangularAsync() instead.' );
185189

186-
return this.fromEquirectangularAsync( equirectangular, renderTarget );
190+
this._setSizeFromTexture( equirectangular );
191+
192+
const cubeUVRenderTarget = renderTarget || this._allocateTargets();
193+
194+
this.fromEquirectangularAsync( equirectangular, cubeUVRenderTarget );
195+
196+
return cubeUVRenderTarget;
187197

188198
}
189199

@@ -210,7 +220,13 @@ class PMREMGenerator {
210220

211221
console.warn( 'THREE.PMREMGenerator: .fromCubemap() called before the backend is initialized. Try using .fromCubemapAsync() instead.' );
212222

213-
return this.fromCubemapAsync( cubemap, renderTarget );
223+
this._setSizeFromTexture( cubemap );
224+
225+
const cubeUVRenderTarget = renderTarget || this._allocateTargets();
226+
227+
this.fromCubemapAsync( cubemap, renderTarget );
228+
229+
return cubeUVRenderTarget;
214230

215231
}
216232

@@ -278,6 +294,20 @@ class PMREMGenerator {
278294

279295
// private interface
280296

297+
_setSizeFromTexture( texture ) {
298+
299+
if ( texture.mapping === CubeReflectionMapping || texture.mapping === CubeRefractionMapping ) {
300+
301+
this._setSize( texture.image.length === 0 ? 16 : ( texture.image[ 0 ].width || texture.image[ 0 ].image.width ) );
302+
303+
} else { // Equirectangular
304+
305+
this._setSize( texture.image.width / 4 );
306+
307+
}
308+
309+
}
310+
281311
_setSize( cubeSize ) {
282312

283313
this._lodMax = Math.floor( Math.log2( cubeSize ) );
@@ -309,15 +339,7 @@ class PMREMGenerator {
309339

310340
_fromTexture( texture, renderTarget ) {
311341

312-
if ( texture.mapping === CubeReflectionMapping || texture.mapping === CubeRefractionMapping ) {
313-
314-
this._setSize( texture.image.length === 0 ? 16 : ( texture.image[ 0 ].width || texture.image[ 0 ].image.width ) );
315-
316-
} else { // Equirectangular
317-
318-
this._setSize( texture.image.width / 4 );
319-
320-
}
342+
this._setSizeFromTexture( texture );
321343

322344
_oldTarget = this._renderer.getRenderTarget();
323345
_oldActiveCubeFace = this._renderer.getActiveCubeFace();

0 commit comments

Comments
 (0)