• Stars
    star
    381
  • Rank 112,502 (Top 3 %)
  • Language
    JavaScript
  • License
    Apache License 2.0
  • Created over 6 years ago
  • Updated 8 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Use the WebXR Device API today, providing fallbacks to native WebVR 1.1 and Cardboard

WebXR Polyfill

Build Status Build Status

A JavaScript implementation of the WebXR Device API, as well as the WebXR Gamepad Module. This polyfill allows developers to write against the latest specification, providing support when run on browsers that implement the WebVR 1.1 spec, or on mobile devices with no WebVR/WebXR support at all.

The polyfill reflects the stable version of the API which has shipped in multiple browsers.


If you are writing code against the WebVR 1.1 spec, use webvr-polyfill, which supports browsers with the 1.0 spec, or no implementation at all. It is recommended to write your code targeting the WebXR Device API spec however and use this polyfill as browsers begin to implement the latest changes.

The minimal input controls currently supported by WebXR is polyfilled here as well, using the Gamepad API.

Setup

Installing

Download the build at build/webxr-polyfill.js and include it as a script tag, or use a CDN. You can also use the minified file in the same location as webxr-polyfill.min.js.

  <script src='webxr-polyfill.js'></script>
  <!-- or use a link to a CDN -->
  <script src='https://cdn.jsdelivr.net/npm/webxr-polyfill@latest/build/webxr-polyfill.js'></script>

Or if you're using a build tool like browserify or webpack, install it via npm.

$ npm install --save webxr-polyfill

Building Locally

$ npm run build

Using

The webxr-polyfill exposes a single constructor, WebXRPolyfill that takes an object for configuration. See full configuration options at API.

Be sure to instantiate the polyfill before calling any of your XR code! The polyfill needs to patch the API if it does not exist so your content code can assume that the WebXR API will just work.

If using script tags, a WebXRPolyfill global constructor will exist.

var polyfill = new WebXRPolyfill();

In a modular ES6 world, import and instantiate the constructor similarly.

import WebXRPolyfill from 'webxr-polyfill';
const polyfill = new WebXRPolyfill();

API

new WebXRPolyfill(config)

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 JavaScript implementation of the WebXR API only on mobile. (default: true)
  • 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. (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)

Browser Support

Development note: babel support is currently removed, handle definitively in #63

There are 3 builds provided: build/webxr-polyfill.js, an ES5 transpiled build, its minified counterpart build/webxr-polyfill.min.js, and an untranspiled ES Modules version 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
  • Object.assign
  • Promise
  • Symbol
  • Map
  • Array#includes

Check the .babelrc configuration and ensure the polyfill runs in whatever browsers you choose to support.

Polyfilling Rules

  • If 'xr' in navigator === false:
    • WebXR classes (e.g. XRDevice, XRSession) will be added to the global
    • navigator.xr will be polyfilled.
    • If the platform has a VRDisplay from the WebVR 1.1 spec available:
      • navigator.xr.requestDevice() will return a polyfilled XRDevice wrapping the VRDisplay.
    • If the platform does not have a VRDisplay, config.cardboard === true, and on mobile:
      • navigator.xr.requestDevice() will return a polyfilled XRDevice based on CardboardVRDisplay.
    • If WebGLRenderingContext.prototype.setCompatibleXRDevice is not a function:
      • Polyfill all WebGLRenderingContext.prototype.setCompatibleXRDevice and a creation attribute for { compatibleXrDevice }.
      • Polyfills HTMLCanvasElement.prototype.getContext to support a xrpresent type. Returns a polyfilled XRPresentationContext (via CanvasRenderingContext2D or ImageBitmapRenderingContext if supported) used for mirroring and magic window.
  • If 'xr' in navigator === true, config.cardboard === true and on mobile:
    • Overwrite navigator.xr.requestDevice so that a native XRDevice is returned if it exists, and if not, return a polyfilled XRDevice based on CardboardVRDisplay.

In the future, when the WebXR API is implemented on a platform but inconsistent with spec (due to new spec changes or inconsistencies), the polyfill will attempt to patch these differences without overwriting the native behavior.

Not supported/Caveats

  • XRWebGLLayer.framebufferScaleFactor

License

This program is free software for both commercial and non-commercial use, distributed under the Apache 2.0 License.

More Repositories

1

webxr

Repository for the WebXR Device API Specification.
Bikeshed
2,971
star
2

webvr-polyfill

Use WebVR today, without requiring a special browser build.
JavaScript
1,403
star
3

webxr-samples

Samples to demonstrate use of the WebXR Device API
HTML
996
star
4

webxr-input-profiles

WebXR Gamepad assets, source library, and schema
JavaScript
191
star
5

webvrrocks

Your guide to Virtual Reality in the browser.
HTML
123
star
6

