brianzinn / react-babylonjs

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

Uncaught Error: No camera defined after upgrade #242

Closed smoya-ekoo closed 1 year ago

smoya-ekoo commented 1 year ago

I have the error after upgrade node modules today

looks like if engine._renderLoop run before camera is mounted Before upgrade works well but reverse changes doesn't solve it 😢

Uncaught Error: No camera defined

//package.json
"@babylonjs/core": "^5.22.1",
"@babylonjs/gui": "^5.22.1",
"@babylonjs/loaders": "^5.22.1",
"@babylonjs/materials": "^5.22.1",
"@babylonjs/serializers": "^5.22.1",
"@babylonjs/gui-editor": "^5.22.1",
"@babylonjs/inspector": "^5.22.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-babylonjs": "^3.1.7",
...

node v16.17.0 plantform windows 10 x64

with this simple code

<Engine>
  <Scene autoClear={false} clearColor={new Color4(0, 0, 0, 0)}>
    <freeCamera
        name="mainCamera"
        position={new Vector3(0, 0, 0)}
        rotation={new Vector3(0, Angle.FromDegrees(0).radians(), 0)}
        target={new Vector3(0, 0, 0)}
    />
  </Scene>
</Engine>

I try with clean install but problem persists

Someone have same problem?

brianzinn commented 1 year ago

Thanks for reporting - I can check when home in 2-3 hours

brianzinn commented 1 year ago

@smoya-ekoo - are you able to reproduce with a code sandbox or github repo? This is working on react-bablonjs 3.1.9 (just published), react 18 and @babylonjs/* 5.2.1: https://codesandbox.io/s/floral-water-els13w?file=/src/App.tsx

smoya-ekoo commented 1 year ago

Yes! happen after upgrade, thanks

https://codesandbox.io/s/solitary-react18-2-s1n5cg?file=/src/App.tsx

image

image

brianzinn commented 1 year ago

hi @smoya-ekoo - Thanks for the code sandbox - seems that the scheduling in React 18.2 is a bit different and I was making assumption that the cameras would be supplied with the <Engine ../> on first render. Anyway, can you give 3.1.10 a try now - I just tested on your codesandbox.

I just did a quick fix, but maybe I should add something to generate warnings for people who forget to add a camera - maybe by putting a warning if the camera doesn't show up after a certain number of renders. I can maybe also indicate a higher priority for rendering...