pmndrs / react-three-rapier

🤺 Rapier physics in React
https://react-three-rapier.pmnd.rs
MIT License
1.03k stars 57 forks source link

Dynamic creation of RigidBody for specific models within a glTF file. #617

Closed JacobOsense2223 closed 7 months ago

JacobOsense2223 commented 7 months ago

How can I dynamically generate RigidBody for specific sub-objects of a glTF model?

wiledal commented 7 months ago

When importing the gltf, you get an outline of the entire scene. You can use that outline to map out each child and wrap them within a RigidBody.

const Component = () => {
  const gltf = useGLTF('/something.gltf')

  return <>
    {Object.values(gltf.nodes).map(node => <RigidBody>{node}</RigidBody>)}
  </>
}

Example: https://codesandbox.io/p/sandbox/angery-berbs--asset-transforms-from-blender-ebl16y?file=%2Fsrc%2FApp.tsx%3A8%2C7

In this example I have tagged each object in Blender with userData which allows me to import it and transform each object separately, with unique settings.