I am taking a screenshot of the scene and I want to display that screenshot on another page by having it in localStorage.
Every screenshot that I take is nothing but transparent. Here is the code.
useEffect(() => {
const { current: canvas } = reactCanvas;
if (!canvas) return;
const engine = new Engine(
canvas,
antialias,
engineOptions,
adaptToDeviceRatio,
{ preserveDrawingBuffer: true, stencil: true }
);
const scene = new Scene(engine, sceneOptions);
if (scene.isReady()) {
onSceneReady(scene);
} else {
scene.onReadyObservable.addOnce((scene) => onSceneReady(scene));
}
scene.clearColor = new BABYLON.Color4(0, 0, 0, 0);
engine.runRenderLoop(() => {
if (typeof onRender === 'function') onRender(scene);
scene.render();
});
const resize = () => {
scene.getEngine().resize();
};
setTimeout(() => {
const camera = scene.cameras[0];
BABYLON.Tools.CreateScreenshot(
engine,
camera,
{ width: 1024, height: 300 },
function (data) {
console.log(data);
window.localStorage.setItem('productImage', data);
}
);
}, 2000);
if (window) {
window.addEventListener('resize', resize);
}
return () => {
scene.getEngine().dispose();
if (window) {
window.removeEventListener('resize', resize);
}
};
}, [
antialias,
engineOptions,
adaptToDeviceRatio,
sceneOptions,
onRender,
]);
or
I use time out to wait for the scene to be rendered and then to take a screenshot.