|
@@ -7,46 +7,59 @@ var BABYLON;
|
|
|
(function (BABYLON) {
|
|
|
var WebVRFreeCamera = (function (_super) {
|
|
|
__extends(WebVRFreeCamera, _super);
|
|
|
- function WebVRFreeCamera(name, position, scene, compensateDistortion, vrCameraMetrics) {
|
|
|
+ function WebVRFreeCamera(name, position, scene, compensateDistortion, vrCameraMetrics, webVROptions) {
|
|
|
+ var _this = this;
|
|
|
if (compensateDistortion === void 0) { compensateDistortion = true; }
|
|
|
if (vrCameraMetrics === void 0) { vrCameraMetrics = BABYLON.VRCameraMetrics.GetDefault(); }
|
|
|
+ if (webVROptions === void 0) { webVROptions = {}; }
|
|
|
_super.call(this, name, position, scene);
|
|
|
- this._hmdDevice = null;
|
|
|
- this._sensorDevice = null;
|
|
|
+ this.webVROptions = webVROptions;
|
|
|
+ this._vrDevice = null;
|
|
|
this._cacheState = null;
|
|
|
this._vrEnabled = false;
|
|
|
vrCameraMetrics.compensateDistortion = compensateDistortion;
|
|
|
this.setCameraRigMode(BABYLON.Camera.RIG_MODE_VR, { vrCameraMetrics: vrCameraMetrics });
|
|
|
- this._getWebVRDevices = this._getWebVRDevices.bind(this);
|
|
|
+ //this._getWebVRDevices = this._getWebVRDevices.bind(this);
|
|
|
+ if (!this.getEngine().vrDisplaysPromise) {
|
|
|
+ BABYLON.Tools.Error("WebVR is not enabled on your browser");
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ //TODO get the metrics updated using the device's eye parameters!
|
|
|
+ this.getEngine().vrDisplaysPromise.then(function (devices) {
|
|
|
+ if (devices.length > 0) {
|
|
|
+ _this._vrEnabled = true;
|
|
|
+ if (_this.webVROptions.displayName) {
|
|
|
+ devices.some(function (device) {
|
|
|
+ if (device.displayName === _this.webVROptions.displayName) {
|
|
|
+ _this._vrDevice = device;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ //choose the first one
|
|
|
+ _this._vrDevice = devices[0];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ BABYLON.Tools.Error("No WebVR devices found!");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
this.rotationQuaternion = new BABYLON.Quaternion();
|
|
|
this._quaternionCache = new BABYLON.Quaternion();
|
|
|
}
|
|
|
- WebVRFreeCamera.prototype._getWebVRDevices = function (devices) {
|
|
|
- var size = devices.length;
|
|
|
- var i = 0;
|
|
|
- // Reset devices.
|
|
|
- this._sensorDevice = null;
|
|
|
- this._hmdDevice = null;
|
|
|
- // Search for a HmdDevice.
|
|
|
- while (i < size && this._hmdDevice === null) {
|
|
|
- if (devices[i] instanceof HMDVRDevice) {
|
|
|
- this._hmdDevice = devices[i];
|
|
|
- }
|
|
|
- i++;
|
|
|
- }
|
|
|
- i = 0;
|
|
|
- while (i < size && this._sensorDevice === null) {
|
|
|
- if (devices[i] instanceof PositionSensorVRDevice && (!this._hmdDevice || devices[i].hardwareUnitId === this._hmdDevice.hardwareUnitId)) {
|
|
|
- this._sensorDevice = devices[i];
|
|
|
- }
|
|
|
- i++;
|
|
|
- }
|
|
|
- this._vrEnabled = this._sensorDevice && this._hmdDevice ? true : false;
|
|
|
- };
|
|
|
WebVRFreeCamera.prototype._checkInputs = function () {
|
|
|
if (this._vrEnabled) {
|
|
|
- this._cacheState = this._sensorDevice.getState();
|
|
|
- this.rotationQuaternion.copyFrom(this._cacheState.orientation);
|
|
|
+ this._cacheState = this._vrDevice.getPose();
|
|
|
+ this.rotationQuaternion.copyFromFloats(this._cacheState.orientation[0], this._cacheState.orientation[1], this._cacheState.orientation[2], this._cacheState.orientation[3]);
|
|
|
+ if (this.webVROptions.trackPosition) {
|
|
|
+ this.position.copyFromFloats(this._cacheState.position[0], this._cacheState.position[1], -this._cacheState.position[2]);
|
|
|
+ this.webVROptions.positionScale && this.position.scaleInPlace(this.webVROptions.positionScale);
|
|
|
+ }
|
|
|
//Flip in XY plane
|
|
|
this.rotationQuaternion.z *= -1;
|
|
|
this.rotationQuaternion.w *= -1;
|
|
@@ -60,21 +73,19 @@ var BABYLON;
|
|
|
WebVRFreeCamera.prototype.attachControl = function (element, noPreventDefault) {
|
|
|
_super.prototype.attachControl.call(this, element, noPreventDefault);
|
|
|
noPreventDefault = BABYLON.Camera.ForceAttachControlToAlwaysPreventDefault ? false : noPreventDefault;
|
|
|
- if (navigator.getVRDevices) {
|
|
|
- navigator.getVRDevices().then(this._getWebVRDevices);
|
|
|
- }
|
|
|
- else if (navigator.mozGetVRDevices) {
|
|
|
- navigator.mozGetVRDevices(this._getWebVRDevices);
|
|
|
+ if (this._vrEnabled) {
|
|
|
+ this.getEngine().enableVR(this._vrDevice);
|
|
|
}
|
|
|
};
|
|
|
WebVRFreeCamera.prototype.detachControl = function (element) {
|
|
|
_super.prototype.detachControl.call(this, element);
|
|
|
this._vrEnabled = false;
|
|
|
+ this.getEngine().disableVR();
|
|
|
};
|
|
|
WebVRFreeCamera.prototype.requestVRFullscreen = function (requestPointerlock) {
|
|
|
- if (!this._hmdDevice)
|
|
|
- return;
|
|
|
- this.getEngine().switchFullscreen(requestPointerlock, { vrDisplay: this._hmdDevice });
|
|
|
+ //Backwards comp.
|
|
|
+ BABYLON.Tools.Warn("requestVRFullscreen is deprecated. Use engine.switchFullscreen() instead");
|
|
|
+ this.getEngine().switchFullscreen(requestPointerlock);
|
|
|
};
|
|
|
WebVRFreeCamera.prototype.getTypeName = function () {
|
|
|
return "WebVRFreeCamera";
|
|
@@ -100,6 +111,6 @@ var BABYLON;
|
|
|
this._initialQuaternion.normalize();
|
|
|
};
|
|
|
return WebVRFreeCamera;
|
|
|
- })(BABYLON.FreeCamera);
|
|
|
+ }(BABYLON.FreeCamera));
|
|
|
BABYLON.WebVRFreeCamera = WebVRFreeCamera;
|
|
|
})(BABYLON || (BABYLON = {}));
|