pmndrs / use-cannon

👋💣 physics based hooks for @react-three/fiber
https://cannon.pmnd.rs
2.76k stars 155 forks source link

useTrimesh scale #441

Closed holdwl2016 closed 8 months ago

holdwl2016 commented 11 months ago

direct use cannon-es


loader.load('/duck.glb', (gltf: any) => {
  const duck = gltf.scene.children[0].children[0]
  duck.scale.set(0.01, 0.01, 0.01)
  scene.add(duck)
  meshes.push(duck)
  const shape = new CANNON.Trimesh(duck.geometry.attributes.position.array, duck.geometry.index.array)
  shape.scale.set(0.01, 0.01, 0.01)
  const body = new CANNON.Body({ mass: 3, material, position: new CANNON.Vec3(0, 5, 0) })
  body.addShape(shape, new CANNON.Vec3(0, 0, 0))
  world.addBody(body)
  bodyMeshes.push(body)
})

I can use scale function: shape.scale.set(0.01, 0.01, 0.01)

use @react-three/cannon


const Duck = () => {
  const { nodes, materials }= useGLTF("/duck.glb");
  const data = nodes.LOD3spShape.geometry
  const [duckRef, api] = useTrimesh(() => ({ 
    mass: 2, position: [2, 5, 0],
    args: [data.attributes.position.array,  data.index.array] }), useRef<Mesh>(null))
  return (
    <mesh ref={duckRef} scale={0.01}
      geometry={nodes.LOD3spShape.geometry}
      material={materials["blinn3-fx"]}
    />
  )
}

how to use scale now? I didn't find this feature

las6 commented 9 months ago

@holdwl2016 did you ever find a proper way to do this? I've stumbled on to the same exact issue.

My current not-so-great workaround for this is to do a one-time scaling of the geometry to match the desired mesh scale value. You can do this in your case by calling data.scale(0.01, 0.01, 0.01) or nodes.LOD3spShape.geometry.scale() once with the desired scale before passing the data to useTrimesh(). FYI, scale() mutates the original data, so take that into account.

See for more information about the scale method on BufferGeometry.

holdwl2016 commented 8 months ago

thank you