Repository navigation
Expand file tree
/
Copy pathPlanetOrbitControls.js
More file actions
165 lines (145 loc) · 5.31 KB
/
Copy pathPlanetOrbitControls.js
File metadata and controls
165 lines (145 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import * as THREE from 'three';
// ============================================================================
// Orbit camera for Planet view mode. Orbits the planet center (origin):
// - drag (any button) : orbit
// - wheel : zoom (clamped between just above the surface and a
// far orbit)
// Smooth-damped spherical camera, world-up +Y. Mirrors the math style of
// EditorControls but always targets the origin.
// ============================================================================
const DEG = Math.PI / 180;
export class PlanetOrbitControls {
constructor(camera, domElement, radius) {
this.camera = camera;
this.dom = domElement;
this.enabled = true;
this.setRadius(radius);
this.phi = 65 * DEG; this.goalPhi = 65 * DEG;
this.theta = 35 * DEG; this.goalTheta = 35 * DEG;
this.minPhi = 2 * DEG;
this.maxPhi = 178 * DEG;
this.onFirstInteract = null;
this._interacted = false;
this._drag = null;
this._touches = new Map();
this._pinch = null;
this._onDown = this._onDown.bind(this);
this._onMove = this._onMove.bind(this);
this._onUp = this._onUp.bind(this);
this._onWheel = this._onWheel.bind(this);
this._onCtx = (e) => e.preventDefault();
domElement.addEventListener('pointerdown', this._onDown);
domElement.addEventListener('pointermove', this._onMove);
domElement.addEventListener('pointerup', this._onUp);
domElement.addEventListener('pointercancel', this._onUp);
domElement.addEventListener('wheel', this._onWheel, { passive: false });
domElement.addEventListener('contextmenu', this._onCtx);
}
setRadius(radius) {
this.planetRadius = radius;
this.minDist = radius * 1.02;
this.maxDist = radius * 6.0;
const def = radius * 2.6;
this.dist = def; this.goalDist = def;
}
dispose() {
const d = this.dom;
d.removeEventListener('pointerdown', this._onDown);
d.removeEventListener('pointermove', this._onMove);
d.removeEventListener('pointerup', this._onUp);
d.removeEventListener('pointercancel', this._onUp);
d.removeEventListener('wheel', this._onWheel);
d.removeEventListener('contextmenu', this._onCtx);
}
_markInteract() {
if (!this._interacted) {
this._interacted = true;
if (this.onFirstInteract) this.onFirstInteract();
}
}
_onDown(e) {
if (!this.enabled) return;
this._markInteract();
if (e.pointerType === 'touch') {
this._touches.set(e.pointerId, { x: e.clientX, y: e.clientY });
this._pinch = this._getPinchState();
} else {
this._drag = { x: e.clientX, y: e.clientY };
}
this.dom.setPointerCapture(e.pointerId);
}
_orbitByPixels(dx, dy) {
this.goalTheta -= dx * 0.005;
this.goalPhi = Math.min(Math.max(this.goalPhi - dy * 0.004, this.minPhi), this.maxPhi);
}
_getPinchState() {
const pts = Array.from(this._touches.values());
if (pts.length < 2) return null;
const [a, b] = pts;
const dx = b.x - a.x, dy = b.y - a.y;
return { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2, dist: Math.hypot(dx, dy) || 1, zoom: this.goalDist };
}
_onMove(e) {
if (e.pointerType === 'touch' && this._touches.has(e.pointerId)) {
const prev = this._touches.get(e.pointerId);
this._touches.set(e.pointerId, { x: e.clientX, y: e.clientY });
const pts = Array.from(this._touches.values());
if (pts.length >= 2) {
const next = this._getPinchState();
if (this._pinch && next) {
this._orbitByPixels(next.x - this._pinch.x, next.y - this._pinch.y);
this.goalDist = Math.min(Math.max(this._pinch.zoom * (this._pinch.dist / next.dist), this.minDist), this.maxDist);
this._pinch.x = next.x;
this._pinch.y = next.y;
}
return;
}
if (pts.length === 1 && this._pinch === null && prev) this._orbitByPixels(e.clientX - prev.x, e.clientY - prev.y);
return;
}
if (!this._drag) return;
const dx = e.clientX - this._drag.x;
const dy = e.clientY - this._drag.y;
this._drag.x = e.clientX;
this._drag.y = e.clientY;
this._orbitByPixels(dx, dy);
}
_onUp(e) {
if (e.pointerType === 'touch') {
this._touches.delete(e.pointerId);
this._pinch = this._getPinchState();
try { this.dom.releasePointerCapture(e.pointerId); } catch { /* ok */ }
return;
}
if (this._drag) {
this._drag = null;
try { this.dom.releasePointerCapture(e.pointerId); } catch { /* ok */ }
}
}
_onWheel(e) {
if (!this.enabled) return;
e.preventDefault();
this._markInteract();
this.goalDist = Math.min(
Math.max(this.goalDist * Math.exp(e.deltaY * 0.0011), this.minDist),
this.maxDist
);
}
update(dt) {
const k = 1 - Math.exp(-dt * 9);
this.dist += (this.goalDist - this.dist) * k;
this.phi += (this.goalPhi - this.phi) * k;
this.theta += (this.goalTheta - this.theta) * k;
const sinPhi = Math.sin(this.phi);
this.camera.position.set(
this.dist * sinPhi * Math.sin(this.theta),
this.dist * Math.cos(this.phi),
this.dist * sinPhi * Math.cos(this.theta)
);
this.camera.up.set(0, 1, 0);
this.camera.lookAt(0, 0, 0);
}
get azimuthDeg() { return ((this.theta / DEG) % 360 + 360) % 360; }
get elevationDeg() { return 90 - this.phi / DEG; }
get distance() { return this.dist; }
}