Skip to content

Commit d1c9b3b

Browse files
authored
Fix ScreenshotTools (#16081)
* Fix oneffecterrorobs * Fix NormalMaterial with ThinInstances * Fix Animation loop in CreateAndStartAnimation * Fix lineMesh cloned alpha values * Fix recursive loop in onPointerOutAction * Fix ScreenshotTools
1 parent 199362d commit d1c9b3b

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed

packages/dev/core/src/Meshes/csg.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,10 @@ export class CSG {
568568
}
569569

570570
const subMeshes = mesh.subMeshes;
571+
if (!subMeshes) {
572+
// eslint-disable-next-line no-throw-literal
573+
throw "BABYLON.CSG: Mesh has no submeshes";
574+
}
571575

572576
for (let sm = 0, sml = subMeshes.length; sm < sml; sm++) {
573577
for (let i = subMeshes[sm].indexStart, il = subMeshes[sm].indexCount + subMeshes[sm].indexStart; i < il; i += 3) {

packages/dev/core/src/Misc/screenshotTools.ts

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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
};

packages/dev/core/src/PostProcesses/RenderPipeline/Pipelines/ssao2RenderingPipeline.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,8 @@ export class SSAO2RenderingPipeline extends PostProcessRenderPipeline {
467467
return;
468468
}
469469

470-
const ssaoCombineSize = horizontal ? this._ssaoCombinePostProcess.width : this._ssaoCombinePostProcess.height;
470+
const ratio = this._ratio.blurRatio || this._ratio;
471+
const ssaoCombineSize = horizontal ? this._originalColorPostProcess.width * ratio : this._originalColorPostProcess.height * ratio;
471472
const originalColorSize = horizontal ? this._originalColorPostProcess.width : this._originalColorPostProcess.height;
472473

473474
effect.setFloat("outSize", ssaoCombineSize > 0 ? ssaoCombineSize : originalColorSize);

0 commit comments

Comments
 (0)