var DeviceOrientationControls = function ( object ) {// 参数是camera
var scope = this;
var EPS = 0.000001;
this.object = object;
this.enabled = true;
this.deviceOrientation = {};
this.screenOrientation = 0;
this.alphaOffset = 0; // radians
var onDeviceOrientationChangeEvent = function ( event ) {
scope.deviceOrientation = event;
};
var onScreenOrientationChangeEvent = function () {
scope.screenOrientation = window.orientation || 0;
};
var setObjectQuaternion = function () {
var zee = new Laya.Vector3( 0, 0, 1 );
var q0 = new Laya.Quaternion();
var q1 = new Laya.Quaternion( - Math.sqrt( 0.5 ), 0, 0, Math.sqrt( 0.5 ) ); // - PI/2 around the x-axis
return function ( quaternion, alpha, beta, gamma, orient ) {
Laya.Quaternion.createFromYawPitchRoll(beta, alpha, - gamma ,quaternion)
Laya.Quaternion.multiply(quaternion,q1,quaternion);
Laya.Quaternion.createFromAxisAngle(zee, - orient, q0);
Laya.Quaternion.multiply(quaternion,q0,quaternion);
scope.object.transform.rotation = quaternion;
};
}();
this.connect = function () {
onScreenOrientationChangeEvent(); // run once on load
// iOS 13+
if ( window.DeviceOrientationEvent !== undefined && typeof window.DeviceOrientationEvent.requestPermission === 'function' ) {
window.DeviceOrientationEvent.requestPermission().then( function ( response ) {
if ( response == 'granted' ) {
window.addEventListener( 'orientationchange', onScreenOrientationChangeEvent );
window.addEventListener( 'deviceorientation', onDeviceOrientationChangeEvent );
}
} ).catch( function ( error ) {
console.error( 'DeviceOrientationControls: Unable to use DeviceOrientation API:', error );
} );
} else {
window.addEventListener( 'orientationchange', onScreenOrientationChangeEvent );
window.addEventListener( 'deviceorientation', onDeviceOrientationChangeEvent );
}
scope.enabled = true;
};
this.disconnect = function () {
window.removeEventListener( 'orientationchange', onScreenOrientationChangeEvent );
window.removeEventListener( 'deviceorientation', onDeviceOrientationChangeEvent );
scope.enabled = false;
};
this.update = ( function () {
var lastQuaternion = new Laya.Quaternion();
return function () {
if ( scope.enabled === false ) return;
var device = scope.deviceOrientation;
if ( device ) {
var alpha = device.alpha ? scope.degToRad( device.alpha ) + scope.alphaOffset : 0; // Z
var beta = device.beta ? scope.degToRad( device.beta ) : 0; // X'
var gamma = device.gamma ? scope.degToRad( device.gamma ) : 0; // Y''
var orient = scope.screenOrientation ? scope.degToRad( scope.screenOrientation ) : 0; // O
console.log(alpha,beta,gamma)
setObjectQuaternion( scope.object.transform.rotation, alpha, beta, gamma, orient );
if ( 8 * ( 1 - Laya.Quaternion.dot(lastQuaternion,scope.object.transform.rotation) ) > EPS ) {
lastQuaternion.cloneTo( scope.object.transform.rotation );
}
}
};
} )();
this.degToRad = function(degrees){
return degrees * (Math.PI / 180);
}
this.dispose = function () {
scope.disconnect();
};
this.connect();
};
使用方法:
var deviceControl;
class LoadModel{
constructor(resource){
}
onModelLoad(){
deviceControl = new DeviceOrientationControls(camera);
this.animate();
}
animate(){
var _this = this;
deviceControl.update();
requestAnimationFrame(_this.animate.bind(_this));
}
}