@vyr/three 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +26 -0
- package/src/actor/ComposerServiceActor.ts +107 -0
- package/src/actor/GeometryActor.ts +13 -0
- package/src/actor/HTMLConvertActor.ts +55 -0
- package/src/actor/MaterialActor.ts +13 -0
- package/src/actor/NodeActor.ts +25 -0
- package/src/actor/OrbitControllerActor.ts +110 -0
- package/src/actor/PassActor.ts +24 -0
- package/src/actor/SceneServiceActor.ts +122 -0
- package/src/actor/TextureActor.ts +13 -0
- package/src/actor/TransformControllerActor.ts +23 -0
- package/src/actor/index.ts +10 -0
- package/src/asset/index.ts +187 -0
- package/src/controls/CameraControls.ts +2360 -0
- package/src/controls/TransformControls.ts +1747 -0
- package/src/controls/index.ts +2 -0
- package/src/descriptor/ComposerServiceDescriptor.ts +47 -0
- package/src/descriptor/GeoMapDescriptor.ts +24 -0
- package/src/descriptor/HTMLConvertDescriptor.ts +12 -0
- package/src/descriptor/InstancedMeshDescriptor.ts +21 -0
- package/src/descriptor/MeshDescriptor.ts +16 -0
- package/src/descriptor/ModelDescriptor.ts +15 -0
- package/src/descriptor/OrbitControllerDescriptor.ts +84 -0
- package/src/descriptor/OrthographicCameraDescriptor.ts +12 -0
- package/src/descriptor/ParticleDescriptor.ts +88 -0
- package/src/descriptor/PassDescriptor.ts +33 -0
- package/src/descriptor/PerspectiveCameraDescriptor.ts +15 -0
- package/src/descriptor/PointsDescriptor.ts +16 -0
- package/src/descriptor/SceneServiceDescriptor.ts +39 -0
- package/src/descriptor/SpriteDescriptor.ts +16 -0
- package/src/descriptor/TextDescriptor.ts +41 -0
- package/src/descriptor/TransformControllerDescriptor.ts +32 -0
- package/src/descriptor/animation/AnimationActionDescriptor.ts +52 -0
- package/src/descriptor/geometry/BoxGeometryDescriptor.ts +26 -0
- package/src/descriptor/geometry/BufferGeometryDescriptor.ts +15 -0
- package/src/descriptor/geometry/CircleGeometryDescriptor.ts +22 -0
- package/src/descriptor/geometry/CylinderGeometryDescriptor.ts +30 -0
- package/src/descriptor/geometry/ExtrudeGeometryDescriptor.ts +35 -0
- package/src/descriptor/geometry/GeometryDescriptor.ts +8 -0
- package/src/descriptor/geometry/PlaneGeometryDescriptor.ts +22 -0
- package/src/descriptor/geometry/RingGeometryDescriptor.ts +26 -0
- package/src/descriptor/geometry/SphereGeometryDescriptor.ts +27 -0
- package/src/descriptor/geometry/SurfaceGeometryDescriptor.ts +15 -0
- package/src/descriptor/geometry/TubeGeometryDescriptor.ts +25 -0
- package/src/descriptor/helper/AxesHelperDescriptor.ts +8 -0
- package/src/descriptor/index.ts +45 -0
- package/src/descriptor/light/AmbientLightDescriptor.ts +8 -0
- package/src/descriptor/light/DirectionalLightDescriptor.ts +33 -0
- package/src/descriptor/light/HemisphereLightDescriptor.ts +16 -0
- package/src/descriptor/light/LightDescriptor.ts +16 -0
- package/src/descriptor/light/PointLightDescriptor.ts +24 -0
- package/src/descriptor/light/RectAreaLightDescriptor.ts +20 -0
- package/src/descriptor/light/SpotLightDescriptor.ts +30 -0
- package/src/descriptor/material/MaterialDescriptor.ts +84 -0
- package/src/descriptor/material/MeshBasicMaterialDescriptor.ts +53 -0
- package/src/descriptor/material/MeshPhongMaterialDescriptor.ts +102 -0
- package/src/descriptor/material/MeshStandardMaterialDescriptor.ts +99 -0
- package/src/descriptor/material/PointsMaterialDescriptor.ts +31 -0
- package/src/descriptor/material/ShaderMaterialDescriptor.ts +35 -0
- package/src/descriptor/material/ShadowMaterialDescriptor.ts +19 -0
- package/src/descriptor/material/SpriteMaterialDescriptor.ts +31 -0
- package/src/descriptor/texture/TextureDescriptor.ts +110 -0
- package/src/index.ts +9 -0
- package/src/interpreter/ComposerServiceInterpreter.ts +25 -0
- package/src/interpreter/GeoMapInterpreter.ts +253 -0
- package/src/interpreter/HTMLConvertInterpreter.ts +31 -0
- package/src/interpreter/InstancedMeshInterpreter.ts +76 -0
- package/src/interpreter/MeshInterpreter.ts +25 -0
- package/src/interpreter/ModelInterpreter.ts +61 -0
- package/src/interpreter/NodeInterpreter.ts +65 -0
- package/src/interpreter/OrbitControllerInterpreter.ts +47 -0
- package/src/interpreter/OrthographicCameraInterpreter.ts +13 -0
- package/src/interpreter/ParticleInterpreter.ts +221 -0
- package/src/interpreter/PassInterpreter.ts +43 -0
- package/src/interpreter/PerspectiveCameraInterpreter.ts +33 -0
- package/src/interpreter/PointsInterpreter.ts +61 -0
- package/src/interpreter/SceneServiceInterpreter.ts +119 -0
- package/src/interpreter/ServiceSchedulerInterpreter.ts +23 -0
- package/src/interpreter/SpriteInterpreter.ts +45 -0
- package/src/interpreter/TextInterpreter.ts +76 -0
- package/src/interpreter/TransformControllerInterpreter.ts +44 -0
- package/src/interpreter/animation/AnimationActionInterpreter.ts +66 -0
- package/src/interpreter/geometry/BoxGeometryInterpreter.ts +34 -0
- package/src/interpreter/geometry/BufferGeometryInterpreter.ts +47 -0
- package/src/interpreter/geometry/CircleGeometryInterpreter.ts +34 -0
- package/src/interpreter/geometry/CylinderGeometryInterpreter.ts +34 -0
- package/src/interpreter/geometry/ExtrudeGeometryInterpreter.ts +55 -0
- package/src/interpreter/geometry/PlaneGeometryInterpreter.ts +34 -0
- package/src/interpreter/geometry/RingGeometryInterpreter.ts +34 -0
- package/src/interpreter/geometry/SphereGeometryInterpreter.ts +34 -0
- package/src/interpreter/geometry/SurfaceGeometryInterpreter.ts +39 -0
- package/src/interpreter/geometry/TubeGeometryInterpreter.ts +42 -0
- package/src/interpreter/helper/AxesHelperInterpreter.ts +38 -0
- package/src/interpreter/index.ts +45 -0
- package/src/interpreter/light/AmbientLightInterpreter.ts +30 -0
- package/src/interpreter/light/DirectionalLightInterpreter.ts +84 -0
- package/src/interpreter/light/HemisphereLightInterpreter.ts +32 -0
- package/src/interpreter/light/PointLightInterpreter.ts +46 -0
- package/src/interpreter/light/RectAreaLightInterpreter.ts +34 -0
- package/src/interpreter/light/SpotLightInterpreter.ts +68 -0
- package/src/interpreter/material/MaterialInterpreter.ts +34 -0
- package/src/interpreter/material/MeshBasicMaterialInterpreter.ts +43 -0
- package/src/interpreter/material/MeshPhongMaterialInterpreter.ts +63 -0
- package/src/interpreter/material/MeshStandardMaterialInterpreter.ts +58 -0
- package/src/interpreter/material/PointsMaterialInterpreter.ts +36 -0
- package/src/interpreter/material/ShaderMaterialInterpreter.ts +51 -0
- package/src/interpreter/material/ShadowMaterialInterpreter.ts +31 -0
- package/src/interpreter/material/SpriteMaterialInterpreter.ts +36 -0
- package/src/interpreter/texture/TextureInterpreter.ts +59 -0
- package/src/locale/Language.ts +10 -0
- package/src/locale/LanguageProvider.ts +16 -0
- package/src/locale/index.ts +2 -0
- package/src/preset/execute/GeoMap/drilldown.ts +61 -0
- package/src/preset/execute/GeoMap/index.ts +1 -0
- package/src/preset/execute/index.ts +1 -0
- package/src/preset/index.ts +7 -0
- package/src/preset/routine/GeoMap/drilldown.ts +26 -0
- package/src/preset/routine/GeoMap/index.ts +1 -0
- package/src/preset/routine/index.ts +1 -0
- package/src/utils/dispose/index.ts +23 -0
- package/src/utils/geometry/index.ts +82 -0
- package/src/utils/index.ts +7 -0
- package/src/utils/material/index.ts +53 -0
- package/src/utils/pickup/index.ts +16 -0
- package/src/utils/random/index.ts +7 -0
- package/src/utils/text/index.ts +492 -0
- package/src/utils/texture/index.ts +19 -0
|
@@ -0,0 +1,2360 @@
|
|
|
1
|
+
import { Graphics } from '@vyr/engine';
|
|
2
|
+
import { EventDispatcher, Vector3, Vector2, Spherical, Box3, Sphere, Quaternion, Matrix4, Raycaster, PerspectiveCamera, OrthographicCamera, Object3D, Mesh, Vector4 } from 'three'
|
|
3
|
+
|
|
4
|
+
const MOUSE_BUTTON = {
|
|
5
|
+
LEFT: 1,
|
|
6
|
+
RIGHT: 2,
|
|
7
|
+
MIDDLE: 4,
|
|
8
|
+
};
|
|
9
|
+
const ACTION = Object.freeze({
|
|
10
|
+
NONE: 0,
|
|
11
|
+
ROTATE: 1,
|
|
12
|
+
TRUCK: 2,
|
|
13
|
+
OFFSET: 4,
|
|
14
|
+
DOLLY: 8,
|
|
15
|
+
ZOOM: 16,
|
|
16
|
+
TOUCH_ROTATE: 32,
|
|
17
|
+
TOUCH_TRUCK: 64,
|
|
18
|
+
TOUCH_OFFSET: 128,
|
|
19
|
+
TOUCH_DOLLY: 256,
|
|
20
|
+
TOUCH_ZOOM: 512,
|
|
21
|
+
TOUCH_DOLLY_TRUCK: 1024,
|
|
22
|
+
TOUCH_DOLLY_OFFSET: 2048,
|
|
23
|
+
TOUCH_DOLLY_ROTATE: 4096,
|
|
24
|
+
TOUCH_ZOOM_TRUCK: 8192,
|
|
25
|
+
TOUCH_ZOOM_OFFSET: 16384,
|
|
26
|
+
TOUCH_ZOOM_ROTATE: 32768,
|
|
27
|
+
});
|
|
28
|
+
const DOLLY_DIRECTION = {
|
|
29
|
+
NONE: 0,
|
|
30
|
+
IN: 1,
|
|
31
|
+
OUT: -1,
|
|
32
|
+
};
|
|
33
|
+
function isPerspectiveCamera(camera: any): camera is PerspectiveCamera {
|
|
34
|
+
return camera.isPerspectiveCamera;
|
|
35
|
+
}
|
|
36
|
+
function isOrthographicCamera(camera: any): camera is OrthographicCamera {
|
|
37
|
+
return camera.isOrthographicCamera;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const PI_2 = Math.PI * 2;
|
|
41
|
+
const PI_HALF = Math.PI / 2;
|
|
42
|
+
|
|
43
|
+
const EPSILON = 1e-5;
|
|
44
|
+
const DEG2RAD = Math.PI / 180;
|
|
45
|
+
function clamp(value: number, min: number, max: number) {
|
|
46
|
+
return Math.max(min, Math.min(max, value));
|
|
47
|
+
}
|
|
48
|
+
function approxZero(number: number, error = EPSILON) {
|
|
49
|
+
return Math.abs(number) < error;
|
|
50
|
+
}
|
|
51
|
+
function approxEquals(a: number, b: number, error = EPSILON) {
|
|
52
|
+
return approxZero(a - b, error);
|
|
53
|
+
}
|
|
54
|
+
function roundToStep(value: number, step: number) {
|
|
55
|
+
return Math.round(value / step) * step;
|
|
56
|
+
}
|
|
57
|
+
function infinityToMaxNumber(value: number) {
|
|
58
|
+
if (isFinite(value))
|
|
59
|
+
return value;
|
|
60
|
+
if (value < 0)
|
|
61
|
+
return -Number.MAX_VALUE;
|
|
62
|
+
return Number.MAX_VALUE;
|
|
63
|
+
}
|
|
64
|
+
function maxNumberToInfinity(value: number) {
|
|
65
|
+
if (Math.abs(value) < Number.MAX_VALUE)
|
|
66
|
+
return value;
|
|
67
|
+
return value * Infinity;
|
|
68
|
+
}
|
|
69
|
+
// https://docs.unity3d.com/ScriptReference/Mathf.SmoothDamp.html
|
|
70
|
+
// https://github.com/Unity-Technologies/UnityCsReference/blob/a2bdfe9b3c4cd4476f44bf52f848063bfaf7b6b9/Runtime/Export/Math/Mathf.cs#L308
|
|
71
|
+
function smoothDamp(current: number, target: number, currentVelocityRef: { value: number }, smoothTime: number, maxSpeed = Infinity, deltaTime: number) {
|
|
72
|
+
// Based on Game Programming Gems 4 Chapter 1.10
|
|
73
|
+
smoothTime = Math.max(0.0001, smoothTime);
|
|
74
|
+
const omega = 2 / smoothTime;
|
|
75
|
+
const x = omega * deltaTime;
|
|
76
|
+
const exp = 1 / (1 + x + 0.48 * x * x + 0.235 * x * x * x);
|
|
77
|
+
let change = current - target;
|
|
78
|
+
const originalTo = target;
|
|
79
|
+
// Clamp maximum speed
|
|
80
|
+
const maxChange = maxSpeed * smoothTime;
|
|
81
|
+
change = clamp(change, -maxChange, maxChange);
|
|
82
|
+
target = current - change;
|
|
83
|
+
const temp = (currentVelocityRef.value + omega * change) * deltaTime;
|
|
84
|
+
currentVelocityRef.value = (currentVelocityRef.value - omega * temp) * exp;
|
|
85
|
+
let output = target + (change + temp) * exp;
|
|
86
|
+
// Prevent overshooting
|
|
87
|
+
if (originalTo - current > 0.0 === output > originalTo) {
|
|
88
|
+
output = originalTo;
|
|
89
|
+
currentVelocityRef.value = (output - originalTo) / deltaTime;
|
|
90
|
+
}
|
|
91
|
+
return output;
|
|
92
|
+
}
|
|
93
|
+
// https://docs.unity3d.com/ScriptReference/Vector3.SmoothDamp.html
|
|
94
|
+
// https://github.com/Unity-Technologies/UnityCsReference/blob/a2bdfe9b3c4cd4476f44bf52f848063bfaf7b6b9/Runtime/Export/Math/Vector3.cs#L97
|
|
95
|
+
function smoothDampVec3(current: Vector3, target: Vector3, currentVelocityRef: Vector3, smoothTime: number, maxSpeed = Infinity, deltaTime: number, out: Vector3) {
|
|
96
|
+
// Based on Game Programming Gems 4 Chapter 1.10
|
|
97
|
+
smoothTime = Math.max(0.0001, smoothTime);
|
|
98
|
+
const omega = 2 / smoothTime;
|
|
99
|
+
const x = omega * deltaTime;
|
|
100
|
+
const exp = 1 / (1 + x + 0.48 * x * x + 0.235 * x * x * x);
|
|
101
|
+
let targetX = target.x;
|
|
102
|
+
let targetY = target.y;
|
|
103
|
+
let targetZ = target.z;
|
|
104
|
+
let changeX = current.x - targetX;
|
|
105
|
+
let changeY = current.y - targetY;
|
|
106
|
+
let changeZ = current.z - targetZ;
|
|
107
|
+
const originalToX = targetX;
|
|
108
|
+
const originalToY = targetY;
|
|
109
|
+
const originalToZ = targetZ;
|
|
110
|
+
// Clamp maximum speed
|
|
111
|
+
const maxChange = maxSpeed * smoothTime;
|
|
112
|
+
const maxChangeSq = maxChange * maxChange;
|
|
113
|
+
const magnitudeSq = changeX * changeX + changeY * changeY + changeZ * changeZ;
|
|
114
|
+
if (magnitudeSq > maxChangeSq) {
|
|
115
|
+
const magnitude = Math.sqrt(magnitudeSq);
|
|
116
|
+
changeX = changeX / magnitude * maxChange;
|
|
117
|
+
changeY = changeY / magnitude * maxChange;
|
|
118
|
+
changeZ = changeZ / magnitude * maxChange;
|
|
119
|
+
}
|
|
120
|
+
targetX = current.x - changeX;
|
|
121
|
+
targetY = current.y - changeY;
|
|
122
|
+
targetZ = current.z - changeZ;
|
|
123
|
+
const tempX = (currentVelocityRef.x + omega * changeX) * deltaTime;
|
|
124
|
+
const tempY = (currentVelocityRef.y + omega * changeY) * deltaTime;
|
|
125
|
+
const tempZ = (currentVelocityRef.z + omega * changeZ) * deltaTime;
|
|
126
|
+
currentVelocityRef.x = (currentVelocityRef.x - omega * tempX) * exp;
|
|
127
|
+
currentVelocityRef.y = (currentVelocityRef.y - omega * tempY) * exp;
|
|
128
|
+
currentVelocityRef.z = (currentVelocityRef.z - omega * tempZ) * exp;
|
|
129
|
+
out.x = targetX + (changeX + tempX) * exp;
|
|
130
|
+
out.y = targetY + (changeY + tempY) * exp;
|
|
131
|
+
out.z = targetZ + (changeZ + tempZ) * exp;
|
|
132
|
+
// Prevent overshooting
|
|
133
|
+
const origMinusCurrentX = originalToX - current.x;
|
|
134
|
+
const origMinusCurrentY = originalToY - current.y;
|
|
135
|
+
const origMinusCurrentZ = originalToZ - current.z;
|
|
136
|
+
const outMinusOrigX = out.x - originalToX;
|
|
137
|
+
const outMinusOrigY = out.y - originalToY;
|
|
138
|
+
const outMinusOrigZ = out.z - originalToZ;
|
|
139
|
+
if (origMinusCurrentX * outMinusOrigX + origMinusCurrentY * outMinusOrigY + origMinusCurrentZ * outMinusOrigZ > 0) {
|
|
140
|
+
out.x = originalToX;
|
|
141
|
+
out.y = originalToY;
|
|
142
|
+
out.z = originalToZ;
|
|
143
|
+
currentVelocityRef.x = (out.x - originalToX) / deltaTime;
|
|
144
|
+
currentVelocityRef.y = (out.y - originalToY) / deltaTime;
|
|
145
|
+
currentVelocityRef.z = (out.z - originalToZ) / deltaTime;
|
|
146
|
+
}
|
|
147
|
+
return out;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function extractClientCoordFromEvent(pointers: Pointer[], out: Vector2) {
|
|
151
|
+
out.set(0, 0);
|
|
152
|
+
if (pointers.length === 0) return
|
|
153
|
+
pointers.forEach((pointer) => {
|
|
154
|
+
out.x += pointer.clientX;
|
|
155
|
+
out.y += pointer.clientY;
|
|
156
|
+
});
|
|
157
|
+
out.x /= pointers.length;
|
|
158
|
+
out.y /= pointers.length;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function notSupportedInOrthographicCamera(camera: any, message: string): camera is OrthographicCamera {
|
|
162
|
+
if (isOrthographicCamera(camera)) {
|
|
163
|
+
console.warn(`${message} is not supported in OrthographicCamera`);
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const VERSION = '2.7.4'; // will be replaced with `version` in package.json during the build process.
|
|
170
|
+
const TOUCH_DOLLY_FACTOR = 1 / 8;
|
|
171
|
+
const isBrowser = typeof window !== 'undefined';
|
|
172
|
+
const isMac = isBrowser && /Mac/.test(navigator.platform);
|
|
173
|
+
const isPointerEventsNotSupported = !(isBrowser && 'PointerEvent' in window); // macOS Safari 12 does not support PointerEvents API
|
|
174
|
+
let _ORIGIN = Object.freeze(new Vector3(0, 0, 0));
|
|
175
|
+
let _AXIS_Y = Object.freeze(new Vector3(0, 1, 0));
|
|
176
|
+
let _AXIS_Z = Object.freeze(new Vector3(0, 0, 1));
|
|
177
|
+
let _v2 = new Vector2();
|
|
178
|
+
let _v3A = new Vector3();
|
|
179
|
+
let _v3B = new Vector3();
|
|
180
|
+
let _v3C = new Vector3();
|
|
181
|
+
let _cameraDirection = new Vector3();
|
|
182
|
+
let _xColumn = new Vector3();
|
|
183
|
+
let _yColumn = new Vector3();
|
|
184
|
+
let _zColumn = new Vector3();
|
|
185
|
+
let _deltaTarget = new Vector3();
|
|
186
|
+
let _deltaOffset = new Vector3();
|
|
187
|
+
let _sphericalA = new Spherical();
|
|
188
|
+
let _sphericalB = new Spherical();
|
|
189
|
+
let _box3A = new Box3();
|
|
190
|
+
let _box3B = new Box3();
|
|
191
|
+
let _sphere = new Sphere();
|
|
192
|
+
let _quaternionA = new Quaternion();
|
|
193
|
+
let _quaternionB = new Quaternion();
|
|
194
|
+
let _rotationMatrix = new Matrix4();
|
|
195
|
+
let _raycaster = new Raycaster();
|
|
196
|
+
|
|
197
|
+
const mouseButtons = {
|
|
198
|
+
left: ACTION.ROTATE,
|
|
199
|
+
middle: ACTION.DOLLY,
|
|
200
|
+
right: ACTION.TRUCK,
|
|
201
|
+
wheel: ACTION.NONE,
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const touches = {
|
|
205
|
+
one: ACTION.TOUCH_ROTATE,
|
|
206
|
+
two: ACTION.NONE,
|
|
207
|
+
three: ACTION.TOUCH_TRUCK,
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
interface Pointer {
|
|
211
|
+
pointerId: number
|
|
212
|
+
clientX: number
|
|
213
|
+
clientY: number
|
|
214
|
+
deltaX: number,
|
|
215
|
+
deltaY: number,
|
|
216
|
+
mouseButton: number | null
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
class CameraControls extends EventDispatcher<any> {
|
|
220
|
+
static get ACTION() {
|
|
221
|
+
return ACTION;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
private _listeners!: any
|
|
225
|
+
minPolarAngle = 0; // radians
|
|
226
|
+
maxPolarAngle = Math.PI; // radians
|
|
227
|
+
minAzimuthAngle = -Infinity; // radians
|
|
228
|
+
maxAzimuthAngle = Infinity; // radians
|
|
229
|
+
minDistance = Number.EPSILON;
|
|
230
|
+
maxDistance = Infinity;
|
|
231
|
+
infinityDolly = false;
|
|
232
|
+
minZoom = 0.01;
|
|
233
|
+
maxZoom = Infinity;
|
|
234
|
+
smoothTime = 0.25;
|
|
235
|
+
draggingSmoothTime = 0.125;
|
|
236
|
+
maxSpeed = Infinity;
|
|
237
|
+
azimuthRotateSpeed = 1.0;
|
|
238
|
+
polarRotateSpeed = 1.0;
|
|
239
|
+
dollySpeed = 1.0;
|
|
240
|
+
dollyDragInverted = false;
|
|
241
|
+
truckSpeed = 2.0;
|
|
242
|
+
dollyToCursor = false;
|
|
243
|
+
dragToOffset = false;
|
|
244
|
+
verticalDragToForward = false;
|
|
245
|
+
boundaryFriction = 0.0;
|
|
246
|
+
restThreshold = 0.01;
|
|
247
|
+
colliderMeshes = [];
|
|
248
|
+
cancel = () => { }
|
|
249
|
+
_enabled = true
|
|
250
|
+
_state: number = ACTION.NONE;
|
|
251
|
+
_viewport: Vector4 | null = null;
|
|
252
|
+
_changedDolly = 0;
|
|
253
|
+
_changedZoom = 0;
|
|
254
|
+
_hasRested = true;
|
|
255
|
+
_boundaryEnclosesCamera = false;
|
|
256
|
+
_needsUpdate = true;
|
|
257
|
+
_updatedLastTime = false;
|
|
258
|
+
_elementRect = new DOMRect();
|
|
259
|
+
_isDragging = false;
|
|
260
|
+
_dragNeedsUpdate = true;
|
|
261
|
+
_activePointers: Pointer[] = [];
|
|
262
|
+
_lockedPointer: Pointer | null = null;
|
|
263
|
+
_interactiveArea = new DOMRect(0, 0, 1, 1);
|
|
264
|
+
_isUserControllingRotate = false;
|
|
265
|
+
_isUserControllingDolly = false;
|
|
266
|
+
_isUserControllingTruck = false;
|
|
267
|
+
_isUserControllingOffset = false;
|
|
268
|
+
_isUserControllingZoom = false;
|
|
269
|
+
_lastDollyDirection = DOLLY_DIRECTION.NONE;
|
|
270
|
+
_thetaVelocity = { value: 0 };
|
|
271
|
+
_phiVelocity = { value: 0 };
|
|
272
|
+
_radiusVelocity = { value: 0 };
|
|
273
|
+
_targetVelocity = new Vector3();
|
|
274
|
+
_focalOffsetVelocity = new Vector3();
|
|
275
|
+
_zoomVelocity = { value: 0 };
|
|
276
|
+
_truckInternal = (deltaX: number, deltaY: number, dragToOffset: boolean) => {
|
|
277
|
+
let truckX;
|
|
278
|
+
let pedestalY;
|
|
279
|
+
if (isPerspectiveCamera(this._camera)) {
|
|
280
|
+
const offset = _v3A.copy(this._camera.position).sub(this._target);
|
|
281
|
+
// half of the fov is center to top of screen
|
|
282
|
+
const fov = this._camera.getEffectiveFOV() * DEG2RAD;
|
|
283
|
+
const targetDistance = offset.length() * Math.tan(fov * 0.5);
|
|
284
|
+
truckX = (this.truckSpeed * deltaX * targetDistance / this._elementRect.height);
|
|
285
|
+
pedestalY = (this.truckSpeed * deltaY * targetDistance / this._elementRect.height);
|
|
286
|
+
}
|
|
287
|
+
else if (isOrthographicCamera(this._camera)) {
|
|
288
|
+
const camera = this._camera;
|
|
289
|
+
truckX = deltaX * (camera.right - camera.left) / camera.zoom / this._elementRect.width;
|
|
290
|
+
pedestalY = deltaY * (camera.top - camera.bottom) / camera.zoom / this._elementRect.height;
|
|
291
|
+
}
|
|
292
|
+
else {
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
if (this.verticalDragToForward) {
|
|
296
|
+
dragToOffset ?
|
|
297
|
+
this.setFocalOffset(this._focalOffsetEnd.x + truckX, this._focalOffsetEnd.y, this._focalOffsetEnd.z, true) :
|
|
298
|
+
this.truck(truckX, 0, true);
|
|
299
|
+
this.forward(-pedestalY, true);
|
|
300
|
+
}
|
|
301
|
+
else {
|
|
302
|
+
dragToOffset ?
|
|
303
|
+
this.setFocalOffset(this._focalOffsetEnd.x + truckX, this._focalOffsetEnd.y + pedestalY, this._focalOffsetEnd.z, true) :
|
|
304
|
+
this.truck(truckX, pedestalY, true);
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
_rotateInternal = (deltaX: number, deltaY: number) => {
|
|
308
|
+
const theta = PI_2 * this.azimuthRotateSpeed * deltaX / this._elementRect.height; // divide by *height* to refer the resolution
|
|
309
|
+
const phi = PI_2 * this.polarRotateSpeed * deltaY / this._elementRect.height;
|
|
310
|
+
this.rotate(theta, phi, true);
|
|
311
|
+
};
|
|
312
|
+
_dollyInternal = (delta: number, x: number, y: number) => {
|
|
313
|
+
const dollyScale = Math.pow(0.95, -delta * this.dollySpeed);
|
|
314
|
+
const lastDistance = this._sphericalEnd.radius;
|
|
315
|
+
const distance = this._sphericalEnd.radius * dollyScale;
|
|
316
|
+
const clampedDistance = clamp(distance, this.minDistance, this.maxDistance);
|
|
317
|
+
const overflowedDistance = clampedDistance - distance;
|
|
318
|
+
if (this.infinityDolly && this.dollyToCursor) {
|
|
319
|
+
this._dollyToNoClamp(distance, true);
|
|
320
|
+
}
|
|
321
|
+
else if (this.infinityDolly && !this.dollyToCursor) {
|
|
322
|
+
this.dollyInFixed(overflowedDistance, true);
|
|
323
|
+
this._dollyToNoClamp(clampedDistance, true);
|
|
324
|
+
}
|
|
325
|
+
else {
|
|
326
|
+
this._dollyToNoClamp(clampedDistance, true);
|
|
327
|
+
}
|
|
328
|
+
if (this.dollyToCursor) {
|
|
329
|
+
this._changedDolly += (this.infinityDolly ? distance : clampedDistance) - lastDistance;
|
|
330
|
+
this._dollyControlCoord.set(x, y);
|
|
331
|
+
}
|
|
332
|
+
this._lastDollyDirection = Math.sign(-delta);
|
|
333
|
+
};
|
|
334
|
+
_zoomInternal = (delta: number, x: number, y: number) => {
|
|
335
|
+
const zoomScale = Math.pow(0.95, delta * this.dollySpeed);
|
|
336
|
+
const lastZoom = this._zoom;
|
|
337
|
+
const zoom = this._zoom * zoomScale;
|
|
338
|
+
// for both PerspectiveCamera and OrthographicCamera
|
|
339
|
+
this.zoomTo(zoom, true);
|
|
340
|
+
if (this.dollyToCursor) {
|
|
341
|
+
this._changedZoom += zoom - lastZoom;
|
|
342
|
+
this._dollyControlCoord.set(x, y);
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
_nearPlaneCorners = [
|
|
346
|
+
new Vector3(),
|
|
347
|
+
new Vector3(),
|
|
348
|
+
new Vector3(),
|
|
349
|
+
new Vector3(),
|
|
350
|
+
];
|
|
351
|
+
_target = new Vector3();
|
|
352
|
+
_focalOffset = new Vector3();
|
|
353
|
+
_domElement!: HTMLElement
|
|
354
|
+
dragStartPosition = new Vector2();
|
|
355
|
+
lastDragPosition = new Vector2();
|
|
356
|
+
dollyStart = new Vector2();
|
|
357
|
+
_camera!: PerspectiveCamera | OrthographicCamera
|
|
358
|
+
_yAxisUpSpace!: Quaternion
|
|
359
|
+
_yAxisUpSpaceInverse!: Quaternion
|
|
360
|
+
_targetEnd!: Vector3
|
|
361
|
+
_focalOffsetEnd!: Vector3
|
|
362
|
+
_spherical!: Spherical
|
|
363
|
+
_sphericalEnd!: Spherical
|
|
364
|
+
_lastDistance!: number
|
|
365
|
+
_zoom!: number
|
|
366
|
+
_zoomEnd!: number
|
|
367
|
+
_lastZoom!: number
|
|
368
|
+
_boundary!: Box3
|
|
369
|
+
_cameraUp0!: Vector3
|
|
370
|
+
_target0!: Vector3
|
|
371
|
+
_position0!: Vector3
|
|
372
|
+
_zoom0!: number
|
|
373
|
+
_focalOffset0!: Vector3
|
|
374
|
+
_dollyControlCoord = new Vector2()
|
|
375
|
+
mouseButtons: { left: number; middle: number; right: number; wheel: number }
|
|
376
|
+
touches: { one: number; two: number; three: number }
|
|
377
|
+
events: number[] = []
|
|
378
|
+
lockPointer
|
|
379
|
+
unlockPointer
|
|
380
|
+
constructor(graphics: Graphics) {
|
|
381
|
+
super();
|
|
382
|
+
this.mouseButtons = mouseButtons
|
|
383
|
+
this.touches = touches
|
|
384
|
+
const onPointerDown = (event: PointerEvent) => {
|
|
385
|
+
if (!this._enabled || !this._domElement)
|
|
386
|
+
return;
|
|
387
|
+
if (this._interactiveArea.left !== 0 ||
|
|
388
|
+
this._interactiveArea.top !== 0 ||
|
|
389
|
+
this._interactiveArea.width !== 1 ||
|
|
390
|
+
this._interactiveArea.height !== 1) {
|
|
391
|
+
const elRect = this._domElement.getBoundingClientRect();
|
|
392
|
+
const left = event.clientX / elRect.width;
|
|
393
|
+
const top = event.clientY / elRect.height;
|
|
394
|
+
// check if the interactiveArea contains the drag start position.
|
|
395
|
+
if (left < this._interactiveArea.left ||
|
|
396
|
+
left > this._interactiveArea.right ||
|
|
397
|
+
top < this._interactiveArea.top ||
|
|
398
|
+
top > this._interactiveArea.bottom)
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
// Don't call `event.preventDefault()` on the pointerdown event
|
|
402
|
+
// to keep receiving pointermove evens outside dragging iframe
|
|
403
|
+
// https://taye.me/blog/tips/2015/11/16/mouse-drag-outside-iframe/
|
|
404
|
+
const mouseButton = event.pointerType !== 'mouse' ? null :
|
|
405
|
+
(event.buttons & MOUSE_BUTTON.LEFT) === MOUSE_BUTTON.LEFT ? MOUSE_BUTTON.LEFT :
|
|
406
|
+
(event.buttons & MOUSE_BUTTON.MIDDLE) === MOUSE_BUTTON.MIDDLE ? MOUSE_BUTTON.MIDDLE :
|
|
407
|
+
(event.buttons & MOUSE_BUTTON.RIGHT) === MOUSE_BUTTON.RIGHT ? MOUSE_BUTTON.RIGHT :
|
|
408
|
+
null;
|
|
409
|
+
if (mouseButton !== null) {
|
|
410
|
+
const zombiePointer = this._findPointerByMouseButton(mouseButton);
|
|
411
|
+
zombiePointer && this._disposePointer(zombiePointer);
|
|
412
|
+
}
|
|
413
|
+
if ((event.buttons & MOUSE_BUTTON.LEFT) === MOUSE_BUTTON.LEFT && this._lockedPointer)
|
|
414
|
+
return;
|
|
415
|
+
const pointer: Pointer = {
|
|
416
|
+
pointerId: event.pointerId,
|
|
417
|
+
clientX: event.clientX,
|
|
418
|
+
clientY: event.clientY,
|
|
419
|
+
deltaX: 0,
|
|
420
|
+
deltaY: 0,
|
|
421
|
+
mouseButton,
|
|
422
|
+
};
|
|
423
|
+
this._activePointers.push(pointer);
|
|
424
|
+
this._domElement.ownerDocument.removeEventListener('pointermove', onPointerMove, { passive: false } as any);
|
|
425
|
+
this._domElement.ownerDocument.removeEventListener('pointerup', onPointerUp);
|
|
426
|
+
this._domElement.ownerDocument.addEventListener('pointermove', onPointerMove, { passive: false });
|
|
427
|
+
this._domElement.ownerDocument.addEventListener('pointerup', onPointerUp);
|
|
428
|
+
this._isDragging = true;
|
|
429
|
+
startDragging(event);
|
|
430
|
+
};
|
|
431
|
+
const onMouseDown = (event: MouseEvent) => {
|
|
432
|
+
if (!this._enabled || !this._domElement || this._lockedPointer)
|
|
433
|
+
return;
|
|
434
|
+
if (this._interactiveArea.left !== 0 ||
|
|
435
|
+
this._interactiveArea.top !== 0 ||
|
|
436
|
+
this._interactiveArea.width !== 1 ||
|
|
437
|
+
this._interactiveArea.height !== 1) {
|
|
438
|
+
const elRect = this._domElement.getBoundingClientRect();
|
|
439
|
+
const left = event.clientX / elRect.width;
|
|
440
|
+
const top = event.clientY / elRect.height;
|
|
441
|
+
// check if the interactiveArea contains the drag start position.
|
|
442
|
+
if (left < this._interactiveArea.left ||
|
|
443
|
+
left > this._interactiveArea.right ||
|
|
444
|
+
top < this._interactiveArea.top ||
|
|
445
|
+
top > this._interactiveArea.bottom)
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
const mouseButton = (event.buttons & MOUSE_BUTTON.LEFT) === MOUSE_BUTTON.LEFT ? MOUSE_BUTTON.LEFT :
|
|
449
|
+
(event.buttons & MOUSE_BUTTON.MIDDLE) === MOUSE_BUTTON.MIDDLE ? MOUSE_BUTTON.MIDDLE :
|
|
450
|
+
(event.buttons & MOUSE_BUTTON.RIGHT) === MOUSE_BUTTON.RIGHT ? MOUSE_BUTTON.RIGHT :
|
|
451
|
+
null;
|
|
452
|
+
if (mouseButton !== null) {
|
|
453
|
+
const zombiePointer = this._findPointerByMouseButton(mouseButton);
|
|
454
|
+
zombiePointer && this._disposePointer(zombiePointer);
|
|
455
|
+
}
|
|
456
|
+
const pointer = {
|
|
457
|
+
pointerId: 1,
|
|
458
|
+
clientX: event.clientX,
|
|
459
|
+
clientY: event.clientY,
|
|
460
|
+
deltaX: 0,
|
|
461
|
+
deltaY: 0,
|
|
462
|
+
mouseButton: (event.buttons & MOUSE_BUTTON.LEFT) === MOUSE_BUTTON.LEFT ? MOUSE_BUTTON.LEFT :
|
|
463
|
+
(event.buttons & MOUSE_BUTTON.MIDDLE) === MOUSE_BUTTON.LEFT ? MOUSE_BUTTON.MIDDLE :
|
|
464
|
+
(event.buttons & MOUSE_BUTTON.RIGHT) === MOUSE_BUTTON.LEFT ? MOUSE_BUTTON.RIGHT :
|
|
465
|
+
null,
|
|
466
|
+
};
|
|
467
|
+
this._activePointers.push(pointer);
|
|
468
|
+
// see https://github.com/microsoft/TypeScript/issues/32912#issuecomment-522142969
|
|
469
|
+
// eslint-disable-next-line no-undef
|
|
470
|
+
this._domElement.ownerDocument.removeEventListener('mousemove', onMouseMove);
|
|
471
|
+
this._domElement.ownerDocument.removeEventListener('mouseup', onMouseUp);
|
|
472
|
+
this._domElement.ownerDocument.addEventListener('mousemove', onMouseMove);
|
|
473
|
+
this._domElement.ownerDocument.addEventListener('mouseup', onMouseUp);
|
|
474
|
+
this._isDragging = true;
|
|
475
|
+
startDragging(event);
|
|
476
|
+
};
|
|
477
|
+
const onPointerMove = (event: PointerEvent) => {
|
|
478
|
+
if (event.cancelable)
|
|
479
|
+
event.preventDefault();
|
|
480
|
+
const pointerId = event.pointerId;
|
|
481
|
+
const pointer = this._lockedPointer || this._findPointerById(pointerId);
|
|
482
|
+
if (!pointer)
|
|
483
|
+
return;
|
|
484
|
+
pointer.clientX = event.clientX;
|
|
485
|
+
pointer.clientY = event.clientY;
|
|
486
|
+
pointer.deltaX = event.movementX;
|
|
487
|
+
pointer.deltaY = event.movementY;
|
|
488
|
+
this._state = 0;
|
|
489
|
+
if (event.pointerType === 'touch') {
|
|
490
|
+
switch (this._activePointers.length) {
|
|
491
|
+
case 1:
|
|
492
|
+
this._state = this.touches.one;
|
|
493
|
+
break;
|
|
494
|
+
case 2:
|
|
495
|
+
this._state = this.touches.two;
|
|
496
|
+
break;
|
|
497
|
+
case 3:
|
|
498
|
+
this._state = this.touches.three;
|
|
499
|
+
break;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
else {
|
|
503
|
+
if ((!this._isDragging && this._lockedPointer) ||
|
|
504
|
+
this._isDragging && (event.buttons & MOUSE_BUTTON.LEFT) === MOUSE_BUTTON.LEFT) {
|
|
505
|
+
this._state = this._state | this.mouseButtons.left;
|
|
506
|
+
}
|
|
507
|
+
if (this._isDragging && (event.buttons & MOUSE_BUTTON.MIDDLE) === MOUSE_BUTTON.MIDDLE) {
|
|
508
|
+
this._state = this._state | this.mouseButtons.middle;
|
|
509
|
+
}
|
|
510
|
+
if (this._isDragging && (event.buttons & MOUSE_BUTTON.RIGHT) === MOUSE_BUTTON.RIGHT) {
|
|
511
|
+
this._state = this._state | this.mouseButtons.right;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
dragging();
|
|
515
|
+
};
|
|
516
|
+
const onMouseMove = (event: MouseEvent) => {
|
|
517
|
+
const pointer = this._lockedPointer || this._findPointerById(1);
|
|
518
|
+
if (!pointer)
|
|
519
|
+
return;
|
|
520
|
+
pointer.clientX = event.clientX;
|
|
521
|
+
pointer.clientY = event.clientY;
|
|
522
|
+
pointer.deltaX = event.movementX;
|
|
523
|
+
pointer.deltaY = event.movementY;
|
|
524
|
+
this._state = 0;
|
|
525
|
+
if (this._lockedPointer ||
|
|
526
|
+
(event.buttons & MOUSE_BUTTON.LEFT) === MOUSE_BUTTON.LEFT) {
|
|
527
|
+
this._state = this._state | this.mouseButtons.left;
|
|
528
|
+
}
|
|
529
|
+
if ((event.buttons & MOUSE_BUTTON.MIDDLE) === MOUSE_BUTTON.MIDDLE) {
|
|
530
|
+
this._state = this._state | this.mouseButtons.middle;
|
|
531
|
+
}
|
|
532
|
+
if ((event.buttons & MOUSE_BUTTON.RIGHT) === MOUSE_BUTTON.RIGHT) {
|
|
533
|
+
this._state = this._state | this.mouseButtons.right;
|
|
534
|
+
}
|
|
535
|
+
dragging();
|
|
536
|
+
};
|
|
537
|
+
const onPointerUp = (event: PointerEvent) => {
|
|
538
|
+
const pointer = this._findPointerById(event.pointerId);
|
|
539
|
+
if (pointer && pointer === this._lockedPointer)
|
|
540
|
+
return;
|
|
541
|
+
pointer && this._disposePointer(pointer);
|
|
542
|
+
if (event.pointerType === 'touch') {
|
|
543
|
+
switch (this._activePointers.length) {
|
|
544
|
+
case 0:
|
|
545
|
+
this._state = ACTION.NONE;
|
|
546
|
+
break;
|
|
547
|
+
case 1:
|
|
548
|
+
this._state = this.touches.one;
|
|
549
|
+
break;
|
|
550
|
+
case 2:
|
|
551
|
+
this._state = this.touches.two;
|
|
552
|
+
break;
|
|
553
|
+
case 3:
|
|
554
|
+
this._state = this.touches.three;
|
|
555
|
+
break;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
else {
|
|
559
|
+
this._state = ACTION.NONE;
|
|
560
|
+
}
|
|
561
|
+
endDragging();
|
|
562
|
+
};
|
|
563
|
+
const onMouseUp = () => {
|
|
564
|
+
const pointer = this._findPointerById(1);
|
|
565
|
+
if (pointer && pointer === this._lockedPointer)
|
|
566
|
+
return;
|
|
567
|
+
pointer && this._disposePointer(pointer);
|
|
568
|
+
this._state = ACTION.NONE;
|
|
569
|
+
endDragging();
|
|
570
|
+
};
|
|
571
|
+
let lastScrollTimeStamp = -1;
|
|
572
|
+
const onMouseWheel = (event: WheelEvent) => {
|
|
573
|
+
if (!this._domElement)
|
|
574
|
+
return;
|
|
575
|
+
if (!this._enabled || this.mouseButtons.wheel === ACTION.NONE)
|
|
576
|
+
return;
|
|
577
|
+
if (this._interactiveArea.left !== 0 ||
|
|
578
|
+
this._interactiveArea.top !== 0 ||
|
|
579
|
+
this._interactiveArea.width !== 1 ||
|
|
580
|
+
this._interactiveArea.height !== 1) {
|
|
581
|
+
const elRect = this._domElement.getBoundingClientRect();
|
|
582
|
+
const left = event.clientX / elRect.width;
|
|
583
|
+
const top = event.clientY / elRect.height;
|
|
584
|
+
// check if the interactiveArea contains the drag start position.
|
|
585
|
+
if (left < this._interactiveArea.left ||
|
|
586
|
+
left > this._interactiveArea.right ||
|
|
587
|
+
top < this._interactiveArea.top ||
|
|
588
|
+
top > this._interactiveArea.bottom)
|
|
589
|
+
return;
|
|
590
|
+
}
|
|
591
|
+
event.preventDefault();
|
|
592
|
+
|
|
593
|
+
if (this.dollyToCursor ||
|
|
594
|
+
this.mouseButtons.wheel === ACTION.ROTATE ||
|
|
595
|
+
this.mouseButtons.wheel === ACTION.TRUCK) {
|
|
596
|
+
const now = performance.now();
|
|
597
|
+
// only need to fire this at scroll start.
|
|
598
|
+
if (lastScrollTimeStamp - now < 1000)
|
|
599
|
+
this._getClientRect(this._elementRect);
|
|
600
|
+
lastScrollTimeStamp = now;
|
|
601
|
+
}
|
|
602
|
+
// Ref: https://github.com/cedricpinson/osgjs/blob/00e5a7e9d9206c06fdde0436e1d62ab7cb5ce853/sources/osgViewer/input/source/InputSourceMouse.js#L89-L103
|
|
603
|
+
const deltaYFactor = isMac ? -1 : -3;
|
|
604
|
+
const delta = (event.deltaMode === 1) ? event.deltaY / deltaYFactor : event.deltaY / (deltaYFactor * 10);
|
|
605
|
+
const x = this.dollyToCursor ? (event.clientX - this._elementRect.x) / this._elementRect.width * 2 - 1 : 0;
|
|
606
|
+
const y = this.dollyToCursor ? (event.clientY - this._elementRect.y) / this._elementRect.height * -2 + 1 : 0;
|
|
607
|
+
|
|
608
|
+
switch (this.mouseButtons.wheel) {
|
|
609
|
+
case ACTION.ROTATE: {
|
|
610
|
+
this._rotateInternal(event.deltaX, event.deltaY);
|
|
611
|
+
this._isUserControllingRotate = true;
|
|
612
|
+
break;
|
|
613
|
+
}
|
|
614
|
+
case ACTION.TRUCK: {
|
|
615
|
+
this._truckInternal(event.deltaX, event.deltaY, false);
|
|
616
|
+
this._isUserControllingTruck = true;
|
|
617
|
+
break;
|
|
618
|
+
}
|
|
619
|
+
case ACTION.OFFSET: {
|
|
620
|
+
this._truckInternal(event.deltaX, event.deltaY, true);
|
|
621
|
+
this._isUserControllingOffset = true;
|
|
622
|
+
break;
|
|
623
|
+
}
|
|
624
|
+
case ACTION.DOLLY: {
|
|
625
|
+
this._dollyInternal(-delta, x, y);
|
|
626
|
+
this._isUserControllingDolly = true;
|
|
627
|
+
break;
|
|
628
|
+
}
|
|
629
|
+
case ACTION.ZOOM: {
|
|
630
|
+
this._zoomInternal(-delta, x, y);
|
|
631
|
+
this._isUserControllingZoom = true;
|
|
632
|
+
break;
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
this.dispatchEvent({ type: 'control' });
|
|
636
|
+
};
|
|
637
|
+
const onContextMenu = (event: MouseEvent) => {
|
|
638
|
+
if (!this._domElement || !this._enabled)
|
|
639
|
+
return;
|
|
640
|
+
// contextmenu event is fired right after pointerdown/mousedown.
|
|
641
|
+
// remove attached handlers and active pointer, if interrupted by contextmenu.
|
|
642
|
+
if (this.mouseButtons.right === CameraControls.ACTION.NONE) {
|
|
643
|
+
const pointerId = event instanceof PointerEvent ? event.pointerId :
|
|
644
|
+
event instanceof MouseEvent ? 0 :
|
|
645
|
+
0;
|
|
646
|
+
const pointer = this._findPointerById(pointerId);
|
|
647
|
+
pointer && this._disposePointer(pointer);
|
|
648
|
+
this._domElement.ownerDocument.removeEventListener('pointermove', onPointerMove, { passive: false } as any);
|
|
649
|
+
this._domElement.ownerDocument.removeEventListener('pointerup', onPointerUp);
|
|
650
|
+
this._domElement.ownerDocument.removeEventListener('mousemove', onMouseMove);
|
|
651
|
+
this._domElement.ownerDocument.removeEventListener('mouseup', onMouseUp);
|
|
652
|
+
return;
|
|
653
|
+
}
|
|
654
|
+
event.preventDefault();
|
|
655
|
+
};
|
|
656
|
+
const startDragging = (event?: MouseEvent) => {
|
|
657
|
+
if (!this._enabled)
|
|
658
|
+
return;
|
|
659
|
+
extractClientCoordFromEvent(this._activePointers, _v2);
|
|
660
|
+
this._getClientRect(this._elementRect);
|
|
661
|
+
this.dragStartPosition.copy(_v2);
|
|
662
|
+
this.lastDragPosition.copy(_v2);
|
|
663
|
+
const isMultiTouch = this._activePointers.length >= 2;
|
|
664
|
+
if (isMultiTouch) {
|
|
665
|
+
// 2 finger pinch
|
|
666
|
+
const dx = _v2.x - this._activePointers[1].clientX;
|
|
667
|
+
const dy = _v2.y - this._activePointers[1].clientY;
|
|
668
|
+
const distance = Math.sqrt(dx * dx + dy * dy);
|
|
669
|
+
this.dollyStart.set(0, distance);
|
|
670
|
+
// center coords of 2 finger truck
|
|
671
|
+
const x = (this._activePointers[0].clientX + this._activePointers[1].clientX) * 0.5;
|
|
672
|
+
const y = (this._activePointers[0].clientY + this._activePointers[1].clientY) * 0.5;
|
|
673
|
+
this.lastDragPosition.set(x, y);
|
|
674
|
+
}
|
|
675
|
+
this._state = 0;
|
|
676
|
+
if (!event) {
|
|
677
|
+
if (this._lockedPointer)
|
|
678
|
+
this._state = this._state | this.mouseButtons.left;
|
|
679
|
+
}
|
|
680
|
+
else if ('pointerType' in event && event.pointerType === 'touch') {
|
|
681
|
+
switch (this._activePointers.length) {
|
|
682
|
+
case 1:
|
|
683
|
+
this._state = this.touches.one;
|
|
684
|
+
break;
|
|
685
|
+
case 2:
|
|
686
|
+
this._state = this.touches.two;
|
|
687
|
+
break;
|
|
688
|
+
case 3:
|
|
689
|
+
this._state = this.touches.three;
|
|
690
|
+
break;
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
else {
|
|
694
|
+
if (!this._lockedPointer && (event.buttons & MOUSE_BUTTON.LEFT) === MOUSE_BUTTON.LEFT) {
|
|
695
|
+
this._state = this._state | this.mouseButtons.left;
|
|
696
|
+
}
|
|
697
|
+
if ((event.buttons & MOUSE_BUTTON.MIDDLE) === MOUSE_BUTTON.MIDDLE) {
|
|
698
|
+
this._state = this._state | this.mouseButtons.middle;
|
|
699
|
+
}
|
|
700
|
+
if ((event.buttons & MOUSE_BUTTON.RIGHT) === MOUSE_BUTTON.RIGHT) {
|
|
701
|
+
this._state = this._state | this.mouseButtons.right;
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
// stop current movement on drag start
|
|
705
|
+
if ((this._state & ACTION.ROTATE) === ACTION.ROTATE ||
|
|
706
|
+
(this._state & ACTION.TOUCH_ROTATE) === ACTION.TOUCH_ROTATE ||
|
|
707
|
+
(this._state & ACTION.TOUCH_DOLLY_ROTATE) === ACTION.TOUCH_DOLLY_ROTATE ||
|
|
708
|
+
(this._state & ACTION.TOUCH_ZOOM_ROTATE) === ACTION.TOUCH_ZOOM_ROTATE) {
|
|
709
|
+
this._sphericalEnd.theta = this._spherical.theta;
|
|
710
|
+
this._sphericalEnd.phi = this._spherical.phi;
|
|
711
|
+
this._thetaVelocity.value = 0;
|
|
712
|
+
this._phiVelocity.value = 0;
|
|
713
|
+
}
|
|
714
|
+
if ((this._state & ACTION.TRUCK) === ACTION.TRUCK ||
|
|
715
|
+
(this._state & ACTION.TOUCH_TRUCK) === ACTION.TOUCH_TRUCK ||
|
|
716
|
+
(this._state & ACTION.TOUCH_DOLLY_TRUCK) === ACTION.TOUCH_DOLLY_TRUCK ||
|
|
717
|
+
(this._state & ACTION.TOUCH_ZOOM_TRUCK) === ACTION.TOUCH_ZOOM_TRUCK) {
|
|
718
|
+
this._targetEnd.copy(this._target);
|
|
719
|
+
this._targetVelocity.set(0, 0, 0);
|
|
720
|
+
}
|
|
721
|
+
if ((this._state & ACTION.DOLLY) === ACTION.DOLLY ||
|
|
722
|
+
(this._state & ACTION.TOUCH_DOLLY) === ACTION.TOUCH_DOLLY ||
|
|
723
|
+
(this._state & ACTION.TOUCH_DOLLY_TRUCK) === ACTION.TOUCH_DOLLY_TRUCK ||
|
|
724
|
+
(this._state & ACTION.TOUCH_DOLLY_OFFSET) === ACTION.TOUCH_DOLLY_OFFSET ||
|
|
725
|
+
(this._state & ACTION.TOUCH_DOLLY_ROTATE) === ACTION.TOUCH_DOLLY_ROTATE) {
|
|
726
|
+
this._sphericalEnd.radius = this._spherical.radius;
|
|
727
|
+
this._radiusVelocity.value = 0;
|
|
728
|
+
}
|
|
729
|
+
if ((this._state & ACTION.ZOOM) === ACTION.ZOOM ||
|
|
730
|
+
(this._state & ACTION.TOUCH_ZOOM) === ACTION.TOUCH_ZOOM ||
|
|
731
|
+
(this._state & ACTION.TOUCH_ZOOM_TRUCK) === ACTION.TOUCH_ZOOM_TRUCK ||
|
|
732
|
+
(this._state & ACTION.TOUCH_ZOOM_OFFSET) === ACTION.TOUCH_ZOOM_OFFSET ||
|
|
733
|
+
(this._state & ACTION.TOUCH_ZOOM_ROTATE) === ACTION.TOUCH_ZOOM_ROTATE) {
|
|
734
|
+
this._zoomEnd = this._zoom;
|
|
735
|
+
this._zoomVelocity.value = 0;
|
|
736
|
+
}
|
|
737
|
+
if ((this._state & ACTION.OFFSET) === ACTION.OFFSET ||
|
|
738
|
+
(this._state & ACTION.TOUCH_OFFSET) === ACTION.TOUCH_OFFSET ||
|
|
739
|
+
(this._state & ACTION.TOUCH_DOLLY_OFFSET) === ACTION.TOUCH_DOLLY_OFFSET ||
|
|
740
|
+
(this._state & ACTION.TOUCH_ZOOM_OFFSET) === ACTION.TOUCH_ZOOM_OFFSET) {
|
|
741
|
+
this._focalOffsetEnd.copy(this._focalOffset);
|
|
742
|
+
this._focalOffsetVelocity.set(0, 0, 0);
|
|
743
|
+
}
|
|
744
|
+
this.dispatchEvent({ type: 'controlstart' });
|
|
745
|
+
};
|
|
746
|
+
const dragging = () => {
|
|
747
|
+
if (!this._enabled || !this._dragNeedsUpdate)
|
|
748
|
+
return;
|
|
749
|
+
this._dragNeedsUpdate = false;
|
|
750
|
+
extractClientCoordFromEvent(this._activePointers, _v2);
|
|
751
|
+
// When pointer lock is enabled clientX, clientY, screenX, and screenY remain 0.
|
|
752
|
+
// If pointer lock is enabled, use the Delta directory, and assume active-pointer is not multiple.
|
|
753
|
+
const isPointerLockActive = this._domElement && document.pointerLockElement === this._domElement;
|
|
754
|
+
const lockedPointer = isPointerLockActive ? this._lockedPointer || this._activePointers[0] : null;
|
|
755
|
+
const deltaX = lockedPointer ? -lockedPointer.deltaX : this.lastDragPosition.x - _v2.x;
|
|
756
|
+
const deltaY = lockedPointer ? -lockedPointer.deltaY : this.lastDragPosition.y - _v2.y;
|
|
757
|
+
this.lastDragPosition.copy(_v2);
|
|
758
|
+
if ((this._state & ACTION.ROTATE) === ACTION.ROTATE ||
|
|
759
|
+
(this._state & ACTION.TOUCH_ROTATE) === ACTION.TOUCH_ROTATE ||
|
|
760
|
+
(this._state & ACTION.TOUCH_DOLLY_ROTATE) === ACTION.TOUCH_DOLLY_ROTATE ||
|
|
761
|
+
(this._state & ACTION.TOUCH_ZOOM_ROTATE) === ACTION.TOUCH_ZOOM_ROTATE) {
|
|
762
|
+
this._rotateInternal(deltaX, deltaY);
|
|
763
|
+
this._isUserControllingRotate = true;
|
|
764
|
+
}
|
|
765
|
+
if ((this._state & ACTION.DOLLY) === ACTION.DOLLY ||
|
|
766
|
+
(this._state & ACTION.ZOOM) === ACTION.ZOOM) {
|
|
767
|
+
const dollyX = this.dollyToCursor ? (this.dragStartPosition.x - this._elementRect.x) / this._elementRect.width * 2 - 1 : 0;
|
|
768
|
+
const dollyY = this.dollyToCursor ? (this.dragStartPosition.y - this._elementRect.y) / this._elementRect.height * -2 + 1 : 0;
|
|
769
|
+
const dollyDirection = this.dollyDragInverted ? -1 : 1;
|
|
770
|
+
if ((this._state & ACTION.DOLLY) === ACTION.DOLLY) {
|
|
771
|
+
this._dollyInternal(dollyDirection * deltaY * TOUCH_DOLLY_FACTOR, dollyX, dollyY);
|
|
772
|
+
this._isUserControllingDolly = true;
|
|
773
|
+
}
|
|
774
|
+
else {
|
|
775
|
+
this._zoomInternal(dollyDirection * deltaY * TOUCH_DOLLY_FACTOR, dollyX, dollyY);
|
|
776
|
+
this._isUserControllingZoom = true;
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
if ((this._state & ACTION.TOUCH_DOLLY) === ACTION.TOUCH_DOLLY ||
|
|
780
|
+
(this._state & ACTION.TOUCH_ZOOM) === ACTION.TOUCH_ZOOM ||
|
|
781
|
+
(this._state & ACTION.TOUCH_DOLLY_TRUCK) === ACTION.TOUCH_DOLLY_TRUCK ||
|
|
782
|
+
(this._state & ACTION.TOUCH_ZOOM_TRUCK) === ACTION.TOUCH_ZOOM_TRUCK ||
|
|
783
|
+
(this._state & ACTION.TOUCH_DOLLY_OFFSET) === ACTION.TOUCH_DOLLY_OFFSET ||
|
|
784
|
+
(this._state & ACTION.TOUCH_ZOOM_OFFSET) === ACTION.TOUCH_ZOOM_OFFSET ||
|
|
785
|
+
(this._state & ACTION.TOUCH_DOLLY_ROTATE) === ACTION.TOUCH_DOLLY_ROTATE ||
|
|
786
|
+
(this._state & ACTION.TOUCH_ZOOM_ROTATE) === ACTION.TOUCH_ZOOM_ROTATE) {
|
|
787
|
+
const dx = _v2.x - this._activePointers[1].clientX;
|
|
788
|
+
const dy = _v2.y - this._activePointers[1].clientY;
|
|
789
|
+
const distance = Math.sqrt(dx * dx + dy * dy);
|
|
790
|
+
const dollyDelta = this.dollyStart.y - distance;
|
|
791
|
+
this.dollyStart.set(0, distance);
|
|
792
|
+
const dollyX = this.dollyToCursor ? (this.lastDragPosition.x - this._elementRect.x) / this._elementRect.width * 2 - 1 : 0;
|
|
793
|
+
const dollyY = this.dollyToCursor ? (this.lastDragPosition.y - this._elementRect.y) / this._elementRect.height * -2 + 1 : 0;
|
|
794
|
+
if ((this._state & ACTION.TOUCH_DOLLY) === ACTION.TOUCH_DOLLY ||
|
|
795
|
+
(this._state & ACTION.TOUCH_DOLLY_ROTATE) === ACTION.TOUCH_DOLLY_ROTATE ||
|
|
796
|
+
(this._state & ACTION.TOUCH_DOLLY_TRUCK) === ACTION.TOUCH_DOLLY_TRUCK ||
|
|
797
|
+
(this._state & ACTION.TOUCH_DOLLY_OFFSET) === ACTION.TOUCH_DOLLY_OFFSET) {
|
|
798
|
+
this._dollyInternal(dollyDelta * TOUCH_DOLLY_FACTOR, dollyX, dollyY);
|
|
799
|
+
this._isUserControllingDolly = true;
|
|
800
|
+
}
|
|
801
|
+
else {
|
|
802
|
+
this._zoomInternal(dollyDelta * TOUCH_DOLLY_FACTOR, dollyX, dollyY);
|
|
803
|
+
this._isUserControllingZoom = true;
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
if ((this._state & ACTION.TRUCK) === ACTION.TRUCK ||
|
|
807
|
+
(this._state & ACTION.TOUCH_TRUCK) === ACTION.TOUCH_TRUCK ||
|
|
808
|
+
(this._state & ACTION.TOUCH_DOLLY_TRUCK) === ACTION.TOUCH_DOLLY_TRUCK ||
|
|
809
|
+
(this._state & ACTION.TOUCH_ZOOM_TRUCK) === ACTION.TOUCH_ZOOM_TRUCK) {
|
|
810
|
+
this._truckInternal(deltaX, deltaY, false);
|
|
811
|
+
this._isUserControllingTruck = true;
|
|
812
|
+
}
|
|
813
|
+
if ((this._state & ACTION.OFFSET) === ACTION.OFFSET ||
|
|
814
|
+
(this._state & ACTION.TOUCH_OFFSET) === ACTION.TOUCH_OFFSET ||
|
|
815
|
+
(this._state & ACTION.TOUCH_DOLLY_OFFSET) === ACTION.TOUCH_DOLLY_OFFSET ||
|
|
816
|
+
(this._state & ACTION.TOUCH_ZOOM_OFFSET) === ACTION.TOUCH_ZOOM_OFFSET) {
|
|
817
|
+
this._truckInternal(deltaX, deltaY, true);
|
|
818
|
+
this._isUserControllingOffset = true;
|
|
819
|
+
}
|
|
820
|
+
this.dispatchEvent({ type: 'control' });
|
|
821
|
+
};
|
|
822
|
+
const endDragging = () => {
|
|
823
|
+
extractClientCoordFromEvent(this._activePointers, _v2);
|
|
824
|
+
this.lastDragPosition.copy(_v2);
|
|
825
|
+
this._dragNeedsUpdate = false;
|
|
826
|
+
if (this._activePointers.length === 0 ||
|
|
827
|
+
(this._activePointers.length === 1 && this._activePointers[0] === this._lockedPointer)) {
|
|
828
|
+
this._isDragging = false;
|
|
829
|
+
}
|
|
830
|
+
if (this._activePointers.length === 0 && this._domElement) {
|
|
831
|
+
// eslint-disable-next-line no-undef
|
|
832
|
+
this._domElement.ownerDocument.removeEventListener('pointermove', onPointerMove, { passive: false } as any);
|
|
833
|
+
this._domElement.ownerDocument.removeEventListener('mousemove', onMouseMove);
|
|
834
|
+
this._domElement.ownerDocument.removeEventListener('pointerup', onPointerUp);
|
|
835
|
+
this._domElement.ownerDocument.removeEventListener('mouseup', onMouseUp);
|
|
836
|
+
this.dispatchEvent({ type: 'controlend' });
|
|
837
|
+
}
|
|
838
|
+
};
|
|
839
|
+
this.lockPointer = () => {
|
|
840
|
+
if (!this._enabled || !this._domElement)
|
|
841
|
+
return;
|
|
842
|
+
this.cancel();
|
|
843
|
+
// Element.requestPointerLock is allowed to happen without any pointer active - create a faux one for compatibility with controls
|
|
844
|
+
this._lockedPointer = {
|
|
845
|
+
pointerId: -1,
|
|
846
|
+
clientX: 0,
|
|
847
|
+
clientY: 0,
|
|
848
|
+
deltaX: 0,
|
|
849
|
+
deltaY: 0,
|
|
850
|
+
mouseButton: null,
|
|
851
|
+
};
|
|
852
|
+
this._activePointers.push(this._lockedPointer);
|
|
853
|
+
// eslint-disable-next-line no-undef
|
|
854
|
+
this._domElement.ownerDocument.removeEventListener('pointermove', onPointerMove, { passive: false } as any);
|
|
855
|
+
this._domElement.ownerDocument.removeEventListener('pointerup', onPointerUp);
|
|
856
|
+
this._domElement.requestPointerLock();
|
|
857
|
+
this._domElement.ownerDocument.addEventListener('pointerlockchange', onPointerLockChange);
|
|
858
|
+
this._domElement.ownerDocument.addEventListener('pointerlockerror', onPointerLockError);
|
|
859
|
+
this._domElement.ownerDocument.addEventListener('pointermove', onPointerMove, { passive: false });
|
|
860
|
+
this._domElement.ownerDocument.addEventListener('pointerup', onPointerUp);
|
|
861
|
+
startDragging();
|
|
862
|
+
};
|
|
863
|
+
this.unlockPointer = () => {
|
|
864
|
+
if (this._lockedPointer !== null) {
|
|
865
|
+
this._disposePointer(this._lockedPointer);
|
|
866
|
+
this._lockedPointer = null;
|
|
867
|
+
}
|
|
868
|
+
document.exitPointerLock();
|
|
869
|
+
this.cancel();
|
|
870
|
+
if (!this._domElement)
|
|
871
|
+
return;
|
|
872
|
+
this._domElement.ownerDocument.removeEventListener('pointerlockchange', onPointerLockChange);
|
|
873
|
+
this._domElement.ownerDocument.removeEventListener('pointerlockerror', onPointerLockError);
|
|
874
|
+
};
|
|
875
|
+
const onPointerLockChange = () => {
|
|
876
|
+
const isPointerLockActive = this._domElement && this._domElement.ownerDocument.pointerLockElement === this._domElement;
|
|
877
|
+
if (!isPointerLockActive)
|
|
878
|
+
this.unlockPointer();
|
|
879
|
+
};
|
|
880
|
+
const onPointerLockError = () => {
|
|
881
|
+
this.unlockPointer();
|
|
882
|
+
};
|
|
883
|
+
this._addAllEventListeners = (domElement) => {
|
|
884
|
+
this._domElement = domElement;
|
|
885
|
+
this._domElement.style.touchAction = 'none';
|
|
886
|
+
this._domElement.style.userSelect = 'none';
|
|
887
|
+
this._domElement.style.webkitUserSelect = 'none';
|
|
888
|
+
this.events.push(graphics.engine.inputSystem.listen('pointerdown', onPointerDown, { target: domElement }))
|
|
889
|
+
isPointerEventsNotSupported && this.events.push(graphics.engine.inputSystem.listen('mousedown', onMouseDown, { target: domElement }));
|
|
890
|
+
this.events.push(graphics.engine.inputSystem.listen('pointercancel', onPointerUp, { target: domElement }));
|
|
891
|
+
this.events.push(graphics.engine.inputSystem.listen('wheel', onMouseWheel, { target: domElement, passive: false }));
|
|
892
|
+
this.events.push(graphics.engine.inputSystem.listen('contextmenu', onContextMenu, { target: domElement }));
|
|
893
|
+
};
|
|
894
|
+
this._removeAllEventListeners = () => {
|
|
895
|
+
if (!this._domElement)
|
|
896
|
+
return;
|
|
897
|
+
this._domElement.style.touchAction = '';
|
|
898
|
+
this._domElement.style.userSelect = '';
|
|
899
|
+
this._domElement.style.webkitUserSelect = '';
|
|
900
|
+
for (const event of this.events) graphics.engine.inputSystem.unlisten(event)
|
|
901
|
+
this.events.length = 0
|
|
902
|
+
this._domElement.ownerDocument.removeEventListener('pointermove', onPointerMove, { passive: false } as any);
|
|
903
|
+
this._domElement.ownerDocument.removeEventListener('mousemove', onMouseMove);
|
|
904
|
+
this._domElement.ownerDocument.removeEventListener('pointerup', onPointerUp);
|
|
905
|
+
this._domElement.ownerDocument.removeEventListener('mouseup', onMouseUp);
|
|
906
|
+
this._domElement.ownerDocument.removeEventListener('pointerlockchange', onPointerLockChange);
|
|
907
|
+
this._domElement.ownerDocument.removeEventListener('pointerlockerror', onPointerLockError);
|
|
908
|
+
};
|
|
909
|
+
this.cancel = () => {
|
|
910
|
+
if (this._state === ACTION.NONE)
|
|
911
|
+
return;
|
|
912
|
+
this._state = ACTION.NONE;
|
|
913
|
+
this._activePointers.length = 0;
|
|
914
|
+
endDragging();
|
|
915
|
+
};
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
/**
|
|
919
|
+
* The camera to be controlled
|
|
920
|
+
* @category Properties
|
|
921
|
+
*/
|
|
922
|
+
get camera() {
|
|
923
|
+
return this._camera;
|
|
924
|
+
}
|
|
925
|
+
set camera(camera) {
|
|
926
|
+
this._camera = camera;
|
|
927
|
+
this.updateCameraUp();
|
|
928
|
+
this._camera.updateProjectionMatrix();
|
|
929
|
+
this._updateNearPlaneCorners();
|
|
930
|
+
this._needsUpdate = true;
|
|
931
|
+
}
|
|
932
|
+
/**
|
|
933
|
+
* Whether or not the controls are enabled.
|
|
934
|
+
* `false` to disable user dragging/touch-move, but all methods works.
|
|
935
|
+
* @category Properties
|
|
936
|
+
*/
|
|
937
|
+
get enabled() {
|
|
938
|
+
return this._enabled;
|
|
939
|
+
}
|
|
940
|
+
set enabled(enabled) {
|
|
941
|
+
this._enabled = enabled;
|
|
942
|
+
if (!this._domElement)
|
|
943
|
+
return;
|
|
944
|
+
if (enabled) {
|
|
945
|
+
this._domElement.style.touchAction = 'none';
|
|
946
|
+
this._domElement.style.userSelect = 'none';
|
|
947
|
+
this._domElement.style.webkitUserSelect = 'none';
|
|
948
|
+
}
|
|
949
|
+
else {
|
|
950
|
+
this.cancel();
|
|
951
|
+
this._domElement.style.touchAction = '';
|
|
952
|
+
this._domElement.style.userSelect = '';
|
|
953
|
+
this._domElement.style.webkitUserSelect = '';
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
/**
|
|
957
|
+
* Returns `true` if the controls are active updating.
|
|
958
|
+
* readonly value.
|
|
959
|
+
* @category Properties
|
|
960
|
+
*/
|
|
961
|
+
get active() {
|
|
962
|
+
return !this._hasRested;
|
|
963
|
+
}
|
|
964
|
+
/**
|
|
965
|
+
* Getter for the current `ACTION`.
|
|
966
|
+
* readonly value.
|
|
967
|
+
* @category Properties
|
|
968
|
+
*/
|
|
969
|
+
get currentAction() {
|
|
970
|
+
return this._state;
|
|
971
|
+
}
|
|
972
|
+
/**
|
|
973
|
+
* get/set Current distance.
|
|
974
|
+
* @category Properties
|
|
975
|
+
*/
|
|
976
|
+
get distance() {
|
|
977
|
+
return this._spherical.radius;
|
|
978
|
+
}
|
|
979
|
+
set distance(distance) {
|
|
980
|
+
if (this._spherical.radius === distance &&
|
|
981
|
+
this._sphericalEnd.radius === distance)
|
|
982
|
+
return;
|
|
983
|
+
this._spherical.radius = distance;
|
|
984
|
+
this._sphericalEnd.radius = distance;
|
|
985
|
+
this._needsUpdate = true;
|
|
986
|
+
}
|
|
987
|
+
// horizontal angle
|
|
988
|
+
/**
|
|
989
|
+
* get/set the azimuth angle (horizontal) in radians.
|
|
990
|
+
* Every 360 degrees turn is added to `.azimuthAngle` value, which is accumulative.
|
|
991
|
+
* @category Properties
|
|
992
|
+
*/
|
|
993
|
+
get azimuthAngle() {
|
|
994
|
+
return this._spherical.theta;
|
|
995
|
+
}
|
|
996
|
+
set azimuthAngle(azimuthAngle) {
|
|
997
|
+
if (this._spherical.theta === azimuthAngle &&
|
|
998
|
+
this._sphericalEnd.theta === azimuthAngle)
|
|
999
|
+
return;
|
|
1000
|
+
this._spherical.theta = azimuthAngle;
|
|
1001
|
+
this._sphericalEnd.theta = azimuthAngle;
|
|
1002
|
+
this._needsUpdate = true;
|
|
1003
|
+
}
|
|
1004
|
+
// vertical angle
|
|
1005
|
+
/**
|
|
1006
|
+
* get/set the polar angle (vertical) in radians.
|
|
1007
|
+
* @category Properties
|
|
1008
|
+
*/
|
|
1009
|
+
get polarAngle() {
|
|
1010
|
+
return this._spherical.phi;
|
|
1011
|
+
}
|
|
1012
|
+
set polarAngle(polarAngle) {
|
|
1013
|
+
if (this._spherical.phi === polarAngle &&
|
|
1014
|
+
this._sphericalEnd.phi === polarAngle)
|
|
1015
|
+
return;
|
|
1016
|
+
this._spherical.phi = polarAngle;
|
|
1017
|
+
this._sphericalEnd.phi = polarAngle;
|
|
1018
|
+
this._needsUpdate = true;
|
|
1019
|
+
}
|
|
1020
|
+
/**
|
|
1021
|
+
* Whether camera position should be enclosed in the boundary or not.
|
|
1022
|
+
* @category Properties
|
|
1023
|
+
*/
|
|
1024
|
+
get boundaryEnclosesCamera() {
|
|
1025
|
+
return this._boundaryEnclosesCamera;
|
|
1026
|
+
}
|
|
1027
|
+
set boundaryEnclosesCamera(boundaryEnclosesCamera) {
|
|
1028
|
+
this._boundaryEnclosesCamera = boundaryEnclosesCamera;
|
|
1029
|
+
this._needsUpdate = true;
|
|
1030
|
+
}
|
|
1031
|
+
/**
|
|
1032
|
+
* Set drag-start, touches and wheel enable area in the domElement.
|
|
1033
|
+
* each values are between `0` and `1` inclusive, where `0` is left/top and `1` is right/bottom of the screen.
|
|
1034
|
+
* e.g. `{ x: 0, y: 0, width: 1, height: 1 }` for entire area.
|
|
1035
|
+
* @category Properties
|
|
1036
|
+
*/
|
|
1037
|
+
set interactiveArea(interactiveArea: DOMRect) {
|
|
1038
|
+
this._interactiveArea.width = clamp(interactiveArea.width, 0, 1);
|
|
1039
|
+
this._interactiveArea.height = clamp(interactiveArea.height, 0, 1);
|
|
1040
|
+
this._interactiveArea.x = clamp(interactiveArea.x, 0, 1 - this._interactiveArea.width);
|
|
1041
|
+
this._interactiveArea.y = clamp(interactiveArea.y, 0, 1 - this._interactiveArea.height);
|
|
1042
|
+
}
|
|
1043
|
+
/**
|
|
1044
|
+
* Adds the specified event listener.
|
|
1045
|
+
* Applicable event types (which is `K`) are:
|
|
1046
|
+
* | Event name | Timing |
|
|
1047
|
+
* | ------------------- | ------ |
|
|
1048
|
+
* | `'controlstart'` | When the user starts to control the camera via mouse / touches. ¹ |
|
|
1049
|
+
* | `'control'` | When the user controls the camera (dragging). |
|
|
1050
|
+
* | `'controlend'` | When the user ends to control the camera. ¹ |
|
|
1051
|
+
* | `'transitionstart'` | When any kind of transition starts, either user control or using a method with `enableTransition = true` |
|
|
1052
|
+
* | `'update'` | When the camera position is updated. |
|
|
1053
|
+
* | `'wake'` | When the camera starts moving. |
|
|
1054
|
+
* | `'rest'` | When the camera movement is below `.restThreshold` ². |
|
|
1055
|
+
* | `'sleep'` | When the camera end moving. |
|
|
1056
|
+
*
|
|
1057
|
+
* 1. `mouseButtons.wheel` (Mouse wheel control) does not emit `'controlstart'` and `'controlend'`. `mouseButtons.wheel` uses scroll-event internally, and scroll-event happens intermittently. That means "start" and "end" cannot be detected.
|
|
1058
|
+
* 2. Due to damping, `sleep` will usually fire a few seconds after the camera _appears_ to have stopped moving. If you want to do something (e.g. enable UI, perform another transition) at the point when the camera has stopped, you probably want the `rest` event. This can be fine tuned using the `.restThreshold` parameter. See the [Rest and Sleep Example](https://yomotsu.github.io/camera-controls/examples/rest-and-sleep.html).
|
|
1059
|
+
*
|
|
1060
|
+
* e.g.
|
|
1061
|
+
* ```
|
|
1062
|
+
* cameraControl.addEventListener( 'controlstart', myCallbackFunction );
|
|
1063
|
+
* ```
|
|
1064
|
+
* @param type event name
|
|
1065
|
+
* @param listener handler function
|
|
1066
|
+
* @category Methods
|
|
1067
|
+
*/
|
|
1068
|
+
addEventListener(type: string, listener: (...args: any[]) => any) {
|
|
1069
|
+
super.addEventListener(type, listener);
|
|
1070
|
+
}
|
|
1071
|
+
/**
|
|
1072
|
+
* Removes the specified event listener
|
|
1073
|
+
* e.g.
|
|
1074
|
+
* ```
|
|
1075
|
+
* cameraControl.addEventListener( 'controlstart', myCallbackFunction );
|
|
1076
|
+
* ```
|
|
1077
|
+
* @param type event name
|
|
1078
|
+
* @param listener handler function
|
|
1079
|
+
* @category Methods
|
|
1080
|
+
*/
|
|
1081
|
+
removeEventListener(type: string, listener: (...args: any[]) => any) {
|
|
1082
|
+
super.removeEventListener(type, listener);
|
|
1083
|
+
}
|
|
1084
|
+
|
|
1085
|
+
removeAllEventListeners(type?: string) {
|
|
1086
|
+
if (!type) {
|
|
1087
|
+
this._listeners = {};
|
|
1088
|
+
return;
|
|
1089
|
+
}
|
|
1090
|
+
if (Array.isArray(this._listeners[type]))
|
|
1091
|
+
this._listeners[type].length = 0;
|
|
1092
|
+
}
|
|
1093
|
+
/**
|
|
1094
|
+
* Rotate azimuthal angle(horizontal) and polar angle(vertical).
|
|
1095
|
+
* Every value is added to the current value.
|
|
1096
|
+
* @param azimuthAngle Azimuth rotate angle. In radian.
|
|
1097
|
+
* @param polarAngle Polar rotate angle. In radian.
|
|
1098
|
+
* @param enableTransition Whether to move smoothly or immediately
|
|
1099
|
+
* @category Methods
|
|
1100
|
+
*/
|
|
1101
|
+
rotate(azimuthAngle: number, polarAngle: number, enableTransition = false) {
|
|
1102
|
+
return this.rotateTo(this._sphericalEnd.theta + azimuthAngle, this._sphericalEnd.phi + polarAngle, enableTransition);
|
|
1103
|
+
}
|
|
1104
|
+
/**
|
|
1105
|
+
* Rotate azimuthal angle(horizontal) to the given angle and keep the same polar angle(vertical) target.
|
|
1106
|
+
*
|
|
1107
|
+
* e.g.
|
|
1108
|
+
* ```
|
|
1109
|
+
* cameraControls.rotateAzimuthTo( 30 * MathUtils.DEG2RAD, true );
|
|
1110
|
+
* ```
|
|
1111
|
+
* @param azimuthAngle Azimuth rotate angle. In radian.
|
|
1112
|
+
* @param enableTransition Whether to move smoothly or immediately
|
|
1113
|
+
* @category Methods
|
|
1114
|
+
*/
|
|
1115
|
+
rotateAzimuthTo(azimuthAngle: number, enableTransition = false) {
|
|
1116
|
+
return this.rotateTo(azimuthAngle, this._sphericalEnd.phi, enableTransition);
|
|
1117
|
+
}
|
|
1118
|
+
/**
|
|
1119
|
+
* Rotate polar angle(vertical) to the given angle and keep the same azimuthal angle(horizontal) target.
|
|
1120
|
+
*
|
|
1121
|
+
* e.g.
|
|
1122
|
+
* ```
|
|
1123
|
+
* cameraControls.rotatePolarTo( 30 * MathUtils.DEG2RAD, true );
|
|
1124
|
+
* ```
|
|
1125
|
+
* @param polarAngle Polar rotate angle. In radian.
|
|
1126
|
+
* @param enableTransition Whether to move smoothly or immediately
|
|
1127
|
+
* @category Methods
|
|
1128
|
+
*/
|
|
1129
|
+
rotatePolarTo(polarAngle: number, enableTransition = false) {
|
|
1130
|
+
return this.rotateTo(this._sphericalEnd.theta, polarAngle, enableTransition);
|
|
1131
|
+
}
|
|
1132
|
+
/**
|
|
1133
|
+
* Rotate azimuthal angle(horizontal) and polar angle(vertical) to the given angle.
|
|
1134
|
+
* Camera view will rotate over the orbit pivot absolutely:
|
|
1135
|
+
*
|
|
1136
|
+
* azimuthAngle
|
|
1137
|
+
* ```
|
|
1138
|
+
* 0º
|
|
1139
|
+
* \
|
|
1140
|
+
* 90º -----+----- -90º
|
|
1141
|
+
* \
|
|
1142
|
+
* 180º
|
|
1143
|
+
* ```
|
|
1144
|
+
* | direction | angle |
|
|
1145
|
+
* | --------- | ---------------------- |
|
|
1146
|
+
* | front | 0º |
|
|
1147
|
+
* | left | 90º (`Math.PI / 2`) |
|
|
1148
|
+
* | right | -90º (`- Math.PI / 2`) |
|
|
1149
|
+
* | back | 180º (`Math.PI`) |
|
|
1150
|
+
*
|
|
1151
|
+
* polarAngle
|
|
1152
|
+
* ```
|
|
1153
|
+
* 180º
|
|
1154
|
+
* |
|
|
1155
|
+
* 90º
|
|
1156
|
+
* |
|
|
1157
|
+
* 0º
|
|
1158
|
+
* ```
|
|
1159
|
+
* | direction | angle |
|
|
1160
|
+
* | -------------------- | ---------------------- |
|
|
1161
|
+
* | top/sky | 180º (`Math.PI`) |
|
|
1162
|
+
* | horizontal from view | 90º (`Math.PI / 2`) |
|
|
1163
|
+
* | bottom/floor | 0º |
|
|
1164
|
+
*
|
|
1165
|
+
* @param azimuthAngle Azimuth rotate angle to. In radian.
|
|
1166
|
+
* @param polarAngle Polar rotate angle to. In radian.
|
|
1167
|
+
* @param enableTransition Whether to move smoothly or immediately
|
|
1168
|
+
* @category Methods
|
|
1169
|
+
*/
|
|
1170
|
+
rotateTo(azimuthAngle: number, polarAngle: number, enableTransition = false) {
|
|
1171
|
+
this._isUserControllingRotate = false;
|
|
1172
|
+
const theta = clamp(azimuthAngle, this.minAzimuthAngle, this.maxAzimuthAngle);
|
|
1173
|
+
const phi = clamp(polarAngle, this.minPolarAngle, this.maxPolarAngle);
|
|
1174
|
+
this._sphericalEnd.theta = theta;
|
|
1175
|
+
this._sphericalEnd.phi = phi;
|
|
1176
|
+
this._sphericalEnd.makeSafe();
|
|
1177
|
+
this._needsUpdate = true;
|
|
1178
|
+
if (!enableTransition) {
|
|
1179
|
+
this._spherical.theta = this._sphericalEnd.theta;
|
|
1180
|
+
this._spherical.phi = this._sphericalEnd.phi;
|
|
1181
|
+
}
|
|
1182
|
+
const resolveImmediately = !enableTransition ||
|
|
1183
|
+
approxEquals(this._spherical.theta, this._sphericalEnd.theta, this.restThreshold) &&
|
|
1184
|
+
approxEquals(this._spherical.phi, this._sphericalEnd.phi, this.restThreshold);
|
|
1185
|
+
return this._createOnRestPromise(resolveImmediately);
|
|
1186
|
+
}
|
|
1187
|
+
/**
|
|
1188
|
+
* Dolly in/out camera position.
|
|
1189
|
+
* @param distance Distance of dollyIn. Negative number for dollyOut.
|
|
1190
|
+
* @param enableTransition Whether to move smoothly or immediately.
|
|
1191
|
+
* @category Methods
|
|
1192
|
+
*/
|
|
1193
|
+
dolly(distance: number, enableTransition = false) {
|
|
1194
|
+
return this.dollyTo(this._sphericalEnd.radius - distance, enableTransition);
|
|
1195
|
+
}
|
|
1196
|
+
/**
|
|
1197
|
+
* Dolly in/out camera position to given distance.
|
|
1198
|
+
* @param distance Distance of dolly.
|
|
1199
|
+
* @param enableTransition Whether to move smoothly or immediately.
|
|
1200
|
+
* @category Methods
|
|
1201
|
+
*/
|
|
1202
|
+
dollyTo(distance: number, enableTransition = false) {
|
|
1203
|
+
this._isUserControllingDolly = false;
|
|
1204
|
+
this._lastDollyDirection = DOLLY_DIRECTION.NONE;
|
|
1205
|
+
this._changedDolly = 0;
|
|
1206
|
+
return this._dollyToNoClamp(clamp(distance, this.minDistance, this.maxDistance), enableTransition);
|
|
1207
|
+
}
|
|
1208
|
+
_dollyToNoClamp(distance: number, enableTransition = false) {
|
|
1209
|
+
const lastRadius = this._sphericalEnd.radius;
|
|
1210
|
+
const hasCollider = this.colliderMeshes.length >= 1;
|
|
1211
|
+
if (hasCollider) {
|
|
1212
|
+
const maxDistanceByCollisionTest = this._collisionTest();
|
|
1213
|
+
const isCollided = approxEquals(maxDistanceByCollisionTest, this._spherical.radius);
|
|
1214
|
+
const isDollyIn = lastRadius > distance;
|
|
1215
|
+
if (!isDollyIn && isCollided)
|
|
1216
|
+
return Promise.resolve();
|
|
1217
|
+
this._sphericalEnd.radius = Math.min(distance, maxDistanceByCollisionTest);
|
|
1218
|
+
}
|
|
1219
|
+
else {
|
|
1220
|
+
this._sphericalEnd.radius = distance;
|
|
1221
|
+
}
|
|
1222
|
+
this._needsUpdate = true;
|
|
1223
|
+
if (!enableTransition) {
|
|
1224
|
+
this._spherical.radius = this._sphericalEnd.radius;
|
|
1225
|
+
}
|
|
1226
|
+
const resolveImmediately = !enableTransition || approxEquals(this._spherical.radius, this._sphericalEnd.radius, this.restThreshold);
|
|
1227
|
+
return this._createOnRestPromise(resolveImmediately);
|
|
1228
|
+
}
|
|
1229
|
+
/**
|
|
1230
|
+
* Dolly in, but does not change the distance between the target and the camera, and moves the target position instead.
|
|
1231
|
+
* Specify a negative value for dolly out.
|
|
1232
|
+
* @param distance Distance of dolly.
|
|
1233
|
+
* @param enableTransition Whether to move smoothly or immediately.
|
|
1234
|
+
* @category Methods
|
|
1235
|
+
*/
|
|
1236
|
+
dollyInFixed(distance: number, enableTransition = false) {
|
|
1237
|
+
this._targetEnd.add(this._getCameraDirection(_cameraDirection).multiplyScalar(distance));
|
|
1238
|
+
if (!enableTransition) {
|
|
1239
|
+
this._target.copy(this._targetEnd);
|
|
1240
|
+
}
|
|
1241
|
+
const resolveImmediately = !enableTransition ||
|
|
1242
|
+
approxEquals(this._target.x, this._targetEnd.x, this.restThreshold) &&
|
|
1243
|
+
approxEquals(this._target.y, this._targetEnd.y, this.restThreshold) &&
|
|
1244
|
+
approxEquals(this._target.z, this._targetEnd.z, this.restThreshold);
|
|
1245
|
+
return this._createOnRestPromise(resolveImmediately);
|
|
1246
|
+
}
|
|
1247
|
+
/**
|
|
1248
|
+
* Zoom in/out camera. The value is added to camera zoom.
|
|
1249
|
+
* Limits set with `.minZoom` and `.maxZoom`
|
|
1250
|
+
* @param zoomStep zoom scale
|
|
1251
|
+
* @param enableTransition Whether to move smoothly or immediately
|
|
1252
|
+
* @category Methods
|
|
1253
|
+
*/
|
|
1254
|
+
zoom(zoomStep: number, enableTransition = false) {
|
|
1255
|
+
return this.zoomTo(this._zoomEnd + zoomStep, enableTransition);
|
|
1256
|
+
}
|
|
1257
|
+
/**
|
|
1258
|
+
* Zoom in/out camera to given scale. The value overwrites camera zoom.
|
|
1259
|
+
* Limits set with .minZoom and .maxZoom
|
|
1260
|
+
* @param zoom
|
|
1261
|
+
* @param enableTransition
|
|
1262
|
+
* @category Methods
|
|
1263
|
+
*/
|
|
1264
|
+
zoomTo(zoom: number, enableTransition = false) {
|
|
1265
|
+
this._isUserControllingZoom = false;
|
|
1266
|
+
this._zoomEnd = clamp(zoom, this.minZoom, this.maxZoom);
|
|
1267
|
+
this._needsUpdate = true;
|
|
1268
|
+
if (!enableTransition) {
|
|
1269
|
+
this._zoom = this._zoomEnd;
|
|
1270
|
+
}
|
|
1271
|
+
const resolveImmediately = !enableTransition || approxEquals(this._zoom, this._zoomEnd, this.restThreshold);
|
|
1272
|
+
this._changedZoom = 0;
|
|
1273
|
+
return this._createOnRestPromise(resolveImmediately);
|
|
1274
|
+
}
|
|
1275
|
+
/**
|
|
1276
|
+
* @deprecated `pan()` has been renamed to `truck()`
|
|
1277
|
+
* @category Methods
|
|
1278
|
+
*/
|
|
1279
|
+
pan(x: number, y: number, enableTransition = false) {
|
|
1280
|
+
console.warn('`pan` has been renamed to `truck`');
|
|
1281
|
+
return this.truck(x, y, enableTransition);
|
|
1282
|
+
}
|
|
1283
|
+
/**
|
|
1284
|
+
* Truck and pedestal camera using current azimuthal angle
|
|
1285
|
+
* @param x Horizontal translate amount
|
|
1286
|
+
* @param y Vertical translate amount
|
|
1287
|
+
* @param enableTransition Whether to move smoothly or immediately
|
|
1288
|
+
* @category Methods
|
|
1289
|
+
*/
|
|
1290
|
+
truck(x: number, y: number, enableTransition = false) {
|
|
1291
|
+
this._camera.updateMatrix();
|
|
1292
|
+
_xColumn.setFromMatrixColumn(this._camera.matrix, 0);
|
|
1293
|
+
_yColumn.setFromMatrixColumn(this._camera.matrix, 1);
|
|
1294
|
+
_xColumn.multiplyScalar(x);
|
|
1295
|
+
_yColumn.multiplyScalar(-y);
|
|
1296
|
+
const offset = _v3A.copy(_xColumn).add(_yColumn);
|
|
1297
|
+
const to = _v3B.copy(this._targetEnd).add(offset);
|
|
1298
|
+
return this.moveTo(to.x, to.y, to.z, enableTransition);
|
|
1299
|
+
}
|
|
1300
|
+
/**
|
|
1301
|
+
* Move forward / backward.
|
|
1302
|
+
* @param distance Amount to move forward / backward. Negative value to move backward
|
|
1303
|
+
* @param enableTransition Whether to move smoothly or immediately
|
|
1304
|
+
* @category Methods
|
|
1305
|
+
*/
|
|
1306
|
+
forward(distance: number, enableTransition = false) {
|
|
1307
|
+
_v3A.setFromMatrixColumn(this._camera.matrix, 0);
|
|
1308
|
+
_v3A.crossVectors(this._camera.up, _v3A);
|
|
1309
|
+
_v3A.multiplyScalar(distance);
|
|
1310
|
+
const to = _v3B.copy(this._targetEnd).add(_v3A);
|
|
1311
|
+
return this.moveTo(to.x, to.y, to.z, enableTransition);
|
|
1312
|
+
}
|
|
1313
|
+
/**
|
|
1314
|
+
* Move up / down.
|
|
1315
|
+
* @param height Amount to move up / down. Negative value to move down
|
|
1316
|
+
* @param enableTransition Whether to move smoothly or immediately
|
|
1317
|
+
* @category Methods
|
|
1318
|
+
*/
|
|
1319
|
+
elevate(height: number, enableTransition = false) {
|
|
1320
|
+
_v3A.copy(this._camera.up).multiplyScalar(height);
|
|
1321
|
+
return this.moveTo(this._targetEnd.x + _v3A.x, this._targetEnd.y + _v3A.y, this._targetEnd.z + _v3A.z, enableTransition);
|
|
1322
|
+
}
|
|
1323
|
+
/**
|
|
1324
|
+
* Move target position to given point.
|
|
1325
|
+
* @param x x coord to move center position
|
|
1326
|
+
* @param y y coord to move center position
|
|
1327
|
+
* @param z z coord to move center position
|
|
1328
|
+
* @param enableTransition Whether to move smoothly or immediately
|
|
1329
|
+
* @category Methods
|
|
1330
|
+
*/
|
|
1331
|
+
moveTo(x: number, y: number, z: number, enableTransition = false) {
|
|
1332
|
+
this._isUserControllingTruck = false;
|
|
1333
|
+
const offset = _v3A.set(x, y, z).sub(this._targetEnd);
|
|
1334
|
+
this._encloseToBoundary(this._targetEnd, offset, this.boundaryFriction);
|
|
1335
|
+
this._needsUpdate = true;
|
|
1336
|
+
if (!enableTransition) {
|
|
1337
|
+
this._target.copy(this._targetEnd);
|
|
1338
|
+
}
|
|
1339
|
+
const resolveImmediately = !enableTransition ||
|
|
1340
|
+
approxEquals(this._target.x, this._targetEnd.x, this.restThreshold) &&
|
|
1341
|
+
approxEquals(this._target.y, this._targetEnd.y, this.restThreshold) &&
|
|
1342
|
+
approxEquals(this._target.z, this._targetEnd.z, this.restThreshold);
|
|
1343
|
+
return this._createOnRestPromise(resolveImmediately);
|
|
1344
|
+
}
|
|
1345
|
+
/**
|
|
1346
|
+
* Look in the given point direction.
|
|
1347
|
+
* @param x point x.
|
|
1348
|
+
* @param y point y.
|
|
1349
|
+
* @param z point z.
|
|
1350
|
+
* @param enableTransition Whether to move smoothly or immediately.
|
|
1351
|
+
* @returns Transition end promise
|
|
1352
|
+
* @category Methods
|
|
1353
|
+
*/
|
|
1354
|
+
lookInDirectionOf(x: number, y: number, z: number, enableTransition = false) {
|
|
1355
|
+
const point = _v3A.set(x, y, z);
|
|
1356
|
+
const direction = point.sub(this._targetEnd).normalize();
|
|
1357
|
+
const position = direction.multiplyScalar(-this._sphericalEnd.radius).add(this._targetEnd);
|
|
1358
|
+
return this.setPosition(position.x, position.y, position.z, enableTransition);
|
|
1359
|
+
}
|
|
1360
|
+
/**
|
|
1361
|
+
* Fit the viewport to the box or the bounding box of the object, using the nearest axis. paddings are in unit.
|
|
1362
|
+
* set `cover: true` to fill enter screen.
|
|
1363
|
+
* e.g.
|
|
1364
|
+
* ```
|
|
1365
|
+
* cameraControls.fitToBox( myMesh );
|
|
1366
|
+
* ```
|
|
1367
|
+
* @param box3OrObject Axis aligned bounding box to fit the view.
|
|
1368
|
+
* @param enableTransition Whether to move smoothly or immediately.
|
|
1369
|
+
* @param options | `<object>` { cover: boolean, paddingTop: number, paddingLeft: number, paddingBottom: number, paddingRight: number }
|
|
1370
|
+
* @returns Transition end promise
|
|
1371
|
+
* @category Methods
|
|
1372
|
+
*/
|
|
1373
|
+
fitToBox(box3OrObject: Box3 | Object3D, enableTransition: boolean, { cover = false, paddingLeft = 0, paddingRight = 0, paddingBottom = 0, paddingTop = 0 } = {}) {
|
|
1374
|
+
const promises = [];
|
|
1375
|
+
//@ts-ignore
|
|
1376
|
+
const aabb = box3OrObject.isBox3 ? _box3A.copy(box3OrObject) : _box3A.setFromObject(box3OrObject);
|
|
1377
|
+
if (aabb.isEmpty()) {
|
|
1378
|
+
console.warn('camera-controls: fitTo() cannot be used with an empty box. Aborting');
|
|
1379
|
+
Promise.resolve();
|
|
1380
|
+
}
|
|
1381
|
+
// round to closest axis ( forward | backward | right | left | top | bottom )
|
|
1382
|
+
const theta = roundToStep(this._sphericalEnd.theta, PI_HALF);
|
|
1383
|
+
const phi = roundToStep(this._sphericalEnd.phi, PI_HALF);
|
|
1384
|
+
promises.push(this.rotateTo(theta, phi, enableTransition));
|
|
1385
|
+
const normal = _v3A.setFromSpherical(this._sphericalEnd).normalize();
|
|
1386
|
+
const rotation = _quaternionA.setFromUnitVectors(normal, _AXIS_Z);
|
|
1387
|
+
const viewFromPolar = approxEquals(Math.abs(normal.y), 1);
|
|
1388
|
+
if (viewFromPolar) {
|
|
1389
|
+
rotation.multiply(_quaternionB.setFromAxisAngle(_AXIS_Y, theta));
|
|
1390
|
+
}
|
|
1391
|
+
rotation.multiply(this._yAxisUpSpaceInverse);
|
|
1392
|
+
// make oriented bounding box
|
|
1393
|
+
const bb = _box3B.makeEmpty();
|
|
1394
|
+
// left bottom back corner
|
|
1395
|
+
_v3B.copy(aabb.min).applyQuaternion(rotation);
|
|
1396
|
+
bb.expandByPoint(_v3B);
|
|
1397
|
+
// right bottom back corner
|
|
1398
|
+
_v3B.copy(aabb.min).setX(aabb.max.x).applyQuaternion(rotation);
|
|
1399
|
+
bb.expandByPoint(_v3B);
|
|
1400
|
+
// left top back corner
|
|
1401
|
+
_v3B.copy(aabb.min).setY(aabb.max.y).applyQuaternion(rotation);
|
|
1402
|
+
bb.expandByPoint(_v3B);
|
|
1403
|
+
// right top back corner
|
|
1404
|
+
_v3B.copy(aabb.max).setZ(aabb.min.z).applyQuaternion(rotation);
|
|
1405
|
+
bb.expandByPoint(_v3B);
|
|
1406
|
+
// left bottom front corner
|
|
1407
|
+
_v3B.copy(aabb.min).setZ(aabb.max.z).applyQuaternion(rotation);
|
|
1408
|
+
bb.expandByPoint(_v3B);
|
|
1409
|
+
// right bottom front corner
|
|
1410
|
+
_v3B.copy(aabb.max).setY(aabb.min.y).applyQuaternion(rotation);
|
|
1411
|
+
bb.expandByPoint(_v3B);
|
|
1412
|
+
// left top front corner
|
|
1413
|
+
_v3B.copy(aabb.max).setX(aabb.min.x).applyQuaternion(rotation);
|
|
1414
|
+
bb.expandByPoint(_v3B);
|
|
1415
|
+
// right top front corner
|
|
1416
|
+
_v3B.copy(aabb.max).applyQuaternion(rotation);
|
|
1417
|
+
bb.expandByPoint(_v3B);
|
|
1418
|
+
// add padding
|
|
1419
|
+
bb.min.x -= paddingLeft;
|
|
1420
|
+
bb.min.y -= paddingBottom;
|
|
1421
|
+
bb.max.x += paddingRight;
|
|
1422
|
+
bb.max.y += paddingTop;
|
|
1423
|
+
rotation.setFromUnitVectors(_AXIS_Z, normal);
|
|
1424
|
+
if (viewFromPolar) {
|
|
1425
|
+
rotation.premultiply(_quaternionB.invert());
|
|
1426
|
+
}
|
|
1427
|
+
rotation.premultiply(this._yAxisUpSpace);
|
|
1428
|
+
const bbSize = bb.getSize(_v3A);
|
|
1429
|
+
const center = bb.getCenter(_v3B).applyQuaternion(rotation);
|
|
1430
|
+
if (isPerspectiveCamera(this._camera)) {
|
|
1431
|
+
const distance = this.getDistanceToFitBox(bbSize.x, bbSize.y, bbSize.z, cover);
|
|
1432
|
+
promises.push(this.moveTo(center.x, center.y, center.z, enableTransition));
|
|
1433
|
+
promises.push(this.dollyTo(distance, enableTransition));
|
|
1434
|
+
promises.push(this.setFocalOffset(0, 0, 0, enableTransition));
|
|
1435
|
+
}
|
|
1436
|
+
else if (isOrthographicCamera(this._camera)) {
|
|
1437
|
+
const camera = this._camera;
|
|
1438
|
+
const width = camera.right - camera.left;
|
|
1439
|
+
const height = camera.top - camera.bottom;
|
|
1440
|
+
const zoom = cover ? Math.max(width / bbSize.x, height / bbSize.y) : Math.min(width / bbSize.x, height / bbSize.y);
|
|
1441
|
+
promises.push(this.moveTo(center.x, center.y, center.z, enableTransition));
|
|
1442
|
+
promises.push(this.zoomTo(zoom, enableTransition));
|
|
1443
|
+
promises.push(this.setFocalOffset(0, 0, 0, enableTransition));
|
|
1444
|
+
}
|
|
1445
|
+
return Promise.all(promises);
|
|
1446
|
+
}
|
|
1447
|
+
/**
|
|
1448
|
+
* Fit the viewport to the sphere or the bounding sphere of the object.
|
|
1449
|
+
* @param sphereOrMesh
|
|
1450
|
+
* @param enableTransition
|
|
1451
|
+
* @category Methods
|
|
1452
|
+
*/
|
|
1453
|
+
fitToSphere(sphereOrMesh: Sphere | Mesh, enableTransition: boolean) {
|
|
1454
|
+
const promises = [];
|
|
1455
|
+
const isSphere = sphereOrMesh instanceof Sphere;
|
|
1456
|
+
const boundingSphere = isSphere ?
|
|
1457
|
+
_sphere.copy(sphereOrMesh) :
|
|
1458
|
+
CameraControls.createBoundingSphere(sphereOrMesh, _sphere);
|
|
1459
|
+
promises.push(this.moveTo(boundingSphere.center.x, boundingSphere.center.y, boundingSphere.center.z, enableTransition));
|
|
1460
|
+
if (isPerspectiveCamera(this._camera)) {
|
|
1461
|
+
const distanceToFit = this.getDistanceToFitSphere(boundingSphere.radius);
|
|
1462
|
+
promises.push(this.dollyTo(distanceToFit, enableTransition));
|
|
1463
|
+
}
|
|
1464
|
+
else if (isOrthographicCamera(this._camera)) {
|
|
1465
|
+
const width = this._camera.right - this._camera.left;
|
|
1466
|
+
const height = this._camera.top - this._camera.bottom;
|
|
1467
|
+
const diameter = 2 * boundingSphere.radius;
|
|
1468
|
+
const zoom = Math.min(width / diameter, height / diameter);
|
|
1469
|
+
promises.push(this.zoomTo(zoom, enableTransition));
|
|
1470
|
+
}
|
|
1471
|
+
promises.push(this.setFocalOffset(0, 0, 0, enableTransition));
|
|
1472
|
+
return Promise.all(promises);
|
|
1473
|
+
}
|
|
1474
|
+
/**
|
|
1475
|
+
* Look at the `target` from the `position`.
|
|
1476
|
+
* @param positionX
|
|
1477
|
+
* @param positionY
|
|
1478
|
+
* @param positionZ
|
|
1479
|
+
* @param targetX
|
|
1480
|
+
* @param targetY
|
|
1481
|
+
* @param targetZ
|
|
1482
|
+
* @param enableTransition
|
|
1483
|
+
* @category Methods
|
|
1484
|
+
*/
|
|
1485
|
+
setLookAt(positionX: number, positionY: number, positionZ: number, targetX: number, targetY: number, targetZ: number, enableTransition = false) {
|
|
1486
|
+
this._isUserControllingRotate = false;
|
|
1487
|
+
this._isUserControllingDolly = false;
|
|
1488
|
+
this._isUserControllingTruck = false;
|
|
1489
|
+
this._lastDollyDirection = DOLLY_DIRECTION.NONE;
|
|
1490
|
+
this._changedDolly = 0;
|
|
1491
|
+
const target = _v3B.set(targetX, targetY, targetZ);
|
|
1492
|
+
const position = _v3A.set(positionX, positionY, positionZ);
|
|
1493
|
+
this._targetEnd.copy(target);
|
|
1494
|
+
this._sphericalEnd.setFromVector3(position.sub(target).applyQuaternion(this._yAxisUpSpace));
|
|
1495
|
+
this.normalizeRotations();
|
|
1496
|
+
this._needsUpdate = true;
|
|
1497
|
+
if (!enableTransition) {
|
|
1498
|
+
this._target.copy(this._targetEnd);
|
|
1499
|
+
this._spherical.copy(this._sphericalEnd);
|
|
1500
|
+
}
|
|
1501
|
+
const resolveImmediately = !enableTransition ||
|
|
1502
|
+
approxEquals(this._target.x, this._targetEnd.x, this.restThreshold) &&
|
|
1503
|
+
approxEquals(this._target.y, this._targetEnd.y, this.restThreshold) &&
|
|
1504
|
+
approxEquals(this._target.z, this._targetEnd.z, this.restThreshold) &&
|
|
1505
|
+
approxEquals(this._spherical.theta, this._sphericalEnd.theta, this.restThreshold) &&
|
|
1506
|
+
approxEquals(this._spherical.phi, this._sphericalEnd.phi, this.restThreshold) &&
|
|
1507
|
+
approxEquals(this._spherical.radius, this._sphericalEnd.radius, this.restThreshold);
|
|
1508
|
+
return this._createOnRestPromise(resolveImmediately);
|
|
1509
|
+
}
|
|
1510
|
+
/**
|
|
1511
|
+
* Similar to setLookAt, but it interpolates between two states.
|
|
1512
|
+
* @param positionAX
|
|
1513
|
+
* @param positionAY
|
|
1514
|
+
* @param positionAZ
|
|
1515
|
+
* @param targetAX
|
|
1516
|
+
* @param targetAY
|
|
1517
|
+
* @param targetAZ
|
|
1518
|
+
* @param positionBX
|
|
1519
|
+
* @param positionBY
|
|
1520
|
+
* @param positionBZ
|
|
1521
|
+
* @param targetBX
|
|
1522
|
+
* @param targetBY
|
|
1523
|
+
* @param targetBZ
|
|
1524
|
+
* @param t
|
|
1525
|
+
* @param enableTransition
|
|
1526
|
+
* @category Methods
|
|
1527
|
+
*/
|
|
1528
|
+
lerpLookAt(positionAX: number, positionAY: number, positionAZ: number, targetAX: number, targetAY: number, targetAZ: number, positionBX: number, positionBY: number, positionBZ: number, targetBX: number, targetBY: number, targetBZ: number, t: number, enableTransition = false) {
|
|
1529
|
+
this._isUserControllingRotate = false;
|
|
1530
|
+
this._isUserControllingDolly = false;
|
|
1531
|
+
this._isUserControllingTruck = false;
|
|
1532
|
+
this._lastDollyDirection = DOLLY_DIRECTION.NONE;
|
|
1533
|
+
this._changedDolly = 0;
|
|
1534
|
+
const targetA = _v3A.set(targetAX, targetAY, targetAZ);
|
|
1535
|
+
const positionA = _v3B.set(positionAX, positionAY, positionAZ);
|
|
1536
|
+
_sphericalA.setFromVector3(positionA.sub(targetA).applyQuaternion(this._yAxisUpSpace));
|
|
1537
|
+
const targetB = _v3C.set(targetBX, targetBY, targetBZ);
|
|
1538
|
+
const positionB = _v3B.set(positionBX, positionBY, positionBZ);
|
|
1539
|
+
_sphericalB.setFromVector3(positionB.sub(targetB).applyQuaternion(this._yAxisUpSpace));
|
|
1540
|
+
this._targetEnd.copy(targetA.lerp(targetB, t)); // tricky
|
|
1541
|
+
const deltaTheta = _sphericalB.theta - _sphericalA.theta;
|
|
1542
|
+
const deltaPhi = _sphericalB.phi - _sphericalA.phi;
|
|
1543
|
+
const deltaRadius = _sphericalB.radius - _sphericalA.radius;
|
|
1544
|
+
this._sphericalEnd.set(_sphericalA.radius + deltaRadius * t, _sphericalA.phi + deltaPhi * t, _sphericalA.theta + deltaTheta * t);
|
|
1545
|
+
this.normalizeRotations();
|
|
1546
|
+
this._needsUpdate = true;
|
|
1547
|
+
if (!enableTransition) {
|
|
1548
|
+
this._target.copy(this._targetEnd);
|
|
1549
|
+
this._spherical.copy(this._sphericalEnd);
|
|
1550
|
+
}
|
|
1551
|
+
const resolveImmediately = !enableTransition ||
|
|
1552
|
+
approxEquals(this._target.x, this._targetEnd.x, this.restThreshold) &&
|
|
1553
|
+
approxEquals(this._target.y, this._targetEnd.y, this.restThreshold) &&
|
|
1554
|
+
approxEquals(this._target.z, this._targetEnd.z, this.restThreshold) &&
|
|
1555
|
+
approxEquals(this._spherical.theta, this._sphericalEnd.theta, this.restThreshold) &&
|
|
1556
|
+
approxEquals(this._spherical.phi, this._sphericalEnd.phi, this.restThreshold) &&
|
|
1557
|
+
approxEquals(this._spherical.radius, this._sphericalEnd.radius, this.restThreshold);
|
|
1558
|
+
return this._createOnRestPromise(resolveImmediately);
|
|
1559
|
+
}
|
|
1560
|
+
/**
|
|
1561
|
+
* Set angle and distance by given position.
|
|
1562
|
+
* An alias of `setLookAt()`, without target change. Thus keep gazing at the current target
|
|
1563
|
+
* @param positionX
|
|
1564
|
+
* @param positionY
|
|
1565
|
+
* @param positionZ
|
|
1566
|
+
* @param enableTransition
|
|
1567
|
+
* @category Methods
|
|
1568
|
+
*/
|
|
1569
|
+
setPosition(positionX: number, positionY: number, positionZ: number, enableTransition = false) {
|
|
1570
|
+
return this.setLookAt(positionX, positionY, positionZ, this._targetEnd.x, this._targetEnd.y, this._targetEnd.z, enableTransition);
|
|
1571
|
+
}
|
|
1572
|
+
/**
|
|
1573
|
+
* Set the target position where gaze at.
|
|
1574
|
+
* An alias of `setLookAt()`, without position change. Thus keep the same position.
|
|
1575
|
+
* @param targetX
|
|
1576
|
+
* @param targetY
|
|
1577
|
+
* @param targetZ
|
|
1578
|
+
* @param enableTransition
|
|
1579
|
+
* @category Methods
|
|
1580
|
+
*/
|
|
1581
|
+
setTarget(targetX: number, targetY: number, targetZ: number, enableTransition = false) {
|
|
1582
|
+
const pos = this.getPosition(_v3A);
|
|
1583
|
+
const promise = this.setLookAt(pos.x, pos.y, pos.z, targetX, targetY, targetZ, enableTransition);
|
|
1584
|
+
// see https://github.com/yomotsu/camera-controls/issues/335
|
|
1585
|
+
this._sphericalEnd.phi = clamp(this._sphericalEnd.phi, this.minPolarAngle, this.maxPolarAngle);
|
|
1586
|
+
return promise;
|
|
1587
|
+
}
|
|
1588
|
+
/**
|
|
1589
|
+
* Set focal offset using the screen parallel coordinates. z doesn't affect in Orthographic as with Dolly.
|
|
1590
|
+
* @param x
|
|
1591
|
+
* @param y
|
|
1592
|
+
* @param z
|
|
1593
|
+
* @param enableTransition
|
|
1594
|
+
* @category Methods
|
|
1595
|
+
*/
|
|
1596
|
+
setFocalOffset(x: number, y: number, z: number, enableTransition = false) {
|
|
1597
|
+
this._isUserControllingOffset = false;
|
|
1598
|
+
this._focalOffsetEnd.set(x, y, z);
|
|
1599
|
+
this._needsUpdate = true;
|
|
1600
|
+
if (!enableTransition)
|
|
1601
|
+
this._focalOffset.copy(this._focalOffsetEnd);
|
|
1602
|
+
const resolveImmediately = !enableTransition ||
|
|
1603
|
+
approxEquals(this._focalOffset.x, this._focalOffsetEnd.x, this.restThreshold) &&
|
|
1604
|
+
approxEquals(this._focalOffset.y, this._focalOffsetEnd.y, this.restThreshold) &&
|
|
1605
|
+
approxEquals(this._focalOffset.z, this._focalOffsetEnd.z, this.restThreshold);
|
|
1606
|
+
return this._createOnRestPromise(resolveImmediately);
|
|
1607
|
+
}
|
|
1608
|
+
/**
|
|
1609
|
+
* Set orbit point without moving the camera.
|
|
1610
|
+
* SHOULD NOT RUN DURING ANIMATIONS. `setOrbitPoint()` will immediately fix the positions.
|
|
1611
|
+
* @param targetX
|
|
1612
|
+
* @param targetY
|
|
1613
|
+
* @param targetZ
|
|
1614
|
+
* @category Methods
|
|
1615
|
+
*/
|
|
1616
|
+
setOrbitPoint(targetX: number, targetY: number, targetZ: number) {
|
|
1617
|
+
this._camera.updateMatrixWorld();
|
|
1618
|
+
_xColumn.setFromMatrixColumn(this._camera.matrixWorldInverse, 0);
|
|
1619
|
+
_yColumn.setFromMatrixColumn(this._camera.matrixWorldInverse, 1);
|
|
1620
|
+
_zColumn.setFromMatrixColumn(this._camera.matrixWorldInverse, 2);
|
|
1621
|
+
const position = _v3A.set(targetX, targetY, targetZ);
|
|
1622
|
+
const distance = position.distanceTo(this._camera.position);
|
|
1623
|
+
const cameraToPoint = position.sub(this._camera.position);
|
|
1624
|
+
_xColumn.multiplyScalar(cameraToPoint.x);
|
|
1625
|
+
_yColumn.multiplyScalar(cameraToPoint.y);
|
|
1626
|
+
_zColumn.multiplyScalar(cameraToPoint.z);
|
|
1627
|
+
_v3A.copy(_xColumn).add(_yColumn).add(_zColumn);
|
|
1628
|
+
_v3A.z = _v3A.z + distance;
|
|
1629
|
+
this.dollyTo(distance, false);
|
|
1630
|
+
this.setFocalOffset(-_v3A.x, _v3A.y, -_v3A.z, false);
|
|
1631
|
+
this.moveTo(targetX, targetY, targetZ, false);
|
|
1632
|
+
}
|
|
1633
|
+
/**
|
|
1634
|
+
* Set the boundary box that encloses the target of the camera. box3 is in Box3
|
|
1635
|
+
* @param box3
|
|
1636
|
+
* @category Methods
|
|
1637
|
+
*/
|
|
1638
|
+
setBoundary(box3: Box3) {
|
|
1639
|
+
if (!box3) {
|
|
1640
|
+
this._boundary.min.set(-Infinity, -Infinity, -Infinity);
|
|
1641
|
+
this._boundary.max.set(Infinity, Infinity, Infinity);
|
|
1642
|
+
this._needsUpdate = true;
|
|
1643
|
+
return;
|
|
1644
|
+
}
|
|
1645
|
+
this._boundary.copy(box3);
|
|
1646
|
+
this._boundary.clampPoint(this._targetEnd, this._targetEnd);
|
|
1647
|
+
this._needsUpdate = true;
|
|
1648
|
+
}
|
|
1649
|
+
/**
|
|
1650
|
+
* Set (or unset) the current viewport.
|
|
1651
|
+
* Set this when you want to use renderer viewport and .dollyToCursor feature at the same time.
|
|
1652
|
+
* @param viewportOrX
|
|
1653
|
+
* @param y
|
|
1654
|
+
* @param width
|
|
1655
|
+
* @param height
|
|
1656
|
+
* @category Methods
|
|
1657
|
+
*/
|
|
1658
|
+
setViewport(viewportOrX: number | Vector4, y: number, width: number, height: number) {
|
|
1659
|
+
if (viewportOrX === null) { // null
|
|
1660
|
+
this._viewport = null;
|
|
1661
|
+
return;
|
|
1662
|
+
}
|
|
1663
|
+
this._viewport = this._viewport || new Vector4();
|
|
1664
|
+
if (typeof viewportOrX === 'number') { // number
|
|
1665
|
+
this._viewport.set(viewportOrX, y, width, height);
|
|
1666
|
+
}
|
|
1667
|
+
else { // Vector4
|
|
1668
|
+
this._viewport.copy(viewportOrX);
|
|
1669
|
+
}
|
|
1670
|
+
}
|
|
1671
|
+
/**
|
|
1672
|
+
* Calculate the distance to fit the box.
|
|
1673
|
+
* @param width box width
|
|
1674
|
+
* @param height box height
|
|
1675
|
+
* @param depth box depth
|
|
1676
|
+
* @returns distance
|
|
1677
|
+
* @category Methods
|
|
1678
|
+
*/
|
|
1679
|
+
getDistanceToFitBox(width: number, height: number, depth: number, cover = false) {
|
|
1680
|
+
if (notSupportedInOrthographicCamera(this._camera, 'getDistanceToFitBox'))
|
|
1681
|
+
return this._spherical.radius;
|
|
1682
|
+
const boundingRectAspect = width / height;
|
|
1683
|
+
const fov = this._camera.getEffectiveFOV() * DEG2RAD;
|
|
1684
|
+
const aspect = this._camera.aspect;
|
|
1685
|
+
const heightToFit = (cover ? boundingRectAspect > aspect : boundingRectAspect < aspect) ? height : width / aspect;
|
|
1686
|
+
return heightToFit * 0.5 / Math.tan(fov * 0.5) + depth * 0.5;
|
|
1687
|
+
}
|
|
1688
|
+
/**
|
|
1689
|
+
* Calculate the distance to fit the sphere.
|
|
1690
|
+
* @param radius sphere radius
|
|
1691
|
+
* @returns distance
|
|
1692
|
+
* @category Methods
|
|
1693
|
+
*/
|
|
1694
|
+
getDistanceToFitSphere(radius: number) {
|
|
1695
|
+
if (notSupportedInOrthographicCamera(this._camera, 'getDistanceToFitSphere'))
|
|
1696
|
+
return this._spherical.radius;
|
|
1697
|
+
// https://stackoverflow.com/a/44849975
|
|
1698
|
+
const vFOV = this._camera.getEffectiveFOV() * DEG2RAD;
|
|
1699
|
+
const hFOV = Math.atan(Math.tan(vFOV * 0.5) * this._camera.aspect) * 2;
|
|
1700
|
+
const fov = 1 < this._camera.aspect ? vFOV : hFOV;
|
|
1701
|
+
return radius / (Math.sin(fov * 0.5));
|
|
1702
|
+
}
|
|
1703
|
+
/**
|
|
1704
|
+
* Returns the orbit center position, where the camera looking at.
|
|
1705
|
+
* @param out The receiving Vector3 instance to copy the result
|
|
1706
|
+
* @param receiveEndValue Whether receive the transition end coords or current. default is `true`
|
|
1707
|
+
* @category Methods
|
|
1708
|
+
*/
|
|
1709
|
+
getTarget(out: Vector3, receiveEndValue = true) {
|
|
1710
|
+
const _out = !!out && out.isVector3 ? out : new Vector3();
|
|
1711
|
+
return _out.copy(receiveEndValue ? this._targetEnd : this._target);
|
|
1712
|
+
}
|
|
1713
|
+
/**
|
|
1714
|
+
* Returns the camera position.
|
|
1715
|
+
* @param out The receiving Vector3 instance to copy the result
|
|
1716
|
+
* @param receiveEndValue Whether receive the transition end coords or current. default is `true`
|
|
1717
|
+
* @category Methods
|
|
1718
|
+
*/
|
|
1719
|
+
getPosition(out: Vector3, receiveEndValue = true) {
|
|
1720
|
+
const _out = !!out && out.isVector3 ? out : new Vector3();
|
|
1721
|
+
return _out.setFromSpherical(receiveEndValue ? this._sphericalEnd : this._spherical).applyQuaternion(this._yAxisUpSpaceInverse).add(receiveEndValue ? this._targetEnd : this._target);
|
|
1722
|
+
}
|
|
1723
|
+
/**
|
|
1724
|
+
* Returns the spherical coordinates of the orbit.
|
|
1725
|
+
* @param out The receiving Spherical instance to copy the result
|
|
1726
|
+
* @param receiveEndValue Whether receive the transition end coords or current. default is `true`
|
|
1727
|
+
* @category Methods
|
|
1728
|
+
*/
|
|
1729
|
+
getSpherical(out: Spherical, receiveEndValue = true) {
|
|
1730
|
+
const _out = !!out && out instanceof Spherical ? out : new Spherical();
|
|
1731
|
+
return _out.copy(receiveEndValue ? this._sphericalEnd : this._spherical);
|
|
1732
|
+
}
|
|
1733
|
+
/**
|
|
1734
|
+
* Returns the focal offset, which is how much the camera appears to be translated in screen parallel coordinates.
|
|
1735
|
+
* @param out The receiving Vector3 instance to copy the result
|
|
1736
|
+
* @param receiveEndValue Whether receive the transition end coords or current. default is `true`
|
|
1737
|
+
* @category Methods
|
|
1738
|
+
*/
|
|
1739
|
+
getFocalOffset(out: Vector3, receiveEndValue = true) {
|
|
1740
|
+
const _out = !!out && out.isVector3 ? out : new Vector3();
|
|
1741
|
+
return _out.copy(receiveEndValue ? this._focalOffsetEnd : this._focalOffset);
|
|
1742
|
+
}
|
|
1743
|
+
/**
|
|
1744
|
+
* Normalize camera azimuth angle rotation between 0 and 360 degrees.
|
|
1745
|
+
* @category Methods
|
|
1746
|
+
*/
|
|
1747
|
+
normalizeRotations() {
|
|
1748
|
+
this._sphericalEnd.theta = this._sphericalEnd.theta % PI_2;
|
|
1749
|
+
if (this._sphericalEnd.theta < 0)
|
|
1750
|
+
this._sphericalEnd.theta += PI_2;
|
|
1751
|
+
this._spherical.theta += PI_2 * Math.round((this._sphericalEnd.theta - this._spherical.theta) / PI_2);
|
|
1752
|
+
}
|
|
1753
|
+
|
|
1754
|
+
listen(camera: PerspectiveCamera | OrthographicCamera) {
|
|
1755
|
+
this._camera = camera;
|
|
1756
|
+
// this.dragStartPosition = new Vector2();
|
|
1757
|
+
// this.lastDragPosition = new Vector2();
|
|
1758
|
+
// this.dollyStart = new Vector2();
|
|
1759
|
+
|
|
1760
|
+
this._yAxisUpSpace = new Quaternion().setFromUnitVectors(this._camera.up, _AXIS_Y);
|
|
1761
|
+
this._yAxisUpSpaceInverse = this._yAxisUpSpace.clone().invert();
|
|
1762
|
+
// the location
|
|
1763
|
+
this._targetEnd = this._target.clone();
|
|
1764
|
+
this._focalOffsetEnd = this._focalOffset.clone();
|
|
1765
|
+
// rotation
|
|
1766
|
+
this._spherical = new Spherical().setFromVector3(_v3A.copy(this._camera.position).applyQuaternion(this._yAxisUpSpace));
|
|
1767
|
+
this._sphericalEnd = this._spherical.clone();
|
|
1768
|
+
this._lastDistance = this._spherical.radius;
|
|
1769
|
+
this._zoom = this._camera.zoom;
|
|
1770
|
+
this._zoomEnd = this._zoom;
|
|
1771
|
+
this._lastZoom = this._zoom;
|
|
1772
|
+
// collisionTest uses nearPlane.s
|
|
1773
|
+
|
|
1774
|
+
this._updateNearPlaneCorners();
|
|
1775
|
+
// Target cannot move outside of this box
|
|
1776
|
+
this._boundary = new Box3(new Vector3(-Infinity, -Infinity, -Infinity), new Vector3(Infinity, Infinity, Infinity));
|
|
1777
|
+
// reset
|
|
1778
|
+
this._cameraUp0 = this._camera.up.clone();
|
|
1779
|
+
this._target0 = this._target.clone();
|
|
1780
|
+
this._position0 = this._camera.position.clone();
|
|
1781
|
+
this._zoom0 = this._zoom;
|
|
1782
|
+
this._focalOffset0 = this._focalOffset.clone();
|
|
1783
|
+
// configs
|
|
1784
|
+
this.mouseButtons = {
|
|
1785
|
+
left: ACTION.ROTATE,
|
|
1786
|
+
middle: ACTION.DOLLY,
|
|
1787
|
+
right: ACTION.TRUCK,
|
|
1788
|
+
wheel: isPerspectiveCamera(this._camera) ? ACTION.DOLLY :
|
|
1789
|
+
isOrthographicCamera(this._camera) ? ACTION.ZOOM :
|
|
1790
|
+
ACTION.NONE,
|
|
1791
|
+
};
|
|
1792
|
+
this.touches = {
|
|
1793
|
+
one: ACTION.TOUCH_ROTATE,
|
|
1794
|
+
two: isPerspectiveCamera(this._camera) ? ACTION.TOUCH_DOLLY_TRUCK :
|
|
1795
|
+
isOrthographicCamera(this._camera) ? ACTION.TOUCH_ZOOM_TRUCK :
|
|
1796
|
+
ACTION.NONE,
|
|
1797
|
+
three: ACTION.TOUCH_TRUCK,
|
|
1798
|
+
};
|
|
1799
|
+
}
|
|
1800
|
+
|
|
1801
|
+
/**
|
|
1802
|
+
* Reset all rotation and position to defaults.
|
|
1803
|
+
* @param enableTransition
|
|
1804
|
+
* @category Methods
|
|
1805
|
+
*/
|
|
1806
|
+
reset(enableTransition = false) {
|
|
1807
|
+
if (!approxEquals(this._camera.up.x, this._cameraUp0.x) ||
|
|
1808
|
+
!approxEquals(this._camera.up.y, this._cameraUp0.y) ||
|
|
1809
|
+
!approxEquals(this._camera.up.z, this._cameraUp0.z)) {
|
|
1810
|
+
this._camera.up.copy(this._cameraUp0);
|
|
1811
|
+
const position = this.getPosition(_v3A);
|
|
1812
|
+
this.updateCameraUp();
|
|
1813
|
+
this.setPosition(position.x, position.y, position.z);
|
|
1814
|
+
}
|
|
1815
|
+
const promises = [
|
|
1816
|
+
this.setLookAt(this._position0.x, this._position0.y, this._position0.z, this._target0.x, this._target0.y, this._target0.z, enableTransition),
|
|
1817
|
+
this.setFocalOffset(this._focalOffset0.x, this._focalOffset0.y, this._focalOffset0.z, enableTransition),
|
|
1818
|
+
this.zoomTo(this._zoom0, enableTransition),
|
|
1819
|
+
];
|
|
1820
|
+
|
|
1821
|
+
return Promise.all(promises);
|
|
1822
|
+
}
|
|
1823
|
+
/**
|
|
1824
|
+
* Set current camera position as the default position.
|
|
1825
|
+
* @category Methods
|
|
1826
|
+
*/
|
|
1827
|
+
saveState() {
|
|
1828
|
+
this._cameraUp0.copy(this._camera.up);
|
|
1829
|
+
this.getTarget(this._target0);
|
|
1830
|
+
this.getPosition(this._position0);
|
|
1831
|
+
this._zoom0 = this._zoom;
|
|
1832
|
+
this._focalOffset0.copy(this._focalOffset);
|
|
1833
|
+
}
|
|
1834
|
+
/**
|
|
1835
|
+
* Sync camera-up direction.
|
|
1836
|
+
* When camera-up vector is changed, `.updateCameraUp()` must be called.
|
|
1837
|
+
* @category Methods
|
|
1838
|
+
*/
|
|
1839
|
+
updateCameraUp() {
|
|
1840
|
+
this._yAxisUpSpace.setFromUnitVectors(this._camera.up, _AXIS_Y);
|
|
1841
|
+
this._yAxisUpSpaceInverse.copy(this._yAxisUpSpace).invert();
|
|
1842
|
+
}
|
|
1843
|
+
/**
|
|
1844
|
+
* Apply current camera-up direction to the camera.
|
|
1845
|
+
* The orbit system will be re-initialized with the current position.
|
|
1846
|
+
* @category Methods
|
|
1847
|
+
*/
|
|
1848
|
+
applyCameraUp() {
|
|
1849
|
+
const cameraDirection = _v3A.subVectors(this._target, this._camera.position).normalize();
|
|
1850
|
+
// So first find the vector off to the side, orthogonal to both this.object.up and
|
|
1851
|
+
// the "view" vector.
|
|
1852
|
+
const side = _v3B.crossVectors(cameraDirection, this._camera.up);
|
|
1853
|
+
// Then find the vector orthogonal to both this "side" vector and the "view" vector.
|
|
1854
|
+
// This vector will be the new "up" vector.
|
|
1855
|
+
this._camera.up.crossVectors(side, cameraDirection).normalize();
|
|
1856
|
+
this._camera.updateMatrixWorld();
|
|
1857
|
+
const position = this.getPosition(_v3A);
|
|
1858
|
+
this.updateCameraUp();
|
|
1859
|
+
this.setPosition(position.x, position.y, position.z);
|
|
1860
|
+
}
|
|
1861
|
+
/**
|
|
1862
|
+
* Update camera position and directions.
|
|
1863
|
+
* This should be called in your tick loop every time, and returns true if re-rendering is needed.
|
|
1864
|
+
* @param delta
|
|
1865
|
+
* @returns updated
|
|
1866
|
+
* @category Methods
|
|
1867
|
+
*/
|
|
1868
|
+
update(delta: number) {
|
|
1869
|
+
const deltaTheta = this._sphericalEnd.theta - this._spherical.theta;
|
|
1870
|
+
const deltaPhi = this._sphericalEnd.phi - this._spherical.phi;
|
|
1871
|
+
const deltaRadius = this._sphericalEnd.radius - this._spherical.radius;
|
|
1872
|
+
const deltaTarget = _deltaTarget.subVectors(this._targetEnd, this._target);
|
|
1873
|
+
const deltaOffset = _deltaOffset.subVectors(this._focalOffsetEnd, this._focalOffset);
|
|
1874
|
+
const deltaZoom = this._zoomEnd - this._zoom;
|
|
1875
|
+
// update theta
|
|
1876
|
+
if (approxZero(deltaTheta)) {
|
|
1877
|
+
this._thetaVelocity.value = 0;
|
|
1878
|
+
this._spherical.theta = this._sphericalEnd.theta;
|
|
1879
|
+
}
|
|
1880
|
+
else {
|
|
1881
|
+
const smoothTime = this._isUserControllingRotate ? this.draggingSmoothTime : this.smoothTime;
|
|
1882
|
+
this._spherical.theta = smoothDamp(this._spherical.theta, this._sphericalEnd.theta, this._thetaVelocity, smoothTime, Infinity, delta);
|
|
1883
|
+
this._needsUpdate = true;
|
|
1884
|
+
}
|
|
1885
|
+
// update phi
|
|
1886
|
+
if (approxZero(deltaPhi)) {
|
|
1887
|
+
this._phiVelocity.value = 0;
|
|
1888
|
+
this._spherical.phi = this._sphericalEnd.phi;
|
|
1889
|
+
}
|
|
1890
|
+
else {
|
|
1891
|
+
const smoothTime = this._isUserControllingRotate ? this.draggingSmoothTime : this.smoothTime;
|
|
1892
|
+
this._spherical.phi = smoothDamp(this._spherical.phi, this._sphericalEnd.phi, this._phiVelocity, smoothTime, Infinity, delta);
|
|
1893
|
+
this._needsUpdate = true;
|
|
1894
|
+
}
|
|
1895
|
+
// update distance
|
|
1896
|
+
if (approxZero(deltaRadius)) {
|
|
1897
|
+
this._radiusVelocity.value = 0;
|
|
1898
|
+
this._spherical.radius = this._sphericalEnd.radius;
|
|
1899
|
+
}
|
|
1900
|
+
else {
|
|
1901
|
+
const smoothTime = this._isUserControllingDolly ? this.draggingSmoothTime : this.smoothTime;
|
|
1902
|
+
this._spherical.radius = smoothDamp(this._spherical.radius, this._sphericalEnd.radius, this._radiusVelocity, smoothTime, this.maxSpeed, delta);
|
|
1903
|
+
this._needsUpdate = true;
|
|
1904
|
+
}
|
|
1905
|
+
// update target position
|
|
1906
|
+
if (approxZero(deltaTarget.x) && approxZero(deltaTarget.y) && approxZero(deltaTarget.z)) {
|
|
1907
|
+
this._targetVelocity.set(0, 0, 0);
|
|
1908
|
+
this._target.copy(this._targetEnd);
|
|
1909
|
+
}
|
|
1910
|
+
else {
|
|
1911
|
+
const smoothTime = this._isUserControllingTruck ? this.draggingSmoothTime : this.smoothTime;
|
|
1912
|
+
smoothDampVec3(this._target, this._targetEnd, this._targetVelocity, smoothTime, this.maxSpeed, delta, this._target);
|
|
1913
|
+
this._needsUpdate = true;
|
|
1914
|
+
}
|
|
1915
|
+
// update focalOffset
|
|
1916
|
+
if (approxZero(deltaOffset.x) && approxZero(deltaOffset.y) && approxZero(deltaOffset.z)) {
|
|
1917
|
+
this._focalOffsetVelocity.set(0, 0, 0);
|
|
1918
|
+
this._focalOffset.copy(this._focalOffsetEnd);
|
|
1919
|
+
}
|
|
1920
|
+
else {
|
|
1921
|
+
const smoothTime = this._isUserControllingOffset ? this.draggingSmoothTime : this.smoothTime;
|
|
1922
|
+
smoothDampVec3(this._focalOffset, this._focalOffsetEnd, this._focalOffsetVelocity, smoothTime, this.maxSpeed, delta, this._focalOffset);
|
|
1923
|
+
this._needsUpdate = true;
|
|
1924
|
+
}
|
|
1925
|
+
// update zoom
|
|
1926
|
+
if (approxZero(deltaZoom)) {
|
|
1927
|
+
this._zoomVelocity.value = 0;
|
|
1928
|
+
this._zoom = this._zoomEnd;
|
|
1929
|
+
}
|
|
1930
|
+
else {
|
|
1931
|
+
const smoothTime = this._isUserControllingZoom ? this.draggingSmoothTime : this.smoothTime;
|
|
1932
|
+
this._zoom = smoothDamp(this._zoom, this._zoomEnd, this._zoomVelocity, smoothTime, Infinity, delta);
|
|
1933
|
+
}
|
|
1934
|
+
if (this.dollyToCursor) {
|
|
1935
|
+
if (isPerspectiveCamera(this._camera) && this._changedDolly !== 0) {
|
|
1936
|
+
const dollyControlAmount = this._spherical.radius - this._lastDistance;
|
|
1937
|
+
const camera = this._camera;
|
|
1938
|
+
const cameraDirection = this._getCameraDirection(_cameraDirection);
|
|
1939
|
+
const planeX = _v3A.copy(cameraDirection).cross(camera.up).normalize();
|
|
1940
|
+
if (planeX.lengthSq() === 0)
|
|
1941
|
+
planeX.x = 1.0;
|
|
1942
|
+
const planeY = _v3B.crossVectors(planeX, cameraDirection);
|
|
1943
|
+
const worldToScreen = this._sphericalEnd.radius * Math.tan(camera.getEffectiveFOV() * DEG2RAD * 0.5);
|
|
1944
|
+
const prevRadius = this._sphericalEnd.radius - dollyControlAmount;
|
|
1945
|
+
const lerpRatio = (prevRadius - this._sphericalEnd.radius) / this._sphericalEnd.radius;
|
|
1946
|
+
const cursor = _v3C.copy(this._targetEnd)
|
|
1947
|
+
.add(planeX.multiplyScalar(this._dollyControlCoord.x * worldToScreen * camera.aspect))
|
|
1948
|
+
.add(planeY.multiplyScalar(this._dollyControlCoord.y * worldToScreen));
|
|
1949
|
+
const newTargetEnd = _v3A.copy(this._targetEnd).lerp(cursor, lerpRatio);
|
|
1950
|
+
const isMin = this._lastDollyDirection === DOLLY_DIRECTION.IN && this._spherical.radius <= this.minDistance;
|
|
1951
|
+
const isMax = this._lastDollyDirection === DOLLY_DIRECTION.OUT && this.maxDistance <= this._spherical.radius;
|
|
1952
|
+
if (this.infinityDolly && (isMin || isMax)) {
|
|
1953
|
+
this._sphericalEnd.radius -= dollyControlAmount;
|
|
1954
|
+
this._spherical.radius -= dollyControlAmount;
|
|
1955
|
+
const dollyAmount = _v3B.copy(cameraDirection).multiplyScalar(-dollyControlAmount);
|
|
1956
|
+
newTargetEnd.add(dollyAmount);
|
|
1957
|
+
}
|
|
1958
|
+
// target position may be moved beyond boundary.
|
|
1959
|
+
this._boundary.clampPoint(newTargetEnd, newTargetEnd);
|
|
1960
|
+
const targetEndDiff = _v3B.subVectors(newTargetEnd, this._targetEnd);
|
|
1961
|
+
this._targetEnd.copy(newTargetEnd);
|
|
1962
|
+
this._target.add(targetEndDiff);
|
|
1963
|
+
this._changedDolly -= dollyControlAmount;
|
|
1964
|
+
if (approxZero(this._changedDolly))
|
|
1965
|
+
this._changedDolly = 0;
|
|
1966
|
+
}
|
|
1967
|
+
else if (isOrthographicCamera(this._camera) && this._changedZoom !== 0) {
|
|
1968
|
+
const dollyControlAmount = this._zoom - this._lastZoom;
|
|
1969
|
+
const camera = this._camera;
|
|
1970
|
+
const worldCursorPosition = _v3A.set(this._dollyControlCoord.x, this._dollyControlCoord.y, (camera.near + camera.far) / (camera.near - camera.far)).unproject(camera);
|
|
1971
|
+
const quaternion = _v3B.set(0, 0, -1).applyQuaternion(camera.quaternion);
|
|
1972
|
+
const cursor = _v3C.copy(worldCursorPosition).add(quaternion.multiplyScalar(-worldCursorPosition.dot(camera.up)));
|
|
1973
|
+
const prevZoom = this._zoom - dollyControlAmount;
|
|
1974
|
+
const lerpRatio = -(prevZoom - this._zoom) / this._zoom;
|
|
1975
|
+
// find the "distance" (aka plane constant in three.js) of Plane
|
|
1976
|
+
// from a given position (this._targetEnd) and normal vector (cameraDirection)
|
|
1977
|
+
// https://www.maplesoft.com/support/help/maple/view.aspx?path=MathApps%2FEquationOfAPlaneNormal#bkmrk0
|
|
1978
|
+
const cameraDirection = this._getCameraDirection(_cameraDirection);
|
|
1979
|
+
const prevPlaneConstant = this._targetEnd.dot(cameraDirection);
|
|
1980
|
+
const newTargetEnd = _v3A.copy(this._targetEnd).lerp(cursor, lerpRatio);
|
|
1981
|
+
const newPlaneConstant = newTargetEnd.dot(cameraDirection);
|
|
1982
|
+
// Pull back the camera depth that has moved, to be the camera stationary as zoom
|
|
1983
|
+
const pullBack = cameraDirection.multiplyScalar(newPlaneConstant - prevPlaneConstant);
|
|
1984
|
+
newTargetEnd.sub(pullBack);
|
|
1985
|
+
// target position may be moved beyond boundary.
|
|
1986
|
+
this._boundary.clampPoint(newTargetEnd, newTargetEnd);
|
|
1987
|
+
const targetEndDiff = _v3B.subVectors(newTargetEnd, this._targetEnd);
|
|
1988
|
+
this._targetEnd.copy(newTargetEnd);
|
|
1989
|
+
this._target.add(targetEndDiff);
|
|
1990
|
+
// this._target.copy( this._targetEnd );
|
|
1991
|
+
this._changedZoom -= dollyControlAmount;
|
|
1992
|
+
if (approxZero(this._changedZoom))
|
|
1993
|
+
this._changedZoom = 0;
|
|
1994
|
+
}
|
|
1995
|
+
}
|
|
1996
|
+
if (this._camera.zoom !== this._zoom) {
|
|
1997
|
+
this._camera.zoom = this._zoom;
|
|
1998
|
+
this._camera.updateProjectionMatrix();
|
|
1999
|
+
this._updateNearPlaneCorners();
|
|
2000
|
+
this._needsUpdate = true;
|
|
2001
|
+
}
|
|
2002
|
+
this._dragNeedsUpdate = true;
|
|
2003
|
+
// collision detection
|
|
2004
|
+
const maxDistance = this._collisionTest();
|
|
2005
|
+
this._spherical.radius = Math.min(this._spherical.radius, maxDistance);
|
|
2006
|
+
// decompose spherical to the camera position
|
|
2007
|
+
this._spherical.makeSafe();
|
|
2008
|
+
this._camera.position.setFromSpherical(this._spherical).applyQuaternion(this._yAxisUpSpaceInverse).add(this._target);
|
|
2009
|
+
this._camera.lookAt(this._target);
|
|
2010
|
+
// set offset after the orbit movement
|
|
2011
|
+
const affectOffset = !approxZero(this._focalOffset.x) ||
|
|
2012
|
+
!approxZero(this._focalOffset.y) ||
|
|
2013
|
+
!approxZero(this._focalOffset.z);
|
|
2014
|
+
if (affectOffset) {
|
|
2015
|
+
this._camera.updateMatrixWorld();
|
|
2016
|
+
_xColumn.setFromMatrixColumn(this._camera.matrix, 0);
|
|
2017
|
+
_yColumn.setFromMatrixColumn(this._camera.matrix, 1);
|
|
2018
|
+
_zColumn.setFromMatrixColumn(this._camera.matrix, 2);
|
|
2019
|
+
_xColumn.multiplyScalar(this._focalOffset.x);
|
|
2020
|
+
_yColumn.multiplyScalar(-this._focalOffset.y);
|
|
2021
|
+
_zColumn.multiplyScalar(this._focalOffset.z); // notice: z-offset will not affect in Orthographic.
|
|
2022
|
+
_v3A.copy(_xColumn).add(_yColumn).add(_zColumn);
|
|
2023
|
+
this._camera.position.add(_v3A);
|
|
2024
|
+
}
|
|
2025
|
+
if (this._boundaryEnclosesCamera) {
|
|
2026
|
+
this._encloseToBoundary(this._camera.position.copy(this._target), _v3A.setFromSpherical(this._spherical).applyQuaternion(this._yAxisUpSpaceInverse), 1.0);
|
|
2027
|
+
}
|
|
2028
|
+
const updated = this._needsUpdate;
|
|
2029
|
+
if (updated && !this._updatedLastTime) {
|
|
2030
|
+
this._hasRested = false;
|
|
2031
|
+
this.dispatchEvent({ type: 'wake' });
|
|
2032
|
+
this.dispatchEvent({ type: 'update' });
|
|
2033
|
+
}
|
|
2034
|
+
else if (updated) {
|
|
2035
|
+
this.dispatchEvent({ type: 'update' });
|
|
2036
|
+
if (approxZero(deltaTheta, this.restThreshold) &&
|
|
2037
|
+
approxZero(deltaPhi, this.restThreshold) &&
|
|
2038
|
+
approxZero(deltaRadius, this.restThreshold) &&
|
|
2039
|
+
approxZero(deltaTarget.x, this.restThreshold) &&
|
|
2040
|
+
approxZero(deltaTarget.y, this.restThreshold) &&
|
|
2041
|
+
approxZero(deltaTarget.z, this.restThreshold) &&
|
|
2042
|
+
approxZero(deltaOffset.x, this.restThreshold) &&
|
|
2043
|
+
approxZero(deltaOffset.y, this.restThreshold) &&
|
|
2044
|
+
approxZero(deltaOffset.z, this.restThreshold) &&
|
|
2045
|
+
approxZero(deltaZoom, this.restThreshold) &&
|
|
2046
|
+
!this._hasRested) {
|
|
2047
|
+
this._hasRested = true;
|
|
2048
|
+
this.dispatchEvent({ type: 'rest' });
|
|
2049
|
+
}
|
|
2050
|
+
}
|
|
2051
|
+
else if (!updated && this._updatedLastTime) {
|
|
2052
|
+
this.dispatchEvent({ type: 'sleep' });
|
|
2053
|
+
}
|
|
2054
|
+
this._lastDistance = this._spherical.radius;
|
|
2055
|
+
this._lastZoom = this._zoom;
|
|
2056
|
+
this._updatedLastTime = updated;
|
|
2057
|
+
this._needsUpdate = false;
|
|
2058
|
+
return updated;
|
|
2059
|
+
}
|
|
2060
|
+
/**
|
|
2061
|
+
* Get all state in JSON string
|
|
2062
|
+
* @category Methods
|
|
2063
|
+
*/
|
|
2064
|
+
toJSON() {
|
|
2065
|
+
return JSON.stringify({
|
|
2066
|
+
enabled: this._enabled,
|
|
2067
|
+
minDistance: this.minDistance,
|
|
2068
|
+
maxDistance: infinityToMaxNumber(this.maxDistance),
|
|
2069
|
+
minZoom: this.minZoom,
|
|
2070
|
+
maxZoom: infinityToMaxNumber(this.maxZoom),
|
|
2071
|
+
minPolarAngle: this.minPolarAngle,
|
|
2072
|
+
maxPolarAngle: infinityToMaxNumber(this.maxPolarAngle),
|
|
2073
|
+
minAzimuthAngle: infinityToMaxNumber(this.minAzimuthAngle),
|
|
2074
|
+
maxAzimuthAngle: infinityToMaxNumber(this.maxAzimuthAngle),
|
|
2075
|
+
smoothTime: this.smoothTime,
|
|
2076
|
+
draggingSmoothTime: this.draggingSmoothTime,
|
|
2077
|
+
dollySpeed: this.dollySpeed,
|
|
2078
|
+
truckSpeed: this.truckSpeed,
|
|
2079
|
+
dollyToCursor: this.dollyToCursor,
|
|
2080
|
+
verticalDragToForward: this.verticalDragToForward,
|
|
2081
|
+
target: this._targetEnd.toArray(),
|
|
2082
|
+
position: _v3A.setFromSpherical(this._sphericalEnd).add(this._targetEnd).toArray(),
|
|
2083
|
+
zoom: this._zoomEnd,
|
|
2084
|
+
focalOffset: this._focalOffsetEnd.toArray(),
|
|
2085
|
+
target0: this._target0.toArray(),
|
|
2086
|
+
position0: this._position0.toArray(),
|
|
2087
|
+
zoom0: this._zoom0,
|
|
2088
|
+
focalOffset0: this._focalOffset0.toArray(),
|
|
2089
|
+
});
|
|
2090
|
+
}
|
|
2091
|
+
/**
|
|
2092
|
+
* Reproduce the control state with JSON. enableTransition is where anim or not in a boolean.
|
|
2093
|
+
* @param json
|
|
2094
|
+
* @param enableTransition
|
|
2095
|
+
* @category Methods
|
|
2096
|
+
*/
|
|
2097
|
+
fromJSON(json: string, enableTransition = false) {
|
|
2098
|
+
const obj = JSON.parse(json);
|
|
2099
|
+
this.enabled = obj.enabled;
|
|
2100
|
+
this.minDistance = obj.minDistance;
|
|
2101
|
+
this.maxDistance = maxNumberToInfinity(obj.maxDistance);
|
|
2102
|
+
this.minZoom = obj.minZoom;
|
|
2103
|
+
this.maxZoom = maxNumberToInfinity(obj.maxZoom);
|
|
2104
|
+
this.minPolarAngle = obj.minPolarAngle;
|
|
2105
|
+
this.maxPolarAngle = maxNumberToInfinity(obj.maxPolarAngle);
|
|
2106
|
+
this.minAzimuthAngle = maxNumberToInfinity(obj.minAzimuthAngle);
|
|
2107
|
+
this.maxAzimuthAngle = maxNumberToInfinity(obj.maxAzimuthAngle);
|
|
2108
|
+
this.smoothTime = obj.smoothTime;
|
|
2109
|
+
this.draggingSmoothTime = obj.draggingSmoothTime;
|
|
2110
|
+
this.dollySpeed = obj.dollySpeed;
|
|
2111
|
+
this.truckSpeed = obj.truckSpeed;
|
|
2112
|
+
this.dollyToCursor = obj.dollyToCursor;
|
|
2113
|
+
this.verticalDragToForward = obj.verticalDragToForward;
|
|
2114
|
+
this._target0.fromArray(obj.target0);
|
|
2115
|
+
this._position0.fromArray(obj.position0);
|
|
2116
|
+
this._zoom0 = obj.zoom0;
|
|
2117
|
+
this._focalOffset0.fromArray(obj.focalOffset0);
|
|
2118
|
+
this.moveTo(obj.target[0], obj.target[1], obj.target[2], enableTransition);
|
|
2119
|
+
_sphericalA.setFromVector3(_v3A.fromArray(obj.position).sub(this._targetEnd).applyQuaternion(this._yAxisUpSpace));
|
|
2120
|
+
this.rotateTo(_sphericalA.theta, _sphericalA.phi, enableTransition);
|
|
2121
|
+
this.dollyTo(_sphericalA.radius, enableTransition);
|
|
2122
|
+
this.zoomTo(obj.zoom, enableTransition);
|
|
2123
|
+
this.setFocalOffset(obj.focalOffset[0], obj.focalOffset[1], obj.focalOffset[2], enableTransition);
|
|
2124
|
+
this._needsUpdate = true;
|
|
2125
|
+
}
|
|
2126
|
+
/**
|
|
2127
|
+
* Attach all internal event handlers to enable drag control.
|
|
2128
|
+
* @category Methods
|
|
2129
|
+
*/
|
|
2130
|
+
connect(domElement: HTMLElement) {
|
|
2131
|
+
if (this._domElement) {
|
|
2132
|
+
console.warn('camera-controls is already connected.');
|
|
2133
|
+
return;
|
|
2134
|
+
}
|
|
2135
|
+
domElement.setAttribute('data-camera-controls-version', VERSION);
|
|
2136
|
+
this._addAllEventListeners(domElement);
|
|
2137
|
+
this._getClientRect(this._elementRect);
|
|
2138
|
+
|
|
2139
|
+
// this.update(0);
|
|
2140
|
+
}
|
|
2141
|
+
/**
|
|
2142
|
+
* Detach all internal event handlers to disable drag control.
|
|
2143
|
+
*/
|
|
2144
|
+
disconnect() {
|
|
2145
|
+
this.cancel();
|
|
2146
|
+
this._removeAllEventListeners();
|
|
2147
|
+
if (this._domElement) {
|
|
2148
|
+
this._domElement.removeAttribute('data-camera-controls-version');
|
|
2149
|
+
//@ts-ignore
|
|
2150
|
+
this._domElement = undefined;
|
|
2151
|
+
}
|
|
2152
|
+
}
|
|
2153
|
+
/**
|
|
2154
|
+
* Dispose the cameraControls instance itself, remove all eventListeners.
|
|
2155
|
+
* @category Methods
|
|
2156
|
+
*/
|
|
2157
|
+
dispose() {
|
|
2158
|
+
// remove all user event listeners
|
|
2159
|
+
this.removeAllEventListeners();
|
|
2160
|
+
// remove all internal event listeners
|
|
2161
|
+
this.disconnect();
|
|
2162
|
+
}
|
|
2163
|
+
// it's okay to expose public though
|
|
2164
|
+
_getTargetDirection(out: Vector3) {
|
|
2165
|
+
// divide by distance to normalize, lighter than `Vector3.prototype.normalize()`
|
|
2166
|
+
return out.setFromSpherical(this._spherical).divideScalar(this._spherical.radius).applyQuaternion(this._yAxisUpSpaceInverse);
|
|
2167
|
+
}
|
|
2168
|
+
// it's okay to expose public though
|
|
2169
|
+
_getCameraDirection(out: Vector3) {
|
|
2170
|
+
return this._getTargetDirection(out).negate();
|
|
2171
|
+
}
|
|
2172
|
+
_findPointerById(pointerId: number) {
|
|
2173
|
+
return this._activePointers.find((activePointer) => activePointer.pointerId === pointerId);
|
|
2174
|
+
}
|
|
2175
|
+
_findPointerByMouseButton(mouseButton: number) {
|
|
2176
|
+
return this._activePointers.find((activePointer) => activePointer.mouseButton === mouseButton);
|
|
2177
|
+
}
|
|
2178
|
+
_disposePointer(pointer: Pointer) {
|
|
2179
|
+
this._activePointers.splice(this._activePointers.indexOf(pointer), 1);
|
|
2180
|
+
}
|
|
2181
|
+
_encloseToBoundary(position: Vector3, offset: Vector3, friction: number) {
|
|
2182
|
+
const offsetLength2 = offset.lengthSq();
|
|
2183
|
+
if (offsetLength2 === 0.0) { // sanity check
|
|
2184
|
+
return position;
|
|
2185
|
+
}
|
|
2186
|
+
// See: https://twitter.com/FMS_Cat/status/1106508958640988161
|
|
2187
|
+
const newTarget = _v3B.copy(offset).add(position); // target
|
|
2188
|
+
const clampedTarget = this._boundary.clampPoint(newTarget, _v3C); // clamped target
|
|
2189
|
+
const deltaClampedTarget = clampedTarget.sub(newTarget); // newTarget -> clampedTarget
|
|
2190
|
+
const deltaClampedTargetLength2 = deltaClampedTarget.lengthSq(); // squared length of deltaClampedTarget
|
|
2191
|
+
if (deltaClampedTargetLength2 === 0.0) { // when the position doesn't have to be clamped
|
|
2192
|
+
return position.add(offset);
|
|
2193
|
+
}
|
|
2194
|
+
else if (deltaClampedTargetLength2 === offsetLength2) { // when the position is completely stuck
|
|
2195
|
+
return position;
|
|
2196
|
+
}
|
|
2197
|
+
else if (friction === 0.0) {
|
|
2198
|
+
return position.add(offset).add(deltaClampedTarget);
|
|
2199
|
+
}
|
|
2200
|
+
else {
|
|
2201
|
+
const offsetFactor = 1.0 + friction * deltaClampedTargetLength2 / offset.dot(deltaClampedTarget);
|
|
2202
|
+
return position
|
|
2203
|
+
.add(_v3B.copy(offset).multiplyScalar(offsetFactor))
|
|
2204
|
+
.add(deltaClampedTarget.multiplyScalar(1.0 - friction));
|
|
2205
|
+
}
|
|
2206
|
+
}
|
|
2207
|
+
_updateNearPlaneCorners() {
|
|
2208
|
+
if (isPerspectiveCamera(this._camera)) {
|
|
2209
|
+
const camera = this._camera;
|
|
2210
|
+
const near = camera.near;
|
|
2211
|
+
const fov = camera.getEffectiveFOV() * DEG2RAD;
|
|
2212
|
+
const heightHalf = Math.tan(fov * 0.5) * near; // near plain half height
|
|
2213
|
+
const widthHalf = heightHalf * camera.aspect; // near plain half width
|
|
2214
|
+
this._nearPlaneCorners[0].set(-widthHalf, -heightHalf, 0);
|
|
2215
|
+
this._nearPlaneCorners[1].set(widthHalf, -heightHalf, 0);
|
|
2216
|
+
this._nearPlaneCorners[2].set(widthHalf, heightHalf, 0);
|
|
2217
|
+
this._nearPlaneCorners[3].set(-widthHalf, heightHalf, 0);
|
|
2218
|
+
}
|
|
2219
|
+
else if (isOrthographicCamera(this._camera)) {
|
|
2220
|
+
const camera = this._camera;
|
|
2221
|
+
const zoomInv = 1 / camera.zoom;
|
|
2222
|
+
const left = camera.left * zoomInv;
|
|
2223
|
+
const right = camera.right * zoomInv;
|
|
2224
|
+
const top = camera.top * zoomInv;
|
|
2225
|
+
const bottom = camera.bottom * zoomInv;
|
|
2226
|
+
this._nearPlaneCorners[0].set(left, top, 0);
|
|
2227
|
+
this._nearPlaneCorners[1].set(right, top, 0);
|
|
2228
|
+
this._nearPlaneCorners[2].set(right, bottom, 0);
|
|
2229
|
+
this._nearPlaneCorners[3].set(left, bottom, 0);
|
|
2230
|
+
}
|
|
2231
|
+
}
|
|
2232
|
+
// lateUpdate
|
|
2233
|
+
_collisionTest() {
|
|
2234
|
+
let distance = Infinity;
|
|
2235
|
+
const hasCollider = this.colliderMeshes.length >= 1;
|
|
2236
|
+
if (!hasCollider)
|
|
2237
|
+
return distance;
|
|
2238
|
+
if (notSupportedInOrthographicCamera(this._camera, '_collisionTest'))
|
|
2239
|
+
return distance;
|
|
2240
|
+
const rayDirection = this._getTargetDirection(_cameraDirection);
|
|
2241
|
+
_rotationMatrix.lookAt(_ORIGIN, rayDirection, this._camera.up);
|
|
2242
|
+
for (let i = 0; i < 4; i++) {
|
|
2243
|
+
const nearPlaneCorner = _v3B.copy(this._nearPlaneCorners[i]);
|
|
2244
|
+
nearPlaneCorner.applyMatrix4(_rotationMatrix);
|
|
2245
|
+
const origin = _v3C.addVectors(this._target, nearPlaneCorner);
|
|
2246
|
+
_raycaster.set(origin, rayDirection);
|
|
2247
|
+
_raycaster.far = this._spherical.radius + 1;
|
|
2248
|
+
const intersects = _raycaster.intersectObjects(this.colliderMeshes);
|
|
2249
|
+
if (intersects.length !== 0 && intersects[0].distance < distance) {
|
|
2250
|
+
distance = intersects[0].distance;
|
|
2251
|
+
}
|
|
2252
|
+
}
|
|
2253
|
+
return distance;
|
|
2254
|
+
}
|
|
2255
|
+
/**
|
|
2256
|
+
* Get its client rect and package into given `DOMRect` .
|
|
2257
|
+
*/
|
|
2258
|
+
_getClientRect(target: DOMRect) {
|
|
2259
|
+
if (!this._domElement)
|
|
2260
|
+
return;
|
|
2261
|
+
const rect = this._domElement.getBoundingClientRect();
|
|
2262
|
+
target.x = rect.left;
|
|
2263
|
+
target.y = rect.top;
|
|
2264
|
+
if (this._viewport) {
|
|
2265
|
+
target.x += this._viewport.x;
|
|
2266
|
+
target.y += rect.height - this._viewport.w - this._viewport.y;
|
|
2267
|
+
target.width = this._viewport.z;
|
|
2268
|
+
target.height = this._viewport.w;
|
|
2269
|
+
}
|
|
2270
|
+
else {
|
|
2271
|
+
target.width = rect.width;
|
|
2272
|
+
target.height = rect.height;
|
|
2273
|
+
}
|
|
2274
|
+
return target;
|
|
2275
|
+
}
|
|
2276
|
+
_createOnRestPromise(resolveImmediately: boolean) {
|
|
2277
|
+
if (resolveImmediately)
|
|
2278
|
+
return Promise.resolve();
|
|
2279
|
+
this._hasRested = false;
|
|
2280
|
+
this.dispatchEvent({ type: 'transitionstart' });
|
|
2281
|
+
return new Promise<void>((resolve) => {
|
|
2282
|
+
const onResolve = () => {
|
|
2283
|
+
this.removeEventListener('rest', onResolve);
|
|
2284
|
+
resolve();
|
|
2285
|
+
};
|
|
2286
|
+
this.addEventListener('rest', onResolve);
|
|
2287
|
+
});
|
|
2288
|
+
}
|
|
2289
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2290
|
+
_addAllEventListeners(_domElement: HTMLElement) { }
|
|
2291
|
+
_removeAllEventListeners() { }
|
|
2292
|
+
/**
|
|
2293
|
+
* backward compatible
|
|
2294
|
+
* @deprecated use smoothTime (in seconds) instead
|
|
2295
|
+
* @category Properties
|
|
2296
|
+
*/
|
|
2297
|
+
get dampingFactor() {
|
|
2298
|
+
console.warn('.dampingFactor has been deprecated. use smoothTime (in seconds) instead.');
|
|
2299
|
+
return 0;
|
|
2300
|
+
}
|
|
2301
|
+
/**
|
|
2302
|
+
* backward compatible
|
|
2303
|
+
* @deprecated use smoothTime (in seconds) instead
|
|
2304
|
+
* @category Properties
|
|
2305
|
+
*/
|
|
2306
|
+
set dampingFactor(_) {
|
|
2307
|
+
console.warn('.dampingFactor has been deprecated. use smoothTime (in seconds) instead.');
|
|
2308
|
+
}
|
|
2309
|
+
/**
|
|
2310
|
+
* backward compatible
|
|
2311
|
+
* @deprecated use draggingSmoothTime (in seconds) instead
|
|
2312
|
+
* @category Properties
|
|
2313
|
+
*/
|
|
2314
|
+
get draggingDampingFactor() {
|
|
2315
|
+
console.warn('.draggingDampingFactor has been deprecated. use draggingSmoothTime (in seconds) instead.');
|
|
2316
|
+
return 0;
|
|
2317
|
+
}
|
|
2318
|
+
/**
|
|
2319
|
+
* backward compatible
|
|
2320
|
+
* @deprecated use draggingSmoothTime (in seconds) instead
|
|
2321
|
+
* @category Properties
|
|
2322
|
+
*/
|
|
2323
|
+
set draggingDampingFactor(_) {
|
|
2324
|
+
console.warn('.draggingDampingFactor has been deprecated. use draggingSmoothTime (in seconds) instead.');
|
|
2325
|
+
}
|
|
2326
|
+
static createBoundingSphere(object3d: Object3D, out = new Sphere()) {
|
|
2327
|
+
const boundingSphere = out;
|
|
2328
|
+
const center = boundingSphere.center;
|
|
2329
|
+
_box3A.makeEmpty();
|
|
2330
|
+
// find the center
|
|
2331
|
+
object3d.traverseVisible((object) => {
|
|
2332
|
+
//@ts-ignore
|
|
2333
|
+
if (!object.isMesh)
|
|
2334
|
+
return;
|
|
2335
|
+
_box3A.expandByObject(object);
|
|
2336
|
+
});
|
|
2337
|
+
_box3A.getCenter(center);
|
|
2338
|
+
// find the radius
|
|
2339
|
+
let maxRadiusSq = 0;
|
|
2340
|
+
object3d.traverseVisible((object) => {
|
|
2341
|
+
//@ts-ignore
|
|
2342
|
+
if (!object.isMesh)
|
|
2343
|
+
return;
|
|
2344
|
+
const mesh = object;
|
|
2345
|
+
//@ts-ignore
|
|
2346
|
+
const geometry = mesh.geometry.clone();
|
|
2347
|
+
geometry.applyMatrix4(mesh.matrixWorld);
|
|
2348
|
+
const bufferGeometry = geometry;
|
|
2349
|
+
const position = bufferGeometry.attributes.position;
|
|
2350
|
+
for (let i = 0, l = position.count; i < l; i++) {
|
|
2351
|
+
_v3A.fromBufferAttribute(position, i);
|
|
2352
|
+
maxRadiusSq = Math.max(maxRadiusSq, center.distanceToSquared(_v3A));
|
|
2353
|
+
}
|
|
2354
|
+
});
|
|
2355
|
+
boundingSphere.radius = Math.sqrt(maxRadiusSq);
|
|
2356
|
+
return boundingSphere;
|
|
2357
|
+
}
|
|
2358
|
+
}
|
|
2359
|
+
|
|
2360
|
+
export { CameraControls };
|