brianzinn / react-babylonjs

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

Loaded Model merge meshes #216

Closed dottodot closed 2 years ago

dottodot commented 2 years ago

I'm trying to merge meshes on a loaded model but it always returns null and I'm not sure if it's react-babylonjs issue or not.

I'm using this https://github.com/KhronosGroup/glTF-Sample-Models/blob/master/2.0/Box/glTF-Binary/Box.glb and implementing as follows.

function BoxModel() {
  const onModelLoaded = ({meshes}) => {
    const merged = Mesh.MergeMeshes(meshes);
    console.log(merged);
  };

  return (
    <Suspense fallback={null}>
      <Model
        rootUrl={`models/`}
        sceneFilename="Box.glb"
        scaleToDimension={3}
        position={new Vector3(0, -2, 0)}
        onModelLoaded={onModelLoaded}
      />
    </Suspense>
  );
}

If I use a mesh created using MeshBuilder it works as expected.

MeshBuilder.CreateSphere("sphere1", {diameter: 2, segments: 16}, scene);
brianzinn commented 2 years ago

I've never used MergeMeshes, but it looks like it ought to work - was just reading the docs here: https://doc.babylonjs.com/divingDeeper/mesh/mergeMeshes

One thing that I do is parent to a rootmesh in the Component, so this may work:

const onModelLoaded = ({rootMesh}) => {
    const merged = Mesh.MergeMeshes([rootMesh]);
    console.log(merged);
  };

I would probably drop down to the hook itself as likely you want to dispose the original meshes (2nd parameter). The component is here - it's mainly a wrapper that calls a hook and disposes the model on component unmount: https://github.com/brianzinn/react-babylonjs/blob/master/packages/react-babylonjs/src/customComponents/Model.tsx

Are you able to share a code sandbox otherswise?

brianzinn commented 2 years ago

Closing from inactivity and no reply - I am unable to reproduce locally. Please re-open if you have more feedback - thank you.