webxr-ar-module

Repository for the WebXR Augmented Reality Module
Bikeshed
113
star
7

webxr-hand-input

A feature repo for working on hand input support in WebXR. Feature lead: Manish Goregaokar
Bikeshed
104
star
8

proposals

Initial proposals for future Immersive Web work (see README)
95
star
9

cardboard-vr-display

A JavaScript implementation of a WebVR 1.1 VRDisplay
JavaScript
92
star
10

layers

A feature repo for working on multi-layer support in WebXR. Feature leads: Rik Cabanier and Artem Bolgar (Oculus)
HTML
89
star
11

immersiveweb.dev

Repo for immersiveweb.dev
HTML
88
star
12

hit-test

Bikeshed
78
star
13

real-world-geometry

Additions to WebXR Device API for exposing real world data (Lead: Piotr Bialecki). Plane detection: https://immersive-web.github.io/real-world-geometry/plane-detection.html
Bikeshed
72
star
14

navigation

Repository for the discussion and research in to navigating from page to page whilst staying in immersive mode. Feature leads: Rik Cabanier and Brandon Jones
70
star
15

dom-overlays

A feature incubation repo for layering DOM content on/in WebXR content. Feature lead: Piotr Bialecki
Bikeshed
70
star
16

model-element

Repository for the <model> tag. Feature leads: Marcos Cรกceres and Laszlo Gombos
HTML
62
star
17

WebXR-WebGPU-Binding

Bikeshed
58
star
18

computer-vision

A feature repo for exploring computer vision APIs for XR. Feature Lead: Blair MacIntyre
54
star
19

depth-sensing

Specification: https://immersive-web.github.io/depth-sensing/ Explainer: https://github.com/immersive-web/depth-sensing/blob/main/explainer.md
Bikeshed
53
star
20

marker-tracking

Additions to the WebXR Device API for Marker Tracking. Repo contacts Rik Cabanier and Piotr Bialecki
HTML
52
star
21

anchors

Bikeshed
50
star
22

raw-camera-access

Spec draft: https://immersive-web.github.io/raw-camera-access/. Repository for experimentation around exposing raw camera access through WebXR Device API. Feature leads: Piotr Bialecki, Alex Turner, Nicholas Butko
Bikeshed
38
star
23

geo-alignment

For work toward a feature in WebXR to geo-align coordinate systems. Feature lead: Blair MacIntyre
HTML
35
star
24

lighting-estimation

A proposal for adding lighting estimation API to WebXR Device API. Feature lead: Kip Gilbert
Makefile
32
star
25

immersive-web-weekly

The site and content for the Immersive Web Weekly newsletter
Handlebars
31
star
26

webvr-polyfill-dpdb

An up-to-date Device Parameter Database for the WebVR Polyfill
JavaScript
31
star
27

webxr-gamepads-module

Repository for the WebXR Gamepads Module
Bikeshed
30
star
28

administrivia

A repo for managing the adminstrative tasks of the Immersive Web Group.
25
star
29

webxr-layers-polyfill

WebXR Layers Polyfill library. Lead: Rik Cabanier.
JavaScript
21
star
30

privacy-and-security

Cross specification concerns and suggestions for privacy and security for the immersive web (Feature lead: Mounir Lamouri)
16
star
31

webxr-test-api

WPT testing API for WebXR
Bikeshed
14
star
32

immersive-web.github.io

Organization page for the Immersive Web Working Group and Community Group
14
star
33

webvr

Archival repository for the legacy WebVR Specification.
HTML
13
star
34

spatial-favicons

A proposal for favicon support for a new data type. Feature lead: Rik Cabanier
12
star
35

body-tracking

Bikeshed
12
star
36

ar-common

A repo for discussing and capturing information about cross-API aspects of augmented reality
11
star
37

real-world-meshing

Bikeshed
10
star
38

capture

Capture composited content, layers and real world through a privacy preserving high level API
7
star
39

semantic-labels

This repository will list all the semantic labels that can be returned by various WebXR APIs
7
star
40

occlusion

HTML
6
star
41

detached-elements

Using HTML/CSS to bring elements outside the window in Immersive Web browsers. Feature leads: Dean Jackson and Ada Rose Cannon
HTML
5
star
42

community-resources

Common resources for all Immersive Web Community Group repositories
5
star
43

homepage

Homepage of the Immersive Web WG
HTML
4
star
44

performance-improvements

A feature-incubation repo for XR-related performance improvements. Feature lead: Trevor F. Smith
2
star
45

front-facing-camera

Incubation repository for access to front-facing-camera. Repo contact Piotr Bialecki and Rik Cabanier
HTML
1
star
46

webxr-accessibility

WebXR Accessibility
HTML
1
star