Spaces:
Running
Running
File size: 26,924 Bytes
0073426 9b1c4ac c423f3d 742d619 9b1c4ac 2a56e9e 742d619 0073426 2a56e9e 9b1c4ac 742d619 0073426 742d619 2a56e9e 742d619 0073426 742d619 2a56e9e 742d619 0073426 742d619 2a56e9e 9b1c4ac 2a56e9e 742d619 2a56e9e 742d619 2a56e9e 9b1c4ac 2a56e9e 742d619 fd3c3d3 0073426 9b1c4ac 742d619 2a56e9e 0073426 2a56e9e fd3c3d3 2a56e9e fd3c3d3 2a56e9e 742d619 2a56e9e fd3c3d3 742d619 9b1c4ac 742d619 2a56e9e fd3c3d3 2a56e9e 742d619 fd3c3d3 2a56e9e 742d619 fd3c3d3 40eec9e fd3c3d3 40eec9e 0073426 2a56e9e fd3c3d3 40eec9e 0073426 2a56e9e f2a61e1 fd3c3d3 742d619 9b1c4ac 2a56e9e 490015c 2a56e9e fd3c3d3 2a56e9e fd3c3d3 9b1c4ac 2a56e9e fd3c3d3 2a56e9e c423f3d 2a56e9e dd7950b fd3c3d3 dd7950b fd3c3d3 dd7950b fd3c3d3 dd7950b 9b1c4ac dd7950b fd3c3d3 dd7950b 9b1c4ac fd3c3d3 dd7950b fd3c3d3 dd7950b 9b1c4ac dd7950b fd3c3d3 dd7950b 9b1c4ac dd7950b fd3c3d3 dd7950b fd3c3d3 dd7950b 9b1c4ac dd7950b 9b1c4ac dd7950b fd3c3d3 dd7950b 9b1c4ac dd7950b 9b1c4ac dd7950b fd3c3d3 dd7950b fd3c3d3 dd7950b fd3c3d3 dd7950b 9b1c4ac dd7950b 9b1c4ac dd7950b 9b1c4ac fd3c3d3 9b1c4ac dd7950b fd3c3d3 dd7950b 9b1c4ac fd3c3d3 dd7950b fd3c3d3 dd7950b fd3c3d3 dd7950b 9b1c4ac dd7950b 9b1c4ac dd7950b fd3c3d3 dd7950b 9b1c4ac dd7950b 9b1c4ac dd7950b fd3c3d3 dd7950b fd3c3d3 9b1c4ac dd7950b 9b1c4ac dd7950b 9b1c4ac dd7950b 9b1c4ac dd7950b fd3c3d3 dd7950b 9b1c4ac dd7950b 9b1c4ac dd7950b fd3c3d3 dd7950b fd3c3d3 dd7950b 9b1c4ac dd7950b fd3c3d3 dd7950b 9b1c4ac dd7950b fd3c3d3 dd7950b 9b1c4ac dd7950b fd3c3d3 dd7950b 9b1c4ac dd7950b 9b1c4ac fd3c3d3 dd7950b 9b1c4ac dd7950b 9b1c4ac dd7950b fd3c3d3 dd7950b fd3c3d3 dd7950b fd3c3d3 dd7950b fd3c3d3 dd7950b 9b1c4ac fd3c3d3 dd7950b 9b1c4ac fd3c3d3 9b1c4ac dd7950b fd3c3d3 dd7950b fd3c3d3 dd7950b fd3c3d3 dd7950b fd3c3d3 dd7950b 9b1c4ac dd7950b 9b1c4ac dd7950b 9b1c4ac dd7950b 9b1c4ac dd7950b fd3c3d3 dd7950b 9b1c4ac dd7950b 9b1c4ac dd7950b fd3c3d3 dd7950b 9b1c4ac dd7950b fd3c3d3 dd7950b fd3c3d3 dd7950b d1d9c09 dd7950b fd3c3d3 dd7950b d1d9c09 dd7950b fd3c3d3 d1d9c09 fd3c3d3 d1d9c09 fd3c3d3 d1d9c09 fd3c3d3 d1d9c09 fd3c3d3 d1d9c09 fd3c3d3 dd7950b d1d9c09 fd3c3d3 0e9dace dd7950b fd3c3d3 9b1c4ac fd3c3d3 9b1c4ac fd3c3d3 dd7950b fd3c3d3 dd7950b fd3c3d3 d1d9c09 fd3c3d3 9b1c4ac fd3c3d3 dd7950b fd3c3d3 d1d9c09 fd3c3d3 dd7950b fd3c3d3 d1d9c09 dd7950b fd3c3d3 d1d9c09 dd7950b fd3c3d3 9b1c4ac d1d9c09 fd3c3d3 9b1c4ac d1d9c09 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 | // orbit-camera.js
var OrbitCamera = pc.createScript('orbitCamera');
// Camera attributes
OrbitCamera.attributes.add('distanceMax', { type: 'number', default: 20, title: 'Distance Max' });
OrbitCamera.attributes.add('distanceMin', { type: 'number', default: 1, title: 'Distance Min' });
OrbitCamera.attributes.add('pitchAngleMax', { type: 'number', default: 90, title: 'Pitch Angle Max (degrees)' });
OrbitCamera.attributes.add('pitchAngleMin', { type: 'number', default: 0, title: 'Pitch Angle Min (degrees)' });
OrbitCamera.attributes.add('yawAngleMax', { type: 'number', default: 360, title: 'Yaw Angle Max (degrees)' });
OrbitCamera.attributes.add('yawAngleMin', { type: 'number', default: -360, title: 'Yaw Angle Min (degrees)' });
OrbitCamera.attributes.add('minY', { type: 'number', default: 0, title: 'Minimum Y', description: 'Minimum Y value for camera world position' });
OrbitCamera.attributes.add('inertiaFactor', { type: 'number', default: 0.2, title: 'Inertia Factor' });
OrbitCamera.attributes.add('focusEntity', { type: 'entity', title: 'Focus Entity' });
OrbitCamera.attributes.add('frameOnStart', { type: 'boolean', default: true, title: 'Frame on Start' });
Object.defineProperty(OrbitCamera.prototype, 'distance', {
get: function () { return this._targetDistance; },
set: function (value) { this._targetDistance = this._clampDistance(value); }
});
Object.defineProperty(OrbitCamera.prototype, 'orthoHeight', {
get: function () { return this.entity.camera.orthoHeight; },
set: function (value) { this.entity.camera.orthoHeight = Math.max(0, value); }
});
Object.defineProperty(OrbitCamera.prototype, 'pitch', {
get: function () { return this._targetPitch; },
set: function (value) { this._targetPitch = this._clampPitchAngle(value); }
});
Object.defineProperty(OrbitCamera.prototype, 'yaw', {
get: function () { return this._targetYaw; },
set: function (value) { this._targetYaw = this._clampYawAngle(value); }
});
Object.defineProperty(OrbitCamera.prototype, 'pivotPoint', {
get: function () { return this._pivotPoint; },
set: function (value) { this._pivotPoint.copy(value); }
});
OrbitCamera.prototype.focus = function (focusEntity) {
this._buildAabb(focusEntity);
var halfExtents = this._modelsAabb.halfExtents;
var radius = Math.max(halfExtents.x, Math.max(halfExtents.y, halfExtents.z));
this.distance = (radius * 1.5) / Math.sin(0.5 * this.entity.camera.fov * pc.math.DEG_TO_RAD);
this._removeInertia();
this._pivotPoint.copy(this._modelsAabb.center);
};
OrbitCamera.distanceBetween = new pc.Vec3();
OrbitCamera.prototype.resetAndLookAtPoint = function (resetPoint, lookAtPoint) {
this.pivotPoint.copy(lookAtPoint);
this.entity.setPosition(resetPoint);
this.entity.lookAt(lookAtPoint);
var distance = OrbitCamera.distanceBetween;
distance.sub2(lookAtPoint, resetPoint);
this.distance = distance.length();
this.pivotPoint.copy(lookAtPoint);
var cameraQuat = this.entity.getRotation();
this.yaw = this._calcYaw(cameraQuat);
this.pitch = this._calcPitch(cameraQuat, this.yaw);
this._removeInertia();
this._updatePosition();
};
OrbitCamera.prototype.resetAndLookAtEntity = function (resetPoint, entity) {
this._buildAabb(entity);
this.resetAndLookAtPoint(resetPoint, this._modelsAabb.center);
};
OrbitCamera.prototype.reset = function (yaw, pitch, distance) {
this.pitch = pitch;
this.yaw = yaw;
this.distance = distance;
this._removeInertia();
};
OrbitCamera.prototype.resetToPosition = function (position, lookAtPoint) {
this.entity.setPosition(position);
this.entity.lookAt(lookAtPoint);
this._pivotPoint.copy(lookAtPoint);
var distanceVec = new pc.Vec3();
distanceVec.sub2(position, lookAtPoint);
this._targetDistance = this._distance = distanceVec.length();
var cameraQuat = this.entity.getRotation();
this._targetYaw = this._yaw = this._calcYaw(cameraQuat);
this._targetPitch = this._pitch = this._calcPitch(cameraQuat, this._yaw);
this._removeInertia();
this._updatePosition();
};
// ==== Helper: calc cam Y if pivot became 'pivot' (used to enforce minY) ====
OrbitCamera.prototype.worldCameraYForPivot = function(pivot) {
var quat = new pc.Quat();
quat.setFromEulerAngles(this._pitch, this._yaw, 0);
var forward = new pc.Vec3();
quat.transformVector(pc.Vec3.FORWARD, forward);
var camPos = pivot.clone();
camPos.add(forward.clone().scale(-this._distance));
return camPos.y;
};
////////////////////////////////////////////////////////////////////////////////
// Private Methods //
////////////////////////////////////////////////////////////////////////////////
OrbitCamera.prototype.initialize = function () {
var self = this;
var onWindowResize = function () { self._checkAspectRatio(); };
window.addEventListener('resize', onWindowResize, false);
this._checkAspectRatio();
this._modelsAabb = new pc.BoundingBox();
this._buildAabb(this.focusEntity || this.app.root);
this.entity.lookAt(this._modelsAabb.center);
this._pivotPoint = new pc.Vec3();
this._pivotPoint.copy(this._modelsAabb.center);
var cameraQuat = this.entity.getRotation();
this._yaw = this._calcYaw(cameraQuat);
this._pitch = this._clampPitchAngle(this._calcPitch(cameraQuat, this._yaw));
this.entity.setLocalEulerAngles(this._pitch, this._yaw, 0);
this._distance = 0;
this._targetYaw = this._yaw;
this._targetPitch = this._pitch;
if (this.frameOnStart) {
this.focus(this.focusEntity || this.app.root);
} else {
var distanceBetween = new pc.Vec3();
distanceBetween.sub2(this.entity.getPosition(), this._pivotPoint);
this._distance = this._clampDistance(distanceBetween.length());
}
this._targetDistance = this._distance;
this.on('attr:distanceMin', function () { this._distance = this._clampDistance(this._distance); });
this.on('attr:distanceMax', function () { this._distance = this._clampDistance(this._distance); });
this.on('attr:pitchAngleMin', function () { this._pitch = this._clampPitchAngle(this._pitch); });
this.on('attr:pitchAngleMax', function () { this._pitch = this._clampPitchAngle(this._pitch); });
this.on('attr:focusEntity', function (value) {
if (this.frameOnStart) { this.focus(value || this.app.root); }
else { this.resetAndLookAtEntity(this.entity.getPosition(), value || this.app.root); }
});
this.on('attr:frameOnStart', function (value) {
if (value) { this.focus(this.focusEntity || this.app.root); }
});
this.on('destroy', function () { window.removeEventListener('resize', onWindowResize, false); });
};
OrbitCamera.prototype.update = function (dt) {
var t = this.inertiaFactor === 0 ? 1 : Math.min(dt / this.inertiaFactor, 1);
this._distance = pc.math.lerp(this._distance, this._targetDistance, t);
this._yaw = pc.math.lerp(this._yaw, this._targetYaw, t);
this._pitch = pc.math.lerp(this._pitch, this._targetPitch, t);
this._updatePosition();
};
OrbitCamera.prototype._updatePosition = function () {
this.entity.setLocalPosition(0, 0, 0);
this.entity.setLocalEulerAngles(this._pitch, this._yaw, 0);
var position = this.entity.getPosition();
position.copy(this.entity.forward);
position.mulScalar(-this._distance);
position.add(this.pivotPoint);
// Clamp camera's Y position so it never goes below minY
position.y = Math.max(position.y, this.minY);
this.entity.setPosition(position);
};
OrbitCamera.prototype._removeInertia = function () {
this._yaw = this._targetYaw;
this._pitch = this._targetPitch;
this._distance = this._targetDistance;
};
OrbitCamera.prototype._checkAspectRatio = function () {
var height = this.app.graphicsDevice.height;
var width = this.app.graphicsDevice.width;
this.entity.camera.horizontalFov = (height > width);
};
OrbitCamera.prototype._buildAabb = function (entity) {
var i, m, meshInstances = [];
var renders = entity.findComponents('render');
for (i = 0; i < renders.length; i++) {
var render = renders[i];
for (m = 0; m < render.meshInstances.length; m++) {
meshInstances.push(render.meshInstances[m]);
}
}
var models = entity.findComponents('model');
for (i = 0; i < models.length; i++) {
var model = models[i];
for (m = 0; m < model.meshInstances.length; m++) {
meshInstances.push(model.meshInstances[m]);
}
}
var gsplats = entity.findComponents('gsplat');
for (i = 0; i < gsplats.length; i++) {
var gsplat = gsplats[i];
var instance = gsplat.instance;
if (instance?.meshInstance) {
meshInstances.push(instance.meshInstance);
}
}
for (i = 0; i < meshInstances.length; i++) {
if (i === 0) {
this._modelsAabb.copy(meshInstances[i].aabb);
} else {
this._modelsAabb.add(meshInstances[i].aabb);
}
}
};
OrbitCamera.prototype._calcYaw = function (quat) {
var transformedForward = new pc.Vec3();
quat.transformVector(pc.Vec3.FORWARD, transformedForward);
return Math.atan2(-transformedForward.x, -transformedForward.z) * pc.math.RAD_TO_DEG;
};
OrbitCamera.prototype._clampDistance = function (distance) {
if (this.distanceMax > 0) {
return pc.math.clamp(distance, this.distanceMin, this.distanceMax);
}
return Math.max(distance, this.distanceMin);
};
OrbitCamera.prototype._clampPitchAngle = function (pitch) {
return pc.math.clamp(pitch, this.pitchAngleMin, this.pitchAngleMax);
};
OrbitCamera.prototype._clampYawAngle = function (yaw) {
return pc.math.clamp(yaw, -this.yawAngleMax, -this.yawAngleMin);
};
OrbitCamera.quatWithoutYaw = new pc.Quat();
OrbitCamera.yawOffset = new pc.Quat();
OrbitCamera.prototype._calcPitch = function (quat, yaw) {
var quatWithoutYaw = OrbitCamera.quatWithoutYaw;
var yawOffset = OrbitCamera.yawOffset;
yawOffset.setFromEulerAngles(0, -yaw, 0);
quatWithoutYaw.mul2(yawOffset, quat);
var transformedForward = new pc.Vec3();
quatWithoutYaw.transformVector(pc.Vec3.FORWARD, transformedForward);
return Math.atan2(-transformedForward.y, -transformedForward.z) * pc.math.RAD_TO_DEG;
};
// ===================== Orbit Camera Input Mouse Script ========================
var OrbitCameraInputMouse = pc.createScript('orbitCameraInputMouse');
OrbitCameraInputMouse.attributes.add('orbitSensitivity', { type: 'number', default: 0.3, title: 'Orbit Sensitivity' });
OrbitCameraInputMouse.attributes.add('distanceSensitivity', { type: 'number', default: 0.4, title: 'Distance Sensitivity' });
OrbitCameraInputMouse.prototype.initialize = function () {
this.orbitCamera = this.entity.script.orbitCamera;
if (this.orbitCamera) {
var self = this;
var onMouseOut = function () { self.onMouseOut(); };
this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onMouseDown, this);
this.app.mouse.on(pc.EVENT_MOUSEUP, this.onMouseUp, this);
this.app.mouse.on(pc.EVENT_MOUSEMOVE, this.onMouseMove, this);
this.app.mouse.on(pc.EVENT_MOUSEWHEEL, this.onMouseWheel, this);
window.addEventListener('mouseout', onMouseOut, false);
this.on('destroy', function () {
this.app.mouse.off(pc.EVENT_MOUSEDOWN, this.onMouseDown, this);
this.app.mouse.off(pc.EVENT_MOUSEUP, this.onMouseUp, this);
this.app.mouse.off(pc.EVENT_MOUSEMOVE, this.onMouseMove, this);
this.app.mouse.off(pc.EVENT_MOUSEWHEEL, this.onMouseWheel, this);
window.removeEventListener('mouseout', onMouseOut, false);
});
}
this.app.mouse.disableContextMenu();
this.lookButtonDown = false;
this.panButtonDown = false;
this.lastPoint = new pc.Vec2();
};
OrbitCameraInputMouse.fromWorldPoint = new pc.Vec3();
OrbitCameraInputMouse.toWorldPoint = new pc.Vec3();
OrbitCameraInputMouse.worldDiff = new pc.Vec3();
OrbitCameraInputMouse.prototype.pan = function (screenPoint) {
var fromWorldPoint = OrbitCameraInputMouse.fromWorldPoint;
var toWorldPoint = OrbitCameraInputMouse.toWorldPoint;
var worldDiff = OrbitCameraInputMouse.worldDiff;
var camera = this.entity.camera;
var distance = this.orbitCamera.distance;
camera.screenToWorld(screenPoint.x, screenPoint.y, distance, fromWorldPoint);
camera.screenToWorld(this.lastPoint.x, this.lastPoint.y, distance, toWorldPoint);
worldDiff.sub2(toWorldPoint, fromWorldPoint);
// Enforce minY like vertical/horizontal pan with arrows
var proposedPivot = this.orbitCamera.pivotPoint.clone().add(worldDiff);
var minY = this.orbitCamera.minY;
var resultingY = this.orbitCamera.worldCameraYForPivot(proposedPivot);
if (resultingY >= minY - 1e-4) {
this.orbitCamera.pivotPoint.add(worldDiff);
} else {
// Try horizontal-only
worldDiff.y = 0;
proposedPivot = this.orbitCamera.pivotPoint.clone().add(worldDiff);
resultingY = this.orbitCamera.worldCameraYForPivot(proposedPivot);
if (resultingY >= minY - 1e-4) {
this.orbitCamera.pivotPoint.add(worldDiff);
}
}
};
OrbitCameraInputMouse.prototype.onMouseDown = function (event) {
switch (event.button) {
case pc.MOUSEBUTTON_LEFT: this.panButtonDown = true; break;
case pc.MOUSEBUTTON_MIDDLE:
case pc.MOUSEBUTTON_RIGHT: this.lookButtonDown = true; break;
}
};
OrbitCameraInputMouse.prototype.onMouseUp = function (event) {
switch (event.button) {
case pc.MOUSEBUTTON_LEFT: this.panButtonDown = false; break;
case pc.MOUSEBUTTON_MIDDLE:
case pc.MOUSEBUTTON_RIGHT: this.lookButtonDown = false; break;
}
};
OrbitCameraInputMouse.prototype.onMouseMove = function (event) {
if (this.lookButtonDown) {
var sens = this.orbitSensitivity;
var deltaPitch = event.dy * sens; // mouse up (dy < 0) raises pitch
var deltaYaw = event.dx * sens;
var currPitch = this.orbitCamera.pitch;
var currYaw = this.orbitCamera.yaw;
var currDist = this.orbitCamera.distance;
var currPivot = this.orbitCamera.pivotPoint.clone();
var camQuat = new pc.Quat();
camQuat.setFromEulerAngles(currPitch, currYaw, 0);
var forward = new pc.Vec3();
camQuat.transformVector(pc.Vec3.FORWARD, forward);
var preY = currPivot.y + (-forward.y) * currDist;
var proposedPitch = currPitch - deltaPitch;
var testQuat = new pc.Quat();
testQuat.setFromEulerAngles(proposedPitch, currYaw, 0);
var testForward = new pc.Vec3();
testQuat.transformVector(pc.Vec3.FORWARD, testForward);
var proposedY = currPivot.y + (-testForward.y) * currDist;
var minY = this.orbitCamera.minY;
var wouldGoBelow = proposedY < minY - 1e-4;
if (wouldGoBelow && (proposedY < preY)) {
this.orbitCamera.yaw = currYaw - deltaYaw;
} else {
this.orbitCamera.pitch = proposedPitch;
this.orbitCamera.yaw = currYaw - deltaYaw;
}
} else if (this.panButtonDown) {
this.pan(new pc.Vec2(event.x, event.y));
}
this.lastPoint.set(event.x, event.y);
};
OrbitCameraInputMouse.prototype.onMouseWheel = function (event) {
if (this.entity.camera.projection === pc.PROJECTION_PERSPECTIVE) {
this.orbitCamera.distance -= event.wheelDelta * this.distanceSensitivity * (this.orbitCamera.distance * 0.1);
} else {
this.orbitCamera.orthoHeight -= event.wheelDelta * this.distanceSensitivity * (this.orbitCamera.orthoHeight * 0.1);
}
event.event.preventDefault();
};
OrbitCameraInputMouse.prototype.onMouseOut = function () {
this.lookButtonDown = false;
this.panButtonDown = false;
};
// =================== Orbit Camera Input Touch Script ========================
var OrbitCameraInputTouch = pc.createScript('orbitCameraInputTouch');
OrbitCameraInputTouch.attributes.add('orbitSensitivity', { type: 'number', default: 0.6, title: 'Orbit Sensitivity' });
OrbitCameraInputTouch.attributes.add('distanceSensitivity', { type: 'number', default: 0.5, title: 'Distance Sensitivity' });
OrbitCameraInputTouch.prototype.initialize = function () {
this.orbitCamera = this.entity.script.orbitCamera;
this.lastTouchPoint = new pc.Vec2();
this.lastPinchMidPoint = new pc.Vec2();
this.lastPinchDistance = 0;
if (this.orbitCamera && this.app.touch) {
this.app.touch.on(pc.EVENT_TOUCHSTART, this.onTouchStartEndCancel, this);
this.app.touch.on(pc.EVENT_TOUCHEND, this.onTouchStartEndCancel, this);
this.app.touch.on(pc.EVENT_TOUCHCANCEL, this.onTouchStartEndCancel, this);
this.app.touch.on(pc.EVENT_TOUCHMOVE, this.onTouchMove, this);
this.on('destroy', function () {
this.app.touch.off(pc.EVENT_TOUCHSTART, this.onTouchStartEndCancel, this);
this.app.touch.off(pc.EVENT_TOUCHEND, this.onTouchStartEndCancel, this);
this.app.touch.off(pc.EVENT_TOUCHCANCEL, this.onTouchStartEndCancel, this);
this.app.touch.off(pc.EVENT_TOUCHMOVE, this.onTouchMove, this);
});
}
};
OrbitCameraInputTouch.prototype.getPinchDistance = function (pointA, pointB) {
var dx = pointA.x - pointB.x;
var dy = pointA.y - pointB.y;
return Math.sqrt((dx * dx) + (dy * dy));
};
OrbitCameraInputTouch.prototype.calcMidPoint = function (pointA, pointB, result) {
result.set(pointB.x - pointA.x, pointB.y - pointA.y);
result.mulScalar(0.5);
result.x += pointA.x;
result.y += pointA.y;
};
OrbitCameraInputTouch.prototype.onTouchStartEndCancel = function (event) {
var touches = event.touches;
if (touches.length === 1) {
this.lastTouchPoint.set(touches[0].x, touches[0].y);
} else if (touches.length === 2) {
this.lastPinchDistance = this.getPinchDistance(touches[0], touches[1]);
this.calcMidPoint(touches[0], touches[1], this.lastPinchMidPoint);
}
};
OrbitCameraInputTouch.fromWorldPoint = new pc.Vec3();
OrbitCameraInputTouch.toWorldPoint = new pc.Vec3();
OrbitCameraInputTouch.worldDiff = new pc.Vec3();
OrbitCameraInputTouch.prototype.pan = function (midPoint) {
var fromWorldPoint = OrbitCameraInputTouch.fromWorldPoint;
var toWorldPoint = OrbitCameraInputTouch.toWorldPoint;
var worldDiff = OrbitCameraInputTouch.worldDiff;
var camera = this.entity.camera;
var distance = this.orbitCamera.distance;
camera.screenToWorld(midPoint.x, midPoint.y, distance, fromWorldPoint);
camera.screenToWorld(this.lastPinchMidPoint.x, this.lastPinchMidPoint.y, distance, toWorldPoint);
worldDiff.sub2(toWorldPoint, fromWorldPoint);
var proposedPivot = this.orbitCamera.pivotPoint.clone().add(worldDiff);
var minY = this.orbitCamera.minY;
var resultingY = this.orbitCamera.worldCameraYForPivot(proposedPivot);
if (resultingY >= minY - 1e-4) {
this.orbitCamera.pivotPoint.add(worldDiff);
} else {
worldDiff.y = 0;
proposedPivot = this.orbitCamera.pivotPoint.clone().add(worldDiff);
resultingY = this.orbitCamera.worldCameraYForPivot(proposedPivot);
if (resultingY >= minY - 1e-4) {
this.orbitCamera.pivotPoint.add(worldDiff);
}
}
};
OrbitCameraInputTouch.pinchMidPoint = new pc.Vec2();
OrbitCameraInputTouch.prototype.onTouchMove = function (event) {
var pinchMidPoint = OrbitCameraInputTouch.pinchMidPoint;
var touches = event.touches;
if (touches.length === 1) {
var touch = touches[0];
var sens = this.orbitSensitivity;
var deltaPitch = (touch.y - this.lastTouchPoint.y) * sens;
var deltaYaw = (touch.x - this.lastTouchPoint.x) * sens;
var currPitch = this.orbitCamera.pitch;
var currYaw = this.orbitCamera.yaw;
var currDist = this.orbitCamera.distance;
var currPivot = this.orbitCamera.pivotPoint.clone();
var camQuat = new pc.Quat();
camQuat.setFromEulerAngles(currPitch, currYaw, 0);
var forward = new pc.Vec3();
camQuat.transformVector(pc.Vec3.FORWARD, forward);
var preY = currPivot.y + (-forward.y) * currDist;
var proposedPitch = currPitch - deltaPitch;
var testQuat = new pc.Quat();
testQuat.setFromEulerAngles(proposedPitch, currYaw, 0);
var testForward = new pc.Vec3();
testQuat.transformVector(pc.Vec3.FORWARD, testForward);
var proposedY = currPivot.y + (-testForward.y) * currDist;
var minY = this.orbitCamera.minY;
var wouldGoBelow = proposedY < minY - 1e-4;
if (wouldGoBelow && (proposedY < preY)) {
this.orbitCamera.yaw = currYaw - deltaYaw;
} else {
this.orbitCamera.pitch = proposedPitch;
this.orbitCamera.yaw = currYaw - deltaYaw;
}
this.lastTouchPoint.set(touch.x, touch.y);
} else if (touches.length === 2) {
var currentPinchDistance = this.getPinchDistance(touches[0], touches[1]);
var diffInPinchDistance = currentPinchDistance - this.lastPinchDistance;
this.lastPinchDistance = currentPinchDistance;
this.orbitCamera.distance -= (diffInPinchDistance * this.distanceSensitivity * 0.1) * (this.orbitCamera.distance * 0.1);
this.calcMidPoint(touches[0], touches[1], pinchMidPoint);
this.pan(pinchMidPoint);
this.lastPinchMidPoint.copy(pinchMidPoint);
}
};
// =================== Orbit Camera Input Keyboard ========================
// Arrows:
// - No modifier: Up/Down = vertical pan; Left/Right = strafe
// - With Shift: Up/Down = orbit pitch; Left/Right = orbit yaw (inverted as requested)
// - With Ctrl: Up/Down = zoom in/out
var OrbitCameraInputKeyboard = pc.createScript('orbitCameraInputKeyboard');
OrbitCameraInputKeyboard.attributes.add('forwardSpeed', { type: 'number', default: 1.2, title: 'Vertical Speed (rel. to distance)' });
OrbitCameraInputKeyboard.attributes.add('strafeSpeed', { type: 'number', default: 1.2, title: 'Left/Right Speed (rel. to distance)' });
// Fine-tuning
OrbitCameraInputKeyboard.attributes.add('orbitPitchSpeedDeg', { type: 'number', default: 90, title: 'Orbit Pitch Speed (deg/s) [Shift+Up/Down]' });
OrbitCameraInputKeyboard.attributes.add('orbitYawSpeedDeg', { type: 'number', default: 120, title: 'Orbit Yaw Speed (deg/s) [Shift+Left/Right]' });
OrbitCameraInputKeyboard.attributes.add('zoomKeySensitivity', { type: 'number', default: 3.0, title: 'Zoom Sensitivity (1/s) [Ctrl+Up/Down]' });
OrbitCameraInputKeyboard.prototype.initialize = function () {
this.orbitCamera = this.entity.script.orbitCamera;
this.keyboard = this.app.keyboard || null;
};
OrbitCameraInputKeyboard.prototype.update = function (dt) {
if (!this.orbitCamera || !this.keyboard) return;
var up = this.keyboard.isPressed(pc.KEY_UP);
var dn = this.keyboard.isPressed(pc.KEY_DOWN);
var lt = this.keyboard.isPressed(pc.KEY_LEFT);
var rt = this.keyboard.isPressed(pc.KEY_RIGHT);
var shift = this.keyboard.isPressed(pc.KEY_SHIFT);
var ctrl = this.keyboard.isPressed(pc.KEY_CONTROL);
// ---- SHIFT: Orbit (pitch / yaw) ----
if (shift && (up || dn || lt || rt)) {
// Yaw (left/right) — inverted per request: Shift+Right => orbit to the right
var yawDir = (rt ? 1 : 0) - (lt ? 1 : 0); // right=+1, left=-1
if (yawDir !== 0) {
var dYaw = yawDir * this.orbitYawSpeedDeg * dt;
this.orbitCamera.yaw = this.orbitCamera.yaw + dYaw; // NOTE: '+' (inversion)
}
// Pitch (up/down) — Shift+Up => orbit up, Shift+Down => down
var pitchDir = (up ? 1 : 0) - (dn ? 1 : 0); // up=+1, down=-1
if (pitchDir !== 0) {
var dPitch = pitchDir * this.orbitPitchSpeedDeg * dt;
var currPitch = this.orbitCamera.pitch;
var currYaw = this.orbitCamera.yaw;
var currDist = this.orbitCamera.distance;
var currPivot = this.orbitCamera.pivotPoint.clone();
var camQuat = new pc.Quat().setFromEulerAngles(currPitch, currYaw, 0);
var forward = new pc.Vec3(); camQuat.transformVector(pc.Vec3.FORWARD, forward);
var preY = currPivot.y + (-forward.y) * currDist;
var testPitch = currPitch + dPitch;
var testQuat = new pc.Quat().setFromEulerAngles(testPitch, currYaw, 0);
var testForward = new pc.Vec3(); testQuat.transformVector(pc.Vec3.FORWARD, testForward);
var proposedY = currPivot.y + (-testForward.y) * currDist;
var minY = this.orbitCamera.minY;
var wouldGoBelow = proposedY < minY - 1e-4;
if (!(wouldGoBelow && (proposedY < preY))) {
this.orbitCamera.pitch = testPitch; // clamped by setter
}
}
return;
}
// ---- CTRL: Zoom (up/down) ----
if (ctrl && (up || dn)) {
var zoomSign = (up ? 1 : 0) - (dn ? 1 : 0); // up=zoom in, down=zoom out
if (zoomSign !== 0) {
if (this.entity.camera.projection === pc.PROJECTION_PERSPECTIVE) {
var dz = zoomSign * this.zoomKeySensitivity * (this.orbitCamera.distance * 0.5) * dt;
this.orbitCamera.distance -= dz; // clamped
} else {
var doh = zoomSign * this.zoomKeySensitivity * (this.orbitCamera.orthoHeight * 0.5) * dt;
this.orbitCamera.orthoHeight -= doh; // clamped >= 0
}
}
return;
}
// ---- No modifier: translate (vertical + strafe) ----
var moveVert = (up ? 1 : 0) - (dn ? 1 : 0); // Up=+1 => go up
var moveRight = (rt ? 1 : 0) - (lt ? 1 : 0);
if (moveVert === 0 && moveRight === 0) return;
// Base speeds relative to distance (consistent feel near/far)
var dist = Math.max(0.1, this.orbitCamera.distance);
var speedV = this.forwardSpeed * dist; // vertical speed
var speedR = this.strafeSpeed * dist; // strafe speed
// Vertical move (pure Y), respect minY
var dy = moveVert * speedV * dt;
if (dy !== 0) {
var currentCamY = this.orbitCamera.worldCameraYForPivot(this.orbitCamera.pivotPoint);
var minY = this.orbitCamera.minY;
var proposedCamY = currentCamY + dy;
if (proposedCamY < minY) {
dy = Math.max(dy, minY - currentCamY);
}
if (dy !== 0) {
this.orbitCamera._pivotPoint.y += dy;
}
}
// Strafe on XZ
var right = this.entity.right.clone(); right.y = 0; if (right.lengthSq() > 1e-8) right.normalize();
var dx = moveRight * speedR * dt;
if (dx !== 0) {
this.orbitCamera._pivotPoint.add(right.mulScalar(dx));
}
};
|