summit/frontend/node_modules/three-stdlib/controls/DeviceOrientationControls.c...

1 line
6.2 KiB
Plaintext
Raw Normal View History

2025-12-08 16:31:30 +00:00
{"version":3,"file":"DeviceOrientationControls.cjs","sources":["../../src/controls/DeviceOrientationControls.ts"],"sourcesContent":["import { Camera, Euler, MathUtils, Quaternion, Vector3 } from 'three'\nimport { EventDispatcher } from './EventDispatcher'\nimport { StandardControlsEventMap } from './StandardControlsEventMap'\n\n/**\n * W3C Device Orientation control (http://w3c.github.io/deviceorientation/spec-source-orientation.html)\n */\n\nclass DeviceOrientationControls extends EventDispatcher<StandardControlsEventMap> {\n public object: Camera\n\n private changeEvent = { type: 'change' }\n private EPS = 0.000001\n\n public enabled = true\n public deviceOrientation: Partial<DeviceOrientationEvent> = { alpha: 0, beta: 0, gamma: 0 }\n public screenOrientation: string | number = 0\n public alphaOffset = 0 // radians\n\n constructor(object: Camera) {\n super()\n\n this.object = object\n this.object.rotation.reorder('YXZ')\n\n this.connect()\n }\n\n private onDeviceOrientationChangeEvent = (event: DeviceOrientationEvent): void => {\n this.deviceOrientation = event\n }\n\n private onScreenOrientationChangeEvent = (): void => {\n this.screenOrientation = window.orientation || 0\n }\n\n // The angles alpha, beta and gamma form a set of intrinsic Tait-Bryan angles of type Z-X'-Y''\n\n private zee = new Vector3(0, 0, 1)\n private euler = new Euler()\n private q0 = new Quaternion()\n private q1 = new Quaternion(-Math.sqrt(0.5), 0, 0, Math.sqrt(0.5)) // - PI/2 around the x-axis\n private setObjectQuaternion = (\n quaternion: Quaternion,\n alpha: number,\n beta: number,\n gamma: number,\n orient: number,\n ): void => {\n this.euler.set(beta, alpha, -gamma, 'YXZ') // 'ZXY' for the device, but 'YXZ' for us\n quaternion.setFromEuler(this.euler) // orient the device\n quaternion.multiply(this.q1) // camera looks out the back of the device, not the top\n quaternion.multiply(this.q0.setFromAxisAngle(this.zee, -orient)) // adjust for screen orientation\n }\n\n public connect = (): void => {\n this.onScreenOrientationChangeEvent() // run once on load\n\n // iOS 13+\n\n if (\n window.DeviceOrientationEvent !== undefined &&\n // @ts-ignore\n typeof window.DeviceOrientationEvent.requestPermission === 'function'\n ) {\n // @ts-ignore\n window.DeviceOrientationEvent.requestPermission()\n .then((response: any) => {\n if (response == 'granted') {\n window.addEventListener('orientationchange', this.onScreenOrientationChangeEvent)\n window.addEventListener('deviceorientation', this.onDeviceOrientationChangeEvent)\n }\n })\n .catch((error: any) => {\n console.error('THREE.DeviceOrientationControls: Unable to use DeviceOrientation API:', error)\n })\n } else {\n window.addEventListener('orientationchange', this.onScreenOrientationChangeEvent)\n window.addEventListener('deviceorientation', this.onDeviceOrientationChangeEvent)\n }\n\n this.enabled = true\n }\n\n public disconnect = (): void => {\n window.removeEventListener('orientationchange', this.onScreenOrientationChangeEvent)\n window.removeEventListener('deviceorientation', this.onDeviceOrientationChangeEvent)\n\n this.enabled = false\n }\n\n private lastQuaternion = new Quaternion()\n public update = (): void => {\n if (this.enabled === false) return\n\n const device = this.deviceOrientation\n\n if (device) {\n const alpha = device.alpha ? MathUtils.degToRad(device.alpha) + this.alphaOffset : 0 // Z\n const beta = device.beta ? MathUtils.degToRad(device.beta) : 0 // X'\n const gamma = device.gamma ? MathUtils.degToRad(device.gamma) : 0 // Y''\n const orient = this.screenOrientation ? MathUtils.degToRad(this.screenOrientation as number) : 0 // O\n\n this.setObjectQuaternion(this.object.quaternion, alpha, beta, gamma, orient)\n\n if (8 * (1 - this.lastQuaternion.dot(this.object.quaternion)) > this.EPS) {\n this.lastQuaternion.copy(this.obj