brianzinn / react-babylonjs

React for Babylon 3D engine
https://brianzinn.github.io/react-babylonjs/
812 stars 102 forks source link

Question: How to export a scene as a png file? #202

Closed Avi-Cohen-Nehemia closed 2 years ago

Avi-Cohen-Nehemia commented 2 years ago

Hi There, I was looking in the examples and package's docs but couldn't find a way to export a scene as a png file. Since we are dealing with a canvas element, I thought I could achieve that using vanilla JS but all I get is an empty image.

const link = document.createElement('a');
link.download = 'image.png';
link.href = document.getElementById('my-canvas').toDataURL();
link.click();

I thought maybe there are some special conditions since it's a 3d canvas? Does the package provides a way to export png?

brianzinn commented 2 years ago

Can you share how your Engine is created. You need to set preserveDrawingBuffer: https://doc.babylonjs.com/divingDeeper/scene/renderToPNG#createscreenshot

You can use useScene hook (inside of <Scene ..> </Scene> children) to get the engine/scene and activeCamera if you have just one camera. Does that help?

brianzinn commented 2 years ago

hi @Avi-Cohen-Nehemia I made a code sandbox with the babylon tools: https://codesandbox.io/s/compassionate-ganguly-y2zrf?file=/src/App.tsx

clicking the button triggers download of file: screenshot_22-2-12_15-53

re-open if that doesn't solve your issue. cheers.

Avi-Cohen-Nehemia commented 2 years ago

That did help @brianzinn, thank you very much! 🙏🏼