Skip to content

Commit

Permalink
fix: renderers/webxr/webxrmanager (#7)
Browse files Browse the repository at this point in the history
* fix (three): update WebXRManager
getReferenceSpace / getSession should be nullable

* fix (three): Update WebXRManager
setSession should return Promise<void> instead of void since r125
See: mrdoob/three.js#20754

* fix (three): Update WebXRManager
Trivial rename of arguments

* test (three): add a test
webxr-vr-cube.ts, a simple example featuring WebXR VR

* fix (three): Update XRSession
XRSession should extend EventTarget

* chore (three): Modify tsconfig.json
Forgot to add `webxr-vr-cube.ts` to the tsconfig.json

* docs: add FMS-Cat as a contributor

* refactor: ran prettier:write
  • Loading branch information
Yutaka "FMS_Cat" Obuchi committed Feb 8, 2021
1 parent 5f58cbd commit f82d6ea
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 10 deletions.
10 changes: 10 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@
"contributions": [
"code"
]
},
{
"login": "FMS-Cat",
"name": "Yutaka \"FMS_Cat\" Obuchi",
"avatar_url": "https://avatars.githubusercontent.com/u/7824814?v=4",
"profile": "http://fms-cat.github.io/",
"contributions": [
"code",
"test"
]
}
],
"skipCi": true,
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
<td align="center"><a href="https://github.com/joshuaellis"><img src="https://avatars.githubusercontent.com/u/37798644?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Josh</b></sub></a><br /><a href="#question-joshuaellis" title="Answering Questions">💬</a> <a href="https://github.com/three-types/three-ts-types/issues?q=author%3Ajoshuaellis" title="Bug reports">🐛</a> <a href="https://github.com/three-types/three-ts-types/commits?author=joshuaellis" title="Code">💻</a> <a href="https://github.com/three-types/three-ts-types/commits?author=joshuaellis" title="Documentation">📖</a> <a href="#ideas-joshuaellis" title="Ideas, Planning, & Feedback">🤔</a> <a href="#maintenance-joshuaellis" title="Maintenance">🚧</a> <a href="https://github.com/three-types/three-ts-types/pulls?q=is%3Apr+reviewed-by%3Ajoshuaellis" title="Reviewed Pull Requests">👀</a> <a href="https://github.com/three-types/three-ts-types/commits?author=joshuaellis" title="Tests">⚠️</a></td>
<td align="center"><a href="https://github.com/Methuselah96"><img src="https://avatars.githubusercontent.com/u/693755?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Nathan Bierema</b></sub></a><br /><a href="#question-Methuselah96" title="Answering Questions">💬</a> <a href="https://github.com/three-types/three-ts-types/issues?q=author%3AMethuselah96" title="Bug reports">🐛</a> <a href="https://github.com/three-types/three-ts-types/commits?author=Methuselah96" title="Code">💻</a> <a href="https://github.com/three-types/three-ts-types/commits?author=Methuselah96" title="Documentation">📖</a> <a href="#ideas-Methuselah96" title="Ideas, Planning, & Feedback">🤔</a> <a href="#infra-Methuselah96" title="Infrastructure (Hosting, Build-Tools, etc)">🚇</a> <a href="#maintenance-Methuselah96" title="Maintenance">🚧</a> <a href="https://github.com/three-types/three-ts-types/pulls?q=is%3Apr+reviewed-by%3AMethuselah96" title="Reviewed Pull Requests">👀</a> <a href="https://github.com/three-types/three-ts-types/commits?author=Methuselah96" title="Tests">⚠️</a></td>
<td align="center"><a href="https://human-interactive.org"><img src="https://avatars.githubusercontent.com/u/12612165?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Michael Herzog</b></sub></a><br /><a href="https://github.com/three-types/three-ts-types/commits?author=Mugen87" title="Code">💻</a></td>
<td align="center"><a href="http://fms-cat.github.io/"><img src="https://avatars.githubusercontent.com/u/7824814?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Yutaka "FMS_Cat" Obuchi</b></sub></a><br /><a href="https://github.com/three-types/three-ts-types/commits?author=FMS-Cat" title="Code">💻</a> <a href="https://github.com/three-types/three-ts-types/commits?author=FMS-Cat" title="Tests">⚠️</a></td>
</tr>
</table>

Expand Down
4 changes: 1 addition & 3 deletions types/three/src/renderers/webxr/WebXR.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ export interface XRSessionInit {
requiredFeatures?: string[];
}

export interface XRSession {
addEventListener: () => void;
removeEventListener: () => void;
export interface XRSession extends EventTarget {
requestReferenceSpace(type: XRReferenceSpaceType): Promise<XRReferenceSpace>;
updateRenderState(renderStateInit: XRRenderStateInit): Promise<void>;
requestAnimationFrame(callback: XRFrameRequestCallback): number;
Expand Down
12 changes: 6 additions & 6 deletions types/three/src/renderers/webxr/WebXRManager.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ export class WebXRManager extends EventDispatcher {
*/
isPresenting: boolean;

getController(id: number): Group;
getControllerGrip(id: number): Group;
getHand(id: number): Group;
getController(index: number): Group;
getControllerGrip(index: number): Group;
getHand(index: number): Group;
setFramebufferScaleFactor(value: number): void;
setReferenceSpaceType(value: XRReferenceSpaceType): void;
getReferenceSpace(): XRReferenceSpace;
getSession(): XRSession;
setSession(value: XRSession): void;
getReferenceSpace(): XRReferenceSpace | null;
getSession(): XRSession | null;
setSession(value: XRSession): Promise<void>;
getCamera(camera: Camera): Camera;
setAnimationLoop(callback: XRFrameRequestCallback): void;
dispose(): void;
Expand Down
97 changes: 97 additions & 0 deletions types/three/test/renderers/webxr/webxr-vr-cube.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// A simple WebXR VR example that just shows a cube.

import * as THREE from 'three';

const container = document.createElement('div');

const renderer = new THREE.WebGLRenderer({ antialias: true });
const camera = new THREE.PerspectiveCamera(50, 2, 0.1, 10);
const scene = new THREE.Scene();

const vrButton = document.createElement('button');
let currentSession: THREE.XRSession | null = null;
let mesh: THREE.Mesh;

init();

async function onSessionStarted(session: THREE.XRSession): Promise<void> {
session.addEventListener('end', onSessionEnded);

await renderer.xr.setSession(session);
vrButton.innerText = 'Exit VR';

currentSession = session;
}

function onSessionEnded(): void {
if (currentSession == null) {
return;
}

currentSession.removeEventListener('end', onSessionEnded);
vrButton.innerText = 'Enter VR';

currentSession = null;
}

async function checkVRSupport(): Promise<THREE.XR | null> {
if ('xr' in navigator) {
const xr: THREE.XR = (navigator as any).xr;
const isSupported = await xr.isSessionSupported('immersive-vr').catch(() => false);
if (isSupported) {
return xr;
} else {
return null;
}
} else {
return null;
}
}

function init(): void {
document.body.appendChild(container);

renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(300, 150);
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.xr.enabled = true;
container.appendChild(renderer.domElement);

camera.position.set(0, 1.6, 0);
scene.add(camera);

const geometry = new THREE.BoxGeometry(0.5, 0.5, 0.5);
const material = new THREE.MeshStandardMaterial({ color: 0x00ff00 });
mesh = new THREE.Mesh(geometry, material);
mesh.position.set(0, 1.6, -3);
scene.add(mesh);

const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(1, 1, 1).normalize();
scene.add(directionalLight);

checkVRSupport().then(xr => {
if (xr) {
vrButton.innerText = 'Enter VR';

vrButton.addEventListener('click', async () => {
if (currentSession == null) {
const sessionInit = { optionalFeatures: ['local-floor', 'bounded-floor'] };
const session = await xr.requestSession('immersive-vr', sessionInit);
await onSessionStarted(session);
}
});
} else {
vrButton.innerText = 'VR Not Supported';
}

container.appendChild(vrButton);
});

renderer.setAnimationLoop(() => {
mesh.rotation.x += 0.005;
mesh.rotation.y += 0.01;

renderer.render(scene, camera);
});
}
3 changes: 2 additions & 1 deletion types/three/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"test/materials/materials-variations-lambert.ts",
"test/materials/materials-variations-phong.ts",
"test/materials/materials-variations-physical.ts",
"test/materials/materials-variations-standard.ts"
"test/materials/materials-variations-standard.ts",
"test/renderers/webxr/webxr-vr-cube.ts"
]
}

0 comments on commit f82d6ea

Please sign in to comment.