Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing Pointer coordinates on canvas "onCreated" callback does not update values on neither "useThree" nor "useFrame" state #3284

Open
pedroalmeida415 opened this issue Jun 11, 2024 · 0 comments

Comments

@pedroalmeida415
Copy link

pedroalmeida415 commented Jun 11, 2024

When setting up my canvas, I tried to assign a default value to the Pointer property like so:

onCreated={(state) => {
    state.pointer.set(0, 1) // desired initial position
    state.raycaster.setFromCamera(state.pointer, state.camera)
    console.log(state.pointer) // Vector2 { "x": 0, "y": 1} -- values are correctly set
}}

But when getting the state from inside one of my components, the values haven't changed:

  const pointer = useThree((state) => state.pointer)
  console.log(pointer) // Vector2 { "x": 0, "y": 0} -- wrong values outside useFrame, from the very first render

.
.
.

  const uMouseVec = new THREE.Vector2()

  useFrame((state, delta) => {
    console.log(state.pointer) // Vector2 { "x": 0, "y": 0} -- wrong values inside useFrame as well
    state.raycaster.setFromCamera(state.pointer, state.camera)
    const intersects = state.raycaster.intersectObject(planeArea.current)

    if (intersects.length > 0) {
      variable.material.uniforms.uMouse.value = uMouseVec.set(intersects[0].point.x, intersects[0].point.y)
    }
.
.
.
  })

Calling pointer.set(1,1) inside the component correctly changes the values, but I can't understand why it doesn't work to set the default value on the onCreated callback, I thought it's whole purpose was to do things just like this.

If I'm doing something wrong, please let me know!
Thanks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant