-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: implement touch pan controls again #89
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import * as THREE from 'three'; | ||
import OrbitControls from 'three/examples/js/controls/OrbitControls.js'; | ||
import DeviceOrientationControls from 'three/examples/js/controls/DeviceOrientationControls.js'; | ||
|
||
/** | ||
* Convert a quaternion to an angle | ||
* | ||
* Taken from https://stackoverflow.com/a/35448946 | ||
* Thanks P. Ellul | ||
*/ | ||
function Quat2Angle(x, y, z, w) { | ||
const test = x * y + z * w; | ||
|
||
// singularity at north pole | ||
if (test > 0.499) { | ||
const yaw = 2 * Math.atan2(x, w); | ||
const pitch = Math.PI / 2; | ||
const roll = 0; | ||
|
||
return new THREE.Vector3(pitch, roll, yaw); | ||
} | ||
|
||
// singularity at south pole | ||
if (test < -0.499) { | ||
const yaw = -2 * Math.atan2(x, w); | ||
const pitch = -Math.PI / 2; | ||
const roll = 0; | ||
|
||
return new THREE.Vector3(pitch, roll, yaw); | ||
} | ||
|
||
const sqx = x * x; | ||
const sqy = y * y; | ||
const sqz = z * z; | ||
const yaw = Math.atan2(2 * y * w - 2 * x * z, 1 - 2 * sqy - 2 * sqz); | ||
const pitch = Math.asin(2 * test); | ||
const roll = Math.atan2(2 * x * w - 2 * y * z, 1 - 2 * sqx - 2 * sqz); | ||
|
||
return new THREE.Vector3(pitch, roll, yaw); | ||
} | ||
|
||
class OrbitOrientationControls { | ||
constructor(options) { | ||
this.object = options.camera; | ||
this.domElement = options.canvas; | ||
this.orbit = new OrbitControls(this.object, this.domElement); | ||
|
||
this.speed = 0.5; | ||
this.orbit.target.set(0, 0, -1); | ||
this.orbit.enableZoom = false; | ||
this.orbit.enablePan = false; | ||
this.orbit.rotateSpeed = -this.speed; | ||
|
||
// if orientation is supported | ||
if (options.orientation) { | ||
this.orientation = new DeviceOrientationControls(this.object); | ||
} | ||
} | ||
|
||
update() { | ||
// orientation updates the camera using quaternions and | ||
// orbit updates the camera using angles. They are incompatible | ||
// and one update overrides the other. So before | ||
// orbit overrides orientation we convert our quaternion changes to | ||
// an angle change. Then save the angle into orbit so that | ||
// it will take those into account when it updates the camera and overrides | ||
// our changes | ||
if (this.orientation) { | ||
this.orientation.update(); | ||
|
||
const quat = this.orientation.object.quaternion; | ||
const currentAngle = Quat2Angle(quat.x, quat.y, quat.z, quat.w); | ||
|
||
// we also have to store the last angle since quaternions are b | ||
if (typeof this.lastAngle_ === 'undefined') { | ||
this.lastAngle_ = currentAngle; | ||
} | ||
|
||
this.orbit.rotateLeft((this.lastAngle_.z - currentAngle.z) * (1 + this.speed)); | ||
this.orbit.rotateUp((this.lastAngle_.y - currentAngle.y) * (1 + this.speed)); | ||
this.lastAngle_ = currentAngle; | ||
} | ||
|
||
this.orbit.update(); | ||
} | ||
|
||
dispose() { | ||
this.orbit.dispose(); | ||
|
||
if (this.orientation) { | ||
this.orientation.dispose(); | ||
} | ||
} | ||
|
||
} | ||
|
||
export default OrbitOrientationControls; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A somewhat
hacky
patch toOrbitControls
to allow us to modify the movement a bit so that we can add on the movement fromDeviceOrientationControls
. I felt like this was a better alternative to Grabbing the code, storing it in our repo, and maintaining these small modifications.