react-babylonjs icon indicating copy to clipboard operation
react-babylonjs copied to clipboard

Babylon Version 8 React 18

Open dennemark opened this issue 7 months ago • 16 comments

Hi @brianzinn , react-babylonjs still works smoothly. Pretty solid foundations most of the time I would say. But in our app we stayed for quite a while on version 7.10. Now we upgraded to use CSG2. I am using react-babylonjs with v 3.2.2 and babylonjs with v 7.54.3. This all works out well. If I change to 3.2.4 or set it to ^3.2.2 which will also install 3.2.4, it will stop working because of react 19. Since I currently do not dare to move to react 19 yet, I just pinned it to 3.2.2. However if I use babylon v8, I am getting issues with fromInstance, complaining that the Mesh class is not correct. https://github.com/brianzinn/react-babylonjs/blob/7b3613e5468e14a75ca42b959f9eec117c4f9c1a/packages/react-babylonjs/src/ReactBabylonJSHostConfig.ts#L493 Moving back to v7.54.3 solves the issue. I am now wondering if it would be a hassle to still support react 18 and babylon v8. But I also read in release notes or somewhere, that you try to support r18 and r19 anyways. So this might solve this issue. Just wondering if you have an opinion about this. Kind regards!

Btw. is there a way to sponsor you or send some coffee tip?

dennemark avatar May 10 '25 15:05 dennemark

Hi @dennemark - I’m here for free - thanks!

I want to get a version that supports react 18+19. I just need to check react version and make a small change in renderer. To support Babylon 7+8 - I need to look at the error. Did you try reference AbstractMesh instead? Do you have a strongly typed ref? Can you share more code?

brianzinn avatar May 10 '25 16:05 brianzinn

Hi, I am afk now. Might need to show some code then on monday.

But i just loaded a gltf asset via useAssetManager and took a mesh of loadedMeshes (setMeshes(task.loadedMeshes[0].mesh[0])) and placed in onto <mesh fromInstance={loadedMesh} />

So the loadedMesh was getClassName() Mesh and clazz too. But i think because of different versions it would not align. Maybe because react-babylonjs only had babylong resolution from version 5 to 7 in my package lock file.

dennemark avatar May 10 '25 16:05 dennemark

Ok - let’s connect next week and we can get it working then. I shouldn’t have done a patch version - hope we can fix it soon. Cheers.

brianzinn avatar May 10 '25 16:05 brianzinn

Can you try @latest - there is a beta build.

brianzinn avatar May 11 '25 06:05 brianzinn

So far so good on 3.2.5-beta.2! Thanks a lot!

dennemark avatar May 13 '25 07:05 dennemark

thank @dennemark - i'll publish as latest. hopefully that fixes the React 19 woes as well...

brianzinn avatar May 14 '25 23:05 brianzinn

Hi, there might be still an issue. I do not understand why it was not there before. Maybe i did not install my packages properly, but i am getting a fromInstance error, when i use babylonjs version 8 and react-babylonjs 3.2.5-beta.2... I think it only came up now, since I might have deleted .vite/deps folder. Unfortunately the error message does not point me to the correct line. fromInstance wrong type.

Here is what I could debug. TreeAsset that is throwing the error is using useAssetManager. But the props of it are stranegly undefined, even though they normal should not. So I am guessing the reconciler is not forwarding the props properly:

Here I use TreeAsset - props should be always set: Image

Here we can see how useAssetManager is used: Image

Debugging in browser shows me this:

Image Props are strangely undefined: Image

Continuing a step with the debugger will show me this error (i left out my paths thats why i made two screenshots):

Image Image

Now TreeAsset is being called around three times. The first two times it has properties. But I guess because it is suspended, it does not render with props. Then however suspense finishes and does not have any properties. Not sure if that has to do with useSyncExternalStore. But I guess Suspense works bit different in react 18 vs 19?

dennemark avatar May 16 '25 09:05 dennemark

"dispatcher is null" - are you sure you don't have a mismatch of react and react-dom? also, only one version of react in your app? are you able to create a small repro repo? i cannot reproduce here. i have gone through breaking changes in Babylon 8 and React 19 and it looks like asset manager breaking changes and suspense improvements would not have this impact.

brianzinn avatar May 16 '25 19:05 brianzinn

Sure i am going to have a closer look! I was using react 18.3.1. But i was also surprised of this issue appearing, since it worked on tuesday, but yesterday it suddenly did not. Might be an issue otherwise with checking out to the wrong branch. But i think i cleaned up vite cache. Anyways. With version 7.53.4 and 3.22 it works so far on react 18.

dennemark avatar May 17 '25 13:05 dennemark

I just realized - if it's just a typings issue - it may be due to the global JSX Intrinsic elements. Those changed in React 19 - I think I need to dual write those...

brianzinn avatar May 21 '25 01:05 brianzinn

Maybe that is the case. I just tried again after deleting my node_modules folder. Then I am getting the fromInstance error. "@babylonjs/core": "8.9.0", "react": "^18.3.1", "react-babylonjs": "3.2.5-beta.2", "react-dom": "^18.3.1",

dennemark avatar May 22 '25 15:05 dennemark

Hi, I am currently trying to move to react 19 in my repo. Most things work fine, but there is an issue with useAssetManager still. Different though to the before mentioned issues. The asst manager loads my asset but the position data is undefined. Is this something you have stumbled upon or can reproduce? I can try to make a reproduction.

  const assetManagerResult = useAssetManager([
    {
      taskType: TaskType.Mesh,
      rootUrl: props.rootUrl ?? '',
      sceneFilename: props.filename,
      name: 'Tree',
    },
  ])
  const treeTask = assetManagerResult.taskNameMap['Tree'] as MeshAssetTask

  useLayoutEffect(() => {
    if (treeTask.isCompleted) {
      const rootMesh = treeTask.loadedMeshes.find((m) => m.name === '__root__')
      const meshes = treeTask.loadedMeshes as Mesh[]
      console.log(
        'tree',
        meshes.map((m) => m.getVertexBuffer(VertexBuffer.PositionKind)),
      ) // tree undefined
,[treeTask.isCompleted])

dennemark avatar Jul 30 '25 16:07 dennemark

I don’t think when “isCompleted” charges it would trigger a re-render. I’m on vacation this week - can you confirm your use layout effect is triggered?

brianzinn avatar Jul 30 '25 16:07 brianzinn

going to have a look! have good time :)

dennemark avatar Jul 30 '25 16:07 dennemark

It was the use of useLayoutEffect - i had to switch to useEffect. Seems like their is a slight difference in r18 and r19 in when things are rendered. Going to keep this open until i managed to switch. Dont know if other people still demand babylon v8 and r8 to work.

dennemark avatar Jul 31 '25 07:07 dennemark

I need to find the time to make fully compatible. Thanks for reporting

brianzinn avatar Jul 31 '25 18:07 brianzinn