Browse Source

Merge branch 'master' of https://github.com/BabylonJS/Babylon.js

David Catuhe 10 years ago
parent
commit
de01525093

+ 14 - 0
Babylon/Cameras/babylon.oculusCamera.d.ts

@@ -0,0 +1,14 @@
+declare module BABYLON {
+    class OculusCamera extends FreeCamera {
+        private _leftCamera;
+        private _rightCamera;
+        private _offsetOrientation;
+        private _deviceOrientationHandler;
+        constructor(name: string, position: Vector3, scene: Scene);
+        public _update(): void;
+        public _updateCamera(camera: FreeCamera): void;
+        public _onOrientationEvent(evt: DeviceOrientationEvent): void;
+        public attachControl(element: HTMLElement, noPreventDefault?: boolean): void;
+        public detachControl(element: HTMLElement): void;
+    }
+}

+ 141 - 0
Babylon/Cameras/babylon.oculusCamera.js

@@ -0,0 +1,141 @@
+var __extends = this.__extends || function (d, b) {
+    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
+    function __() { this.constructor = d; }
+    __.prototype = b.prototype;
+    d.prototype = new __();
+};
+var BABYLON;
+(function (BABYLON) {
+    var 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
+    };
+
+    var _OculusInnerCamera = (function (_super) {
+        __extends(_OculusInnerCamera, _super);
+        function _OculusInnerCamera(name, position, scene, isLeftEye) {
+            _super.call(this, name, position, scene);
+            this._workMatrix = new BABYLON.Matrix();
+            this._actualUp = new BABYLON.Vector3(0, 0, 0);
+
+            // Constants
+            this._aspectRatioAspectRatio = OculusRiftDevKit2013_Metric.HResolution / (2 * OculusRiftDevKit2013_Metric.VResolution);
+            this._aspectRatioFov = (2 * Math.atan((OculusRiftDevKit2013_Metric.PostProcessScaleFactor * OculusRiftDevKit2013_Metric.VScreenSize) / (2 * OculusRiftDevKit2013_Metric.EyeToScreenDistance)));
+
+            var hMeters = (OculusRiftDevKit2013_Metric.HScreenSize / 4) - (OculusRiftDevKit2013_Metric.LensSeparationDistance / 2);
+            var h = (4 * hMeters) / OculusRiftDevKit2013_Metric.HScreenSize;
+
+            this._hMatrix = BABYLON.Matrix.Translation(isLeftEye ? h : -h, 0, 0);
+
+            this.viewport = new BABYLON.Viewport(isLeftEye ? 0 : 0.5, 0, 0.5, 1.0);
+
+            this._preViewMatrix = BABYLON.Matrix.Translation(isLeftEye ? .5 * OculusRiftDevKit2013_Metric.InterpupillaryDistance : -.5 * OculusRiftDevKit2013_Metric.InterpupillaryDistance, 0, 0);
+
+            // Postprocess
+            var postProcess = new BABYLON.OculusDistortionCorrectionPostProcess("Oculus Distortion", this, !isLeftEye, OculusRiftDevKit2013_Metric);
+        }
+        _OculusInnerCamera.prototype.getProjectionMatrix = function () {
+            BABYLON.Matrix.PerspectiveFovLHToRef(this._aspectRatioFov, this._aspectRatioAspectRatio, this.minZ, this.maxZ, this._workMatrix);
+            this._workMatrix.multiplyToRef(this._hMatrix, this._projectionMatrix);
+            return this._projectionMatrix;
+        };
+
+        _OculusInnerCamera.prototype._getViewMatrix = function () {
+            BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this._cameraRotationMatrix);
+
+            BABYLON.Vector3.TransformCoordinatesToRef(this._referencePoint, this._cameraRotationMatrix, this._transformedReferencePoint);
+            BABYLON.Vector3.TransformNormalToRef(this.upVector, this._cameraRotationMatrix, this._actualUp);
+
+            // Computing target and final matrix
+            this.position.addToRef(this._transformedReferencePoint, this._currentTarget);
+
+            BABYLON.Matrix.LookAtLHToRef(this.position, this._currentTarget, this._actualUp, this._workMatrix);
+
+            this._workMatrix.multiplyToRef(this._preViewMatrix, this._viewMatrix);
+            return this._viewMatrix;
+        };
+        return _OculusInnerCamera;
+    })(BABYLON.FreeCamera);
+
+    var OculusCamera = (function (_super) {
+        __extends(OculusCamera, _super);
+        function OculusCamera(name, position, scene) {
+            _super.call(this, name, position, scene);
+
+            this._leftCamera = new _OculusInnerCamera(name + "_left", position.clone(), scene, true);
+            this._rightCamera = new _OculusInnerCamera(name + "_right", position.clone(), scene, false);
+
+            this.subCameras.push(this._leftCamera);
+            this.subCameras.push(this._rightCamera);
+
+            this._deviceOrientationHandler = this._onOrientationEvent.bind(this);
+        }
+        OculusCamera.prototype._update = function () {
+            this._leftCamera.position.copyFrom(this.position);
+            this._rightCamera.position.copyFrom(this.position);
+
+            this._updateCamera(this._leftCamera);
+            this._updateCamera(this._rightCamera);
+
+            _super.prototype._update.call(this);
+        };
+
+        OculusCamera.prototype._updateCamera = function (camera) {
+            camera.minZ = this.minZ;
+            camera.maxZ = this.maxZ;
+
+            camera.rotation.x = this.rotation.x;
+            camera.rotation.y = this.rotation.y;
+            camera.rotation.z = this.rotation.z;
+        };
+
+        // Oculus events
+        OculusCamera.prototype._onOrientationEvent = function (evt) {
+            var yaw = evt.alpha / 180 * Math.PI;
+            var pitch = evt.beta / 180 * Math.PI;
+            var roll = evt.gamma / 180 * Math.PI;
+
+            if (!this._offsetOrientation) {
+                this._offsetOrientation = {
+                    yaw: yaw,
+                    pitch: pitch,
+                    roll: roll
+                };
+                return;
+            } else {
+                this.rotation.y += yaw - this._offsetOrientation.yaw;
+                this.rotation.x += pitch - this._offsetOrientation.pitch;
+                this.rotation.z += this._offsetOrientation.roll - roll;
+
+                this._offsetOrientation.yaw = yaw;
+                this._offsetOrientation.pitch = pitch;
+                this._offsetOrientation.roll = roll;
+            }
+        };
+
+        OculusCamera.prototype.attachControl = function (element, noPreventDefault) {
+            _super.prototype.attachControl.call(this, element, noPreventDefault);
+
+            window.addEventListener("deviceorientation", this._deviceOrientationHandler);
+        };
+
+        OculusCamera.prototype.detachControl = function (element) {
+            _super.prototype.detachControl.call(this, element);
+
+            window.removeEventListener("deviceorientation", this._deviceOrientationHandler);
+        };
+        return OculusCamera;
+    })(BABYLON.FreeCamera);
+    BABYLON.OculusCamera = OculusCamera;
+})(BABYLON || (BABYLON = {}));
+//# sourceMappingURL=babylon.oculusCamera.js.map

