MikaFil commited on
Commit
911707b
·
verified ·
1 Parent(s): 40eec9e

Update orbit-camera.js

Browse files
Files changed (1) hide show
  1. orbit-camera.js +80 -8
orbit-camera.js CHANGED
@@ -124,7 +124,7 @@ OrbitCamera.prototype.initialize = function () {
124
  this._pivotPoint.copy(this._modelsAabb.center);
125
 
126
  var cameraQuat = this.entity.getRotation();
127
- this._yaw = this._calcYaw(cameraQuat); // degrees
128
  this._pitch = this._clampPitchAngle(this._calcPitch(cameraQuat, this._yaw));
129
  this.entity.setLocalEulerAngles(this._pitch, this._yaw, 0);
130
 
@@ -156,6 +156,9 @@ OrbitCamera.prototype.initialize = function () {
156
  this.on('destroy', function () {
157
  window.removeEventListener('resize', onWindowResize, false);
158
  });
 
 
 
159
  };
160
 
161
  OrbitCamera.prototype.update = function (dt) {
@@ -231,7 +234,7 @@ OrbitCamera.prototype._buildAabb = function (entity) {
231
  };
232
 
233
  OrbitCamera.prototype._calcYaw = function (quat) {
234
- // FIX: return degrees (was effectively radians before)
235
  var f = new pc.Vec3();
236
  quat.transformVector(pc.Vec3.FORWARD, f);
237
  return Math.atan2(-f.x, -f.z) * pc.math.RAD_TO_DEG;
@@ -549,17 +552,28 @@ var OrbitCameraInputKeyboard = pc.createScript('orbitCameraInputKeyboard');
549
  * - orbitDegreesPerSecond : vitesse d’orbite (deg/s) pour yaw/pitch
550
  * - zoomFactorPerSecond : facteur de zoom (proportionnel à la distance) par seconde
551
  * - panUnitsPerSecond : vitesse de pan (en unités monde = distance * facteur) par seconde
 
552
  */
553
  OrbitCameraInputKeyboard.attributes.add('orbitDegreesPerSecond', { type: 'number', default: 180, title: 'Orbit Degrees / s' });
554
  OrbitCameraInputKeyboard.attributes.add('zoomFactorPerSecond', { type: 'number', default: 2.0, title: 'Zoom Factor / s' });
555
  OrbitCameraInputKeyboard.attributes.add('panUnitsPerSecond', { type: 'number', default: 1.0, title: 'Pan (distance frac) / s' });
 
 
 
 
 
 
 
556
 
557
  OrbitCameraInputKeyboard.prototype.initialize = function () {
558
  this.orbitCamera = this.entity.script.orbitCamera;
559
 
560
- // S’assure que le clavier est bien attaché (certains viewers ne l’initialisent pas)
561
  if (!this.app.keyboard) {
562
  this.app.keyboard = new pc.Keyboard(window);
 
 
 
563
  }
564
 
565
  // Empêche le scroll de la page avec les flèches quand la scène a le focus
@@ -570,13 +584,34 @@ OrbitCameraInputKeyboard.prototype.initialize = function () {
570
  var key = e.key || '';
571
  if (key === 'ArrowUp' || key === 'ArrowDown' || key === 'ArrowLeft' || key === 'ArrowRight') {
572
  e.preventDefault();
 
573
  }
574
  };
575
  window.addEventListener('keydown', this._preventDefault, { passive: false });
576
 
 
 
 
 
 
 
 
 
 
 
 
 
577
  this.on('destroy', function () {
578
  window.removeEventListener('keydown', self._preventDefault, { passive: false });
 
 
579
  });
 
 
 
 
 
 
580
  };
581
 
582
  // Teste si un pitch proposé ferait passer la caméra sous minY (même logique que la souris)
@@ -622,11 +657,27 @@ OrbitCameraInputKeyboard.prototype._panSideways = function (delta) {
622
 
623
  if (resultingY >= minY - 1e-4) {
624
  cam.pivotPoint.add(worldDiff);
 
 
 
 
 
 
625
  } else {
626
  worldDiff.y = 0;
627
  proposedPivot = cam.pivotPoint.clone().add(worldDiff);
628
  resultingY = cam.worldCameraYForPivot(proposedPivot);
629
- if (resultingY >= minY - 1e-4) cam.pivotPoint.add(worldDiff);
 
 
 
 
 
 
 
 
 
 
630
  }
631
  };
632
 
@@ -637,6 +688,12 @@ OrbitCameraInputKeyboard.prototype.update = function (dt) {
637
  var shift = kb.isPressed(pc.KEY_SHIFT);
638
  var cam = this.orbitCamera;
639
 
 
 
 
 
 
 
640
  // --------- Avec SHIFT : zoom & pan latéral ----------
641
  if (shift) {
642
  // Zoom (⇧+↑ / ⇧+↓) — proportionnel à la distance / orthoHeight
@@ -645,12 +702,18 @@ OrbitCameraInputKeyboard.prototype.update = function (dt) {
645
 
646
  if (this.entity.camera.projection === pc.PROJECTION_PERSPECTIVE) {
647
  var dz = k * dt * (cam.distance * 0.8);
 
648
  if (kb.isPressed(pc.KEY_UP)) cam.distance = cam.distance - dz;
649
  if (kb.isPressed(pc.KEY_DOWN)) cam.distance = cam.distance + dz;
 
 
650
  } else {
651
  var dh = k * dt * (this.entity.camera.orthoHeight * 0.8);
 
652
  if (kb.isPressed(pc.KEY_UP)) this.entity.camera.orthoHeight -= dh;
653
  if (kb.isPressed(pc.KEY_DOWN)) this.entity.camera.orthoHeight += dh;
 
 
654
  }
655
  }
656
 
@@ -658,6 +721,8 @@ OrbitCameraInputKeyboard.prototype.update = function (dt) {
658
  if (kb.isPressed(pc.KEY_LEFT) || kb.isPressed(pc.KEY_RIGHT)) {
659
  var speed = this.panUnitsPerSecond * cam.distance; // unités/s
660
  var delta = speed * dt * (kb.isPressed(pc.KEY_LEFT) ? -1 : 1);
 
 
661
  this._panSideways(delta);
662
  }
663
 
@@ -670,16 +735,23 @@ OrbitCameraInputKeyboard.prototype.update = function (dt) {
670
  // Pitch (↑/↓) — protège minY exactement comme la souris
671
  if (kb.isPressed(pc.KEY_UP) || kb.isPressed(pc.KEY_DOWN)) {
672
  var dir = kb.isPressed(pc.KEY_UP) ? -1 : 1; // ↑ = réduire le pitch (orbiter vers le nord)
673
- var currPitch = cam.pitch;
674
- var proposedPitch = currPitch + dir * orbitRate;
675
 
676
  var test = this._wouldGoBelowMinYWithPitch(proposedPitch);
677
  if (!(test.wouldGoBelow && (test.proposedY < test.preY))) {
678
  cam.pitch = proposedPitch;
 
 
 
679
  }
680
  }
681
 
682
  // Yaw (←/→) — orbite horizontale
683
- if (kb.isPressed(pc.KEY_LEFT)) cam.yaw = cam.yaw + orbitRate; // vers la gauche
684
- if (kb.isPressed(pc.KEY_RIGHT)) cam.yaw = cam.yaw - orbitRate; // vers la droite
 
 
 
 
685
  };
 
124
  this._pivotPoint.copy(this._modelsAabb.center);
125
 
126
  var cameraQuat = this.entity.getRotation();
127
+ this._yaw = this._calcYaw(cameraQuat); // degrees
128
  this._pitch = this._clampPitchAngle(this._calcPitch(cameraQuat, this._yaw));
129
  this.entity.setLocalEulerAngles(this._pitch, this._yaw, 0);
130
 
 
156
  this.on('destroy', function () {
157
  window.removeEventListener('resize', onWindowResize, false);
158
  });
159
+
160
+ // --- Logs init ---
161
+ console.log('[OrbitCam] init yaw=%s° pitch=%s° dist=%s', this._yaw.toFixed(2), this._pitch.toFixed(2), this._distance.toFixed(3));
162
  };
163
 
164
  OrbitCamera.prototype.update = function (dt) {
 
234
  };
235
 
236
  OrbitCamera.prototype._calcYaw = function (quat) {
237
+ // CORRECTION: renvoyer des DEGRÉS (pas des radians)
238
  var f = new pc.Vec3();
239
  quat.transformVector(pc.Vec3.FORWARD, f);
240
  return Math.atan2(-f.x, -f.z) * pc.math.RAD_TO_DEG;
 
552
  * - orbitDegreesPerSecond : vitesse d’orbite (deg/s) pour yaw/pitch
553
  * - zoomFactorPerSecond : facteur de zoom (proportionnel à la distance) par seconde
554
  * - panUnitsPerSecond : vitesse de pan (en unités monde = distance * facteur) par seconde
555
+ * - debugLogs : activer les console.log de diagnostic
556
  */
557
  OrbitCameraInputKeyboard.attributes.add('orbitDegreesPerSecond', { type: 'number', default: 180, title: 'Orbit Degrees / s' });
558
  OrbitCameraInputKeyboard.attributes.add('zoomFactorPerSecond', { type: 'number', default: 2.0, title: 'Zoom Factor / s' });
559
  OrbitCameraInputKeyboard.attributes.add('panUnitsPerSecond', { type: 'number', default: 1.0, title: 'Pan (distance frac) / s' });
560
+ OrbitCameraInputKeyboard.attributes.add('debugLogs', { type: 'boolean', default: true, title: 'Enable Debug Logs' });
561
+
562
+ OrbitCameraInputKeyboard.prototype._log = function () {
563
+ if (!this.debugLogs) return;
564
+ // eslint-disable-next-line no-console
565
+ console.log.apply(console, arguments);
566
+ };
567
 
568
  OrbitCameraInputKeyboard.prototype.initialize = function () {
569
  this.orbitCamera = this.entity.script.orbitCamera;
570
 
571
+ // S’assure que le clavier est bien initialisé
572
  if (!this.app.keyboard) {
573
  this.app.keyboard = new pc.Keyboard(window);
574
+ this._log('[OrbitKB] app.keyboard was null -> created new pc.Keyboard(window)');
575
+ } else {
576
+ this._log('[OrbitKB] app.keyboard OK (already present)');
577
  }
578
 
579
  // Empêche le scroll de la page avec les flèches quand la scène a le focus
 
584
  var key = e.key || '';
585
  if (key === 'ArrowUp' || key === 'ArrowDown' || key === 'ArrowLeft' || key === 'ArrowRight') {
586
  e.preventDefault();
587
+ if (self.debugLogs) console.log('[OrbitKB] preventDefault on', key);
588
  }
589
  };
590
  window.addEventListener('keydown', this._preventDefault, { passive: false });
591
 
592
+ // Petits logs sur keydown/keyup pour vérifier la réception des flèches
593
+ this._onKeyDown = function (e) {
594
+ if (!self.debugLogs) return;
595
+ console.log('[OrbitKB] keydown', e.key, 'code=', e.keyCode);
596
+ };
597
+ this._onKeyUp = function (e) {
598
+ if (!self.debugLogs) return;
599
+ console.log('[OrbitKB] keyup ', e.key, 'code=', e.keyCode);
600
+ };
601
+ window.addEventListener('keydown', this._onKeyDown);
602
+ window.addEventListener('keyup', this._onKeyUp);
603
+
604
  this.on('destroy', function () {
605
  window.removeEventListener('keydown', self._preventDefault, { passive: false });
606
+ window.removeEventListener('keydown', self._onKeyDown);
607
+ window.removeEventListener('keyup', self._onKeyUp);
608
  });
609
+
610
+ this._log('[OrbitKB] init done. Camera start yaw=%s pitch=%s dist=%s',
611
+ this.orbitCamera ? this.orbitCamera.yaw : 'n/a',
612
+ this.orbitCamera ? this.orbitCamera.pitch : 'n/a',
613
+ this.orbitCamera ? this.orbitCamera.distance : 'n/a'
614
+ );
615
  };
616
 
617
  // Teste si un pitch proposé ferait passer la caméra sous minY (même logique que la souris)
 
657
 
658
  if (resultingY >= minY - 1e-4) {
659
  cam.pivotPoint.add(worldDiff);
660
+ this._log('[OrbitKB] panSideways OK delta=%s -> pivot=(%s,%s,%s)',
661
+ delta.toFixed(3),
662
+ cam.pivotPoint.x.toFixed(3),
663
+ cam.pivotPoint.y.toFixed(3),
664
+ cam.pivotPoint.z.toFixed(3)
665
+ );
666
  } else {
667
  worldDiff.y = 0;
668
  proposedPivot = cam.pivotPoint.clone().add(worldDiff);
669
  resultingY = cam.worldCameraYForPivot(proposedPivot);
670
+ if (resultingY >= minY - 1e-4) {
671
+ cam.pivotPoint.add(worldDiff);
672
+ this._log('[OrbitKB] panSideways horizontal-only delta=%s -> pivot=(%s,%s,%s)',
673
+ delta.toFixed(3),
674
+ cam.pivotPoint.x.toFixed(3),
675
+ cam.pivotPoint.y.toFixed(3),
676
+ cam.pivotPoint.z.toFixed(3)
677
+ );
678
+ } else {
679
+ this._log('[OrbitKB] panSideways blocked by minY (delta=%s)', delta.toFixed(3));
680
+ }
681
  }
682
  };
683
 
 
688
  var shift = kb.isPressed(pc.KEY_SHIFT);
689
  var cam = this.orbitCamera;
690
 
691
+ // log des états de touches quand quelque chose d'intéressant est enfoncé
692
+ if (this.debugLogs && (kb.isPressed(pc.KEY_UP) || kb.isPressed(pc.KEY_DOWN) || kb.isPressed(pc.KEY_LEFT) || kb.isPressed(pc.KEY_RIGHT))) {
693
+ console.log('[OrbitKB] pressed states: shift=%s up=%s down=%s left=%s right=%s',
694
+ !!shift, !!kb.isPressed(pc.KEY_UP), !!kb.isPressed(pc.KEY_DOWN), !!kb.isPressed(pc.KEY_LEFT), !!kb.isPressed(pc.KEY_RIGHT));
695
+ }
696
+
697
  // --------- Avec SHIFT : zoom & pan latéral ----------
698
  if (shift) {
699
  // Zoom (⇧+↑ / ⇧+↓) — proportionnel à la distance / orthoHeight
 
702
 
703
  if (this.entity.camera.projection === pc.PROJECTION_PERSPECTIVE) {
704
  var dz = k * dt * (cam.distance * 0.8);
705
+ var before = cam.distance;
706
  if (kb.isPressed(pc.KEY_UP)) cam.distance = cam.distance - dz;
707
  if (kb.isPressed(pc.KEY_DOWN)) cam.distance = cam.distance + dz;
708
+ this._log('[OrbitKB] SHIFT Zoom persp d:%s -> %s (dz=%s)',
709
+ before.toFixed(3), cam.distance.toFixed(3), dz.toFixed(3));
710
  } else {
711
  var dh = k * dt * (this.entity.camera.orthoHeight * 0.8);
712
+ var beforeH = this.entity.camera.orthoHeight;
713
  if (kb.isPressed(pc.KEY_UP)) this.entity.camera.orthoHeight -= dh;
714
  if (kb.isPressed(pc.KEY_DOWN)) this.entity.camera.orthoHeight += dh;
715
+ this._log('[OrbitKB] SHIFT Zoom ortho h:%s -> %s (dh=%s)',
716
+ beforeH.toFixed(3), this.entity.camera.orthoHeight.toFixed(3), dh.toFixed(3));
717
  }
718
  }
719
 
 
721
  if (kb.isPressed(pc.KEY_LEFT) || kb.isPressed(pc.KEY_RIGHT)) {
722
  var speed = this.panUnitsPerSecond * cam.distance; // unités/s
723
  var delta = speed * dt * (kb.isPressed(pc.KEY_LEFT) ? -1 : 1);
724
+ this._log('[OrbitKB] SHIFT Pan request delta=%s (speed=%s, dist=%s)',
725
+ delta.toFixed(3), speed.toFixed(3), cam.distance.toFixed(3));
726
  this._panSideways(delta);
727
  }
728
 
 
735
  // Pitch (↑/↓) — protège minY exactement comme la souris
736
  if (kb.isPressed(pc.KEY_UP) || kb.isPressed(pc.KEY_DOWN)) {
737
  var dir = kb.isPressed(pc.KEY_UP) ? -1 : 1; // ↑ = réduire le pitch (orbiter vers le nord)
738
+ var beforePitch = cam.pitch;
739
+ var proposedPitch = beforePitch + dir * orbitRate;
740
 
741
  var test = this._wouldGoBelowMinYWithPitch(proposedPitch);
742
  if (!(test.wouldGoBelow && (test.proposedY < test.preY))) {
743
  cam.pitch = proposedPitch;
744
+ this._log('[OrbitKB] Orbit PITCH %s -> %s (rate=%s)', beforePitch.toFixed(3), cam.pitch.toFixed(3), orbitRate.toFixed(3));
745
+ } else {
746
+ this._log('[OrbitKB] Orbit PITCH blocked by minY (before=%s proposed=%s)', beforePitch.toFixed(3), proposedPitch.toFixed(3));
747
  }
748
  }
749
 
750
  // Yaw (←/→) — orbite horizontale
751
+ if (kb.isPressed(pc.KEY_LEFT) || kb.isPressed(pc.KEY_RIGHT)) {
752
+ var beforeYaw = cam.yaw;
753
+ if (kb.isPressed(pc.KEY_LEFT)) cam.yaw = cam.yaw + orbitRate; // vers la gauche
754
+ if (kb.isPressed(pc.KEY_RIGHT)) cam.yaw = cam.yaw - orbitRate; // vers la droite
755
+ this._log('[OrbitKB] Orbit YAW %s -> %s (rate=%s)', beforeYaw.toFixed(3), cam.yaw.toFixed(3), orbitRate.toFixed(3));
756
+ }
757
  };