Created
October 2, 2014 14:00
-
-
Save demonixis/7f38d40cc1e452ae068c to your computer and use it in GitHub Desktop.
A camera for Babylon.js which use the WebVR API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var OculusRiftCamera = (function () { | |
var camRift = function (position, scene) { | |
BABYLON.OculusCamera.call(this, "OculusRiftCamera", position, scene); | |
this._startVR = this._startVR.bind(this); | |
}; | |
camRift.prototype = Object.create(BABYLON.OculusCamera.prototype); | |
camRift.prototype.attachControl = function (element, noPreventDefault) { | |
BABYLON.OculusCamera.prototype.attachControl.call(this, element, noPreventDefault); | |
if (navigator.getVRDevices) { | |
navigator.getVRDevices().then(this._startVR); | |
} | |
else if (navigator.mozGetVRDevices) { | |
navigator.mozGetVRDevices(this._startVR); | |
} | |
}; | |
camRift.prototype._startVR = function (devices) { | |
var hmdDevice = null, | |
sensorDevice = null; | |
for (var i = 0; i < devices.length; ++i) { | |
if (devices[i] instanceof HMDVRDevice) { | |
hmdDevice = devices[i]; | |
} | |
} | |
for (var i = 0; i < devices.length; ++i) { | |
if (devices[i] instanceof PositionSensorVRDevice && (!hmdDevice || devices[i].hardwareUnitId == hmdDevice.hardwareUnitId)) { | |
sensorDevice = devices[i]; | |
} | |
} | |
var _this = this, | |
enabled = (sensorDevice && hmdDevice) ? true : false, | |
state = null, | |
quaternion = new BABYLON.Quaternion(), | |
rotation = BABYLON.Vector3.Zero; | |
(function loop() { | |
if (enabled) { | |
requestAnimationFrame(loop); | |
state = sensorDevice.getState(); | |
quaternion.copyFromFloats(state.orientation.x, state.orientation.y, state.orientation.z, state.orientation.w); | |
quaternion.toEulerAnglesToRef(rotation); | |
_this.rotation.x = -rotation.z; | |
_this.rotation.y = -rotation.y; | |
_this.rotation.z = rotation.x; | |
// Update the camera | |
_this._update(); | |
} | |
})(); | |
}; | |
return camRift; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment