Update orbit-camera.js
Browse files- orbit-camera.js +154 -52
orbit-camera.js
CHANGED
|
@@ -112,18 +112,23 @@ OrbitCamera.prototype.initialize = function () {
|
|
| 112 |
var onWindowResize = function () { self._checkAspectRatio(); };
|
| 113 |
window.addEventListener('resize', onWindowResize, false);
|
| 114 |
this._checkAspectRatio();
|
|
|
|
| 115 |
this._modelsAabb = new pc.BoundingBox();
|
| 116 |
this._buildAabb(this.focusEntity || this.app.root);
|
|
|
|
| 117 |
this.entity.lookAt(this._modelsAabb.center);
|
| 118 |
this._pivotPoint = new pc.Vec3();
|
| 119 |
this._pivotPoint.copy(this._modelsAabb.center);
|
|
|
|
| 120 |
var cameraQuat = this.entity.getRotation();
|
| 121 |
this._yaw = this._calcYaw(cameraQuat);
|
| 122 |
this._pitch = this._clampPitchAngle(this._calcPitch(cameraQuat, this._yaw));
|
| 123 |
this.entity.setLocalEulerAngles(this._pitch, this._yaw, 0);
|
|
|
|
| 124 |
this._distance = 0;
|
| 125 |
this._targetYaw = this._yaw;
|
| 126 |
this._targetPitch = this._pitch;
|
|
|
|
| 127 |
if (this.frameOnStart) {
|
| 128 |
this.focus(this.focusEntity || this.app.root);
|
| 129 |
} else {
|
|
@@ -132,17 +137,21 @@ OrbitCamera.prototype.initialize = function () {
|
|
| 132 |
this._distance = this._clampDistance(distanceBetween.length());
|
| 133 |
}
|
| 134 |
this._targetDistance = this._distance;
|
|
|
|
| 135 |
this.on('attr:distanceMin', function () { this._distance = this._clampDistance(this._distance); });
|
| 136 |
this.on('attr:distanceMax', function () { this._distance = this._clampDistance(this._distance); });
|
| 137 |
this.on('attr:pitchAngleMin', function () { this._pitch = this._clampPitchAngle(this._pitch); });
|
| 138 |
this.on('attr:pitchAngleMax', function () { this._pitch = this._clampPitchAngle(this._pitch); });
|
|
|
|
| 139 |
this.on('attr:focusEntity', function (value) {
|
| 140 |
if (this.frameOnStart) { this.focus(value || this.app.root); }
|
| 141 |
else { this.resetAndLookAtEntity(this.entity.getPosition(), value || this.app.root); }
|
| 142 |
});
|
|
|
|
| 143 |
this.on('attr:frameOnStart', function (value) {
|
| 144 |
if (value) { this.focus(this.focusEntity || this.app.root); }
|
| 145 |
});
|
|
|
|
| 146 |
this.on('destroy', function () { window.removeEventListener('resize', onWindowResize, false); });
|
| 147 |
};
|
| 148 |
|
|
@@ -157,12 +166,15 @@ OrbitCamera.prototype.update = function (dt) {
|
|
| 157 |
OrbitCamera.prototype._updatePosition = function () {
|
| 158 |
this.entity.setLocalPosition(0, 0, 0);
|
| 159 |
this.entity.setLocalEulerAngles(this._pitch, this._yaw, 0);
|
|
|
|
| 160 |
var position = this.entity.getPosition();
|
| 161 |
position.copy(this.entity.forward);
|
| 162 |
position.mulScalar(-this._distance);
|
| 163 |
position.add(this.pivotPoint);
|
|
|
|
| 164 |
// Clamp camera's Y position so it never goes below minY
|
| 165 |
position.y = Math.max(position.y, this.minY);
|
|
|
|
| 166 |
this.entity.setPosition(position);
|
| 167 |
};
|
| 168 |
|
|
@@ -180,6 +192,7 @@ OrbitCamera.prototype._checkAspectRatio = function () {
|
|
| 180 |
|
| 181 |
OrbitCamera.prototype._buildAabb = function (entity) {
|
| 182 |
var i, m, meshInstances = [];
|
|
|
|
| 183 |
var renders = entity.findComponents('render');
|
| 184 |
for (i = 0; i < renders.length; i++) {
|
| 185 |
var render = renders[i];
|
|
@@ -187,6 +200,7 @@ OrbitCamera.prototype._buildAabb = function (entity) {
|
|
| 187 |
meshInstances.push(render.meshInstances[m]);
|
| 188 |
}
|
| 189 |
}
|
|
|
|
| 190 |
var models = entity.findComponents('model');
|
| 191 |
for (i = 0; i < models.length; i++) {
|
| 192 |
var model = models[i];
|
|
@@ -194,6 +208,7 @@ OrbitCamera.prototype._buildAabb = function (entity) {
|
|
| 194 |
meshInstances.push(model.meshInstances[m]);
|
| 195 |
}
|
| 196 |
}
|
|
|
|
| 197 |
var gsplats = entity.findComponents('gsplat');
|
| 198 |
for (i = 0; i < gsplats.length; i++) {
|
| 199 |
var gsplat = gsplats[i];
|
|
@@ -202,6 +217,7 @@ OrbitCamera.prototype._buildAabb = function (entity) {
|
|
| 202 |
meshInstances.push(instance.meshInstance);
|
| 203 |
}
|
| 204 |
}
|
|
|
|
| 205 |
for (i = 0; i < meshInstances.length; i++) {
|
| 206 |
if (i === 0) {
|
| 207 |
this._modelsAabb.copy(meshInstances[i].aabb);
|
|
@@ -252,14 +268,17 @@ OrbitCameraInputMouse.attributes.add('distanceSensitivity', { type: 'number', de
|
|
| 252 |
|
| 253 |
OrbitCameraInputMouse.prototype.initialize = function () {
|
| 254 |
this.orbitCamera = this.entity.script.orbitCamera;
|
|
|
|
| 255 |
if (this.orbitCamera) {
|
| 256 |
var self = this;
|
| 257 |
-
var onMouseOut = function (
|
|
|
|
| 258 |
this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onMouseDown, this);
|
| 259 |
this.app.mouse.on(pc.EVENT_MOUSEUP, this.onMouseUp, this);
|
| 260 |
this.app.mouse.on(pc.EVENT_MOUSEMOVE, this.onMouseMove, this);
|
| 261 |
this.app.mouse.on(pc.EVENT_MOUSEWHEEL, this.onMouseWheel, this);
|
| 262 |
window.addEventListener('mouseout', onMouseOut, false);
|
|
|
|
| 263 |
this.on('destroy', function () {
|
| 264 |
this.app.mouse.off(pc.EVENT_MOUSEDOWN, this.onMouseDown, this);
|
| 265 |
this.app.mouse.off(pc.EVENT_MOUSEUP, this.onMouseUp, this);
|
|
@@ -268,11 +287,14 @@ OrbitCameraInputMouse.prototype.initialize = function () {
|
|
| 268 |
window.removeEventListener('mouseout', onMouseOut, false);
|
| 269 |
});
|
| 270 |
}
|
|
|
|
| 271 |
this.app.mouse.disableContextMenu();
|
|
|
|
| 272 |
this.lookButtonDown = false;
|
| 273 |
this.panButtonDown = false;
|
| 274 |
this.lastPoint = new pc.Vec2();
|
| 275 |
};
|
|
|
|
| 276 |
OrbitCameraInputMouse.fromWorldPoint = new pc.Vec3();
|
| 277 |
OrbitCameraInputMouse.toWorldPoint = new pc.Vec3();
|
| 278 |
OrbitCameraInputMouse.worldDiff = new pc.Vec3();
|
|
@@ -281,13 +303,16 @@ OrbitCameraInputMouse.prototype.pan = function (screenPoint) {
|
|
| 281 |
var fromWorldPoint = OrbitCameraInputMouse.fromWorldPoint;
|
| 282 |
var toWorldPoint = OrbitCameraInputMouse.toWorldPoint;
|
| 283 |
var worldDiff = OrbitCameraInputMouse.worldDiff;
|
|
|
|
| 284 |
var camera = this.entity.camera;
|
| 285 |
var distance = this.orbitCamera.distance;
|
|
|
|
| 286 |
camera.screenToWorld(screenPoint.x, screenPoint.y, distance, fromWorldPoint);
|
| 287 |
camera.screenToWorld(this.lastPoint.x, this.lastPoint.y, distance, toWorldPoint);
|
|
|
|
| 288 |
worldDiff.sub2(toWorldPoint, fromWorldPoint);
|
| 289 |
|
| 290 |
-
//
|
| 291 |
var proposedPivot = this.orbitCamera.pivotPoint.clone().add(worldDiff);
|
| 292 |
var minY = this.orbitCamera.minY;
|
| 293 |
var resultingY = this.orbitCamera.worldCameraYForPivot(proposedPivot);
|
|
@@ -295,14 +320,13 @@ OrbitCameraInputMouse.prototype.pan = function (screenPoint) {
|
|
| 295 |
if (resultingY >= minY - 1e-4) {
|
| 296 |
this.orbitCamera.pivotPoint.add(worldDiff);
|
| 297 |
} else {
|
| 298 |
-
//
|
| 299 |
worldDiff.y = 0;
|
| 300 |
proposedPivot = this.orbitCamera.pivotPoint.clone().add(worldDiff);
|
| 301 |
resultingY = this.orbitCamera.worldCameraYForPivot(proposedPivot);
|
| 302 |
if (resultingY >= minY - 1e-4) {
|
| 303 |
this.orbitCamera.pivotPoint.add(worldDiff);
|
| 304 |
}
|
| 305 |
-
// Otherwise, suppress pan
|
| 306 |
}
|
| 307 |
};
|
| 308 |
|
|
@@ -313,6 +337,7 @@ OrbitCameraInputMouse.prototype.onMouseDown = function (event) {
|
|
| 313 |
case pc.MOUSEBUTTON_RIGHT: this.lookButtonDown = true; break;
|
| 314 |
}
|
| 315 |
};
|
|
|
|
| 316 |
OrbitCameraInputMouse.prototype.onMouseUp = function (event) {
|
| 317 |
switch (event.button) {
|
| 318 |
case pc.MOUSEBUTTON_LEFT: this.panButtonDown = false; break;
|
|
@@ -320,10 +345,12 @@ OrbitCameraInputMouse.prototype.onMouseUp = function (event) {
|
|
| 320 |
case pc.MOUSEBUTTON_RIGHT: this.lookButtonDown = false; break;
|
| 321 |
}
|
| 322 |
};
|
|
|
|
| 323 |
OrbitCameraInputMouse.prototype.onMouseMove = function (event) {
|
| 324 |
if (this.lookButtonDown) {
|
| 325 |
var sens = this.orbitSensitivity;
|
| 326 |
-
|
|
|
|
| 327 |
var deltaYaw = event.dx * sens;
|
| 328 |
|
| 329 |
var currPitch = this.orbitCamera.pitch;
|
|
@@ -346,6 +373,7 @@ OrbitCameraInputMouse.prototype.onMouseMove = function (event) {
|
|
| 346 |
|
| 347 |
var minY = this.orbitCamera.minY;
|
| 348 |
var wouldGoBelow = proposedY < minY - 1e-4;
|
|
|
|
| 349 |
if (wouldGoBelow && (proposedY < preY)) {
|
| 350 |
this.orbitCamera.yaw = currYaw - deltaYaw;
|
| 351 |
} else {
|
|
@@ -355,6 +383,7 @@ OrbitCameraInputMouse.prototype.onMouseMove = function (event) {
|
|
| 355 |
} else if (this.panButtonDown) {
|
| 356 |
this.pan(new pc.Vec2(event.x, event.y));
|
| 357 |
}
|
|
|
|
| 358 |
this.lastPoint.set(event.x, event.y);
|
| 359 |
};
|
| 360 |
|
|
@@ -366,6 +395,7 @@ OrbitCameraInputMouse.prototype.onMouseWheel = function (event) {
|
|
| 366 |
}
|
| 367 |
event.event.preventDefault();
|
| 368 |
};
|
|
|
|
| 369 |
OrbitCameraInputMouse.prototype.onMouseOut = function () {
|
| 370 |
this.lookButtonDown = false;
|
| 371 |
this.panButtonDown = false;
|
|
@@ -375,16 +405,19 @@ OrbitCameraInputMouse.prototype.onMouseOut = function () {
|
|
| 375 |
var OrbitCameraInputTouch = pc.createScript('orbitCameraInputTouch');
|
| 376 |
OrbitCameraInputTouch.attributes.add('orbitSensitivity', { type: 'number', default: 0.6, title: 'Orbit Sensitivity' });
|
| 377 |
OrbitCameraInputTouch.attributes.add('distanceSensitivity', { type: 'number', default: 0.5, title: 'Distance Sensitivity' });
|
|
|
|
| 378 |
OrbitCameraInputTouch.prototype.initialize = function () {
|
| 379 |
this.orbitCamera = this.entity.script.orbitCamera;
|
| 380 |
this.lastTouchPoint = new pc.Vec2();
|
| 381 |
this.lastPinchMidPoint = new pc.Vec2();
|
| 382 |
this.lastPinchDistance = 0;
|
|
|
|
| 383 |
if (this.orbitCamera && this.app.touch) {
|
| 384 |
this.app.touch.on(pc.EVENT_TOUCHSTART, this.onTouchStartEndCancel, this);
|
| 385 |
this.app.touch.on(pc.EVENT_TOUCHEND, this.onTouchStartEndCancel, this);
|
| 386 |
this.app.touch.on(pc.EVENT_TOUCHCANCEL, this.onTouchStartEndCancel, this);
|
| 387 |
this.app.touch.on(pc.EVENT_TOUCHMOVE, this.onTouchMove, this);
|
|
|
|
| 388 |
this.on('destroy', function () {
|
| 389 |
this.app.touch.off(pc.EVENT_TOUCHSTART, this.onTouchStartEndCancel, this);
|
| 390 |
this.app.touch.off(pc.EVENT_TOUCHEND, this.onTouchStartEndCancel, this);
|
|
@@ -393,17 +426,20 @@ OrbitCameraInputTouch.prototype.initialize = function () {
|
|
| 393 |
});
|
| 394 |
}
|
| 395 |
};
|
|
|
|
| 396 |
OrbitCameraInputTouch.prototype.getPinchDistance = function (pointA, pointB) {
|
| 397 |
var dx = pointA.x - pointB.x;
|
| 398 |
var dy = pointA.y - pointB.y;
|
| 399 |
return Math.sqrt((dx * dx) + (dy * dy));
|
| 400 |
};
|
|
|
|
| 401 |
OrbitCameraInputTouch.prototype.calcMidPoint = function (pointA, pointB, result) {
|
| 402 |
result.set(pointB.x - pointA.x, pointB.y - pointA.y);
|
| 403 |
result.mulScalar(0.5);
|
| 404 |
result.x += pointA.x;
|
| 405 |
result.y += pointA.y;
|
| 406 |
};
|
|
|
|
| 407 |
OrbitCameraInputTouch.prototype.onTouchStartEndCancel = function (event) {
|
| 408 |
var touches = event.touches;
|
| 409 |
if (touches.length === 1) {
|
|
@@ -413,17 +449,22 @@ OrbitCameraInputTouch.prototype.onTouchStartEndCancel = function (event) {
|
|
| 413 |
this.calcMidPoint(touches[0], touches[1], this.lastPinchMidPoint);
|
| 414 |
}
|
| 415 |
};
|
|
|
|
| 416 |
OrbitCameraInputTouch.fromWorldPoint = new pc.Vec3();
|
| 417 |
OrbitCameraInputTouch.toWorldPoint = new pc.Vec3();
|
| 418 |
OrbitCameraInputTouch.worldDiff = new pc.Vec3();
|
|
|
|
| 419 |
OrbitCameraInputTouch.prototype.pan = function (midPoint) {
|
| 420 |
var fromWorldPoint = OrbitCameraInputTouch.fromWorldPoint;
|
| 421 |
var toWorldPoint = OrbitCameraInputTouch.toWorldPoint;
|
| 422 |
var worldDiff = OrbitCameraInputTouch.worldDiff;
|
|
|
|
| 423 |
var camera = this.entity.camera;
|
| 424 |
var distance = this.orbitCamera.distance;
|
|
|
|
| 425 |
camera.screenToWorld(midPoint.x, midPoint.y, distance, fromWorldPoint);
|
| 426 |
camera.screenToWorld(this.lastPinchMidPoint.x, this.lastPinchMidPoint.y, distance, toWorldPoint);
|
|
|
|
| 427 |
worldDiff.sub2(toWorldPoint, fromWorldPoint);
|
| 428 |
|
| 429 |
var proposedPivot = this.orbitCamera.pivotPoint.clone().add(worldDiff);
|
|
@@ -447,9 +488,11 @@ OrbitCameraInputTouch.pinchMidPoint = new pc.Vec2();
|
|
| 447 |
OrbitCameraInputTouch.prototype.onTouchMove = function (event) {
|
| 448 |
var pinchMidPoint = OrbitCameraInputTouch.pinchMidPoint;
|
| 449 |
var touches = event.touches;
|
|
|
|
| 450 |
if (touches.length === 1) {
|
| 451 |
var touch = touches[0];
|
| 452 |
var sens = this.orbitSensitivity;
|
|
|
|
| 453 |
var deltaPitch = (touch.y - this.lastTouchPoint.y) * sens;
|
| 454 |
var deltaYaw = (touch.x - this.lastTouchPoint.x) * sens;
|
| 455 |
|
|
@@ -473,92 +516,151 @@ OrbitCameraInputTouch.prototype.onTouchMove = function (event) {
|
|
| 473 |
|
| 474 |
var minY = this.orbitCamera.minY;
|
| 475 |
var wouldGoBelow = proposedY < minY - 1e-4;
|
|
|
|
| 476 |
if (wouldGoBelow && (proposedY < preY)) {
|
| 477 |
this.orbitCamera.yaw = currYaw - deltaYaw;
|
| 478 |
} else {
|
| 479 |
this.orbitCamera.pitch = proposedPitch;
|
| 480 |
this.orbitCamera.yaw = currYaw - deltaYaw;
|
| 481 |
}
|
|
|
|
| 482 |
this.lastTouchPoint.set(touch.x, touch.y);
|
| 483 |
} else if (touches.length === 2) {
|
| 484 |
var currentPinchDistance = this.getPinchDistance(touches[0], touches[1]);
|
| 485 |
var diffInPinchDistance = currentPinchDistance - this.lastPinchDistance;
|
| 486 |
this.lastPinchDistance = currentPinchDistance;
|
|
|
|
| 487 |
this.orbitCamera.distance -= (diffInPinchDistance * this.distanceSensitivity * 0.1) * (this.orbitCamera.distance * 0.1);
|
|
|
|
| 488 |
this.calcMidPoint(touches[0], touches[1], pinchMidPoint);
|
| 489 |
this.pan(pinchMidPoint);
|
| 490 |
this.lastPinchMidPoint.copy(pinchMidPoint);
|
| 491 |
}
|
| 492 |
};
|
| 493 |
|
| 494 |
-
// ===================
|
| 495 |
-
//
|
|
|
|
|
|
|
|
|
|
| 496 |
var OrbitCameraInputKeyboard = pc.createScript('orbitCameraInputKeyboard');
|
| 497 |
-
|
|
|
|
|
|
|
| 498 |
OrbitCameraInputKeyboard.attributes.add('strafeSpeed', { type: 'number', default: 1.2, title: 'Left/Right Speed (rel. to distance)' });
|
| 499 |
-
OrbitCameraInputKeyboard.attributes.add('fastMultiplier', { type: 'number', default: 2.
|
| 500 |
-
OrbitCameraInputKeyboard.attributes.add('slowMultiplier', { type: 'number', default: 0.5, title: 'Ctrl
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 501 |
|
| 502 |
OrbitCameraInputKeyboard.prototype.initialize = function () {
|
| 503 |
this.orbitCamera = this.entity.script.orbitCamera;
|
| 504 |
this.keyboard = this.app.keyboard || null;
|
| 505 |
-
|
| 506 |
-
// If the app had no keyboard device, this script can't work.
|
| 507 |
-
// (viewer.js update below adds one)
|
| 508 |
};
|
| 509 |
|
| 510 |
OrbitCameraInputKeyboard.prototype.update = function (dt) {
|
| 511 |
if (!this.orbitCamera || !this.keyboard) return;
|
| 512 |
|
| 513 |
-
var
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 514 |
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
|
| 520 |
-
|
|
|
|
|
|
|
|
|
|
| 521 |
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 526 |
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
speedF *= this.slowMultiplier;
|
| 533 |
-
speedR *= this.slowMultiplier;
|
| 534 |
}
|
| 535 |
|
| 536 |
-
//
|
| 537 |
-
|
| 538 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 539 |
|
| 540 |
-
//
|
| 541 |
-
var
|
| 542 |
-
|
| 543 |
-
worldDiff.add(right.mulScalar(moveRight * speedR * dt));
|
| 544 |
|
| 545 |
-
if (
|
| 546 |
|
| 547 |
-
//
|
| 548 |
-
var
|
| 549 |
-
var
|
| 550 |
-
var
|
| 551 |
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
|
| 560 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 561 |
}
|
| 562 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 563 |
}
|
| 564 |
};
|
|
|
|
| 112 |
var onWindowResize = function () { self._checkAspectRatio(); };
|
| 113 |
window.addEventListener('resize', onWindowResize, false);
|
| 114 |
this._checkAspectRatio();
|
| 115 |
+
|
| 116 |
this._modelsAabb = new pc.BoundingBox();
|
| 117 |
this._buildAabb(this.focusEntity || this.app.root);
|
| 118 |
+
|
| 119 |
this.entity.lookAt(this._modelsAabb.center);
|
| 120 |
this._pivotPoint = new pc.Vec3();
|
| 121 |
this._pivotPoint.copy(this._modelsAabb.center);
|
| 122 |
+
|
| 123 |
var cameraQuat = this.entity.getRotation();
|
| 124 |
this._yaw = this._calcYaw(cameraQuat);
|
| 125 |
this._pitch = this._clampPitchAngle(this._calcPitch(cameraQuat, this._yaw));
|
| 126 |
this.entity.setLocalEulerAngles(this._pitch, this._yaw, 0);
|
| 127 |
+
|
| 128 |
this._distance = 0;
|
| 129 |
this._targetYaw = this._yaw;
|
| 130 |
this._targetPitch = this._pitch;
|
| 131 |
+
|
| 132 |
if (this.frameOnStart) {
|
| 133 |
this.focus(this.focusEntity || this.app.root);
|
| 134 |
} else {
|
|
|
|
| 137 |
this._distance = this._clampDistance(distanceBetween.length());
|
| 138 |
}
|
| 139 |
this._targetDistance = this._distance;
|
| 140 |
+
|
| 141 |
this.on('attr:distanceMin', function () { this._distance = this._clampDistance(this._distance); });
|
| 142 |
this.on('attr:distanceMax', function () { this._distance = this._clampDistance(this._distance); });
|
| 143 |
this.on('attr:pitchAngleMin', function () { this._pitch = this._clampPitchAngle(this._pitch); });
|
| 144 |
this.on('attr:pitchAngleMax', function () { this._pitch = this._clampPitchAngle(this._pitch); });
|
| 145 |
+
|
| 146 |
this.on('attr:focusEntity', function (value) {
|
| 147 |
if (this.frameOnStart) { this.focus(value || this.app.root); }
|
| 148 |
else { this.resetAndLookAtEntity(this.entity.getPosition(), value || this.app.root); }
|
| 149 |
});
|
| 150 |
+
|
| 151 |
this.on('attr:frameOnStart', function (value) {
|
| 152 |
if (value) { this.focus(this.focusEntity || this.app.root); }
|
| 153 |
});
|
| 154 |
+
|
| 155 |
this.on('destroy', function () { window.removeEventListener('resize', onWindowResize, false); });
|
| 156 |
};
|
| 157 |
|
|
|
|
| 166 |
OrbitCamera.prototype._updatePosition = function () {
|
| 167 |
this.entity.setLocalPosition(0, 0, 0);
|
| 168 |
this.entity.setLocalEulerAngles(this._pitch, this._yaw, 0);
|
| 169 |
+
|
| 170 |
var position = this.entity.getPosition();
|
| 171 |
position.copy(this.entity.forward);
|
| 172 |
position.mulScalar(-this._distance);
|
| 173 |
position.add(this.pivotPoint);
|
| 174 |
+
|
| 175 |
// Clamp camera's Y position so it never goes below minY
|
| 176 |
position.y = Math.max(position.y, this.minY);
|
| 177 |
+
|
| 178 |
this.entity.setPosition(position);
|
| 179 |
};
|
| 180 |
|
|
|
|
| 192 |
|
| 193 |
OrbitCamera.prototype._buildAabb = function (entity) {
|
| 194 |
var i, m, meshInstances = [];
|
| 195 |
+
|
| 196 |
var renders = entity.findComponents('render');
|
| 197 |
for (i = 0; i < renders.length; i++) {
|
| 198 |
var render = renders[i];
|
|
|
|
| 200 |
meshInstances.push(render.meshInstances[m]);
|
| 201 |
}
|
| 202 |
}
|
| 203 |
+
|
| 204 |
var models = entity.findComponents('model');
|
| 205 |
for (i = 0; i < models.length; i++) {
|
| 206 |
var model = models[i];
|
|
|
|
| 208 |
meshInstances.push(model.meshInstances[m]);
|
| 209 |
}
|
| 210 |
}
|
| 211 |
+
|
| 212 |
var gsplats = entity.findComponents('gsplat');
|
| 213 |
for (i = 0; i < gsplats.length; i++) {
|
| 214 |
var gsplat = gsplats[i];
|
|
|
|
| 217 |
meshInstances.push(instance.meshInstance);
|
| 218 |
}
|
| 219 |
}
|
| 220 |
+
|
| 221 |
for (i = 0; i < meshInstances.length; i++) {
|
| 222 |
if (i === 0) {
|
| 223 |
this._modelsAabb.copy(meshInstances[i].aabb);
|
|
|
|
| 268 |
|
| 269 |
OrbitCameraInputMouse.prototype.initialize = function () {
|
| 270 |
this.orbitCamera = this.entity.script.orbitCamera;
|
| 271 |
+
|
| 272 |
if (this.orbitCamera) {
|
| 273 |
var self = this;
|
| 274 |
+
var onMouseOut = function () { self.onMouseOut(); };
|
| 275 |
+
|
| 276 |
this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onMouseDown, this);
|
| 277 |
this.app.mouse.on(pc.EVENT_MOUSEUP, this.onMouseUp, this);
|
| 278 |
this.app.mouse.on(pc.EVENT_MOUSEMOVE, this.onMouseMove, this);
|
| 279 |
this.app.mouse.on(pc.EVENT_MOUSEWHEEL, this.onMouseWheel, this);
|
| 280 |
window.addEventListener('mouseout', onMouseOut, false);
|
| 281 |
+
|
| 282 |
this.on('destroy', function () {
|
| 283 |
this.app.mouse.off(pc.EVENT_MOUSEDOWN, this.onMouseDown, this);
|
| 284 |
this.app.mouse.off(pc.EVENT_MOUSEUP, this.onMouseUp, this);
|
|
|
|
| 287 |
window.removeEventListener('mouseout', onMouseOut, false);
|
| 288 |
});
|
| 289 |
}
|
| 290 |
+
|
| 291 |
this.app.mouse.disableContextMenu();
|
| 292 |
+
|
| 293 |
this.lookButtonDown = false;
|
| 294 |
this.panButtonDown = false;
|
| 295 |
this.lastPoint = new pc.Vec2();
|
| 296 |
};
|
| 297 |
+
|
| 298 |
OrbitCameraInputMouse.fromWorldPoint = new pc.Vec3();
|
| 299 |
OrbitCameraInputMouse.toWorldPoint = new pc.Vec3();
|
| 300 |
OrbitCameraInputMouse.worldDiff = new pc.Vec3();
|
|
|
|
| 303 |
var fromWorldPoint = OrbitCameraInputMouse.fromWorldPoint;
|
| 304 |
var toWorldPoint = OrbitCameraInputMouse.toWorldPoint;
|
| 305 |
var worldDiff = OrbitCameraInputMouse.worldDiff;
|
| 306 |
+
|
| 307 |
var camera = this.entity.camera;
|
| 308 |
var distance = this.orbitCamera.distance;
|
| 309 |
+
|
| 310 |
camera.screenToWorld(screenPoint.x, screenPoint.y, distance, fromWorldPoint);
|
| 311 |
camera.screenToWorld(this.lastPoint.x, this.lastPoint.y, distance, toWorldPoint);
|
| 312 |
+
|
| 313 |
worldDiff.sub2(toWorldPoint, fromWorldPoint);
|
| 314 |
|
| 315 |
+
// Enforce minY like vertical/horizontal pan with arrows
|
| 316 |
var proposedPivot = this.orbitCamera.pivotPoint.clone().add(worldDiff);
|
| 317 |
var minY = this.orbitCamera.minY;
|
| 318 |
var resultingY = this.orbitCamera.worldCameraYForPivot(proposedPivot);
|
|
|
|
| 320 |
if (resultingY >= minY - 1e-4) {
|
| 321 |
this.orbitCamera.pivotPoint.add(worldDiff);
|
| 322 |
} else {
|
| 323 |
+
// Try horizontal-only
|
| 324 |
worldDiff.y = 0;
|
| 325 |
proposedPivot = this.orbitCamera.pivotPoint.clone().add(worldDiff);
|
| 326 |
resultingY = this.orbitCamera.worldCameraYForPivot(proposedPivot);
|
| 327 |
if (resultingY >= minY - 1e-4) {
|
| 328 |
this.orbitCamera.pivotPoint.add(worldDiff);
|
| 329 |
}
|
|
|
|
| 330 |
}
|
| 331 |
};
|
| 332 |
|
|
|
|
| 337 |
case pc.MOUSEBUTTON_RIGHT: this.lookButtonDown = true; break;
|
| 338 |
}
|
| 339 |
};
|
| 340 |
+
|
| 341 |
OrbitCameraInputMouse.prototype.onMouseUp = function (event) {
|
| 342 |
switch (event.button) {
|
| 343 |
case pc.MOUSEBUTTON_LEFT: this.panButtonDown = false; break;
|
|
|
|
| 345 |
case pc.MOUSEBUTTON_RIGHT: this.lookButtonDown = false; break;
|
| 346 |
}
|
| 347 |
};
|
| 348 |
+
|
| 349 |
OrbitCameraInputMouse.prototype.onMouseMove = function (event) {
|
| 350 |
if (this.lookButtonDown) {
|
| 351 |
var sens = this.orbitSensitivity;
|
| 352 |
+
|
| 353 |
+
var deltaPitch = event.dy * sens; // mouse up (dy < 0) raises pitch
|
| 354 |
var deltaYaw = event.dx * sens;
|
| 355 |
|
| 356 |
var currPitch = this.orbitCamera.pitch;
|
|
|
|
| 373 |
|
| 374 |
var minY = this.orbitCamera.minY;
|
| 375 |
var wouldGoBelow = proposedY < minY - 1e-4;
|
| 376 |
+
|
| 377 |
if (wouldGoBelow && (proposedY < preY)) {
|
| 378 |
this.orbitCamera.yaw = currYaw - deltaYaw;
|
| 379 |
} else {
|
|
|
|
| 383 |
} else if (this.panButtonDown) {
|
| 384 |
this.pan(new pc.Vec2(event.x, event.y));
|
| 385 |
}
|
| 386 |
+
|
| 387 |
this.lastPoint.set(event.x, event.y);
|
| 388 |
};
|
| 389 |
|
|
|
|
| 395 |
}
|
| 396 |
event.event.preventDefault();
|
| 397 |
};
|
| 398 |
+
|
| 399 |
OrbitCameraInputMouse.prototype.onMouseOut = function () {
|
| 400 |
this.lookButtonDown = false;
|
| 401 |
this.panButtonDown = false;
|
|
|
|
| 405 |
var OrbitCameraInputTouch = pc.createScript('orbitCameraInputTouch');
|
| 406 |
OrbitCameraInputTouch.attributes.add('orbitSensitivity', { type: 'number', default: 0.6, title: 'Orbit Sensitivity' });
|
| 407 |
OrbitCameraInputTouch.attributes.add('distanceSensitivity', { type: 'number', default: 0.5, title: 'Distance Sensitivity' });
|
| 408 |
+
|
| 409 |
OrbitCameraInputTouch.prototype.initialize = function () {
|
| 410 |
this.orbitCamera = this.entity.script.orbitCamera;
|
| 411 |
this.lastTouchPoint = new pc.Vec2();
|
| 412 |
this.lastPinchMidPoint = new pc.Vec2();
|
| 413 |
this.lastPinchDistance = 0;
|
| 414 |
+
|
| 415 |
if (this.orbitCamera && this.app.touch) {
|
| 416 |
this.app.touch.on(pc.EVENT_TOUCHSTART, this.onTouchStartEndCancel, this);
|
| 417 |
this.app.touch.on(pc.EVENT_TOUCHEND, this.onTouchStartEndCancel, this);
|
| 418 |
this.app.touch.on(pc.EVENT_TOUCHCANCEL, this.onTouchStartEndCancel, this);
|
| 419 |
this.app.touch.on(pc.EVENT_TOUCHMOVE, this.onTouchMove, this);
|
| 420 |
+
|
| 421 |
this.on('destroy', function () {
|
| 422 |
this.app.touch.off(pc.EVENT_TOUCHSTART, this.onTouchStartEndCancel, this);
|
| 423 |
this.app.touch.off(pc.EVENT_TOUCHEND, this.onTouchStartEndCancel, this);
|
|
|
|
| 426 |
});
|
| 427 |
}
|
| 428 |
};
|
| 429 |
+
|
| 430 |
OrbitCameraInputTouch.prototype.getPinchDistance = function (pointA, pointB) {
|
| 431 |
var dx = pointA.x - pointB.x;
|
| 432 |
var dy = pointA.y - pointB.y;
|
| 433 |
return Math.sqrt((dx * dx) + (dy * dy));
|
| 434 |
};
|
| 435 |
+
|
| 436 |
OrbitCameraInputTouch.prototype.calcMidPoint = function (pointA, pointB, result) {
|
| 437 |
result.set(pointB.x - pointA.x, pointB.y - pointA.y);
|
| 438 |
result.mulScalar(0.5);
|
| 439 |
result.x += pointA.x;
|
| 440 |
result.y += pointA.y;
|
| 441 |
};
|
| 442 |
+
|
| 443 |
OrbitCameraInputTouch.prototype.onTouchStartEndCancel = function (event) {
|
| 444 |
var touches = event.touches;
|
| 445 |
if (touches.length === 1) {
|
|
|
|
| 449 |
this.calcMidPoint(touches[0], touches[1], this.lastPinchMidPoint);
|
| 450 |
}
|
| 451 |
};
|
| 452 |
+
|
| 453 |
OrbitCameraInputTouch.fromWorldPoint = new pc.Vec3();
|
| 454 |
OrbitCameraInputTouch.toWorldPoint = new pc.Vec3();
|
| 455 |
OrbitCameraInputTouch.worldDiff = new pc.Vec3();
|
| 456 |
+
|
| 457 |
OrbitCameraInputTouch.prototype.pan = function (midPoint) {
|
| 458 |
var fromWorldPoint = OrbitCameraInputTouch.fromWorldPoint;
|
| 459 |
var toWorldPoint = OrbitCameraInputTouch.toWorldPoint;
|
| 460 |
var worldDiff = OrbitCameraInputTouch.worldDiff;
|
| 461 |
+
|
| 462 |
var camera = this.entity.camera;
|
| 463 |
var distance = this.orbitCamera.distance;
|
| 464 |
+
|
| 465 |
camera.screenToWorld(midPoint.x, midPoint.y, distance, fromWorldPoint);
|
| 466 |
camera.screenToWorld(this.lastPinchMidPoint.x, this.lastPinchMidPoint.y, distance, toWorldPoint);
|
| 467 |
+
|
| 468 |
worldDiff.sub2(toWorldPoint, fromWorldPoint);
|
| 469 |
|
| 470 |
var proposedPivot = this.orbitCamera.pivotPoint.clone().add(worldDiff);
|
|
|
|
| 488 |
OrbitCameraInputTouch.prototype.onTouchMove = function (event) {
|
| 489 |
var pinchMidPoint = OrbitCameraInputTouch.pinchMidPoint;
|
| 490 |
var touches = event.touches;
|
| 491 |
+
|
| 492 |
if (touches.length === 1) {
|
| 493 |
var touch = touches[0];
|
| 494 |
var sens = this.orbitSensitivity;
|
| 495 |
+
|
| 496 |
var deltaPitch = (touch.y - this.lastTouchPoint.y) * sens;
|
| 497 |
var deltaYaw = (touch.x - this.lastTouchPoint.x) * sens;
|
| 498 |
|
|
|
|
| 516 |
|
| 517 |
var minY = this.orbitCamera.minY;
|
| 518 |
var wouldGoBelow = proposedY < minY - 1e-4;
|
| 519 |
+
|
| 520 |
if (wouldGoBelow && (proposedY < preY)) {
|
| 521 |
this.orbitCamera.yaw = currYaw - deltaYaw;
|
| 522 |
} else {
|
| 523 |
this.orbitCamera.pitch = proposedPitch;
|
| 524 |
this.orbitCamera.yaw = currYaw - deltaYaw;
|
| 525 |
}
|
| 526 |
+
|
| 527 |
this.lastTouchPoint.set(touch.x, touch.y);
|
| 528 |
} else if (touches.length === 2) {
|
| 529 |
var currentPinchDistance = this.getPinchDistance(touches[0], touches[1]);
|
| 530 |
var diffInPinchDistance = currentPinchDistance - this.lastPinchDistance;
|
| 531 |
this.lastPinchDistance = currentPinchDistance;
|
| 532 |
+
|
| 533 |
this.orbitCamera.distance -= (diffInPinchDistance * this.distanceSensitivity * 0.1) * (this.orbitCamera.distance * 0.1);
|
| 534 |
+
|
| 535 |
this.calcMidPoint(touches[0], touches[1], pinchMidPoint);
|
| 536 |
this.pan(pinchMidPoint);
|
| 537 |
this.lastPinchMidPoint.copy(pinchMidPoint);
|
| 538 |
}
|
| 539 |
};
|
| 540 |
|
| 541 |
+
// =================== Orbit Camera Input Keyboard ========================
|
| 542 |
+
// Arrows:
|
| 543 |
+
// - No modifier: Up/Down = vertical pan; Left/Right = strafe
|
| 544 |
+
// - With Shift: Up/Down = orbit pitch; Left/Right = orbit yaw
|
| 545 |
+
// - With Ctrl: Up/Down = zoom in/out
|
| 546 |
var OrbitCameraInputKeyboard = pc.createScript('orbitCameraInputKeyboard');
|
| 547 |
+
|
| 548 |
+
// Keep names for backward compatibility with your viewer.js
|
| 549 |
+
OrbitCameraInputKeyboard.attributes.add('forwardSpeed', { type: 'number', default: 1.2, title: 'Vertical Speed (rel. to distance)' });
|
| 550 |
OrbitCameraInputKeyboard.attributes.add('strafeSpeed', { type: 'number', default: 1.2, title: 'Left/Right Speed (rel. to distance)' });
|
| 551 |
+
OrbitCameraInputKeyboard.attributes.add('fastMultiplier', { type: 'number', default: 2.0, title: 'Shift Speed Multiplier' });
|
| 552 |
+
OrbitCameraInputKeyboard.attributes.add('slowMultiplier', { type: 'number', default: 0.5, title: 'Ctrl Slow Multiplier (when translating)' });
|
| 553 |
+
|
| 554 |
+
// New fine-tuning attributes
|
| 555 |
+
OrbitCameraInputKeyboard.attributes.add('orbitPitchSpeedDeg', { type: 'number', default: 90, title: 'Orbit Pitch Speed (deg/s) [Shift+Up/Down]' });
|
| 556 |
+
OrbitCameraInputKeyboard.attributes.add('orbitYawSpeedDeg', { type: 'number', default: 120, title: 'Orbit Yaw Speed (deg/s) [Shift+Left/Right]' });
|
| 557 |
+
OrbitCameraInputKeyboard.attributes.add('zoomKeySensitivity', { type: 'number', default: 3.0, title: 'Zoom Sensitivity (1/s) [Ctrl+Up/Down]' });
|
| 558 |
|
| 559 |
OrbitCameraInputKeyboard.prototype.initialize = function () {
|
| 560 |
this.orbitCamera = this.entity.script.orbitCamera;
|
| 561 |
this.keyboard = this.app.keyboard || null;
|
|
|
|
|
|
|
|
|
|
| 562 |
};
|
| 563 |
|
| 564 |
OrbitCameraInputKeyboard.prototype.update = function (dt) {
|
| 565 |
if (!this.orbitCamera || !this.keyboard) return;
|
| 566 |
|
| 567 |
+
var up = this.keyboard.isPressed(pc.KEY_UP);
|
| 568 |
+
var dn = this.keyboard.isPressed(pc.KEY_DOWN);
|
| 569 |
+
var lt = this.keyboard.isPressed(pc.KEY_LEFT);
|
| 570 |
+
var rt = this.keyboard.isPressed(pc.KEY_RIGHT);
|
| 571 |
+
|
| 572 |
+
var shift = this.keyboard.isPressed(pc.KEY_SHIFT);
|
| 573 |
+
var ctrl = this.keyboard.isPressed(pc.KEY_CONTROL);
|
| 574 |
+
|
| 575 |
+
// ---- SHIFT: Orbit (pitch / yaw) ----
|
| 576 |
+
if (shift && (up || dn || lt || rt)) {
|
| 577 |
+
// Yaw (left/right) — follow same sign convention as mouse: yaw = yaw - delta
|
| 578 |
+
var yawDeltaDeg = (rt ? 1 : 0) - (lt ? 1 : 0);
|
| 579 |
+
if (yawDeltaDeg !== 0) {
|
| 580 |
+
var dYaw = yawDeltaDeg * this.orbitYawSpeedDeg * dt;
|
| 581 |
+
this.orbitCamera.yaw = this.orbitCamera.yaw - dYaw; // setter clamps
|
| 582 |
+
}
|
| 583 |
|
| 584 |
+
// Pitch (up/down) — enforce minY like mouse rotate
|
| 585 |
+
var pitchDeltaDeg = (up ? 1 : 0) - (dn ? 1 : 0);
|
| 586 |
+
if (pitchDeltaDeg !== 0) {
|
| 587 |
+
var dPitch = pitchDeltaDeg * this.orbitPitchSpeedDeg * dt;
|
| 588 |
|
| 589 |
+
var currPitch = this.orbitCamera.pitch;
|
| 590 |
+
var currYaw = this.orbitCamera.yaw;
|
| 591 |
+
var currDist = this.orbitCamera.distance;
|
| 592 |
+
var currPivot = this.orbitCamera.pivotPoint.clone();
|
| 593 |
|
| 594 |
+
var camQuat = new pc.Quat().setFromEulerAngles(currPitch, currYaw, 0);
|
| 595 |
+
var forward = new pc.Vec3(); camQuat.transformVector(pc.Vec3.FORWARD, forward);
|
| 596 |
+
var preY = currPivot.y + (-forward.y) * currDist;
|
| 597 |
+
|
| 598 |
+
var testPitch = currPitch + dPitch;
|
| 599 |
+
var testQuat = new pc.Quat().setFromEulerAngles(testPitch, currYaw, 0);
|
| 600 |
+
var testForward = new pc.Vec3(); testQuat.transformVector(pc.Vec3.FORWARD, testForward);
|
| 601 |
+
var proposedY = currPivot.y + (-testForward.y) * currDist;
|
| 602 |
+
|
| 603 |
+
var minY = this.orbitCamera.minY;
|
| 604 |
+
var wouldGoBelow = proposedY < minY - 1e-4;
|
| 605 |
|
| 606 |
+
if (!(wouldGoBelow && (proposedY < preY))) {
|
| 607 |
+
this.orbitCamera.pitch = testPitch; // setter clamps
|
| 608 |
+
}
|
| 609 |
+
}
|
| 610 |
+
return;
|
|
|
|
|
|
|
| 611 |
}
|
| 612 |
|
| 613 |
+
// ---- CTRL: Zoom (up/down) ----
|
| 614 |
+
if (ctrl && (up || dn)) {
|
| 615 |
+
var zoomSign = (up ? 1 : 0) - (dn ? 1 : 0); // up => +1 (zoom in), down => -1 (zoom out)
|
| 616 |
+
if (zoomSign !== 0) {
|
| 617 |
+
if (this.entity.camera.projection === pc.PROJECTION_PERSPECTIVE) {
|
| 618 |
+
var dz = zoomSign * this.zoomKeySensitivity * (this.orbitCamera.distance * 0.5) * dt;
|
| 619 |
+
this.orbitCamera.distance -= dz; // setter clamps
|
| 620 |
+
} else {
|
| 621 |
+
var doh = zoomSign * this.zoomKeySensitivity * (this.orbitCamera.orthoHeight * 0.5) * dt;
|
| 622 |
+
this.orbitCamera.orthoHeight -= doh; // clamped >= 0 by setter
|
| 623 |
+
}
|
| 624 |
+
}
|
| 625 |
+
return;
|
| 626 |
+
}
|
| 627 |
|
| 628 |
+
// ---- No modifier: translate (vertical + strafe) ----
|
| 629 |
+
var moveVert = (up ? 1 : 0) - (dn ? 1 : 0); // Up=+1 => go up
|
| 630 |
+
var moveRight = (rt ? 1 : 0) - (lt ? 1 : 0);
|
|
|
|
| 631 |
|
| 632 |
+
if (moveVert === 0 && moveRight === 0) return;
|
| 633 |
|
| 634 |
+
// Base speeds relative to distance (consistent feel near/far)
|
| 635 |
+
var dist = Math.max(0.1, this.orbitCamera.distance);
|
| 636 |
+
var speedV = this.forwardSpeed * dist; // vertical speed (reuses attribute name)
|
| 637 |
+
var speedR = this.strafeSpeed * dist;
|
| 638 |
|
| 639 |
+
// If user also holds Ctrl here (without Shift), we use it as "slow"
|
| 640 |
+
if (ctrl) { speedV *= this.slowMultiplier; speedR *= this.slowMultiplier; }
|
| 641 |
+
if (shift) { speedV *= this.fastMultiplier; speedR *= this.fastMultiplier; }
|
| 642 |
+
|
| 643 |
+
// Vertical move (pure Y in world space), respecting minY exactly:
|
| 644 |
+
var dy = moveVert * speedV * dt;
|
| 645 |
+
if (dy !== 0) {
|
| 646 |
+
var currentCamY = this.orbitCamera.worldCameraYForPivot(this.orbitCamera.pivotPoint);
|
| 647 |
+
var minY = this.orbitCamera.minY;
|
| 648 |
+
|
| 649 |
+
// cameraY with vertical pan changes by the same dy (cameraY = pivotY + const)
|
| 650 |
+
var proposedCamY = currentCamY + dy;
|
| 651 |
+
if (proposedCamY < minY) {
|
| 652 |
+
// clamp movement so we exactly stop at minY
|
| 653 |
+
dy = Math.max(dy, minY - currentCamY);
|
| 654 |
+
}
|
| 655 |
+
if (dy !== 0) {
|
| 656 |
+
this.orbitCamera._pivotPoint.y += dy;
|
| 657 |
}
|
| 658 |
+
}
|
| 659 |
+
|
| 660 |
+
// Strafe move (left/right on XZ plane). Does not change pivot.y, so cameraY unchanged.
|
| 661 |
+
var right = this.entity.right.clone(); right.y = 0; if (right.lengthSq() > 1e-8) right.normalize();
|
| 662 |
+
var dx = moveRight * speedR * dt;
|
| 663 |
+
if (dx !== 0) {
|
| 664 |
+
this.orbitCamera._pivotPoint.add(right.mulScalar(dx));
|
| 665 |
}
|
| 666 |
};
|