brianzinn / react-babylonjs

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

add forward ref to model #270

Closed Spoof14 closed 7 months ago

Spoof14 commented 1 year ago

Trying to use useClick with <Model \/> requires a ref to be passed

Spoof14 commented 1 year ago

Doesn't work. Seems <Model \/> can't take click / hover events?

brianzinn commented 1 year ago

I'd accept that PR without the package.json. For hovering a model it should work from the mesh ref, since it's using an action manager. Does this work: https://brianzinn.github.io/react-babylonjs/examples/models/props

The useHover should work, but the useClick is an issue as it will need to 'click' on the rootMesh I think that in many cases is a TransformNode...

fabiopapajani commented 1 year ago

Hello everyone! I'm using <Model /> in react-babylonjs to do those things:

What I'm encountering is:

I'm wondering if I'm doing something wrong? Or is another way to do that?

Here is the example of what I'm trying to do: https://codesandbox.io/s/react-babylonjs-model-cb145w?file=/src/App.tsx

brianzinn commented 1 year ago

@fabiopapajani onDragEndObservable() isn't supported by planeRotationGizmo: https://doc.babylonjs.com/typedoc/classes/BABYLON.PlaneRotationGizmo image

For the ref not working that's because the Model Functional Component would need a forward ref as per this PR - I would accept this PR if it didn't modify package.json and lock files.

Spoof14 commented 1 year ago

I reverted all package json changes, but I'm just wondering, why is there the node > 16 but < 17 requirement?

brianzinn commented 7 months ago

I should have merged this a lot earlier. Works really well - Thank-you for your contribution.

const MyComponent = () => {
   const modelRef = useRef<Nullable<AbstractMesh>>(null);
   const checkModel = () => {
    console.log('model ref:', modelRef.current);
    if(modelRef.current) {
      modelRef.current.rotation.y += (Math.PI / 32);
    }
  }

  return (
     <button onClick={checkModel}>rotate model</button>
     <Engine ...>
      <Scene ...>
         ... lights/etc
         <Suspense ...>
            <Model ref={modelRef} />
         </Suspense>
      </Scene>
    </Engine>
  )
}
brianzinn commented 7 months ago

I can't merge this though due to conflicts. Do you mind rebasing @Spoof14 - you can just put your Model.tsx (no .yarn files).

Spoof14 commented 7 months ago

I rebased and pushed. Can you check everything is working as expected? I haven't touched this for months

brianzinn commented 7 months ago

Thanks @Spoof14 - I'll double check and publish.