Skip to content

Commit

Permalink
Add two new configuration params: allowCardboardOnDesktop, cardboardC…
Browse files Browse the repository at this point in the history
…onfig, and move global to a configuration option rather than the first argument to WebXRPolyfill constructor, a breaking API change. Also disable babel compilation now until we can decide on an action in #63.
jsantell committed Jul 1, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent fa824a9 commit f79a4cf
Showing 5 changed files with 28 additions and 10 deletions.
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -58,18 +58,25 @@ const polyfill = new WebXRPolyfill();

## API

### new WebXRPolyfill(global, config)
### new WebXRPolyfill(config)

Takes a `global` object (usually `window`), as well as a `config` object with
the following options:
Takes a `config` object with the following options:

* `global`: What global should be used to find needed types. (default: `window` on browsers)
* `webvr`: Whether or not there should be an attempt to fall back to a
WebVR 1.1 VRDisplay. (default: `true`).
* `cardboard`: Whether or not there should be an attempt to fall back to a
* `cardboardConfig`: The configuration to be used for CardboardVRDisplay when used. Has no effect when `cardboard` is `false`, or another XRDevice is used. Possible configuration options can be found [here in the cardboard-vr-display repo](https://github.com/immersive-web/cardboard-vr-display/blob/master/src/options.js). (default: `null`)
* `allowCardboardOnDesktop`: Whether or not to allow cardboard's stereoscopic rendering and pose via sensors on desktop. This is most likely only helpful for development and debugging. (default: `false`)
};

const partials = ['navigator', 'HTMLCanvasElement', 'WebGLRenderingContext'];
JavaScript implementation of the WebXR API only on mobile. (default: `true`)

## Browser Support

**Development note: babel support is currently removed, handle definitively in [#63](https://github.com/immersive-web/webxr-polyfill/issues/63)**

There are 3 builds provided: [build/webxr-polyfill.js](build/webxr-polyfill.js), an ES5 transpiled build, its minified counterpart [build/webxr-polyfill.min.js](build/webxr-polyfill.min.js), and an untranspiled [ES Modules] version [build/webxr-polyfill.module.js](build/webxr-polyfill.module.js). If using the transpiled ES5 build, its up to developers to decide which browser features to polyfill based on their support, as no extra polyfills are included. Some browser features this library uses include:

* TypedArrays
2 changes: 2 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -34,12 +34,14 @@ export default {
replace({
'process.env.NODE_ENV': JSON.stringify('production'),
}),
/*
babel({
include: [
'src/**',
'node_modules/gl-matrix/**'
]
}),
*/
resolve(),
commonjs(),
cleanup({
11 changes: 10 additions & 1 deletion src/WebXRPolyfill.js
Original file line number Diff line number Diff line change
@@ -23,6 +23,8 @@ import { isImageBitmapSupported, isMobile } from './utils';
import { requestXRDevice } from './devices';

const CONFIG_DEFAULTS = {
// The default global to use for needed APIs.
global: GLOBAL,
// Whether support for a browser implementing WebVR 1.1 is enabled.
// If enabled, XR support is powered by native WebVR 1.1 VRDisplays,
// exposed as XRDevices.
@@ -31,6 +33,13 @@ const CONFIG_DEFAULTS = {
// a mobile device, and no other native (1.1 VRDisplay if `webvr` on,
// or XRDevice) found.
cardboard: true,
// The configuration to be used for CardboardVRDisplay when used.
// Has no effect if `cardboard: false` or another XRDevice is used.
// Configuration can be found: https://github.com/immersive-web/cardboard-vr-display/blob/master/src/options.js
cardboardConfig: null,
// Whether a CardboardXRDevice should be created if no WebXR API found
// on desktop or not. Stereoscopic rendering with a gyro often does not make sense on desktop, and probably only useful for debugging.
allowCardboardOnDesktop: false,
};

const partials = ['navigator', 'HTMLCanvasElement', 'WebGLRenderingContext'];
@@ -41,8 +50,8 @@ export default class WebXRPolyfill {
* @param {object?} config
*/
constructor(global, config={}) {
this.global = global || GLOBAL;
this.config = Object.freeze(Object.assign({}, CONFIG_DEFAULTS, config));
this.global = this.config.global;
this.nativeWebXR = 'xr' in this.global.navigator;
this.injected = false;

6 changes: 3 additions & 3 deletions src/devices.js
Original file line number Diff line number Diff line change
@@ -55,8 +55,8 @@ export const requestXRDevice = async function (global, config) {
}

// If cardboard is enabled, there are no native 1.1 VRDisplays,
// and we're on mobile, provide a CardboardXRDevice.
if (config.cardboard && isMobile(global)) {
// and we're on mobile or `allowCardboardOnDesktop`, provide a CardboardXRDevice.
if (config.cardboard && (isMobile(global) || config.allowCardboardOnDesktop)) {
// If we're on Cardboard, make sure that VRFrameData is a global
if (!global.VRFrameData) {
global.VRFrameData = function () {
@@ -68,7 +68,7 @@ export const requestXRDevice = async function (global, config) {
};
}

return new CardboardXRDevice(global);
return new CardboardXRDevice(global, config.cardboardConfig);
}

return null;
6 changes: 3 additions & 3 deletions src/devices/CardboardXRDevice.js
Original file line number Diff line number Diff line change
@@ -22,10 +22,10 @@ export default class CardboardXRDevice extends WebVRDevice {
* constructor from the WebVR 1.1 spec.
*
* @param {VRDisplay} display
* @param {VRFrameData} VRFrameData
* @param {Object?} cardboardConfig
*/
constructor(global) {
const display = new CardboardVRDisplay();
constructor(global, cardboardConfig) {
const display = new CardboardVRDisplay(cardboardConfig || {});
super(global, display);

this.display = display;

0 comments on commit f79a4cf

Please sign in to comment.