Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A camera for Babylon.js which use the WebVR API.
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
You can’t perform that action at this time.