-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import * as React from "react"; | ||
import { useThree, useFrame } from "@react-three/fiber"; | ||
|
||
/** | ||
* true if webXR is supported | ||
*/ | ||
export const useIsWebXRSupported = () => { | ||
const [isSupported, setIsSupported] = React.useState<boolean | "loading">( | ||
"loading" | ||
); | ||
React.useLayoutEffect(() => { | ||
if (!navigator.xr) setIsSupported(false); | ||
else navigator.xr.isSessionSupported("immersive-ar").then(setIsSupported); | ||
}, []); | ||
return isSupported; | ||
}; | ||
|
||
/** | ||
* | ||
*/ | ||
export const useWebXRSession = () => { | ||
const [session, setSession] = React.useState<XRSession>(); | ||
|
||
const [error, setError] = React.useState<Error>(); | ||
if (error) throw error; | ||
|
||
const init = React.useCallback(() => { | ||
if (!navigator.xr || !navigator.xr.isSessionSupported("immersive-ar")) | ||
return; | ||
|
||
return navigator.xr | ||
.requestSession("immersive-ar") | ||
.then(setSession) | ||
.catch(setError); | ||
}, []); | ||
|
||
console.log("session", session); | ||
|
||
return { init, session }; | ||
}; | ||
|
||
export const WebXRControls = ({ | ||
webXRSession, | ||
}: { | ||
webXRSession: XRSession; | ||
}) => { | ||
const [error, setError] = React.useState<Error>(); | ||
if (error) throw error; | ||
|
||
const { gl: renderer, scene, camera } = useThree(); | ||
|
||
React.useLayoutEffect(() => { | ||
console.log("xrSession", webXRSession); | ||
|
||
const gl = renderer.getContext(); | ||
|
||
gl.makeXRCompatible().then(() => { | ||
webXRSession.updateRenderState({ | ||
baseLayer: new XRWebGLLayer(webXRSession, gl), | ||
}); | ||
}); | ||
|
||
let referenceSpace: XRReferenceSpace | XRBoundedReferenceSpace; | ||
|
||
webXRSession.requestReferenceSpace("local").then((r) => { | ||
referenceSpace = r; | ||
|
||
referenceSpace.addEventListener("reset", (evt) => { | ||
console.log("reset"); | ||
if (evt.transform) { | ||
// AR experiences typically should stay grounded to the real world. | ||
// If there's a known origin shift, compensate for it here. | ||
referenceSpace = referenceSpace.getOffsetReferenceSpace( | ||
evt.transform | ||
); | ||
} | ||
}); | ||
}); | ||
|
||
let cancel: number; | ||
|
||
const onXRFrame = (t: number, frame: XRFrame) => { | ||
const glLayer = webXRSession.renderState.baseLayer; | ||
const pose = referenceSpace && frame.getViewerPose(referenceSpace); | ||
|
||
if (glLayer) { | ||
// console.log("frame", frame, "pose", pose); | ||
|
||
gl.bindFramebuffer(gl.FRAMEBUFFER, glLayer.framebuffer); | ||
|
||
// Clear the framebuffer | ||
// renderer.clear(true, true, false); | ||
renderer.render(scene, camera); | ||
|
||
console.log("render"); | ||
|
||
if (pose) { | ||
// Loop through each of the views reported by the frame and draw them | ||
// into the corresponding viewport. | ||
for (const view of pose.views) { | ||
let viewport = glLayer.getViewport(view); | ||
} | ||
} | ||
} | ||
|
||
cancel = webXRSession.requestAnimationFrame(onXRFrame); | ||
}; | ||
|
||
cancel = webXRSession.requestAnimationFrame(onXRFrame); | ||
|
||
return () => webXRSession.cancelAnimationFrame(cancel); | ||
|
||
// session.updateRenderState({ baseLayer: new XRWebGLLayer(session, gl) }); | ||
}, [webXRSession, renderer]); | ||
|
||
return null; | ||
}; |