123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- "use strict";
- var BABYLON = BABYLON || {};
- (function () {
- BABYLON.OculusController = function (scene, target) {
- BABYLON.inputController.call(this, scene, target);
- this._deviceOrientationHandler = this.onOrientationEvent.bind(this);
- this._tempOrientation = { yaw: 0.0, pitch: 0.0, roll: 0.0 };
- this._relativeOrientation = { yaw: 0.0, pitch: 0.0, roll: 0.0 };
- window.addEventListener("deviceorientation", this._deviceOrientationHandler);
- };
- BABYLON.OculusController.prototype = Object.create(BABYLON.inputController.prototype);
- BABYLON.OculusController.prototype.onOrientationEvent = function (ev) {
- this._tempOrientation.yaw = ev.alpha / 180 * Math.PI;
- this._tempOrientation.pitch = ev.beta / 180 * Math.PI;
- this._tempOrientation.roll = ev.gamma / 180 * Math.PI;
- if (!this._lastOrientation) {
- this._lastOrientation = Object.create(this._tempOrientation);
- }
- else {
- this._relativeOrientation.yaw = this._tempOrientation.yaw - this._lastOrientation.yaw;
- this._relativeOrientation.pitch = this._tempOrientation.pitch - this._lastOrientation.pitch;
- this._relativeOrientation.roll = this._tempOrientation.roll - this._lastOrientation.roll;
- var temp = this._tempOrientation;
- this._tempOrientation = this._lastOrientation;
- this._lastOrientation = temp;
- this.target.rotateRelative(this._relativeOrientation);
- }
- };
- BABYLON.OculusController.prototype.dispose = function () {
- window.removeEventListener("deviceorientation", this._deviceOrientationHandler);
- };
- BABYLON.OculusController.CameraSettings_OculusRiftDevKit2013_Metric = {
- HResolution: 1280,
- VResolution: 800,
- HScreenSize: 0.149759993,
- VScreenSize: 0.0935999975,
- VScreenCenter: 0.0467999987,
- EyeToScreenDistance: 0.0410000011,
- LensSeparationDistance: 0.0635000020,
- InterpupillaryDistance: 0.0640000030,
- DistortionK: [1.0, 0.219999999, 0.239999995, 0.0],
- ChromaAbCorrection: [0.995999992, -0.00400000019, 1.01400006, 0.0],
- PostProcessScaleFactor: 1.714605507808412,
- LensCenterOffset: 0.151976421
- };
- BABYLON.OculusOrientedCamera = function (name, position, scene, isLeftEye, ovrSettings, neutralOrientation) {
- BABYLON.Camera.call(this, name, position, scene);
- this._referenceDirection = new BABYLON.Vector3(0, 0, 1);
- this._referenceUp = new BABYLON.Vector3(0, 1, 0);
- this._actualDirection = new BABYLON.Vector3(1, 0, 0);
- this._actualUp = new BABYLON.Vector3(0, 1, 0);
- this._currentTargetPoint = new BABYLON.Vector3(0, 0, 0);
- this._currentOrientation = neutralOrientation || { yaw: 0.0, pitch: 0.0, roll: 0.0 };
- this._currentViewMatrix = new BABYLON.Matrix();
- this._currentOrientationMatrix = new BABYLON.Matrix();
- this._tempMatrix = new BABYLON.Matrix();
- if (isLeftEye) {
- this.viewport = new BABYLON.Viewport(0, 0, 0.5, 1.0);
- } else {
- this.viewport = new BABYLON.Viewport(0.5, 0, 0.5, 1.0);
- }
- this._aspectRatioAspectRatio = ovrSettings.HResolution / (2 * ovrSettings.VResolution);
- this._aspectRatioFov = (2 * Math.atan((ovrSettings.PostProcessScaleFactor * ovrSettings.VScreenSize) / (2 * ovrSettings.EyeToScreenDistance)));
- var hMeters = (ovrSettings.HScreenSize / 4) - (ovrSettings.LensSeparationDistance / 2);
- var h = (4 * hMeters) / ovrSettings.HScreenSize;
- this._hMatrix = BABYLON.Matrix.Translation(isLeftEye ? h : -h, 0, 0);
- this._projectionMatrix = new BABYLON.Matrix();
- this._preViewMatrix = BABYLON.Matrix.Translation(isLeftEye ? .5 * ovrSettings.InterpupillaryDistance : -.5 * ovrSettings.InterpupillaryDistance, 0, 0);
- new BABYLON.oculusDistortionCorrectionPostProcess("Oculus Distortion", this, !isLeftEye, ovrSettings);
- this.resetProjectionMatrix();
- this.resetViewMatrix();
- };
-
- BABYLON.OculusOrientedCamera.buildOculusStereoCamera = function (scene, name, canvas, minZ, maxZ, position, neutralOrientation, useFXAA, ovrSettings) {
- position = position || new BABYLON.Vector2(0, 0);
- neutralOrientation = neutralOrientation || { yaw: 0.0, pitch: 0.0, roll: 0.0 };
- //var controller = new BABYLON.OculusController();
- ovrSettings = ovrSettings || BABYLON.OculusController.CameraSettings_OculusRiftDevKit2013_Metric;
- var leftCamera = new BABYLON.OculusOrientedCamera(name + "_left", position, scene, true, ovrSettings, neutralOrientation);
- leftCamera.minZ = minZ;
- leftCamera.maxZ = maxZ;
- if (useFXAA) {
- new BABYLON.FxaaPostProcess("fxaa_left", 1.0, leftCamera);
- }
- var rightCamera = new BABYLON.OculusOrientedCamera(name + "_right", position, scene, false, ovrSettings, neutralOrientation);
- rightCamera.minZ = minZ;
- rightCamera.maxZ = maxZ;
- if (useFXAA) {
- new BABYLON.FxaaPostProcess("fxaa_right", 1.0, rightCamera);
- }
- scene.activeCameras = [];
- scene.activeCameras.push(leftCamera);
- scene.activeCameras.push(rightCamera);
- leftCamera.attachControl(canvas);
- rightCamera.attachControl(canvas);
- var multiTarget = new BABYLON.inputControllerMultiTarget([leftCamera, rightCamera]);
- var controller = new BABYLON.OculusController(scene, multiTarget);
- var moveController = new BABYLON.keyboardMoveController(scene, multiTarget);
- moveController.attachToCanvas(canvas);
- var result = {
- leftCamera: leftCamera, rightCamera: rightCamera, intermediateControllerTarget: multiTarget,
- oculusController: controller,
- keyboardController: moveController
- };
- result.dispose = function () {
- this.leftCamera.detachControl(canvas);
- this.rightCamera.detachControl(canvas);
- this.leftCamera.dispose();
- this.rightCamera.dispose();
- this.oculusController.dispose();
- this.keyboardController.detachFromCanvas(canvas);
- this.keyboardController.dispose();
- }.bind(result);
- return result;
- };
-
- BABYLON.OculusOrientedCamera.prototype = Object.create(BABYLON.Camera.prototype);
- BABYLON.OculusOrientedCamera.prototype.resetViewMatrix = function () {
- BABYLON.Matrix.RotationYawPitchRollToRef(
- this._currentOrientation.yaw,
- this._currentOrientation.pitch,
- -this._currentOrientation.roll
- , this._currentOrientationMatrix);
- BABYLON.Vector3.TransformCoordinatesToRef(this._referenceDirection, this._currentOrientationMatrix, this._actualDirection);
- BABYLON.Vector3.TransformCoordinatesToRef(this._referenceUp, this._currentOrientationMatrix, this._actualUp);
- BABYLON.Vector3.FromFloatsToRef(this.position.x + this._actualDirection.x, this.position.y + this._actualDirection.y, this.position.z + this._actualDirection.z, this._currentTargetPoint);
- BABYLON.Matrix.LookAtLHToRef(this.position, this._currentTargetPoint, this._actualUp, this._tempMatrix);
- this._tempMatrix.multiplyToRef(this._preViewMatrix, this._currentViewMatrix);
- return this._currentViewMatrix;
- };
- BABYLON.OculusOrientedCamera.prototype.getViewMatrix = function () {
- return this._currentViewMatrix;
- };
- BABYLON.OculusOrientedCamera.prototype._update = function () {
- //if (!this._referenceOculusOrientation) {
- // this._referenceOculusOrientation = { yaw: this._controller._currentOrientation.yaw, pitch: this._controller._currentOrientation.pitch, roll: this._controller._currentOrientation.roll };
- //}
- //else {
- // this._currentOrientation.yaw = this._controller._currentOrientation.yaw - this._referenceOculusOrientation.yaw;
- // this._currentOrientation.pitch = this._controller._currentOrientation.pitch - this._referenceOculusOrientation.pitch;
- // this._currentOrientation.roll = this._controller._currentOrientation.roll - this._referenceOculusOrientation.roll;
- //}
- if (this.controllers) {
- for (var i = 0; i < this.controllers.length; ++i) {
- this.controllers[i].update();
- }
- }
- };
- BABYLON.OculusOrientedCamera.prototype.resetProjectionMatrix = function () {
- BABYLON.Matrix.PerspectiveFovLHToRef(this._aspectRatioFov, this._aspectRatioAspectRatio, this.minZ, this.maxZ, this._tempMatrix);
- this._tempMatrix.multiplyToRef(this._hMatrix, this._projectionMatrix);
- return this._projectionMatrix;
- };
- BABYLON.OculusOrientedCamera.prototype.getProjectionMatrix = function (force) {
- return this._projectionMatrix;
- };
- // implementation of inputControllerTarget
- BABYLON.OculusOrientedCamera.prototype.getOrientation = function () {
- return this._currentOrientation;
- };
- BABYLON.OculusOrientedCamera.prototype.getPosition = function () {
- return this.position;
- };
- BABYLON.OculusOrientedCamera.prototype.moveRelative = function (movementVector) {
- if (!this._tempMoveVector) {
- this._tempMoveVector = new BABYLON.Vector3(0, 0, 0);
- }
- BABYLON.Vector3.TransformCoordinatesToRef(movementVector, this._currentOrientationMatrix, this._tempMoveVector);
- this.position.addInPlace(this._tempMoveVector);
- this.resetViewMatrix();
- };
- BABYLON.OculusOrientedCamera.prototype.rotateRelative = function (rotation) {
- this._currentOrientation.yaw += rotation.yaw;
- this._currentOrientation.pitch += rotation.pitch;
- this._currentOrientation.roll += rotation.roll;
- this.resetViewMatrix();
- };
- })();
|