+ 21 - 0
Babylon/Cameras/babylon.oculusGamepadCamera.d.ts

@@ -0,0 +1,21 @@
+declare module BABYLON {
+    class OculusGamepadCamera extends FreeCamera {
+        private _leftCamera;
+        private _rightCamera;
+        private _offsetOrientation;
+        private _deviceOrientationHandler;
+        private _gamepad;
+        private _gamepads;
+        public angularSensibility: number;
+        public moveSensibility: number;
+        constructor(name: string, position: Vector3, scene: Scene);
+        private _onNewGameConnected(gamepad);
+        public _update(): void;
+        public _checkInputs(): void;
+        public _updateCamera(camera: FreeCamera): void;
+        public _onOrientationEvent(evt: DeviceOrientationEvent): void;
+        public attachControl(element: HTMLElement, noPreventDefault?: boolean): void;
+        public detachControl(element: HTMLElement): void;
+        public dispose(): void;
+    }
+}

+ 175 - 0
Babylon/Cameras/babylon.oculusGamepadCamera.js

@@ -0,0 +1,175 @@
+var __extends = this.__extends || function (d, b) {
+    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
+    function __() { this.constructor = d; }
+    __.prototype = b.prototype;
+    d.prototype = new __();
+};
+var BABYLON;
+(function (BABYLON) {
+    var 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
+    };
+
+    var _OculusInnerGamepadCamera = (function (_super) {
+        __extends(_OculusInnerGamepadCamera, _super);
+        function _OculusInnerGamepadCamera(name, position, scene, isLeftEye) {
+            _super.call(this, name, position, scene);
+            this._workMatrix = new BABYLON.Matrix();
+            this._actualUp = new BABYLON.Vector3(0, 0, 0);
+
+            // Constants
+            this._aspectRatioAspectRatio = OculusRiftDevKit2013_Metric.HResolution / (2 * OculusRiftDevKit2013_Metric.VResolution);
+            this._aspectRatioFov = (2 * Math.atan((OculusRiftDevKit2013_Metric.PostProcessScaleFactor * OculusRiftDevKit2013_Metric.VScreenSize) / (2 * OculusRiftDevKit2013_Metric.EyeToScreenDistance)));
+
+            var hMeters = (OculusRiftDevKit2013_Metric.HScreenSize / 4) - (OculusRiftDevKit2013_Metric.LensSeparationDistance / 2);
+            var h = (4 * hMeters) / OculusRiftDevKit2013_Metric.HScreenSize;
+
+            this._hMatrix = BABYLON.Matrix.Translation(isLeftEye ? h : -h, 0, 0);
+
+            this.viewport = new BABYLON.Viewport(isLeftEye ? 0 : 0.5, 0, 0.5, 1.0);
+
+            this._preViewMatrix = BABYLON.Matrix.Translation(isLeftEye ? .5 * OculusRiftDevKit2013_Metric.InterpupillaryDistance : -.5 * OculusRiftDevKit2013_Metric.InterpupillaryDistance, 0, 0);
+
+            // Postprocess
+            var postProcess = new BABYLON.OculusDistortionCorrectionPostProcess("Oculus Distortion", this, !isLeftEye, OculusRiftDevKit2013_Metric);
+        }
+        _OculusInnerGamepadCamera.prototype.getProjectionMatrix = function () {
+            BABYLON.Matrix.PerspectiveFovLHToRef(this._aspectRatioFov, this._aspectRatioAspectRatio, this.minZ, this.maxZ, this._workMatrix);
+            this._workMatrix.multiplyToRef(this._hMatrix, this._projectionMatrix);
+            return this._projectionMatrix;
+        };
+
+        _OculusInnerGamepadCamera.prototype._getViewMatrix = function () {
+            BABYLON.Matrix.RotationYawPitchRollToRef(this.rotation.y, this.rotation.x, this.rotation.z, this._cameraRotationMatrix);
+
+            BABYLON.Vector3.TransformCoordinatesToRef(this._referencePoint, this._cameraRotationMatrix, this._transformedReferencePoint);
+            BABYLON.Vector3.TransformNormalToRef(this.upVector, this._cameraRotationMatrix, this._actualUp);
+
+            // Computing target and final matrix
+            this.position.addToRef(this._transformedReferencePoint, this._currentTarget);
+
+            BABYLON.Matrix.LookAtLHToRef(this.position, this._currentTarget, this._actualUp, this._workMatrix);
+
+            this._workMatrix.multiplyToRef(this._preViewMatrix, this._viewMatrix);
+            return this._viewMatrix;
+        };
+        return _OculusInnerGamepadCamera;
+    })(BABYLON.FreeCamera);
+
+    var OculusGamepadCamera = (function (_super) {
+        __extends(OculusGamepadCamera, _super);
+        function OculusGamepadCamera(name, position, scene) {
+            var _this = this;
+            _super.call(this, name, position, scene);
+            this.angularSensibility = 200;
+            this.moveSensibility = 75;
+
+            this._leftCamera = new _OculusInnerGamepadCamera(name + "_left", position.clone(), scene, true);
+            this._rightCamera = new _OculusInnerGamepadCamera(name + "_right", position.clone(), scene, false);
+
+            this.subCameras.push(this._leftCamera);
+            this.subCameras.push(this._rightCamera);
+
+            this._deviceOrientationHandler = this._onOrientationEvent.bind(this);
+            this._gamepads = new BABYLON.Gamepads(function (gamepad) {
+                _this._onNewGameConnected(gamepad);
+            });
+        }
+        OculusGamepadCamera.prototype._onNewGameConnected = function (gamepad) {
+            // Only the first gamepad can control the camera
+            if (gamepad.index === 0) {
+                this._gamepad = gamepad;
+            }
+        };
+
+        OculusGamepadCamera.prototype._update = function () {
+            this._leftCamera.position.copyFrom(this.position);
+            this._rightCamera.position.copyFrom(this.position);
+
+            this._updateCamera(this._leftCamera);
+            this._updateCamera(this._rightCamera);
+
+            _super.prototype._update.call(this);
+        };
+
+        OculusGamepadCamera.prototype._checkInputs = function () {
+            if (!this._gamepad) {
+                return;
+            }
+
+            var LSValues = this._gamepad.leftStick;
+            var normalizedLX = LSValues.x / this.moveSensibility;
+            var normalizedLY = LSValues.y / this.moveSensibility;
+            LSValues.x = Math.abs(normalizedLX) > 0.005 ? 0 + normalizedLX : 0;
+            LSValues.y = Math.abs(normalizedLY) > 0.005 ? 0 + normalizedLY : 0;
+
+            var cameraTransform = BABYLON.Matrix.RotationYawPitchRoll(this.rotation.y, this.rotation.x, 0);
+            var deltaTransform = BABYLON.Vector3.TransformCoordinates(new BABYLON.Vector3(LSValues.x, 0, -LSValues.y), cameraTransform);
+            this.cameraDirection = this.cameraDirection.add(deltaTransform);
+        };
+
+        OculusGamepadCamera.prototype._updateCamera = function (camera) {
+            camera.minZ = this.minZ;
+            camera.maxZ = this.maxZ;
+
+            camera.rotation.x = this.rotation.x;
+            camera.rotation.y = this.rotation.y;
+            camera.rotation.z = this.rotation.z;
+        };
+
+        // Oculus events
+        OculusGamepadCamera.prototype._onOrientationEvent = function (evt) {
+            var yaw = evt.alpha / 180 * Math.PI;
+            var pitch = evt.beta / 180 * Math.PI;
+            var roll = evt.gamma / 180 * Math.PI;
+
+            if (!this._offsetOrientation) {
+                this._offsetOrientation = {
+                    yaw: yaw,
+                    pitch: pitch,
+                    roll: roll
+                };
+                return;
+            } else {
+                this.rotation.y += yaw - this._offsetOrientation.yaw;
+                this.rotation.x += pitch - this._offsetOrientation.pitch;
+                this.rotation.z += this._offsetOrientation.roll - roll;
+
+                this._offsetOrientation.yaw = yaw;
+                this._offsetOrientation.pitch = pitch;
+                this._offsetOrientation.roll = roll;
+            }
+        };
+
+        OculusGamepadCamera.prototype.attachControl = function (element, noPreventDefault) {
+            _super.prototype.attachControl.call(this, element, noPreventDefault);
+
+            window.addEventListener("deviceorientation", this._deviceOrientationHandler);
+        };
+
+        OculusGamepadCamera.prototype.detachControl = function (element) {
+            _super.prototype.detachControl.call(this, element);
+
+            window.removeEventListener("deviceorientation", this._deviceOrientationHandler);
+        };
+
+        OculusGamepadCamera.prototype.dispose = function () {
+            this._gamepads.dispose();
+            _super.prototype.dispose.call(this);
+        };
+        return OculusGamepadCamera;
+    })(BABYLON.FreeCamera);
+    BABYLON.OculusGamepadCamera = OculusGamepadCamera;
+})(BABYLON || (BABYLON = {}));
+//# sourceMappingURL=babylon.oculusGamepadCamera.js.map

