Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Platane committed Sep 7, 2024
1 parent 436f7ea commit 273e1ad
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 26 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,8 @@ yarn dev

- ["Dice"](https://skfb.ly/6RtsC) by tnRaro is licensed under [CC BY 4.0](http://creativecommons.org/licenses/by/4.0/).
- ["Lebombo"](https://hdrihaven.com/hdri/?c=indoor&h=lebombo) by Greg Zaal is licensed under [CC0](https://creativecommons.org/share-your-work/public-domain/cc0/)

# TODO

- framebuffer swapping seems ok-ish
- touch detection is broken
7 changes: 6 additions & 1 deletion src/App/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,12 @@ export const App = ({ onReady, onProgress, webXRSession, started }: Props) => {
<ErrorBoundary onError={setError}>
{xr8 && <XR8Controls xr8={xr8} onReady={() => setXr8Ready(true)} />}

{webXRSession && <WebXRControls webXRSession={webXRSession} />}
{webXRSession && (
<WebXRControls
webXRSession={webXRSession}
onPoseFound={() => console.log("pose found")}
/>
)}

<React.Suspense fallback={null}>
<Environment path={"assets/"} files={"lebombo_1k.hdr"} />
Expand Down
4 changes: 2 additions & 2 deletions src/App/Boot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export const Boot = () => {
loadingProgress={
loadingStatus !== "ready" ? loadingStatus.progress : 1
}
onStart={(ar: boolean) => {
if (ar) webXR.init();
onStart={async (ar: boolean) => {
if (ar) await webXR.init();
setStarted(true);
}}
/>
Expand Down
83 changes: 60 additions & 23 deletions src/WebXRCanvas/WebXRControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,70 @@ export const WebXRControls = ({
events,
} = useThree();

const viewRef = React.useRef<XRView>();

useFrame(() => {
const view = viewRef.current;

if (view) {
const glLayer = webXRSession.renderState.baseLayer!;

const gl = renderer.getContext();

const viewport = glLayer.getViewport(view)!;

renderer.setViewport(
viewport.x,
viewport.y,
viewport.width,
viewport.height
);

// TODO create this outside the loop
const newRenderTarget = new THREE.WebGLRenderTarget(
glLayer.framebufferWidth,
glLayer.framebufferHeight,
{
format: THREE.RGBAFormat,
type: THREE.UnsignedByteType,
colorSpace: renderer.outputColorSpace,
stencilBuffer: gl.getContextAttributes()?.stencil,
}
);

// console.log("render");

renderer.setRenderTargetFramebuffer(newRenderTarget, glLayer.framebuffer);
renderer.setRenderTarget(newRenderTarget);

renderer.render(scene, camera);
}
}, 1);

React.useLayoutEffect(() => {
const gl = renderer.getContext();

gl.makeXRCompatible().then(() => {
webXRSession.updateRenderState({
baseLayer: new XRWebGLLayer(webXRSession, gl),
});
});
gl.makeXRCompatible()
.then(() => {
webXRSession.updateRenderState({
baseLayer: new XRWebGLLayer(webXRSession, gl),
});
})
.catch(setError);

let localReference: XRReferenceSpace;

webXRSession.requestReferenceSpace("local-floor").then((r) => {
localReference = r;
webXRSession
.requestReferenceSpace("local-floor")
.then((r) => {
localReference = r;

localReference.addEventListener("reset", ({ transform }) => {
if (transform)
localReference = localReference.getOffsetReferenceSpace(transform);
});
});
localReference.addEventListener("reset", ({ transform }) => {
if (transform)
localReference = localReference.getOffsetReferenceSpace(transform);
});
})
.catch(setError);

let cancel: number;
let lastTime: number;
Expand All @@ -96,11 +141,11 @@ export const WebXRControls = ({
const dt = t - (lastTime ?? t);
lastTime = t;

const glLayer = webXRSession.renderState.baseLayer!;
const pose = localReference && frame.getViewerPose(localReference);

if (pose) {
const view = pose.views[0];
viewRef.current = view;

const l = 4;
camera.position.set(
Expand Down Expand Up @@ -132,6 +177,7 @@ export const WebXRControls = ({
.copy(camera.position)
.addScaledVector(v, t);

console.log("found");
onPoseFound?.();
}
}
Expand All @@ -143,18 +189,9 @@ export const WebXRControls = ({
// camera.projectionMatrix.copy(projectionMatrix);
// camera.projectionMatrixInverse.copy(camera.projectionMatrix).invert();

const viewport = glLayer.getViewport(view)!;
renderer.setViewport(
viewport.x,
viewport.y,
viewport.width,
viewport.height
);
advance(dt);
}

gl.bindFramebuffer(gl.FRAMEBUFFER, glLayer.framebuffer);
advance(dt);

cancel = webXRSession.requestAnimationFrame(onXRFrame);
};

Expand Down

0 comments on commit 273e1ad

Please sign in to comment.