@@ -259,8 +259,32 @@ export function CreateScreenshotUsingRenderTarget(
259259 // Prevent engine to render on screen while we do the screenshot
260260 engine . skipFrameRender = true ;
261261
262- const originalSize = { width : engine . getRenderWidth ( ) , height : engine . getRenderHeight ( ) } ;
263- engine . setSize ( width , height ) ; // we need this call to trigger onResizeObservable with the screenshot width/height on all the subsystems that are observing this event and that needs to (re)create some resources with the right dimensions
262+ const originalGetRenderWidth = engine . getRenderWidth ;
263+ const originalGetRenderHeight = engine . getRenderHeight ;
264+
265+ // Override getRenderWidth and getRenderHeight to return the desired size of the render
266+ // A few internal methods are relying on the canvas size to compute the render size
267+ // so we need to override these methods to ensure the correct size is used during the preparation of the render
268+ // as well as the screenshot
269+ engine . getRenderWidth = ( useScreen = false ) => {
270+ if ( ! useScreen && engine . _currentRenderTarget ) {
271+ return engine . _currentRenderTarget . width ;
272+ }
273+
274+ return width ;
275+ } ;
276+ engine . getRenderHeight = ( useScreen = false ) => {
277+ if ( ! useScreen && engine . _currentRenderTarget ) {
278+ return engine . _currentRenderTarget . height ;
279+ }
280+
281+ return height ;
282+ } ;
283+
284+ // Trigger a resize event to ensure the intermediate renders have the correct size
285+ if ( engine . onResizeObservable . hasObservers ( ) ) {
286+ engine . onResizeObservable . notifyObservers ( engine ) ;
287+ }
264288
265289 const scene = camera . getScene ( ) ;
266290
@@ -312,15 +336,47 @@ export function CreateScreenshotUsingRenderTarget(
312336 } ) ;
313337 scene . incrementRenderId ( ) ;
314338 scene . resetCachedMaterial ( ) ;
315- texture . render ( true ) ;
316- engine . setSize ( originalSize . width , originalSize . height ) ;
317- camera . getProjectionMatrix ( true ) ; // Force cache refresh;
318339
319- engine . skipFrameRender = false ;
340+ // Record the original scene setup
341+ const originalCamera = scene . activeCamera ;
342+ const originalCameras = scene . activeCameras ;
343+ const originalOutputRenderTarget = camera . outputRenderTarget ;
344+ const originalSpritesEnabled = scene . spritesEnabled ;
345+
346+ // Swap with the requested one
347+ scene . activeCamera = camera ;
348+ scene . activeCameras = null ;
349+ camera . outputRenderTarget = texture ;
350+ scene . spritesEnabled = renderSprites ;
351+
352+ // render the scene on the RTT
353+ try {
354+ scene . render ( ) ;
355+ } finally {
356+ // Restore the original scene camera setup
357+ scene . activeCamera = originalCamera ;
358+ scene . activeCameras = originalCameras ;
359+ camera . outputRenderTarget = originalOutputRenderTarget ;
360+ scene . spritesEnabled = originalSpritesEnabled ;
361+
362+ engine . getRenderWidth = originalGetRenderWidth ;
363+ engine . getRenderHeight = originalGetRenderHeight ;
364+
365+ // Trigger a resize event to ensure the intermediate renders have the correct size
366+ if ( engine . onResizeObservable . hasObservers ( ) ) {
367+ engine . onResizeObservable . notifyObservers ( engine ) ;
368+ }
369+
370+ camera . getProjectionMatrix ( true ) ; // Force cache refresh;
371+
372+ engine . skipFrameRender = false ;
373+ }
320374 } ,
321375 ( ) => {
322376 // Restore engine frame rendering on error
323377 engine . skipFrameRender = false ;
378+ engine . getRenderWidth = originalGetRenderWidth ;
379+ engine . getRenderHeight = originalGetRenderHeight ;
324380 }
325381 ) ;
326382 } ;
0 commit comments