+ 9 - 0
Babylon/Cameras/babylon.vrDeviceOrientationCamera.d.ts

@@ -0,0 +1,9 @@
+declare module BABYLON {
+    class VRDeviceOrientationCamera extends OculusCamera {
+        public _alpha: number;
+        public _beta: number;
+        public _gamma: number;
+        constructor(name: string, position: Vector3, scene: Scene);
+        public _onOrientationEvent(evt: DeviceOrientationEvent): void;
+    }
+}

+ 37 - 0
Babylon/Cameras/babylon.vrDeviceOrientationCamera.js

@@ -0,0 +1,37 @@
+var __extends = this.__extends || function (d, b) {
+    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
+    function __() { this.constructor = d; }
+    __.prototype = b.prototype;
+    d.prototype = new __();
+};
+var BABYLON;
+(function (BABYLON) {
+    var VRDeviceOrientationCamera = (function (_super) {
+        __extends(VRDeviceOrientationCamera, _super);
+        function VRDeviceOrientationCamera(name, position, scene) {
+            _super.call(this, name, position, scene);
+            this._alpha = 0;
+            this._beta = 0;
+            this._gamma = 0;
+        }
+        VRDeviceOrientationCamera.prototype._onOrientationEvent = function (evt) {
+            this._alpha = +evt.alpha | 0;
+            this._beta = +evt.beta | 0;
+            this._gamma = +evt.gamma | 0;
+
+            if (this._gamma < 0) {
+                this._gamma = 90 + this._gamma;
+            } else {
+                // Incline it in the correct angle.
+                this._gamma = 270 - this._gamma;
+            }
+
+            this.rotation.x = this._gamma / 180.0 * Math.PI;
+            this.rotation.y = -this._alpha / 180.0 * Math.PI;
+            this.rotation.z = this._beta / 180.0 * Math.PI;
+        };
+        return VRDeviceOrientationCamera;
+    })(BABYLON.OculusCamera);
+    BABYLON.VRDeviceOrientationCamera = VRDeviceOrientationCamera;
+})(BABYLON || (BABYLON = {}));
+//# sourceMappingURL=babylon.vrDeviceOrientationCamera.js.map

