|
@@ -4,7 +4,7 @@ import { FreeCamera } from "../../Cameras/freeCamera";
|
|
import { Quaternion } from "../../Maths/math.vector";
|
|
import { Quaternion } from "../../Maths/math.vector";
|
|
import { Tools } from "../../Misc/tools";
|
|
import { Tools } from "../../Misc/tools";
|
|
import { FreeCameraInputsManager } from "../../Cameras/freeCameraInputsManager";
|
|
import { FreeCameraInputsManager } from "../../Cameras/freeCameraInputsManager";
|
|
-import { Observable } from '../../Misc/observable';
|
|
|
|
|
|
+import { Observable } from "../../Misc/observable";
|
|
|
|
|
|
// Module augmentation to abstract orientation inputs from camera.
|
|
// Module augmentation to abstract orientation inputs from camera.
|
|
declare module "../../Cameras/freeCameraInputsManager" {
|
|
declare module "../../Cameras/freeCameraInputsManager" {
|
|
@@ -25,7 +25,7 @@ declare module "../../Cameras/freeCameraInputsManager" {
|
|
* Add orientation input support to the input manager.
|
|
* Add orientation input support to the input manager.
|
|
* @returns the current input manager
|
|
* @returns the current input manager
|
|
*/
|
|
*/
|
|
-FreeCameraInputsManager.prototype.addDeviceOrientation = function(): FreeCameraInputsManager {
|
|
|
|
|
|
+FreeCameraInputsManager.prototype.addDeviceOrientation = function (): FreeCameraInputsManager {
|
|
if (!this._deviceOrientationInput) {
|
|
if (!this._deviceOrientationInput) {
|
|
this._deviceOrientationInput = new FreeCameraDeviceOrientationInput();
|
|
this._deviceOrientationInput = new FreeCameraDeviceOrientationInput();
|
|
this.add(this._deviceOrientationInput);
|
|
this.add(this._deviceOrientationInput);
|
|
@@ -75,10 +75,11 @@ export class FreeCameraDeviceOrientationInput implements ICameraInput<FreeCamera
|
|
}, timeout);
|
|
}, timeout);
|
|
}
|
|
}
|
|
|
|
|
|
- if (typeof(DeviceOrientationEvent) !== "undefined" && typeof (<any>DeviceOrientationEvent).requestPermission === 'function') {
|
|
|
|
- (<any>DeviceOrientationEvent).requestPermission()
|
|
|
|
|
|
+ if (typeof DeviceOrientationEvent !== "undefined" && typeof (<any>DeviceOrientationEvent).requestPermission === "function") {
|
|
|
|
+ (<any>DeviceOrientationEvent)
|
|
|
|
+ .requestPermission()
|
|
.then((response: string) => {
|
|
.then((response: string) => {
|
|
- if (response == 'granted') {
|
|
|
|
|
|
+ if (response == "granted") {
|
|
window.addEventListener("deviceorientation", eventHandler);
|
|
window.addEventListener("deviceorientation", eventHandler);
|
|
} else {
|
|
} else {
|
|
Tools.Warn("Permission not granted.");
|
|
Tools.Warn("Permission not granted.");
|
|
@@ -102,7 +103,7 @@ export class FreeCameraDeviceOrientationInput implements ICameraInput<FreeCamera
|
|
* @see https://doc.babylonjs.com/how_to/customizing_camera_inputs
|
|
* @see https://doc.babylonjs.com/how_to/customizing_camera_inputs
|
|
*/
|
|
*/
|
|
constructor() {
|
|
constructor() {
|
|
- this._constantTranform = new Quaternion(- Math.sqrt(0.5), 0, 0, Math.sqrt(0.5));
|
|
|
|
|
|
+ this._constantTranform = new Quaternion(-Math.sqrt(0.5), 0, 0, Math.sqrt(0.5));
|
|
this._orientationChanged();
|
|
this._orientationChanged();
|
|
}
|
|
}
|
|
|
|
|
|
@@ -129,11 +130,9 @@ export class FreeCameraDeviceOrientationInput implements ICameraInput<FreeCamera
|
|
* Attach the input controls to a specific dom element to get the input from.
|
|
* Attach the input controls to a specific dom element to get the input from.
|
|
*/
|
|
*/
|
|
public attachControl(): void {
|
|
public attachControl(): void {
|
|
-
|
|
|
|
let hostWindow = this.camera.getScene().getEngine().getHostWindow();
|
|
let hostWindow = this.camera.getScene().getEngine().getHostWindow();
|
|
|
|
|
|
if (hostWindow) {
|
|
if (hostWindow) {
|
|
-
|
|
|
|
const eventHandler = () => {
|
|
const eventHandler = () => {
|
|
hostWindow!.addEventListener("orientationchange", this._orientationChanged);
|
|
hostWindow!.addEventListener("orientationchange", this._orientationChanged);
|
|
hostWindow!.addEventListener("deviceorientation", this._deviceOrientation);
|
|
hostWindow!.addEventListener("deviceorientation", this._deviceOrientation);
|
|
@@ -141,10 +140,11 @@ export class FreeCameraDeviceOrientationInput implements ICameraInput<FreeCamera
|
|
//So this is needed.
|
|
//So this is needed.
|
|
this._orientationChanged();
|
|
this._orientationChanged();
|
|
};
|
|
};
|
|
- if (typeof(DeviceOrientationEvent) !== "undefined" && typeof (<any>DeviceOrientationEvent).requestPermission === 'function') {
|
|
|
|
- (<any>DeviceOrientationEvent).requestPermission()
|
|
|
|
|
|
+ if (typeof DeviceOrientationEvent !== "undefined" && typeof (<any>DeviceOrientationEvent).requestPermission === "function") {
|
|
|
|
+ (<any>DeviceOrientationEvent)
|
|
|
|
+ .requestPermission()
|
|
.then((response: string) => {
|
|
.then((response: string) => {
|
|
- if (response === 'granted') {
|
|
|
|
|
|
+ if (response === "granted") {
|
|
eventHandler();
|
|
eventHandler();
|
|
} else {
|
|
} else {
|
|
Tools.Warn("Permission not granted.");
|
|
Tools.Warn("Permission not granted.");
|
|
@@ -160,10 +160,10 @@ export class FreeCameraDeviceOrientationInput implements ICameraInput<FreeCamera
|
|
}
|
|
}
|
|
|
|
|
|
private _orientationChanged = () => {
|
|
private _orientationChanged = () => {
|
|
- this._screenOrientationAngle = (<any>window.orientation !== undefined ? +<any>window.orientation : ((<any>window.screen).orientation && ((<any>window.screen).orientation)['angle'] ? ((<any>window.screen).orientation).angle : 0));
|
|
|
|
|
|
+ this._screenOrientationAngle = <any>window.orientation !== undefined ? +(<any>window.orientation) : (<any>window.screen).orientation && (<any>window.screen).orientation["angle"] ? (<any>window.screen).orientation.angle : 0;
|
|
this._screenOrientationAngle = -Tools.ToRadians(this._screenOrientationAngle / 2);
|
|
this._screenOrientationAngle = -Tools.ToRadians(this._screenOrientationAngle / 2);
|
|
this._screenQuaternion.copyFromFloats(0, Math.sin(this._screenOrientationAngle), 0, Math.cos(this._screenOrientationAngle));
|
|
this._screenQuaternion.copyFromFloats(0, Math.sin(this._screenOrientationAngle), 0, Math.cos(this._screenOrientationAngle));
|
|
- }
|
|
|
|
|
|
+ };
|
|
|
|
|
|
private _deviceOrientation = (evt: DeviceOrientationEvent) => {
|
|
private _deviceOrientation = (evt: DeviceOrientationEvent) => {
|
|
this._alpha = evt.alpha !== null ? evt.alpha : 0;
|
|
this._alpha = evt.alpha !== null ? evt.alpha : 0;
|
|
@@ -172,12 +172,18 @@ export class FreeCameraDeviceOrientationInput implements ICameraInput<FreeCamera
|
|
if (evt.alpha !== null) {
|
|
if (evt.alpha !== null) {
|
|
this._onDeviceOrientationChangedObservable.notifyObservers();
|
|
this._onDeviceOrientationChangedObservable.notifyObservers();
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
+ };
|
|
|
|
|
|
/**
|
|
/**
|
|
* Detach the current controls from the specified dom element.
|
|
* Detach the current controls from the specified dom element.
|
|
*/
|
|
*/
|
|
- public detachControl(): void {
|
|
|
|
|
|
+ public detachControl(): void;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Detach the current controls from the specified dom element.
|
|
|
|
+ * @param ignored defines an ignored parameter kept for backward compatibility. If you want to define the source input element, you can set engine.inputElement before calling camera.attachControl
|
|
|
|
+ */
|
|
|
|
+ public detachControl(ignored?: any): void {
|
|
window.removeEventListener("orientationchange", this._orientationChanged);
|
|
window.removeEventListener("orientationchange", this._orientationChanged);
|
|
window.removeEventListener("deviceorientation", this._deviceOrientation);
|
|
window.removeEventListener("deviceorientation", this._deviceOrientation);
|
|
this._alpha = 0;
|
|
this._alpha = 0;
|
|
@@ -190,7 +196,9 @@ export class FreeCameraDeviceOrientationInput implements ICameraInput<FreeCamera
|
|
public checkInputs(): void {
|
|
public checkInputs(): void {
|
|
//if no device orientation provided, don't update the rotation.
|
|
//if no device orientation provided, don't update the rotation.
|
|
//Only testing against alpha under the assumption thatnorientation will never be so exact when set.
|
|
//Only testing against alpha under the assumption thatnorientation will never be so exact when set.
|
|
- if (!this._alpha) { return; }
|
|
|
|
|
|
+ if (!this._alpha) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
Quaternion.RotationYawPitchRollToRef(Tools.ToRadians(this._alpha), Tools.ToRadians(this._beta), -Tools.ToRadians(this._gamma), this.camera.rotationQuaternion);
|
|
Quaternion.RotationYawPitchRollToRef(Tools.ToRadians(this._alpha), Tools.ToRadians(this._beta), -Tools.ToRadians(this._gamma), this.camera.rotationQuaternion);
|
|
this._camera.rotationQuaternion.multiplyInPlace(this._screenQuaternion);
|
|
this._camera.rotationQuaternion.multiplyInPlace(this._screenQuaternion);
|
|
this._camera.rotationQuaternion.multiplyInPlace(this._constantTranform);
|
|
this._camera.rotationQuaternion.multiplyInPlace(this._constantTranform);
|