stpete2 commited on
Commit
d512cf7
·
verified ·
1 Parent(s): e135e24

Upload preprocess_manual.js

Browse files
Files changed (1) hide show
  1. preprocess_manual.js +211 -0
preprocess_manual.js CHANGED
@@ -573,9 +573,11 @@ export function preprocessManual(vrmPath, gsPath, scene, camera, renderer, stage
573
  let animationId = null;
574
  function animateControls() {
575
  animationId = requestAnimationFrame(animateControls);
 
576
  cameraControls.controls.update();
577
  renderer.render(scene, camera);
578
  }
 
579
  animateControls();
580
 
581
  // Cleanup function to stop animation
@@ -586,6 +588,213 @@ export function preprocessManual(vrmPath, gsPath, scene, camera, renderer, stage
586
  }
587
  };
588
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
589
  // ========================================
590
  // Step 0: PLY Position Adjustment
591
  // ========================================
@@ -677,9 +886,11 @@ export function preprocessManual(vrmPath, gsPath, scene, camera, renderer, stage
677
  // ★★★ RESTART ANIMATION FOR BOX ADJUSTMENT ★★★
678
  function animateBoxAdjustment() {
679
  animationId = requestAnimationFrame(animateBoxAdjustment);
 
680
  cameraControls.controls.update();
681
  renderer.render(scene, camera);
682
  }
 
683
  animateBoxAdjustment();
684
 
685
  let boxSize = { x: 2, y: 2, z: 2 };
 
573
  let animationId = null;
574
  function animateControls() {
575
  animationId = requestAnimationFrame(animateControls);
576
+ updateCameraFromKeys(); // ⭐ この行を追加
577
  cameraControls.controls.update();
578
  renderer.render(scene, camera);
579
  }
580
+
581
  animateControls();
582
 
583
  // Cleanup function to stop animation
 
588
  }
589
  };
590
 