+ 17 - 0
Babylon/Cameras/babylon.webVRCamera.d.ts

@@ -0,0 +1,17 @@
+declare var HMDVRDevice: any;
+declare var PositionSensorVRDevice: any;
+declare module BABYLON {
+    class WebVRCamera extends OculusCamera {
+        public _hmdDevice: any;
+        public _sensorDevice: any;
+        public _cacheState: any;
+        public _cacheQuaternion: Quaternion;
+        public _cacheRotation: Vector3;
+        public _vrEnabled: boolean;
+        constructor(name: string, position: Vector3, scene: Scene);
+        private _getWebVRDevices(devices);
+        public _update(): void;
+        public attachControl(element: HTMLElement, noPreventDefault?: boolean): void;
+        public detachControl(element: HTMLElement): void;
+    }
+}

+ 80 - 0
Babylon/Cameras/babylon.webVRCamera.js

@@ -0,0 +1,80 @@
+var __extends = this.__extends || function (d, b) {
+    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
+    function __() { this.constructor = d; }
+    __.prototype = b.prototype;
+    d.prototype = new __();
+};
+var BABYLON;
+(function (BABYLON) {
+    var WebVRCamera = (function (_super) {
+        __extends(WebVRCamera, _super);
+        function WebVRCamera(name, position, scene) {
+            _super.call(this, name, position, scene);
+            this._hmdDevice = null;
+            this._sensorDevice = null;
+            this._cacheState = null;
+            this._cacheQuaternion = new BABYLON.Quaternion();
+            this._cacheRotation = BABYLON.Vector3.Zero();
+            this._vrEnabled = false;
+            this._getWebVRDevices = this._getWebVRDevices.bind(this);
+        }
+        WebVRCamera.prototype._getWebVRDevices = function (devices) {
+            var size = devices.length;
+            var i = 0;
+
+            // Reset devices.
+            this._sensorDevice = null;
+            this._hmdDevice = null;
+
+            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;
+        };
+
+        WebVRCamera.prototype._update = function () {
+            if (this._vrEnabled) {
+                this._cacheState = this._sensorDevice.getState();
+                this._cacheQuaternion.copyFromFloats(this._cacheState.orientation.x, this._cacheState.orientation.y, this._cacheState.orientation.z, this._cacheState.orientation.w);
+                this._cacheQuaternion.toEulerAnglesToRef(this._cacheRotation);
+
+                this.rotation.x = -this._cacheRotation.z;
+                this.rotation.y = -this._cacheRotation.y;
+                this.rotation.z = this._cacheRotation.x;
+            }
+
+            _super.prototype._update.call(this);
+        };
+
+        WebVRCamera.prototype.attachControl = function (element, noPreventDefault) {
+            _super.prototype.attachControl.call(this, element, noPreventDefault);
+
+            if (navigator.getVRDevices) {
+                navigator.getVRDevices().then(this._getWebVRDevices);
+            } else if (navigator.mozGetVRDevices) {
+                navigator.mozGetVRDevices(this._getWebVRDevices);
+            }
+        };
+
+        WebVRCamera.prototype.detachControl = function (element) {
+            _super.prototype.detachControl.call(this, element);
+            this._vrEnabled = false;
+        };
+        return WebVRCamera;
+    })(BABYLON.OculusCamera);
+    BABYLON.WebVRCamera = WebVRCamera;
+})(BABYLON || (BABYLON = {}));
+//# sourceMappingURL=babylon.webVRCamera.js.map