immersive-web / webxr-polyfill

Use the WebXR Device API today, providing fallbacks to native WebVR 1.1 and Cardboard
Apache License 2.0
381 stars 84 forks source link

Headset control API #94

Open takahirox opened 4 years ago

takahirox commented 4 years ago

As written in the readme

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

Input can be controlled with Gamepad API by hacking navigator.getGamepads() in JavaScript (I'm not sure if it's recommended tho).

const customGamepad = {
  ...
};

const onSomething = () => {
  customGamepad.buttons[0].pressed = true;
};

navigator.getGamepads = () => {
  return [customGamepad];
};

Similarly, I'm happy if we can somehow control headset pose in JavaScript.

The background of this request is we've been developing WebXR emulator extension which emulates XR devices in JavaScript and I'm trying to integrate with webxr-polyfill. I need a way to control headset in JavaScript to accomplish the emulation.

jsantell commented 4 years ago

To simulate a headset, it's possible to create your own "XRDevice" (like CardboardXRDevice and WebVRDevice) that provides its pose via the abstraction API, similar to how AR viewers can use the webxr-polyfill to connect to ARKit/ARCore (like Mozilla's webxr-viewer). The extension would have to wire up the polyfill with the device, similar to src/WebXRPolyfill.js here; some notes on extending the polyfill #56

takahirox commented 4 years ago

Thanks for the comment. WebXRPolyfill creates device here so it seems we can extend WebXRPolyfill and override _patchNavigatorXR() to create custom device.

class MyWebXRPolyfill extends WebXRPolyfill {
  _patchNavigatorXR() {
    let devicePromise = new CustomXRDevice(this.global, this.config);

    // the following is duplicated with WebXRPolyfill._patchNavigatorXR()
    this.xr = new XR(devicePromise);
    Object.defineProperty(this.global.navigator, 'xr', {
      value: this.xr,
      configurable: true,
    });
  }
}

If we move requestXRDevice() in WebXRPolyfill class the duplicated code in MyWebXRPolyfill may be removed.

class MyWebXRPolyfill extends WebXRPolyfill {
  requestXRDevice() {
    return new CustomXRDevice(this.global, this.config);
  }
}

What do you think?