| 1 | import |
| 2 | { |
| 3 | EventDispatcher, |
| 4 | MOUSE, |
| 5 | Quaternion, |
| 6 | Spherical, |
| 7 | TOUCH, |
| 8 | Vector2, |
| 9 | Vector3, |
| 10 | Plane, |
| 11 | Ray, |
| 12 | MathUtils |
| 13 | } from 'three'; |
| 14 | |
| 15 | // OrbitControls performs orbiting, dollying (zooming), and panning. |
| 16 | // Unlike TrackballControls, it maintains the "up" direction object.up (+Y by default). |
| 17 | // |
| 18 | // Orbit - left mouse / touch: one-finger move |
| 19 | // Zoom - middle mouse, or mousewheel / touch: two-finger spread or squish |
| 20 | // Pan - right mouse, or left mouse + ctrl/meta/shiftKey, or arrow keys / touch: two-finger move |
| 21 | |
| 22 | const _changeEvent = { type: 'change' }; |
| 23 | const _startEvent = { type: 'start' }; |
| 24 | const _endEvent = { type: 'end' }; |
| 25 | const _ray = new Ray(); |
| 26 | const _plane = new Plane(); |
| 27 | const TILT_LIMIT = Math.cos(70 * MathUtils.DEG2RAD); |
| 28 | |
| 29 | class OrbitControls extends EventDispatcher |
| 30 | { |
| 31 | |
| 32 | constructor(object, domElement) |
| 33 | { |
| 34 | |
| 35 | super(); |
| 36 | |
| 37 | this.object = object; |
| 38 | this.domElement = domElement; |
| 39 | this.domElement.style.touchAction = 'none'; // disable touch scroll |
| 40 | |
| 41 | // Set to false to disable this control |
| 42 | this.enabled = true; |
| 43 | |
| 44 | // "target" sets the location of focus, where the object orbits around |
| 45 | this.target = new Vector3(); |
| 46 | |
| 47 | // Sets the 3D cursor (similar to Blender), from which the maxTargetRadius takes effect |
| 48 | this.cursor = new Vector3(); |
| 49 | |
| 50 | // How far you can dolly in and out ( PerspectiveCamera only ) |
| 51 | this.minDistance = 0; |
| 52 | this.maxDistance = Infinity; |
| 53 | |
| 54 | // How far you can zoom in and out ( OrthographicCamera only ) |
| 55 | this.minZoom = 0; |
| 56 | this.maxZoom = Infinity; |
| 57 | |
| 58 | // Limit camera target within a spherical area around the cursor |
| 59 | this.minTargetRadius = 0; |
| 60 | this.maxTargetRadius = Infinity; |
| 61 | |
| 62 | // How far you can orbit vertically, upper and lower limits. |
| 63 | // Range is 0 to Math.PI radians. |
| 64 | this.minPolarAngle = 0; // radians |
| 65 | this.maxPolarAngle = Math.PI; // radians |
| 66 | |
| 67 | // How far you can orbit horizontally, upper and lower limits. |
| 68 | // If set, the interval [ min, max ] must be a sub-interval of [ - 2 PI, 2 PI ], with ( max - min < 2 PI ) |
| 69 | this.minAzimuthAngle = - Infinity; // radians |
| 70 | this.maxAzimuthAngle = Infinity; // radians |
| 71 | |
| 72 | // Set to true to enable damping (inertia) |
| 73 | // If damping is enabled, you must call controls.update() in your animation loop |
| 74 | this.enableDamping = false; |
| 75 | this.dampingFactor = 0.05; |
| 76 | |
| 77 | // This option actually enables dollying in and out; left as "zoom" for backwards compatibility. |
| 78 | // Set to false to disable zooming |
| 79 | this.enableZoom = true; |
| 80 | this.zoomSpeed = 1.0; |
| 81 | |
| 82 | // Set to false to disable rotating |
| 83 | this.enableRotate = true; |
| 84 | this.rotateSpeed = 1.0; |
| 85 | |
| 86 | // Set to false to disable panning |
| 87 | this.enablePan = true; |
| 88 | this.panSpeed = 1.0; |
| 89 | this.screenSpacePanning = true; // if false, pan orthogonal to world-space direction camera.up |
| 90 | this.keyPanSpeed = 7.0; // pixels moved per arrow key push |
| 91 | this.zoomToCursor = false; |
| 92 | |
| 93 | // Set to true to automatically rotate around the target |
| 94 | // If auto-rotate is enabled, you must call controls.update() in your animation loop |
| 95 | this.autoRotate = false; |
| 96 | this.autoRotateSpeed = 2.0; // 30 seconds per orbit when fps is 60 |
| 97 | |
| 98 | // The four arrow keys |
| 99 | this.keys = { LEFT: 'ArrowLeft', UP: 'ArrowUp', RIGHT: 'ArrowRight', BOTTOM: 'ArrowDown' }; |
| 100 | |
| 101 | // Mouse buttons |
| 102 | this.mouseButtons = { LEFT: MOUSE.ROTATE, MIDDLE: MOUSE.DOLLY, RIGHT: MOUSE.PAN }; |
| 103 | |
| 104 | // Touch fingers |
| 105 | this.touches = { ONE: TOUCH.ROTATE, TWO: TOUCH.DOLLY_PAN }; |
| 106 | |
| 107 | // for reset |
| 108 | this.target0 = this.target.clone(); |
| 109 | this.position0 = this.object.position.clone(); |
| 110 | this.zoom0 = this.object.zoom; |
| 111 | |
| 112 | // the target DOM element for key events |
| 113 | this._domElementKeyEvents = null; |
| 114 | |
| 115 | // |
| 116 | // public methods |
| 117 | // |
| 118 | |
| 119 | this.getPolarAngle = function () |
| 120 | { |
| 121 | |
| 122 | return spherical.phi; |
| 123 | |
| 124 | }; |
| 125 | |
| 126 | this.getAzimuthalAngle = function () |
| 127 | { |
| 128 | |
| 129 | return spherical.theta; |
| 130 | |
| 131 | }; |
| 132 | |
| 133 | this.getDistance = function () |
| 134 | { |
| 135 | |
| 136 | return this.object.position.distanceTo(this.target); |
| 137 | |
| 138 | }; |
| 139 | |
| 140 | this.listenToKeyEvents = function (domElement) |
| 141 | { |
| 142 | |
| 143 | domElement.addEventListener('keydown', onKeyDown); |
| 144 | this._domElementKeyEvents = domElement; |
| 145 | |
| 146 | }; |
| 147 | |
| 148 | this.stopListenToKeyEvents = function () |
| 149 | { |
| 150 | |
| 151 | this._domElementKeyEvents.removeEventListener('keydown', onKeyDown); |
| 152 | this._domElementKeyEvents = null; |
| 153 | |
| 154 | }; |
| 155 | |
| 156 | this.saveState = function () |
| 157 | { |
| 158 | |
| 159 | scope.target0.copy(scope.target); |
| 160 | scope.position0.copy(scope.object.position); |
| 161 | scope.zoom0 = scope.object.zoom; |
| 162 | |
| 163 | }; |
| 164 | |
| 165 | this.reset = function () |
| 166 | { |
| 167 | |
| 168 | scope.target.copy(scope.target0); |
| 169 | scope.object.position.copy(scope.position0); |
| 170 | scope.object.zoom = scope.zoom0; |
| 171 | |
| 172 | scope.object.updateProjectionMatrix(); |
| 173 | scope.dispatchEvent(_changeEvent); |
| 174 | |
| 175 | scope.update(); |
| 176 | |
| 177 | state = STATE.NONE; |
| 178 | |
| 179 | }; |
| 180 | |
| 181 | // this method is exposed, but perhaps it would be better if we can make it private... |
| 182 | this.update = function () |
| 183 | { |
| 184 | |
| 185 | const offset = new Vector3(); |
| 186 | |
| 187 | // so camera.up is the orbit axis |
| 188 | const quat = new Quaternion().setFromUnitVectors(object.up, new Vector3(0, 1, 0)); |
| 189 | const quatInverse = quat.clone().invert(); |
| 190 | |
| 191 | const lastPosition = new Vector3(); |
| 192 | const lastQuaternion = new Quaternion(); |
| 193 | const lastTargetPosition = new Vector3(); |
| 194 | |
| 195 | const twoPI = 2 * Math.PI; |
| 196 | |
| 197 | return function update (deltaTime = null) |
| 198 | { |
| 199 | |
| 200 | const position = scope.object.position; |
| 201 | |
| 202 | offset.copy(position).sub(scope.target); |
| 203 | |
| 204 | // rotate offset to "y-axis-is-up" space |
| 205 | offset.applyQuaternion(quat); |
| 206 | |
| 207 | // angle from z-axis around y-axis |
| 208 | spherical.setFromVector3(offset); |
| 209 | |
| 210 | if (scope.autoRotate && state === STATE.NONE) |
| 211 | { |
| 212 | |
| 213 | rotateLeft(getAutoRotationAngle(deltaTime)); |
| 214 | |
| 215 | } |
| 216 | |
| 217 | if (scope.enableDamping) |
| 218 | { |
| 219 | |
| 220 | spherical.theta += sphericalDelta.theta * scope.dampingFactor; |
| 221 | spherical.phi += sphericalDelta.phi * scope.dampingFactor; |
| 222 | |
| 223 | } else |
| 224 | { |
| 225 | |
| 226 | spherical.theta += sphericalDelta.theta; |
| 227 | spherical.phi += sphericalDelta.phi; |
| 228 | |
| 229 | } |
| 230 | |
| 231 | // restrict theta to be between desired limits |
| 232 | |
| 233 | let min = scope.minAzimuthAngle; |
| 234 | let max = scope.maxAzimuthAngle; |
| 235 | |
| 236 | if (isFinite(min) && isFinite(max)) |
| 237 | { |
| 238 | |
| 239 | if (min < - Math.PI) min += twoPI; else if (min > Math.PI) min -= twoPI; |
| 240 | |
| 241 | if (max < - Math.PI) max += twoPI; else if (max > Math.PI) max -= twoPI; |
| 242 | |
| 243 | if (min <= max) |
| 244 | { |
| 245 | |
| 246 | spherical.theta = Math.max(min, Math.min(max, spherical.theta)); |
| 247 | |
| 248 | } else |
| 249 | { |
| 250 | |
| 251 | spherical.theta = (spherical.theta > (min + max) / 2) ? |
| 252 | Math.max(min, spherical.theta) : |
| 253 | Math.min(max, spherical.theta); |
| 254 | |
| 255 | } |
| 256 | |
| 257 | } |
| 258 | |
| 259 | // restrict phi to be between desired limits |
| 260 | spherical.phi = Math.max(scope.minPolarAngle, Math.min(scope.maxPolarAngle, spherical.phi)); |
| 261 | |
| 262 | spherical.makeSafe(); |
| 263 | |
| 264 | |
| 265 | // move target to panned location |
| 266 | |
| 267 | if (scope.enableDamping === true) |
| 268 | { |
| 269 | |
| 270 | scope.target.addScaledVector(panOffset, scope.dampingFactor); |
| 271 | |
| 272 | } else |
| 273 | { |
| 274 | |
| 275 | scope.target.add(panOffset); |
| 276 | |
| 277 | } |
| 278 | |
| 279 | // Limit the target distance from the cursor to create a sphere around the center of interest |
| 280 | scope.target.sub(scope.cursor); |
| 281 | scope.target.clampLength(scope.minTargetRadius, scope.maxTargetRadius); |
| 282 | scope.target.add(scope.cursor); |
| 283 | |
| 284 | let zoomChanged = false; |
| 285 | // adjust the camera position based on zoom only if we're not zooming to the cursor or if it's an ortho camera |
| 286 | // we adjust zoom later in these cases |
| 287 | if (scope.zoomToCursor && performCursorZoom || scope.object.isOrthographicCamera) |
| 288 | { |
| 289 | |
| 290 | spherical.radius = clampDistance(spherical.radius); |
| 291 | |
| 292 | } else |
| 293 | { |
| 294 | |
| 295 | const prevRadius = spherical.radius; |
| 296 | spherical.radius = clampDistance(spherical.radius * scale); |
| 297 | zoomChanged = prevRadius != spherical.radius; |
| 298 | |
| 299 | } |
| 300 | |
| 301 | offset.setFromSpherical(spherical); |
| 302 | |
| 303 | // rotate offset back to "camera-up-vector-is-up" space |
| 304 | offset.applyQuaternion(quatInverse); |
| 305 | |
| 306 | position.copy(scope.target).add(offset); |
| 307 | |
| 308 | scope.object.lookAt(scope.target); |
| 309 | |
| 310 | if (scope.enableDamping === true) |
| 311 | { |
| 312 | |
| 313 | sphericalDelta.theta *= (1 - scope.dampingFactor); |
| 314 | sphericalDelta.phi *= (1 - scope.dampingFactor); |
| 315 | |
| 316 | panOffset.multiplyScalar(1 - scope.dampingFactor); |
| 317 | |
| 318 | } else |
| 319 | { |
| 320 | |
| 321 | sphericalDelta.set(0, 0, 0); |
| 322 | |
| 323 | panOffset.set(0, 0, 0); |
| 324 | |
| 325 | } |
| 326 | |
| 327 | // adjust camera position |
| 328 | if (scope.zoomToCursor && performCursorZoom) |
| 329 | { |
| 330 | |
| 331 | let newRadius = null; |
| 332 | if (scope.object.isPerspectiveCamera) |
| 333 | { |
| 334 | |
| 335 | // move the camera down the pointer ray |
| 336 | // this method avoids floating point error |
| 337 | const prevRadius = offset.length(); |
| 338 | newRadius = clampDistance(prevRadius * scale); |
| 339 | |
| 340 | const radiusDelta = prevRadius - newRadius; |
| 341 | scope.object.position.addScaledVector(dollyDirection, radiusDelta); |
| 342 | scope.object.updateMatrixWorld(); |
| 343 | |
| 344 | zoomChanged = !!radiusDelta; |
| 345 | |
| 346 | } else if (scope.object.isOrthographicCamera) |
| 347 | { |
| 348 | |
| 349 | // adjust the ortho camera position based on zoom changes |
| 350 | const mouseBefore = new Vector3(mouse.x, mouse.y, 0); |
| 351 | mouseBefore.unproject(scope.object); |
| 352 | |
| 353 | const prevZoom = scope.object.zoom; |
| 354 | scope.object.zoom = Math.max(scope.minZoom, Math.min(scope.maxZoom, scope.object.zoom / scale)); |
| 355 | scope.object.updateProjectionMatrix(); |
| 356 | |
| 357 | zoomChanged = prevZoom !== scope.object.zoom; |
| 358 | |
| 359 | const mouseAfter = new Vector3(mouse.x, mouse.y, 0); |
| 360 | mouseAfter.unproject(scope.object); |
| 361 | |
| 362 | scope.object.position.sub(mouseAfter).add(mouseBefore); |
| 363 | scope.object.updateMatrixWorld(); |
| 364 | |
| 365 | newRadius = offset.length(); |
| 366 | |
| 367 | } else |
| 368 | { |
| 369 | |
| 370 | console.warn('WARNING: OrbitControls.js encountered an unknown camera type - zoom to cursor disabled.'); |
| 371 | scope.zoomToCursor = false; |
| 372 | |
| 373 | } |
| 374 | |
| 375 | // handle the placement of the target |
| 376 | if (newRadius !== null) |
| 377 | { |
| 378 | |
| 379 | if (this.screenSpacePanning) |
| 380 | { |
| 381 | |
| 382 | // position the orbit target in front of the new camera position |
| 383 | scope.target.set(0, 0, - 1) |
| 384 | .transformDirection(scope.object.matrix) |
| 385 | .multiplyScalar(newRadius) |
| 386 | .add(scope.object.position); |
| 387 | |
| 388 | } else |
| 389 | { |
| 390 | |
| 391 | // get the ray and translation plane to compute target |
| 392 | _ray.origin.copy(scope.object.position); |
| 393 | _ray.direction.set(0, 0, - 1).transformDirection(scope.object.matrix); |
| 394 | |
| 395 | // if the camera is 20 degrees above the horizon then don't adjust the focus target to avoid |
| 396 | // extremely large values |
| 397 | if (Math.abs(scope.object.up.dot(_ray.direction)) < TILT_LIMIT) |
| 398 | { |
| 399 | |
| 400 | object.lookAt(scope.target); |
| 401 | |
| 402 | } else |
| 403 | { |
| 404 | |
| 405 | _plane.setFromNormalAndCoplanarPoint(scope.object.up, scope.target); |
| 406 | _ray.intersectPlane(_plane, scope.target); |
| 407 | |
| 408 | } |
| 409 | |
| 410 | } |
| 411 | |
| 412 | } |
| 413 | |
| 414 | } else if (scope.object.isOrthographicCamera) |
| 415 | { |
| 416 | |
| 417 | const prevZoom = scope.object.zoom; |
| 418 | scope.object.zoom = Math.max(scope.minZoom, Math.min(scope.maxZoom, scope.object.zoom / scale)); |
| 419 | |
| 420 | if (prevZoom !== scope.object.zoom) |
| 421 | { |
| 422 | |
| 423 | scope.object.updateProjectionMatrix(); |
| 424 | zoomChanged = true; |
| 425 | |
| 426 | } |
| 427 | |
| 428 | } |
| 429 | |
| 430 | scale = 1; |
| 431 | performCursorZoom = false; |
| 432 | |
| 433 | // update condition is: |
| 434 | // min(camera displacement, camera rotation in radians)^2 > EPS |
| 435 | // using small-angle approximation cos(x/2) = 1 - x^2 / 8 |
| 436 | |
| 437 | if (zoomChanged || |
| 438 | lastPosition.distanceToSquared(scope.object.position) > EPS || |
| 439 | 8 * (1 - lastQuaternion.dot(scope.object.quaternion)) > EPS || |
| 440 | lastTargetPosition.distanceToSquared(scope.target) > EPS) |
| 441 | { |
| 442 | |
| 443 | scope.dispatchEvent(_changeEvent); |
| 444 | |
| 445 | lastPosition.copy(scope.object.position); |
| 446 | lastQuaternion.copy(scope.object.quaternion); |
| 447 | lastTargetPosition.copy(scope.target); |
| 448 | |
| 449 | return true; |
| 450 | |
| 451 | } |
| 452 | |
| 453 | return false; |
| 454 | |
| 455 | }; |
| 456 | |
| 457 | }(); |
| 458 | |
| 459 | this.dispose = function () |
| 460 | { |
| 461 | |
| 462 | scope.domElement.removeEventListener('contextmenu', onContextMenu); |
| 463 | |
| 464 | scope.domElement.removeEventListener('pointerdown', onPointerDown); |
| 465 | scope.domElement.removeEventListener('pointercancel', onPointerUp); |
| 466 | scope.domElement.removeEventListener('wheel', onMouseWheel); |
| 467 | |
| 468 | scope.domElement.removeEventListener('pointermove', onPointerMove); |
| 469 | scope.domElement.removeEventListener('pointerup', onPointerUp); |
| 470 | |
| 471 | const document = scope.domElement.getRootNode(); // offscreen canvas compatibility |
| 472 | |
| 473 | document.removeEventListener('keydown', interceptControlDown, { capture: true }); |
| 474 | |
| 475 | if (scope._domElementKeyEvents !== null) |
| 476 | { |
| 477 | |
| 478 | scope._domElementKeyEvents.removeEventListener('keydown', onKeyDown); |
| 479 | scope._domElementKeyEvents = null; |
| 480 | |
| 481 | } |
| 482 | |
| 483 | //scope.dispatchEvent( { type: 'dispose' } ); // should this be added here? |
| 484 | |
| 485 | }; |
| 486 | |
| 487 | // |
| 488 | // internals |
| 489 | // |
| 490 | |
| 491 | const scope = this; |
| 492 | |
| 493 | const STATE = { |
| 494 | NONE: - 1, |
| 495 | ROTATE: 0, |
| 496 | DOLLY: 1, |
| 497 | PAN: 2, |
| 498 | TOUCH_ROTATE: 3, |
| 499 | TOUCH_PAN: 4, |
| 500 | TOUCH_DOLLY_PAN: 5, |
| 501 | TOUCH_DOLLY_ROTATE: 6 |
| 502 | }; |
| 503 | |
| 504 | let state = STATE.NONE; |
| 505 | |
| 506 | const EPS = 0.000001; |
| 507 | |
| 508 | // current position in spherical coordinates |
| 509 | const spherical = new Spherical(); |
| 510 | const sphericalDelta = new Spherical(); |
| 511 | |
| 512 | let scale = 1; |
| 513 | const panOffset = new Vector3(); |
| 514 | |
| 515 | const rotateStart = new Vector2(); |
| 516 | const rotateEnd = new Vector2(); |
| 517 | const rotateDelta = new Vector2(); |
| 518 | |
| 519 | const panStart = new Vector2(); |
| 520 | const panEnd = new Vector2(); |
| 521 | const panDelta = new Vector2(); |
| 522 | |
| 523 | const dollyStart = new Vector2(); |
| 524 | const dollyEnd = new Vector2(); |
| 525 | const dollyDelta = new Vector2(); |
| 526 | |
| 527 | const dollyDirection = new Vector3(); |
| 528 | const mouse = new Vector2(); |
| 529 | let performCursorZoom = false; |
| 530 | |
| 531 | const pointers = []; |
| 532 | const pointerPositions = {}; |
| 533 | |
| 534 | let controlActive = false; |
| 535 | |
| 536 | function getAutoRotationAngle (deltaTime) |
| 537 | { |
| 538 | |
| 539 | if (deltaTime !== null) |
| 540 | { |
| 541 | |
| 542 | return (2 * Math.PI / 60 * scope.autoRotateSpeed) * deltaTime; |
| 543 | |
| 544 | } else |
| 545 | { |
| 546 | |
| 547 | return 2 * Math.PI / 60 / 60 * scope.autoRotateSpeed; |
| 548 | |
| 549 | } |
| 550 | |
| 551 | } |
| 552 | |
| 553 | function getZoomScale (delta) |
| 554 | { |
| 555 | |
| 556 | const normalizedDelta = Math.abs(delta * 0.01); |
| 557 | return Math.pow(0.95, scope.zoomSpeed * normalizedDelta); |
| 558 | |
| 559 | } |
| 560 | |
| 561 | function rotateLeft (angle) |
| 562 | { |
| 563 | |
| 564 | sphericalDelta.theta -= angle; |
| 565 | |
| 566 | } |
| 567 | |
| 568 | function rotateUp (angle) |
| 569 | { |
| 570 | |
| 571 | sphericalDelta.phi -= angle; |
| 572 | |
| 573 | } |
| 574 | |
| 575 | const panLeft = function () |
| 576 | { |
| 577 | |
| 578 | const v = new Vector3(); |
| 579 | |
| 580 | return function panLeft (distance, objectMatrix) |
| 581 | { |
| 582 | |
| 583 | v.setFromMatrixColumn(objectMatrix, 0); // get X column of objectMatrix |
| 584 | v.multiplyScalar(- distance); |
| 585 | |
| 586 | panOffset.add(v); |
| 587 | |
| 588 | }; |
| 589 | |
| 590 | }(); |
| 591 | |
| 592 | const panUp = function () |
| 593 | { |
| 594 | |
| 595 | const v = new Vector3(); |
| 596 | |
| 597 | return function panUp (distance, objectMatrix) |
| 598 | { |
| 599 | |
| 600 | if (scope.screenSpacePanning === true) |
| 601 | { |
| 602 | |
| 603 | v.setFromMatrixColumn(objectMatrix, 1); |
| 604 | |
| 605 | } else |
| 606 | { |
| 607 | |
| 608 | v.setFromMatrixColumn(objectMatrix, 0); |
| 609 | v.crossVectors(scope.object.up, v); |
| 610 | |
| 611 | } |
| 612 | |
| 613 | v.multiplyScalar(distance); |
| 614 | |
| 615 | panOffset.add(v); |
| 616 | |
| 617 | }; |
| 618 | |
| 619 | }(); |
| 620 | |
| 621 | // deltaX and deltaY are in pixels; right and down are positive |
| 622 | const pan = function () |
| 623 | { |
| 624 | |
| 625 | const offset = new Vector3(); |
| 626 | |
| 627 | return function pan (deltaX, deltaY) |
| 628 | { |
| 629 | |
| 630 | const element = scope.domElement; |
| 631 | |
| 632 | if (scope.object.isPerspectiveCamera) |
| 633 | { |
| 634 | |
| 635 | // perspective |
| 636 | const position = scope.object.position; |
| 637 | offset.copy(position).sub(scope.target); |
| 638 | let targetDistance = offset.length(); |
| 639 | |
| 640 | // half of the fov is center to top of screen |
| 641 | targetDistance *= Math.tan((scope.object.fov / 2) * Math.PI / 180.0); |
| 642 | |
| 643 | // we use only clientHeight here so aspect ratio does not distort speed |
| 644 | panLeft(2 * deltaX * targetDistance / element.clientHeight, scope.object.matrix); |
| 645 | panUp(2 * deltaY * targetDistance / element.clientHeight, scope.object.matrix); |
| 646 | |
| 647 | } else if (scope.object.isOrthographicCamera) |
| 648 | { |
| 649 | |
| 650 | // orthographic |
| 651 | panLeft(deltaX * (scope.object.right - scope.object.left) / scope.object.zoom / element.clientWidth, scope.object.matrix); |
| 652 | panUp(deltaY * (scope.object.top - scope.object.bottom) / scope.object.zoom / element.clientHeight, scope.object.matrix); |
| 653 | |
| 654 | } else |
| 655 | { |
| 656 | |
| 657 | // camera neither orthographic nor perspective |
| 658 | console.warn('WARNING: OrbitControls.js encountered an unknown camera type - pan disabled.'); |
| 659 | scope.enablePan = false; |
| 660 | |
| 661 | } |
| 662 | |
| 663 | }; |
| 664 | |
| 665 | }(); |
| 666 | |
| 667 | function dollyOut (dollyScale) |
| 668 | { |
| 669 | |
| 670 | if (scope.object.isPerspectiveCamera || scope.object.isOrthographicCamera) |
| 671 | { |
| 672 | |
| 673 | scale /= dollyScale; |
| 674 | |
| 675 | } else |
| 676 | { |
| 677 | |
| 678 | console.warn('WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.'); |
| 679 | scope.enableZoom = false; |
| 680 | |
| 681 | } |
| 682 | |
| 683 | } |
| 684 | |
| 685 | function dollyIn (dollyScale) |
| 686 | { |
| 687 | |
| 688 | if (scope.object.isPerspectiveCamera || scope.object.isOrthographicCamera) |
| 689 | { |
| 690 | |
| 691 | scale *= dollyScale; |
| 692 | |
| 693 | } else |
| 694 | { |
| 695 | |
| 696 | console.warn('WARNING: OrbitControls.js encountered an unknown camera type - dolly/zoom disabled.'); |
| 697 | scope.enableZoom = false; |
| 698 | |
| 699 | } |
| 700 | |
| 701 | } |
| 702 | |
| 703 | function updateZoomParameters (x, y) |
| 704 | { |
| 705 | |
| 706 | if (!scope.zoomToCursor) |
| 707 | { |
| 708 | |
| 709 | return; |
| 710 | |
| 711 | } |
| 712 | |
| 713 | performCursorZoom = true; |
| 714 | |
| 715 | const rect = scope.domElement.getBoundingClientRect(); |
| 716 | const dx = x - rect.left; |
| 717 | const dy = y - rect.top; |
| 718 | const w = rect.width; |
| 719 | const h = rect.height; |
| 720 | |
| 721 | mouse.x = (dx / w) * 2 - 1; |
| 722 | mouse.y = - (dy / h) * 2 + 1; |
| 723 | |
| 724 | dollyDirection.set(mouse.x, mouse.y, 1).unproject(scope.object).sub(scope.object.position).normalize(); |
| 725 | |
| 726 | } |
| 727 | |
| 728 | function clampDistance (dist) |
| 729 | { |
| 730 | |
| 731 | return Math.max(scope.minDistance, Math.min(scope.maxDistance, dist)); |
| 732 | |
| 733 | } |
| 734 | |
| 735 | // |
| 736 | // event callbacks - update the object state |
| 737 | // |
| 738 | |
| 739 | function handleMouseDownRotate (event) |
| 740 | { |
| 741 | |
| 742 | rotateStart.set(event.clientX, event.clientY); |
| 743 | |
| 744 | } |
| 745 | |
| 746 | function handleMouseDownDolly (event) |
| 747 | { |
| 748 | |
| 749 | updateZoomParameters(event.clientX, event.clientX); |
| 750 | dollyStart.set(event.clientX, event.clientY); |
| 751 | |
| 752 | } |
| 753 | |
| 754 | function handleMouseDownPan (event) |
| 755 | { |
| 756 | |
| 757 | panStart.set(event.clientX, event.clientY); |
| 758 | |
| 759 | } |
| 760 | |
| 761 | function handleMouseMoveRotate (event) |
| 762 | { |
| 763 | |
| 764 | rotateEnd.set(event.clientX, event.clientY); |
| 765 | |
| 766 | rotateDelta.subVectors(rotateEnd, rotateStart).multiplyScalar(scope.rotateSpeed); |
| 767 | |
| 768 | const element = scope.domElement; |
| 769 | |
| 770 | rotateLeft(2 * Math.PI * rotateDelta.x / element.clientHeight); // yes, height |
| 771 | |
| 772 | rotateUp(2 * Math.PI * rotateDelta.y / element.clientHeight); |
| 773 | |
| 774 | rotateStart.copy(rotateEnd); |
| 775 | |
| 776 | scope.update(); |
| 777 | |
| 778 | } |
| 779 | |
| 780 | function handleMouseMoveDolly (event) |
| 781 | { |
| 782 | |
| 783 | dollyEnd.set(event.clientX, event.clientY); |
| 784 | |
| 785 | dollyDelta.subVectors(dollyEnd, dollyStart); |
| 786 | |
| 787 | if (dollyDelta.y > 0) |
| 788 | { |
| 789 | |
| 790 | dollyOut(getZoomScale(dollyDelta.y)); |
| 791 | |
| 792 | } else if (dollyDelta.y < 0) |
| 793 | { |
| 794 | |
| 795 | dollyIn(getZoomScale(dollyDelta.y)); |
| 796 | |
| 797 | } |
| 798 | |
| 799 | dollyStart.copy(dollyEnd); |
| 800 | |
| 801 | scope.update(); |
| 802 | |
| 803 | } |
| 804 | |
| 805 | function handleMouseMovePan (event) |
| 806 | { |
| 807 | |
| 808 | panEnd.set(event.clientX, event.clientY); |
| 809 | |
| 810 | panDelta.subVectors(panEnd, panStart).multiplyScalar(scope.panSpeed); |
| 811 | |
| 812 | pan(panDelta.x, panDelta.y); |
| 813 | |
| 814 | panStart.copy(panEnd); |
| 815 | |
| 816 | scope.update(); |
| 817 | |
| 818 | } |
| 819 | |
| 820 | function handleMouseWheel (event) |
| 821 | { |
| 822 | |
| 823 | updateZoomParameters(event.clientX, event.clientY); |
| 824 | |
| 825 | if (event.deltaY < 0) |
| 826 | { |
| 827 | |
| 828 | dollyIn(getZoomScale(event.deltaY)); |
| 829 | |
| 830 | } else if (event.deltaY > 0) |
| 831 | { |
| 832 | |
| 833 | dollyOut(getZoomScale(event.deltaY)); |
| 834 | |
| 835 | } |
| 836 | |
| 837 | scope.update(); |
| 838 | |
| 839 | } |
| 840 | |
| 841 | function handleKeyDown (event) |
| 842 | { |
| 843 | |
| 844 | let needsUpdate = false; |
| 845 | |
| 846 | switch (event.code) |
| 847 | { |
| 848 | |
| 849 | case scope.keys.UP: |
| 850 | |
| 851 | if (event.ctrlKey || event.metaKey || event.shiftKey) |
| 852 | { |
| 853 | |
| 854 | rotateUp(2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight); |
| 855 | |
| 856 | } else |
| 857 | { |
| 858 | |
| 859 | pan(0, scope.keyPanSpeed); |
| 860 | |
| 861 | } |
| 862 | |
| 863 | needsUpdate = true; |
| 864 | break; |
| 865 | |
| 866 | case scope.keys.BOTTOM: |
| 867 | |
| 868 | if (event.ctrlKey || event.metaKey || event.shiftKey) |
| 869 | { |
| 870 | |
| 871 | rotateUp(- 2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight); |
| 872 | |
| 873 | } else |
| 874 | { |
| 875 | |
| 876 | pan(0, - scope.keyPanSpeed); |
| 877 | |
| 878 | } |
| 879 | |
| 880 | needsUpdate = true; |
| 881 | break; |
| 882 | |
| 883 | case scope.keys.LEFT: |
| 884 | |
| 885 | if (event.ctrlKey || event.metaKey || event.shiftKey) |
| 886 | { |
| 887 | |
| 888 | rotateLeft(2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight); |
| 889 | |
| 890 | } else |
| 891 | { |
| 892 | |
| 893 | pan(scope.keyPanSpeed, 0); |
| 894 | |
| 895 | } |
| 896 | |
| 897 | needsUpdate = true; |
| 898 | break; |
| 899 | |
| 900 | case scope.keys.RIGHT: |
| 901 | |
| 902 | if (event.ctrlKey || event.metaKey || event.shiftKey) |
| 903 | { |
| 904 | |
| 905 | rotateLeft(- 2 * Math.PI * scope.rotateSpeed / scope.domElement.clientHeight); |
| 906 | |
| 907 | } else |
| 908 | { |
| 909 | |
| 910 | pan(- scope.keyPanSpeed, 0); |
| 911 | |
| 912 | } |
| 913 | |
| 914 | needsUpdate = true; |
| 915 | break; |
| 916 | |
| 917 | } |
| 918 | |
| 919 | if (needsUpdate) |
| 920 | { |
| 921 | |
| 922 | // prevent the browser from scrolling on cursor keys |
| 923 | event.preventDefault(); |
| 924 | |
| 925 | scope.update(); |
| 926 | |
| 927 | } |
| 928 | |
| 929 | |
| 930 | } |
| 931 | |
| 932 | function handleTouchStartRotate (event) |
| 933 | { |
| 934 | |
| 935 | if (pointers.length === 1) |
| 936 | { |
| 937 | |
| 938 | rotateStart.set(event.pageX, event.pageY); |
| 939 | |
| 940 | } else |
| 941 | { |
| 942 | |
| 943 | const position = getSecondPointerPosition(event); |
| 944 | |
| 945 | const x = 0.5 * (event.pageX + position.x); |
| 946 | const y = 0.5 * (event.pageY + position.y); |
| 947 | |
| 948 | rotateStart.set(x, y); |
| 949 | |
| 950 | } |
| 951 | |
| 952 | } |
| 953 | |
| 954 | function handleTouchStartPan (event) |
| 955 | { |
| 956 | |
| 957 | if (pointers.length === 1) |
| 958 | { |
| 959 | |
| 960 | panStart.set(event.pageX, event.pageY); |
| 961 | |
| 962 | } else |
| 963 | { |
| 964 | |
| 965 | const position = getSecondPointerPosition(event); |
| 966 | |
| 967 | const x = 0.5 * (event.pageX + position.x); |
| 968 | const y = 0.5 * (event.pageY + position.y); |
| 969 | |
| 970 | panStart.set(x, y); |
| 971 | |
| 972 | } |
| 973 | |
| 974 | } |
| 975 | |
| 976 | function handleTouchStartDolly (event) |
| 977 | { |
| 978 | |
| 979 | const position = getSecondPointerPosition(event); |
| 980 | |
| 981 | const dx = event.pageX - position.x; |
| 982 | const dy = event.pageY - position.y; |
| 983 | |
| 984 | const distance = Math.sqrt(dx * dx + dy * dy); |
| 985 | |
| 986 | dollyStart.set(0, distance); |
| 987 | |
| 988 | } |
| 989 | |
| 990 | function handleTouchStartDollyPan (event) |
| 991 | { |
| 992 | |
| 993 | if (scope.enableZoom) handleTouchStartDolly(event); |
| 994 | |
| 995 | if (scope.enablePan) handleTouchStartPan(event); |
| 996 | |
| 997 | } |
| 998 | |
| 999 | function handleTouchStartDollyRotate (event) |
| 1000 | { |
| 1001 | |
| 1002 | if (scope.enableZoom) handleTouchStartDolly(event); |
| 1003 | |
| 1004 | if (scope.enableRotate) handleTouchStartRotate(event); |
| 1005 | |
| 1006 | } |
| 1007 | |
| 1008 | function handleTouchMoveRotate (event) |
| 1009 | { |
| 1010 | |
| 1011 | if (pointers.length == 1) |
| 1012 | { |
| 1013 | |
| 1014 | rotateEnd.set(event.pageX, event.pageY); |
| 1015 | |
| 1016 | } else |
| 1017 | { |
| 1018 | |
| 1019 | const position = getSecondPointerPosition(event); |
| 1020 | |
| 1021 | const x = 0.5 * (event.pageX + position.x); |
| 1022 | const y = 0.5 * (event.pageY + position.y); |
| 1023 | |
| 1024 | rotateEnd.set(x, y); |
| 1025 | |
| 1026 | } |
| 1027 | |
| 1028 | rotateDelta.subVectors(rotateEnd, rotateStart).multiplyScalar(scope.rotateSpeed); |
| 1029 | |
| 1030 | const element = scope.domElement; |
| 1031 | |
| 1032 | rotateLeft(2 * Math.PI * rotateDelta.x / element.clientHeight); // yes, height |
| 1033 | |
| 1034 | rotateUp(2 * Math.PI * rotateDelta.y / element.clientHeight); |
| 1035 | |
| 1036 | rotateStart.copy(rotateEnd); |
| 1037 | |
| 1038 | } |
| 1039 | |
| 1040 | function handleTouchMovePan (event) |
| 1041 | { |
| 1042 | |
| 1043 | if (pointers.length === 1) |
| 1044 | { |
| 1045 | |
| 1046 | panEnd.set(event.pageX, event.pageY); |
| 1047 | |
| 1048 | } else |
| 1049 | { |
| 1050 | |
| 1051 | const position = getSecondPointerPosition(event); |
| 1052 | |
| 1053 | const x = 0.5 * (event.pageX + position.x); |
| 1054 | const y = 0.5 * (event.pageY + position.y); |
| 1055 | |
| 1056 | panEnd.set(x, y); |
| 1057 | |
| 1058 | } |
| 1059 | |
| 1060 | panDelta.subVectors(panEnd, panStart).multiplyScalar(scope.panSpeed); |
| 1061 | |
| 1062 | pan(panDelta.x, panDelta.y); |
| 1063 | |
| 1064 | panStart.copy(panEnd); |
| 1065 | |
| 1066 | } |
| 1067 | |
| 1068 | function handleTouchMoveDolly (event) |
| 1069 | { |
| 1070 | |
| 1071 | const position = getSecondPointerPosition(event); |
| 1072 | |
| 1073 | const dx = event.pageX - position.x; |
| 1074 | const dy = event.pageY - position.y; |
| 1075 | |
| 1076 | const distance = Math.sqrt(dx * dx + dy * dy); |
| 1077 | |
| 1078 | dollyEnd.set(0, distance); |
| 1079 | |
| 1080 | dollyDelta.set(0, Math.pow(dollyEnd.y / dollyStart.y, scope.zoomSpeed)); |
| 1081 | |
| 1082 | dollyOut(dollyDelta.y); |
| 1083 | |
| 1084 | dollyStart.copy(dollyEnd); |
| 1085 | |
| 1086 | const centerX = (event.pageX + position.x) * 0.5; |
| 1087 | const centerY = (event.pageY + position.y) * 0.5; |
| 1088 | |
| 1089 | updateZoomParameters(centerX, centerY); |
| 1090 | |
| 1091 | } |
| 1092 | |
| 1093 | function handleTouchMoveDollyPan (event) |
| 1094 | { |
| 1095 | |
| 1096 | if (scope.enableZoom) handleTouchMoveDolly(event); |
| 1097 | |
| 1098 | if (scope.enablePan) handleTouchMovePan(event); |
| 1099 | |
| 1100 | } |
| 1101 | |
| 1102 | function handleTouchMoveDollyRotate (event) |
| 1103 | { |
| 1104 | |
| 1105 | if (scope.enableZoom) handleTouchMoveDolly(event); |
| 1106 | |
| 1107 | if (scope.enableRotate) handleTouchMoveRotate(event); |
| 1108 | |
| 1109 | } |
| 1110 | |
| 1111 | // |
| 1112 | // event handlers - FSM: listen for events and reset state |
| 1113 | // |
| 1114 | |
| 1115 | function onPointerDown (event) |
| 1116 | { |
| 1117 | |
| 1118 | if (scope.enabled === false) return; |
| 1119 | |
| 1120 | if (pointers.length === 0) |
| 1121 | { |
| 1122 | |
| 1123 | scope.domElement.setPointerCapture(event.pointerId); |
| 1124 | |
| 1125 | scope.domElement.addEventListener('pointermove', onPointerMove); |
| 1126 | scope.domElement.addEventListener('pointerup', onPointerUp); |
| 1127 | |
| 1128 | } |
| 1129 | |
| 1130 | // |
| 1131 | |
| 1132 | if (isTrackingPointer(event)) return; |
| 1133 | |
| 1134 | // |
| 1135 | |
| 1136 | addPointer(event); |
| 1137 | |
| 1138 | if (event.pointerType === 'touch') |
| 1139 | { |
| 1140 | |
| 1141 | onTouchStart(event); |
| 1142 | |
| 1143 | } else |
| 1144 | { |
| 1145 | |
| 1146 | onMouseDown(event); |
| 1147 | |
| 1148 | } |
| 1149 | |
| 1150 | } |
| 1151 | |
| 1152 | function onPointerMove (event) |
| 1153 | { |
| 1154 | |
| 1155 | if (scope.enabled === false) return; |
| 1156 | |
| 1157 | if (event.pointerType === 'touch') |
| 1158 | { |
| 1159 | |
| 1160 | onTouchMove(event); |
| 1161 | |
| 1162 | } else |
| 1163 | { |
| 1164 | |
| 1165 | onMouseMove(event); |
| 1166 | |
| 1167 | } |
| 1168 | |
| 1169 | } |
| 1170 | |
| 1171 | function onPointerUp (event) |
| 1172 | { |
| 1173 | |
| 1174 | removePointer(event); |
| 1175 | |
| 1176 | switch (pointers.length) |
| 1177 | { |
| 1178 | |
| 1179 | case 0: |
| 1180 | |
| 1181 | scope.domElement.releasePointerCapture(event.pointerId); |
| 1182 | |
| 1183 | scope.domElement.removeEventListener('pointermove', onPointerMove); |
| 1184 | scope.domElement.removeEventListener('pointerup', onPointerUp); |
| 1185 | |
| 1186 | scope.dispatchEvent(_endEvent); |
| 1187 | |
| 1188 | state = STATE.NONE; |
| 1189 | |
| 1190 | break; |
| 1191 | |
| 1192 | case 1: |
| 1193 | |
| 1194 | const pointerId = pointers[0]; |
| 1195 | const position = pointerPositions[pointerId]; |
| 1196 | |
| 1197 | // minimal placeholder event - allows state correction on pointer-up |
| 1198 | onTouchStart({ pointerId: pointerId, pageX: position.x, pageY: position.y }); |
| 1199 | |
| 1200 | break; |
| 1201 | |
| 1202 | } |
| 1203 | |
| 1204 | } |
| 1205 | |
| 1206 | function onMouseDown (event) |
| 1207 | { |
| 1208 | |
| 1209 | let mouseAction; |
| 1210 | |
| 1211 | switch (event.button) |
| 1212 | { |
| 1213 | |
| 1214 | case 0: |
| 1215 | |
| 1216 | mouseAction = scope.mouseButtons.LEFT; |
| 1217 | break; |
| 1218 | |
| 1219 | case 1: |
| 1220 | |
| 1221 | mouseAction = scope.mouseButtons.MIDDLE; |
| 1222 | break; |
| 1223 | |
| 1224 | case 2: |
| 1225 | |
| 1226 | mouseAction = scope.mouseButtons.RIGHT; |
| 1227 | break; |
| 1228 | |
| 1229 | default: |
| 1230 | |
| 1231 | mouseAction = - 1; |
| 1232 | |
| 1233 | } |
| 1234 | |
| 1235 | switch (mouseAction) |
| 1236 | { |
| 1237 | |
| 1238 | case MOUSE.DOLLY: |
| 1239 | |
| 1240 | if (scope.enableZoom === false) return; |
| 1241 | |
| 1242 | handleMouseDownDolly(event); |
| 1243 | |
| 1244 | state = STATE.DOLLY; |
| 1245 | |
| 1246 | break; |
| 1247 | |
| 1248 | case MOUSE.ROTATE: |
| 1249 | |
| 1250 | if (event.ctrlKey || event.metaKey || event.shiftKey) |
| 1251 | { |
| 1252 | |
| 1253 | if (scope.enablePan === false) return; |
| 1254 | |
| 1255 | handleMouseDownPan(event); |
| 1256 | |
| 1257 | state = STATE.PAN; |
| 1258 | |
| 1259 | } else |
| 1260 | { |
| 1261 | |
| 1262 | if (scope.enableRotate === false) return; |
| 1263 | |
| 1264 | handleMouseDownRotate(event); |
| 1265 | |
| 1266 | state = STATE.ROTATE; |
| 1267 | |
| 1268 | } |
| 1269 | |
| 1270 | break; |
| 1271 | |
| 1272 | case MOUSE.PAN: |
| 1273 | |
| 1274 | if (event.ctrlKey || event.metaKey || event.shiftKey) |
| 1275 | { |
| 1276 | |
| 1277 | if (scope.enableRotate === false) return; |
| 1278 | |
| 1279 | handleMouseDownRotate(event); |
| 1280 | |
| 1281 | state = STATE.ROTATE; |
| 1282 | |
| 1283 | } else |
| 1284 | { |
| 1285 | |
| 1286 | if (scope.enablePan === false) return; |
| 1287 | |
| 1288 | handleMouseDownPan(event); |
| 1289 | |
| 1290 | state = STATE.PAN; |
| 1291 | |
| 1292 | } |
| 1293 | |
| 1294 | break; |
| 1295 | |
| 1296 | default: |
| 1297 | |
| 1298 | state = STATE.NONE; |
| 1299 | |
| 1300 | } |
| 1301 | |
| 1302 | if (state !== STATE.NONE) |
| 1303 | { |
| 1304 | |
| 1305 | scope.dispatchEvent(_startEvent); |
| 1306 | |
| 1307 | } |
| 1308 | |
| 1309 | } |
| 1310 | |
| 1311 | function onMouseMove (event) |
| 1312 | { |
| 1313 | |
| 1314 | switch (state) |
| 1315 | { |
| 1316 | |
| 1317 | case STATE.ROTATE: |
| 1318 | |
| 1319 | if (scope.enableRotate === false) return; |
| 1320 | |
| 1321 | handleMouseMoveRotate(event); |
| 1322 | |
| 1323 | break; |
| 1324 | |
| 1325 | case STATE.DOLLY: |
| 1326 | |
| 1327 | if (scope.enableZoom === false) return; |
| 1328 | |
| 1329 | handleMouseMoveDolly(event); |
| 1330 | |
| 1331 | break; |
| 1332 | |
| 1333 | case STATE.PAN: |
| 1334 | |
| 1335 | if (scope.enablePan === false) return; |
| 1336 | |
| 1337 | handleMouseMovePan(event); |
| 1338 | |
| 1339 | break; |
| 1340 | |
| 1341 | } |
| 1342 | |
| 1343 | } |
| 1344 | |
| 1345 | function onMouseWheel (event) |
| 1346 | { |
| 1347 | |
| 1348 | if (scope.enabled === false || scope.enableZoom === false || state !== STATE.NONE) return; |
| 1349 | |
| 1350 | event.preventDefault(); |
| 1351 | |
| 1352 | scope.dispatchEvent(_startEvent); |
| 1353 | |
| 1354 | handleMouseWheel(customWheelEvent(event)); |
| 1355 | |
| 1356 | scope.dispatchEvent(_endEvent); |
| 1357 | |
| 1358 | } |
| 1359 | |
| 1360 | function customWheelEvent (event) |
| 1361 | { |
| 1362 | |
| 1363 | const mode = event.deltaMode; |
| 1364 | |
| 1365 | // minimal wheel event altered to meet delta-zoom demand |
| 1366 | const newEvent = { |
| 1367 | clientX: event.clientX, |
| 1368 | clientY: event.clientY, |
| 1369 | deltaY: event.deltaY, |
| 1370 | }; |
| 1371 | |
| 1372 | switch (mode) |
| 1373 | { |
| 1374 | |
| 1375 | case 1: // LINE_MODE |
| 1376 | newEvent.deltaY *= 16; |
| 1377 | break; |
| 1378 | |
| 1379 | case 2: // PAGE_MODE |
| 1380 | newEvent.deltaY *= 100; |
| 1381 | break; |
| 1382 | |
| 1383 | } |
| 1384 | |
| 1385 | // detect if event was triggered by pinching |
| 1386 | if (event.ctrlKey && !controlActive) |
| 1387 | { |
| 1388 | |
| 1389 | newEvent.deltaY *= 10; |
| 1390 | |
| 1391 | } |
| 1392 | |
| 1393 | return newEvent; |
| 1394 | |
| 1395 | } |
| 1396 | |
| 1397 | function interceptControlDown (event) |
| 1398 | { |
| 1399 | |
| 1400 | if (event.key === 'Control') |
| 1401 | { |
| 1402 | |
| 1403 | controlActive = true; |
| 1404 | |
| 1405 | |
| 1406 | const document = scope.domElement.getRootNode(); // offscreen canvas compatibility |
| 1407 | |
| 1408 | document.addEventListener('keyup', interceptControlUp, { passive: true, capture: true }); |
| 1409 | |
| 1410 | } |
| 1411 | |
| 1412 | } |
| 1413 | |
| 1414 | function interceptControlUp (event) |
| 1415 | { |
| 1416 | |
| 1417 | if (event.key === 'Control') |
| 1418 | { |
| 1419 | |
| 1420 | controlActive = false; |
| 1421 | |
| 1422 | |
| 1423 | const document = scope.domElement.getRootNode(); // offscreen canvas compatibility |
| 1424 | |
| 1425 | document.removeEventListener('keyup', interceptControlUp, { passive: true, capture: true }); |
| 1426 | |
| 1427 | } |
| 1428 | |
| 1429 | } |
| 1430 | |
| 1431 | function onKeyDown (event) |
| 1432 | { |
| 1433 | |
| 1434 | if (scope.enabled === false || scope.enablePan === false) return; |
| 1435 | |
| 1436 | handleKeyDown(event); |
| 1437 | |
| 1438 | } |
| 1439 | |
| 1440 | function onTouchStart (event) |
| 1441 | { |
| 1442 | |
| 1443 | trackPointer(event); |
| 1444 | |
| 1445 | switch (pointers.length) |
| 1446 | { |
| 1447 | |
| 1448 | case 1: |
| 1449 | |
| 1450 | switch (scope.touches.ONE) |
| 1451 | { |
| 1452 | |
| 1453 | case TOUCH.ROTATE: |
| 1454 | |
| 1455 | if (scope.enableRotate === false) return; |
| 1456 | |
| 1457 | handleTouchStartRotate(event); |
| 1458 | |
| 1459 | state = STATE.TOUCH_ROTATE; |
| 1460 | |
| 1461 | break; |
| 1462 | |
| 1463 | case TOUCH.PAN: |
| 1464 | |
| 1465 | if (scope.enablePan === false) return; |
| 1466 | |
| 1467 | handleTouchStartPan(event); |
| 1468 | |
| 1469 | state = STATE.TOUCH_PAN; |
| 1470 | |
| 1471 | break; |
| 1472 | |
| 1473 | default: |
| 1474 | |
| 1475 | state = STATE.NONE; |
| 1476 | |
| 1477 | } |
| 1478 | |
| 1479 | break; |
| 1480 | |
| 1481 | case 2: |
| 1482 | |
| 1483 | switch (scope.touches.TWO) |
| 1484 | { |
| 1485 | |
| 1486 | case TOUCH.DOLLY_PAN: |
| 1487 | |
| 1488 | if (scope.enableZoom === false && scope.enablePan === false) return; |
| 1489 | |
| 1490 | handleTouchStartDollyPan(event); |
| 1491 | |
| 1492 | state = STATE.TOUCH_DOLLY_PAN; |
| 1493 | |
| 1494 | break; |
| 1495 | |
| 1496 | case TOUCH.DOLLY_ROTATE: |
| 1497 | |
| 1498 | if (scope.enableZoom === false && scope.enableRotate === false) return; |
| 1499 | |
| 1500 | handleTouchStartDollyRotate(event); |
| 1501 | |
| 1502 | state = STATE.TOUCH_DOLLY_ROTATE; |
| 1503 | |
| 1504 | break; |
| 1505 | |
| 1506 | default: |
| 1507 | |
| 1508 | state = STATE.NONE; |
| 1509 | |
| 1510 | } |
| 1511 | |
| 1512 | break; |
| 1513 | |
| 1514 | default: |
| 1515 | |
| 1516 | state = STATE.NONE; |
| 1517 | |
| 1518 | } |
| 1519 | |
| 1520 | if (state !== STATE.NONE) |
| 1521 | { |
| 1522 | |
| 1523 | scope.dispatchEvent(_startEvent); |
| 1524 | |
| 1525 | } |
| 1526 | |
| 1527 | } |
| 1528 | |
| 1529 | function onTouchMove (event) |
| 1530 | { |
| 1531 | |
| 1532 | trackPointer(event); |
| 1533 | |
| 1534 | switch (state) |
| 1535 | { |
| 1536 | |
| 1537 | case STATE.TOUCH_ROTATE: |
| 1538 | |
| 1539 | if (scope.enableRotate === false) return; |
| 1540 | |
| 1541 | handleTouchMoveRotate(event); |
| 1542 | |
| 1543 | scope.update(); |
| 1544 | |
| 1545 | break; |
| 1546 | |
| 1547 | case STATE.TOUCH_PAN: |
| 1548 | |
| 1549 | if (scope.enablePan === false) return; |
| 1550 | |
| 1551 | handleTouchMovePan(event); |
| 1552 | |
| 1553 | scope.update(); |
| 1554 | |
| 1555 | break; |
| 1556 | |
| 1557 | case STATE.TOUCH_DOLLY_PAN: |
| 1558 | |
| 1559 | if (scope.enableZoom === false && scope.enablePan === false) return; |
| 1560 | |
| 1561 | handleTouchMoveDollyPan(event); |
| 1562 | |
| 1563 | scope.update(); |
| 1564 | |
| 1565 | break; |
| 1566 | |
| 1567 | case STATE.TOUCH_DOLLY_ROTATE: |
| 1568 | |
| 1569 | if (scope.enableZoom === false && scope.enableRotate === false) return; |
| 1570 | |
| 1571 | handleTouchMoveDollyRotate(event); |
| 1572 | |
| 1573 | scope.update(); |
| 1574 | |
| 1575 | break; |
| 1576 | |
| 1577 | default: |
| 1578 | |
| 1579 | state = STATE.NONE; |
| 1580 | |
| 1581 | } |
| 1582 | |
| 1583 | } |
| 1584 | |
| 1585 | function onContextMenu (event) |
| 1586 | { |
| 1587 | |
| 1588 | if (scope.enabled === false) return; |
| 1589 | |
| 1590 | event.preventDefault(); |
| 1591 | |
| 1592 | } |
| 1593 | |
| 1594 | function addPointer (event) |
| 1595 | { |
| 1596 | |
| 1597 | pointers.push(event.pointerId); |
| 1598 | |
| 1599 | } |
| 1600 | |
| 1601 | function removePointer (event) |
| 1602 | { |
| 1603 | |
| 1604 | delete pointerPositions[event.pointerId]; |
| 1605 | |
| 1606 | for (let i = 0; i < pointers.length; i++) |
| 1607 | { |
| 1608 | |
| 1609 | if (pointers[i] == event.pointerId) |
| 1610 | { |
| 1611 | |
| 1612 | pointers.splice(i, 1); |
| 1613 | return; |
| 1614 | |
| 1615 | } |
| 1616 | |
| 1617 | } |
| 1618 | |
| 1619 | } |
| 1620 | |
| 1621 | function isTrackingPointer (event) |
| 1622 | { |
| 1623 | |
| 1624 | for (let i = 0; i < pointers.length; i++) |
| 1625 | { |
| 1626 | |
| 1627 | if (pointers[i] == event.pointerId) return true; |
| 1628 | |
| 1629 | } |
| 1630 | |
| 1631 | return false; |
| 1632 | |
| 1633 | } |
| 1634 | |
| 1635 | function trackPointer (event) |
| 1636 | { |
| 1637 | |
| 1638 | let position = pointerPositions[event.pointerId]; |
| 1639 | |
| 1640 | if (position === undefined) |
| 1641 | { |
| 1642 | |
| 1643 | position = new Vector2(); |
| 1644 | pointerPositions[event.pointerId] = position; |
| 1645 | |
| 1646 | } |
| 1647 | |
| 1648 | position.set(event.pageX, event.pageY); |
| 1649 | |
| 1650 | } |
| 1651 | |
| 1652 | function getSecondPointerPosition (event) |
| 1653 | { |
| 1654 | |
| 1655 | const pointerId = (event.pointerId === pointers[0]) ? pointers[1] : pointers[0]; |
| 1656 | |
| 1657 | return pointerPositions[pointerId]; |
| 1658 | |
| 1659 | } |
| 1660 | |
| 1661 | // |
| 1662 | |
| 1663 | scope.domElement.addEventListener('contextmenu', onContextMenu); |
| 1664 | |
| 1665 | scope.domElement.addEventListener('pointerdown', onPointerDown); |
| 1666 | scope.domElement.addEventListener('pointercancel', onPointerUp); |
| 1667 | scope.domElement.addEventListener('wheel', onMouseWheel, { passive: false }); |
| 1668 | |
| 1669 | const document = scope.domElement.getRootNode(); // offscreen canvas compatibility |
| 1670 | |
| 1671 | document.addEventListener('keydown', interceptControlDown, { passive: true, capture: true }); |
| 1672 | |
| 1673 | // force an update at start |
| 1674 | |
| 1675 | this.update(); |
| 1676 | |
| 1677 | } |
| 1678 | |
| 1679 | } |
| 1680 | |
| 1681 | export { OrbitControls }; |