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

Set the viewer (camera) startposition in AR/VR #304

Open
chillbert opened this issue Jan 13, 2024 · 3 comments
Open

Set the viewer (camera) startposition in AR/VR #304

chillbert opened this issue Jan 13, 2024 · 3 comments

Comments

@chillbert
Copy link

Is there a way to set the camera position when AR session is starting? Preferably to the same as the non-AR session position. In this example I am setting the camera 2 units in front of the model (0,0,0) - in the non-AR session it looks good, as soon as I start the AR, the camera jumps to 0,0,0.

 <XRButton
  mode={'AR'}
  sessionInit={{ optionalFeatures: ['local-floor'] }}
  enterOnly={false}
  exitOnly={false}
  onError={(error) => alert(JSON.stringify(error))}
>
  {(status) => `WebXR ${status}`}
</XRButton>
<Canvas shadows camera={{ position: [0,2,8], fov: 50 }} gl={{ logarithmicDepthBuffer: true, alpha:true }}>
    <XR>
      <Controllers />
      <Hands />
      <OrbitControls/>
      <fog attach="fog" args={["#d0d0d0", 20, 35]} />
      <ambientLight intensity={1} />
      <RayGrab>
        <XRBox />
      </RayGrab>
    </XR>
</Canvas>
@jjrchrds
Copy link

jjrchrds commented Jan 14, 2024

You can set the XR position with player, not sure yet how to just have it match the Canvas camera

  const { player } = useXR();
  player.position.z = 5;

@chillbert
Copy link
Author

chillbert commented Feb 6, 2024

Thanks, I didn't know useXR()... before I tried const { camera } = useThree(); but that is not available in VR/AR mode.

Isn't useXR a hook that should refresh itself? Since this is showing {"x":0,"y":0,"z":0} even when I (the player right? walk around...

const { player } = useXR();
return <Text color={"red"}>{JSON.stringify(player.position)}</Text>

manually retrieving the player.position like this also didn't help.. it stays: 0

   const { player } = useXR();
   const [position, setPosition] = useState({ x: 0, y: 0, z: 0 });
  useFrame((state, delta) =>{

     setPosition({
      x: player.position.x.toFixed(2), // Using toFixed for readability
      y: player.position.y.toFixed(2),
      z: player.position.z.toFixed(2)
    });
})

@Pipe-Runner
Copy link

Hello @chillbert,
The player object represents the XR rig; the actual camera and controllers are children of this rig. These objects move relative to the rig when you move your HMD or controllers. When you teleport or use a joystick, you can move the rig itself so that everything moves together. To answer your question now, find the child object and get its position.

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

3 participants