Search code examples
reactjsthree.jsreact-three-fiber

Getting object from three fiber components


I am kinda new to react and three fiber so forgive me if this is a dumb question.

Say I have a mesh like so:

<mesh position={functionToGetPos()}>
  <sphereGeometry args={[1, 32, 32]}/>
  <meshStandardMaterial color="hotpink"/>
</mesh>

and I have a function in the component where I want that mesh to be referenced in:

const myFunc = () => {
  let pos = new THREE.Vector3();
  pos = pos.setFromMatrixPosition(object.matrixWorld);
  ...
}

where I want the mesh to be the object.

How would I go about doing that?


Solution

  • You should be able to use refs like this:

    const meshRef = useRef<Mesh>(null);
    
    return (
       <mesh ref={meshRef}/>
    );
    

    after which you can use the ref using meshRef.current.