Skip to content

Commit a6164a6

Browse files
committed
fix: setting and resetting xrSpace on current and previous origin
1 parent 02b638b commit a6164a6

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

examples/layers/app.tsx

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Canvas, useThree } from '@react-three/fiber'
2-
import { createXRStore, useHover, XR, XRLayer } from '@react-three/xr'
2+
import { createXRStore, useHover, XR, XRLayer, XROrigin } from '@react-three/xr'
33
import { Text } from '@react-three/drei'
44
import { useEffect, useMemo, useRef } from 'react'
55
import { Mesh, SRGBColorSpace, VideoTexture } from 'three'
@@ -27,29 +27,30 @@ export function App() {
2727
<Canvas
2828
events={() => ({ enabled: false, priority: 0 })}
2929
style={{ width: '100%', flexGrow: 1 }}
30-
camera={{ position: [0, 1.5, 0], rotation: [0, 0, 0] }}
30+
camera={{ position: [0, 0, 0], rotation: [0, 0, 0] }}
3131
>
3232
<SwitchToXRPointerEvents />
3333
<XR store={store}>
34-
<Text scale={0.03} color="black" position={[-0.6, 1.78, -0.5]}>
34+
<Text scale={0.03} color="black" position={[-0.6, 0.28, -0.5]}>
3535
32x32 XRLayer with DPR=32
3636
</Text>
37-
<XRLayer dpr={32} pixelWidth={32} pixelHeight={32} position={[-0.6, 1.5, -0.5]} scale={0.5} shape="quad">
37+
<XROrigin position={[0, -1.5, 0]} />
38+
<XRLayer dpr={32} pixelWidth={32} pixelHeight={32} position={[-0.6, 0, -0.5]} scale={0.5} shape="quad">
3839
<mesh>
3940
<boxGeometry />
4041
<meshBasicMaterial color="red" toneMapped={false} />
4142
</mesh>
4243
</XRLayer>
4344

44-
<Text scale={0.03} color="black" position={[0, 1.78, -0.5]}>
45+
<Text scale={0.03} color="black" position={[0, 0.28, -0.5]}>
4546
With XRLayer
4647
</Text>
47-
<XRLayer position={[0, 1.5, -0.5]} onClick={() => video.play()} scale={0.5} shape="quad" src={video} />
48+
<XRLayer position={[0, 0, -0.5]} onClick={() => video.play()} scale={0.5} shape="quad" src={video} />
4849

49-
<Text scale={0.03} color="black" position={[0.6, 1.78, -0.5]}>
50+
<Text scale={0.03} color="black" position={[0.6, 0.28, -0.5]}>
5051
Without XRLayer
5152
</Text>
52-
<mesh position={[0.6, 1.5, -0.5]} scale={0.5}>
53+
<mesh position={[0.6, 0, -0.5]} scale={0.5}>
5354
<planeGeometry />
5455
<meshBasicMaterial map={videoTexture} toneMapped={false} />
5556
</mesh>

packages/xr/src/store.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -461,18 +461,22 @@ export function createXRStore<T extends XRElementImplementations>(options?: XRSt
461461
//update origin
462462
const origin = camera.parent ?? scene
463463
if (state.origin != origin) {
464-
origin.xrSpace = referenceSpace
465464
update ??= {}
466465
update.origin = origin
467466
}
468467

469468
//update reference space
470469
if (referenceSpace != state.originReferenceSpace) {
471-
origin.xrSpace = referenceSpace
472470
update ??= {}
473471
update.originReferenceSpace = referenceSpace
474472
}
475473

474+
//set xr space on current origin (and reset on previous)
475+
origin.xrSpace = referenceSpace
476+
if (state.origin != origin && state.origin != null) {
477+
state.origin.xrSpace = undefined
478+
}
479+
476480
if (frame != null) {
477481
if (xrManager != null) {
478482
updateSession(store, frame, xrManager)

0 commit comments

Comments
 (0)