Update orbit-camera.js
Browse files- orbit-camera.js +21 -31
orbit-camera.js
CHANGED
|
@@ -291,7 +291,7 @@ OrbitCameraInputMouse.prototype.initialize = function () {
|
|
| 291 |
this.app.mouse.disableContextMenu();
|
| 292 |
|
| 293 |
this.lookButtonDown = false;
|
| 294 |
-
|
| 295 |
this.lastPoint = new pc.Vec2();
|
| 296 |
};
|
| 297 |
|
|
@@ -541,18 +541,15 @@ OrbitCameraInputTouch.prototype.onTouchMove = function (event) {
|
|
| 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 |
-
//
|
| 555 |
-
OrbitCameraInputKeyboard.attributes.add('orbitPitchSpeedDeg', { type: 'number', default: 90,
|
| 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 |
|
|
@@ -574,17 +571,17 @@ OrbitCameraInputKeyboard.prototype.update = function (dt) {
|
|
| 574 |
|
| 575 |
// ---- SHIFT: Orbit (pitch / yaw) ----
|
| 576 |
if (shift && (up || dn || lt || rt)) {
|
| 577 |
-
// Yaw (left/right) —
|
| 578 |
-
var
|
| 579 |
-
if (
|
| 580 |
-
var dYaw =
|
| 581 |
-
this.orbitCamera.yaw = this.orbitCamera.yaw
|
| 582 |
}
|
| 583 |
|
| 584 |
-
// Pitch (up/down) —
|
| 585 |
-
var
|
| 586 |
-
if (
|
| 587 |
-
var dPitch =
|
| 588 |
|
| 589 |
var currPitch = this.orbitCamera.pitch;
|
| 590 |
var currYaw = this.orbitCamera.yaw;
|
|
@@ -604,7 +601,7 @@ OrbitCameraInputKeyboard.prototype.update = function (dt) {
|
|
| 604 |
var wouldGoBelow = proposedY < minY - 1e-4;
|
| 605 |
|
| 606 |
if (!(wouldGoBelow && (proposedY < preY))) {
|
| 607 |
-
this.orbitCamera.pitch = testPitch; //
|
| 608 |
}
|
| 609 |
}
|
| 610 |
return;
|
|
@@ -612,14 +609,14 @@ OrbitCameraInputKeyboard.prototype.update = function (dt) {
|
|
| 612 |
|
| 613 |
// ---- CTRL: Zoom (up/down) ----
|
| 614 |
if (ctrl && (up || dn)) {
|
| 615 |
-
var zoomSign = (up ? 1 : 0) - (dn ? 1 : 0); // up
|
| 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; //
|
| 620 |
} else {
|
| 621 |
var doh = zoomSign * this.zoomKeySensitivity * (this.orbitCamera.orthoHeight * 0.5) * dt;
|
| 622 |
-
this.orbitCamera.orthoHeight -= doh; // clamped >= 0
|
| 623 |
}
|
| 624 |
}
|
| 625 |
return;
|
|
@@ -633,23 +630,16 @@ OrbitCameraInputKeyboard.prototype.update = function (dt) {
|
|
| 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
|
| 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
|
| 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) {
|
|
@@ -657,7 +647,7 @@ OrbitCameraInputKeyboard.prototype.update = function (dt) {
|
|
| 657 |
}
|
| 658 |
}
|
| 659 |
|
| 660 |
-
// Strafe
|
| 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) {
|
|
|
|
| 291 |
this.app.mouse.disableContextMenu();
|
| 292 |
|
| 293 |
this.lookButtonDown = false;
|
| 294 |
+
this.panButtonDown = false;
|
| 295 |
this.lastPoint = new pc.Vec2();
|
| 296 |
};
|
| 297 |
|
|
|
|
| 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 (inverted as requested)
|
| 545 |
// - With Ctrl: Up/Down = zoom in/out
|
| 546 |
var OrbitCameraInputKeyboard = pc.createScript('orbitCameraInputKeyboard');
|
| 547 |
|
|
|
|
| 548 |
OrbitCameraInputKeyboard.attributes.add('forwardSpeed', { type: 'number', default: 1.2, title: 'Vertical Speed (rel. to distance)' });
|
| 549 |
OrbitCameraInputKeyboard.attributes.add('strafeSpeed', { type: 'number', default: 1.2, title: 'Left/Right Speed (rel. to distance)' });
|
|
|
|
|
|
|
| 550 |
|
| 551 |
+
// Fine-tuning
|
| 552 |
+
OrbitCameraInputKeyboard.attributes.add('orbitPitchSpeedDeg', { type: 'number', default: 90, title: 'Orbit Pitch Speed (deg/s) [Shift+Up/Down]' });
|
| 553 |
OrbitCameraInputKeyboard.attributes.add('orbitYawSpeedDeg', { type: 'number', default: 120, title: 'Orbit Yaw Speed (deg/s) [Shift+Left/Right]' });
|
| 554 |
OrbitCameraInputKeyboard.attributes.add('zoomKeySensitivity', { type: 'number', default: 3.0, title: 'Zoom Sensitivity (1/s) [Ctrl+Up/Down]' });
|
| 555 |
|
|
|
|
| 571 |
|
| 572 |
// ---- SHIFT: Orbit (pitch / yaw) ----
|
| 573 |
if (shift && (up || dn || lt || rt)) {
|
| 574 |
+
// Yaw (left/right) — inverted per request: Shift+Right => orbit to the right
|
| 575 |
+
var yawDir = (rt ? 1 : 0) - (lt ? 1 : 0); // right=+1, left=-1
|
| 576 |
+
if (yawDir !== 0) {
|
| 577 |
+
var dYaw = yawDir * this.orbitYawSpeedDeg * dt;
|
| 578 |
+
this.orbitCamera.yaw = this.orbitCamera.yaw + dYaw; // NOTE: '+' (inversion)
|
| 579 |
}
|
| 580 |
|
| 581 |
+
// Pitch (up/down) — Shift+Up => orbit up, Shift+Down => down
|
| 582 |
+
var pitchDir = (up ? 1 : 0) - (dn ? 1 : 0); // up=+1, down=-1
|
| 583 |
+
if (pitchDir !== 0) {
|
| 584 |
+
var dPitch = pitchDir * this.orbitPitchSpeedDeg * dt;
|
| 585 |
|
| 586 |
var currPitch = this.orbitCamera.pitch;
|
| 587 |
var currYaw = this.orbitCamera.yaw;
|
|
|
|
| 601 |
var wouldGoBelow = proposedY < minY - 1e-4;
|
| 602 |
|
| 603 |
if (!(wouldGoBelow && (proposedY < preY))) {
|
| 604 |
+
this.orbitCamera.pitch = testPitch; // clamped by setter
|
| 605 |
}
|
| 606 |
}
|
| 607 |
return;
|
|
|
|
| 609 |
|
| 610 |
// ---- CTRL: Zoom (up/down) ----
|
| 611 |
if (ctrl && (up || dn)) {
|
| 612 |
+
var zoomSign = (up ? 1 : 0) - (dn ? 1 : 0); // up=zoom in, down=zoom out
|
| 613 |
if (zoomSign !== 0) {
|
| 614 |
if (this.entity.camera.projection === pc.PROJECTION_PERSPECTIVE) {
|
| 615 |
var dz = zoomSign * this.zoomKeySensitivity * (this.orbitCamera.distance * 0.5) * dt;
|
| 616 |
+
this.orbitCamera.distance -= dz; // clamped
|
| 617 |
} else {
|
| 618 |
var doh = zoomSign * this.zoomKeySensitivity * (this.orbitCamera.orthoHeight * 0.5) * dt;
|
| 619 |
+
this.orbitCamera.orthoHeight -= doh; // clamped >= 0
|
| 620 |
}
|
| 621 |
}
|
| 622 |
return;
|
|
|
|
| 630 |
|
| 631 |
// Base speeds relative to distance (consistent feel near/far)
|
| 632 |
var dist = Math.max(0.1, this.orbitCamera.distance);
|
| 633 |
+
var speedV = this.forwardSpeed * dist; // vertical speed
|
| 634 |
+
var speedR = this.strafeSpeed * dist; // strafe speed
|
|
|
|
|
|
|
|
|
|
|
|
|
| 635 |
|
| 636 |
+
// Vertical move (pure Y), respect minY
|
| 637 |
var dy = moveVert * speedV * dt;
|
| 638 |
if (dy !== 0) {
|
| 639 |
var currentCamY = this.orbitCamera.worldCameraYForPivot(this.orbitCamera.pivotPoint);
|
| 640 |
var minY = this.orbitCamera.minY;
|
|
|
|
|
|
|
| 641 |
var proposedCamY = currentCamY + dy;
|
| 642 |
if (proposedCamY < minY) {
|
|
|
|
| 643 |
dy = Math.max(dy, minY - currentCamY);
|
| 644 |
}
|
| 645 |
if (dy !== 0) {
|
|
|
|
| 647 |
}
|
| 648 |
}
|
| 649 |
|
| 650 |
+
// Strafe on XZ
|
| 651 |
var right = this.entity.right.clone(); right.y = 0; if (right.lengthSq() > 1e-8) right.normalize();
|
| 652 |
var dx = moveRight * speedR * dt;
|
| 653 |
if (dx !== 0) {
|