MikaFil commited on
Commit
986d444
·
verified ·
1 Parent(s): 2a56e9e

Update orbit-camera.js

Browse files
Files changed (1) hide show
  1. orbit-camera.js +46 -2
orbit-camera.js CHANGED
@@ -341,6 +341,19 @@ OrbitCameraInputMouse.prototype.initialize = function () {
341
  this.panButtonDown = false;
342
  this.lastPoint = new pc.Vec2();
343
  };
 
 
 
 
 
 
 
 
 
 
 
 
 
344
  OrbitCameraInputMouse.fromWorldPoint = new pc.Vec3();
345
  OrbitCameraInputMouse.toWorldPoint = new pc.Vec3();
346
  OrbitCameraInputMouse.worldDiff = new pc.Vec3();
@@ -353,7 +366,24 @@ OrbitCameraInputMouse.prototype.pan = function (screenPoint) {
353
  camera.screenToWorld(screenPoint.x, screenPoint.y, distance, fromWorldPoint);
354
  camera.screenToWorld(this.lastPoint.x, this.lastPoint.y, distance, toWorldPoint);
355
  worldDiff.sub2(toWorldPoint, fromWorldPoint);
356
- this.orbitCamera.pivotPoint.add(worldDiff);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
357
  var pitchRadians = this.orbitCamera.pitch * pc.math.DEG_TO_RAD;
358
  var minPivotY = this.orbitCamera.distance * Math.sin(pitchRadians);
359
  if (this.orbitCamera.pivotPoint.y < minPivotY) {
@@ -512,7 +542,21 @@ OrbitCameraInputTouch.prototype.pan = function (midPoint) {
512
  camera.screenToWorld(midPoint.x, midPoint.y, distance, fromWorldPoint);
513
  camera.screenToWorld(this.lastPinchMidPoint.x, this.lastPinchMidPoint.y, distance, toWorldPoint);
514
  worldDiff.sub2(toWorldPoint, fromWorldPoint);
515
- this.orbitCamera.pivotPoint.add(worldDiff);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
516
  var pitchRadians = this.orbitCamera.pitch * pc.math.DEG_TO_RAD;
517
  var minPivotY = this.orbitCamera.distance * Math.sin(pitchRadians);
518
  if (this.orbitCamera.pivotPoint.y < minPivotY) {
 
341
  this.panButtonDown = false;
342
  this.lastPoint = new pc.Vec2();
343
  };
344
+
345
+ // Returns the resulting camera world position Y for a pivot
346
+ OrbitCamera.prototype.worldCameraYForPivot = function(pivot) {
347
+ var quat = new pc.Quat();
348
+ quat.setFromEulerAngles(this._pitch, this._yaw, 0);
349
+ var forward = new pc.Vec3();
350
+ quat.transformVector(pc.Vec3.FORWARD, forward);
351
+ var camPos = pivot.clone();
352
+ camPos.add(forward.clone().scale(-this._distance));
353
+ return camPos.y;
354
+ };
355
+
356
+
357
  OrbitCameraInputMouse.fromWorldPoint = new pc.Vec3();
358
  OrbitCameraInputMouse.toWorldPoint = new pc.Vec3();
359
  OrbitCameraInputMouse.worldDiff = new pc.Vec3();
 
366
  camera.screenToWorld(screenPoint.x, screenPoint.y, distance, fromWorldPoint);
367
  camera.screenToWorld(this.lastPoint.x, this.lastPoint.y, distance, toWorldPoint);
368
  worldDiff.sub2(toWorldPoint, fromWorldPoint);
369
+
370
+ // Try proposed new pivot
371
+ var proposedPivot = this.orbitCamera.pivotPoint.clone().add(worldDiff);
372
+ var minY = this.orbitCamera.minY;
373
+ var resultingY = this.orbitCamera.worldCameraYForPivot(proposedPivot);
374
+ if (resultingY >= minY - 1e-4) {
375
+ this.orbitCamera.pivotPoint.add(worldDiff);
376
+ } else {
377
+ // Optionally, restrict only vertical panning
378
+ worldDiff.y = 0;
379
+ proposedPivot = this.orbitCamera.pivotPoint.clone().add(worldDiff);
380
+ resultingY = this.orbitCamera.worldCameraYForPivot(proposedPivot);
381
+ if (resultingY >= minY - 1e-4) {
382
+ this.orbitCamera.pivotPoint.add(worldDiff);
383
+ }
384
+ // else, do nothing (block pan)
385
+ }
386
+
387
  var pitchRadians = this.orbitCamera.pitch * pc.math.DEG_TO_RAD;
388
  var minPivotY = this.orbitCamera.distance * Math.sin(pitchRadians);
389
  if (this.orbitCamera.pivotPoint.y < minPivotY) {
 
542
  camera.screenToWorld(midPoint.x, midPoint.y, distance, fromWorldPoint);
543
  camera.screenToWorld(this.lastPinchMidPoint.x, this.lastPinchMidPoint.y, distance, toWorldPoint);
544
  worldDiff.sub2(toWorldPoint, fromWorldPoint);
545
+
546
+ var proposedPivot = this.orbitCamera.pivotPoint.clone().add(worldDiff);
547
+ var minY = this.orbitCamera.minY;
548
+ var resultingY = this.orbitCamera.worldCameraYForPivot(proposedPivot);
549
+ if (resultingY >= minY - 1e-4) {
550
+ this.orbitCamera.pivotPoint.add(worldDiff);
551
+ } else {
552
+ worldDiff.y = 0;
553
+ proposedPivot = this.orbitCamera.pivotPoint.clone().add(worldDiff);
554
+ resultingY = this.orbitCamera.worldCameraYForPivot(proposedPivot);
555
+ if (resultingY >= minY - 1e-4) {
556
+ this.orbitCamera.pivotPoint.add(worldDiff);
557
+ }
558
+ }
559
+
560
  var pitchRadians = this.orbitCamera.pitch * pc.math.DEG_TO_RAD;
561
  var minPivotY = this.orbitCamera.distance * Math.sin(pitchRadians);
562
  if (this.orbitCamera.pivotPoint.y < minPivotY) {