pmndrs / xr

🤳 VR/AR for react-three-fiber
https://pmndrs.github.io/xr/docs/
Other
2.12k stars 152 forks source link

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

Closed chillbert closed 2 months ago

chillbert commented 8 months ago

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 commented 8 months ago

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 commented 7 months ago

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 commented 6 months ago

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.

bbohlender commented 2 months ago

in v6 the "player rig" is now called the XROrigin. There's a tutorial to set the player origin (and this the player's start position) here https://docs.pmnd.rs/xr/tutorials/origin . This can also allow to mount the origin to a different object so that the player moves in relation to this object.

If something is unclear feel free to reopen :)