pmndrs / use-cannon

👋💣 physics based hooks for @react-three/fiber

Home Page:https://cannon.pmnd.rs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

useTrimesh scale

holdwl2016 opened this issue · comments

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

commented

@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.

thank you