591
+ ////////////////handle key 1213 13:30
592
+
593
+ // ========================================
594
+ // Keyboard Controls for Camera Movement
595
+ // ========================================
596
+ let activeKeys = [];
597
+
598
+ // Key event listeners
599
+ window.addEventListener("keydown", (e) => {
600
+ if (!activeKeys.includes(e.code)) {
601
+ activeKeys.push(e.code);
602
+ }
603
+ });
604
+
605
+ window.addEventListener("keyup", (e) => {
606
+ activeKeys = activeKeys.filter((k) => k !== e.code);
607
+ });
608
+
609
+ window.addEventListener("blur", () => {
610
+ activeKeys = [];
611
+ });
612
+
613
+ // Camera manipulation function (called in animation loop)
614
+ function updateCameraFromKeys() {
615
+ if (activeKeys.length === 0) return;
616
+
617
+ // Get current camera matrix (inverse of view matrix)
618
+ let inv = invert4(camera.matrixWorld.elements);
619
+ if (!inv) return;
620
+
621
+ const shiftKey = activeKeys.includes("ShiftLeft") || activeKeys.includes("ShiftRight");
622
+
623
+ // Movement (arrow keys)
624
+ if (activeKeys.includes("ArrowUp")) {
625
+ if (shiftKey) {
626
+ // Move up (Y axis)
627
+ inv = translate4(inv, 0, -0.03, 0);
628
+ } else {
629
+ // Move forward (Z axis)
630
+ inv = translate4(inv, 0, 0, 0.1);
631
+ }
632
+ }
633
+ if (activeKeys.includes("ArrowDown")) {
634
+ if (shiftKey) {
635
+ // Move down (Y axis)
636
+ inv = translate4(inv, 0, 0.03, 0);
637
+ } else {
638
+ // Move backward (Z axis)
639
+ inv = translate4(inv, 0, 0, -0.1);
640
+ }
641
+ }
642
+ if (activeKeys.includes("ArrowLeft")) {
643
+ // Strafe left (X axis)
644
+ inv = translate4(inv, -0.03, 0, 0);
645
+ }
646
+ if (activeKeys.includes("ArrowRight")) {
647
+ // Strafe right (X axis)
648
+ inv = translate4(inv, 0.03, 0, 0);
649
+ }
650
+
651
+ // Camera angle (WASD keys)
652
+ if (activeKeys.includes("KeyA")) {
653
+ // Turn left (Y axis rotation)
654
+ inv = rotate4(inv, -0.01, 0, 1, 0);
655
+ }
656
+ if (activeKeys.includes("KeyD")) {
657
+ // Turn right (Y axis rotation)
658
+ inv = rotate4(inv, 0.01, 0, 1, 0);
659
+ }
660
+ if (activeKeys.includes("KeyW")) {
661
+ // Tilt up (X axis rotation)
662
+ inv = rotate4(inv, 0.005, 1, 0, 0);
663
+ }
664
+ if (activeKeys.includes("KeyS")) {
665
+ // Tilt down (X axis rotation)
666
+ inv = rotate4(inv, -0.005, 1, 0, 0);
667
+ }
668
+ if (activeKeys.includes("KeyQ")) {
669
+ // Roll counterclockwise (Z axis rotation)
670
+ inv = rotate4(inv, 0.01, 0, 0, 1);
671
+ }
672
+ if (activeKeys.includes("KeyE")) {
673
+ // Roll clockwise (Z axis rotation)
674
+ inv = rotate4(inv, -0.01, 0, 0, 1);
675
+ }
676
+
677
+ // Orbit (I/K and J/L keys)
678
+ if (activeKeys.includes("KeyI") || activeKeys.includes("KeyK") ||
679
+ activeKeys.includes("KeyJ") || activeKeys.includes("KeyL")) {
680
+ let d = 4;
681
+ inv = translate4(inv, 0, 0, d);
682
+ inv = rotate4(
683
+ inv,
684
+ activeKeys.includes("KeyJ") ? -0.05 : activeKeys.includes("KeyL") ? 0.05 : 0,
685
+ 0, 1, 0
686
+ );
687
+ inv = rotate4(
688
+ inv,
689
+ activeKeys.includes("KeyI") ? 0.05 : activeKeys.includes("KeyK") ? -0.05 : 0,
690
+ 1, 0, 0
691
+ );
692
+ inv = translate4(inv, 0, 0, -d);
693
+ }
694
+
695
+ // Apply the transformation back to camera
696
+ const newMatrix = invert4(inv);
697
+ if (newMatrix) {
698
+ camera.matrix.fromArray(newMatrix);
699
+ camera.matrix.decompose(camera.position, camera.quaternion, camera.scale);
700
+ camera.updateMatrixWorld(true);
701
+
702
+ // Disable OrbitControls temporarily when using keyboard
703
+ if (cameraControls && cameraControls.controls) {
704
+ cameraControls.controls.enabled = false;
705
+ // Re-enable after a short delay
706
+ setTimeout(() => {
707
+ if (cameraControls && cameraControls.controls) {
708
+ cameraControls.controls.enabled = true;
709
+ }
710
+ }, 100);
711
+ }
712
+ }
713
+ }
714
+
715
+ // Helper functions for matrix operations (if not already present)
716
+ function invert4(a) {
717
+ let b00 = a[0] * a[5] - a[1] * a[4];
718
+ let b01 = a[0] * a[6] - a[2] * a[4];
719
+ let b02 = a[0] * a[7] - a[3] * a[4];
720
+ let b03 = a[1] * a[6] - a[2] * a[5];
721
+ let b04 = a[1] * a[7] - a[3] * a[5];
722
+ let b05 = a[2] * a[7] - a[3] * a[6];
723
+ let b06 = a[8] * a[13] - a[9] * a[12];
724
+ let b07 = a[8] * a[14] - a[10] * a[12];
725
+ let b08 = a[8] * a[15] - a[11] * a[12];
726
+ let b09 = a[9] * a[14] - a[10] * a[13];
727
+ let b10 = a[9] * a[15] - a[11] * a[13];
728
+ let b11 = a[10] * a[15] - a[11] * a[14];
729
+ let det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
730
+ if (!det) return null;
731
+ return [
732
+ (a[5] * b11 - a[6] * b10 + a[7] * b09) / det,
733
+ (a[2] * b10 - a[1] * b11 - a[3] * b09) / det,
734
+ (a[13] * b05 - a[14] * b04 + a[15] * b03) / det,
735
+ (a[10] * b04 - a[9] * b05 - a[11] * b03) / det,
736
+ (a[6] * b08 - a[4] * b11 - a[7] * b07) / det,
737
+ (a[0] * b11 - a[2] * b08 + a[3] * b07) / det,
738
+ (a[14] * b02 - a[12] * b05 - a[15] * b01) / det,
739
+ (a[8] * b05 - a[10] * b02 + a[11] * b01) / det,
740
+ (a[4] * b10 - a[5] * b08 + a[7] * b06) / det,
741
+ (a[1] * b08 - a[0] * b10 - a[3] * b06) / det,
742
+ (a[12] * b04 - a[13] * b02 + a[15] * b00) / det,
743
+ (a[9] * b02 - a[8] * b04 - a[11] * b00) / det,
744
+ (a[5] * b07 - a[4] * b09 - a[6] * b06) / det,
745
+ (a[0] * b09 - a[1] * b07 + a[2] * b06) / det,
746
+ (a[13] * b01 - a[12] * b03 - a[14] * b00) / det,
747
+ (a[8] * b03 - a[9] * b01 + a[10] * b00) / det,
748
+ ];
749
+ }
750
+
751
+ function rotate4(a, rad, x, y, z) {
752
+ let len = Math.hypot(x, y, z);
753
+ x /= len;
754
+ y /= len;
755
+ z /= len;
756
+ let s = Math.sin(rad);
757
+ let c = Math.cos(rad);
758
+ let t = 1 - c;
759
+ let b00 = x * x * t + c;
760
+ let b01 = y * x * t + z * s;
761
+ let b02 = z * x * t - y * s;
762
+ let b10 = x * y * t - z * s;
763
+ let b11 = y * y * t + c;
764
+ let b12 = z * y * t + x * s;
765
+ let b20 = x * z * t + y * s;
766
+ let b21 = y * z * t - x * s;
767
+ let b22 = z * z * t + c;
768
+ return [
769
+ a[0] * b00 + a[4] * b01 + a[8] * b02,
770
+ a[1] * b00 + a[5] * b01 + a[9] * b02,
771
+ a[2] * b00 + a[6] * b01 + a[10] * b02,
772
+ a[3] * b00 + a[7] * b01 + a[11] * b02,
773
+ a[0] * b10 + a[4] * b11 + a[8] * b12,
774
+ a[1] * b10 + a[5] * b11 + a[9] * b12,
775
+ a[2] * b10 + a[6] * b11 + a[10] * b12,
776
+ a[3] * b10 + a[7] * b11 + a[11] * b12,
777
+ a[0] * b20 + a[4] * b21 + a[8] * b22,
778
+ a[1] * b20 + a[5] * b21 + a[9] * b22,
779
+ a[2] * b20 + a[6] * b21 + a[10] * b22,
780
+ a[3] * b20 + a[7] * b21 + a[11] * b22,
781
+ ...a.slice(12, 16),
782
+ ];
783
+ }
784
+
785
+ function translate4(a, x, y, z) {
786
+ return [
787
+ ...a.slice(0, 12),
788
+ a[0] * x + a[4] * y + a[8] * z + a[12],
789
+ a[1] * x + a[5] * y + a[9] * z + a[13],
790
+ a[2] * x + a[6] * y + a[10] * z + a[14],
791
+ a[3] * x + a[7] * y + a[11] * z + a[15],
792
+ ];
793
+ }
794
+
795
+
796
+ //////////////
797
+
798
  // ========================================
799
  // Step 0: PLY Position Adjustment
800
  // ========================================
 
886
  // ★★★ RESTART ANIMATION FOR BOX ADJUSTMENT ★★★
887
  function animateBoxAdjustment() {
888
  animationId = requestAnimationFrame(animateBoxAdjustment);
889
+ updateCameraFromKeys(); // ⭐ この行を追加
890
  cameraControls.controls.update();
891
  renderer.render(scene, camera);
892
  }
893
+
894
  animateBoxAdjustment();
895
 
896
  let boxSize = { x: 2, y: 2, z: 2 };