• Stars
    star
    1,403
  • Rank 33,517 (Top 0.7 %)
  • Language
    JavaScript
  • License
    Apache License 2.0
  • Created almost 10 years ago
  • Updated almost 4 years ago

Reviews

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

Repository Details

Use WebVR today, without requiring a special browser build.

WebVR Polyfill

Build Status Build Status

A JavaScript implementation of the WebVR spec. This project ensures your WebVR content works on any platform, whether or not the browser/device has native WebVR support, or when there are inconsistencies in implementation.

Take a look at basic WebVR samples that use this polyfill.

Installing

Script

Download the build at build/webvr-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 webvr-polyfill.min.js.

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

npm

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

$ npm install --save webvr-polyfill

Using

Instructions for using versions >=0.10.0. For <=0.9.x versions, see 0.9.40 tag.

The webvr-polyfill exposes a single constructor, WebVRPolyfill that takes an object for configuration. See full configuration options at src/config.js.

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

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

var polyfill = new WebVRPolyfill();

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

import WebVRPolyfill from 'webvr-polyfill';
const polyfill = new WebVRPolyfill();

Here's an example of querying displays and setting up controls based on environment. Remember, you'll still need to provide controls and code to support a desktop-like experience if no native VRDisplays are found, as the CardboardVRDisplay is only on mobile. See the example.

// Polyfill always provides us with `navigator.getVRDisplays`
navigator.getVRDisplays().then(displays => {
  // If we have a native VRDisplay, or if the polyfill
  // provided us with a CardboardVRDisplay, use it
  if (displays.length) {
    vrDisplay = displays[0];
    controls = new THREE.VRControls(camera);
    vrDisplay.requestAnimationFrame(animate);
  } else {
    // If we don't have a VRDisplay, we're probably on
    // a desktop environment, so set up desktop-oriented controls
    controls = new THREE.OrbitControls(camera);
    requestAnimationFrame(animate);
  }
});

iframes

There are some concerns and caveats when embedding polyfilled WebVR content inside iframes. More information is documented in the cardboard-vr-display README.

Goals

The polyfill's goal is to provide a library so that developers can create content targeting the WebVR API without worrying about what browsers and devices their users have in a world of growing, but fragmented support.

The three main components of the polyfill are:

  • Injects a WebVR 1.1 JavaScript implementation if one does not exist
  • Patches browsers that have an incomplete or inconsistent implementation of the API
  • Provide a synthesized CardboardVRDisplay on mobile when WebVR is not supported, or if it does have native support but no native VRDisplays and PROVIDE_MOBILE_VRDISPLAY is true (default).

Performance

Performance is critical for VR. If you find your application is too sluggish, consider tweaking some of the above parameters. In particular, keeping BUFFER_SCALE at 0.5 (the default) will likely help a lot.

Developing

If you're interested in developing and contributing on the polyfill itself, you'll need to have npm installed and familiarize yourself with some commands below. For full list of commands available, see package.json scripts.

$ git clone [email protected]:immersive-web/webvr-polyfill.git
$ cd webvr-polyfill/

# Install dependencies
$ npm install

# Build uncompressed JS file
$ npm run build

# Run tests
$ npm test

# Watch src/* directory and auto-rebuild on changes
$ npm watch

Testing

Right now there are some unit tests in the configuration and logic for how things get polyfilled. Be sure to run tests before submitting any PRs, and bonus points for having new tests!

$ npm test

Due to the nature of the polyfill, be also sure to test the examples with your changes where appropriate.

Releasing a new version

For maintainers only, to cut a new release for npm, use the [npm version] command. The preversion, version and postversion npm scripts will run tests, build, add built files and tag to git, push to github, and publish the new npm version.

npm version <semverstring>

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

webxr-samples

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

webxr-polyfill

Use the WebXR Device API today, providing fallbacks to native WebVR 1.1 and Cardboard
JavaScript
381
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