@selvajs/visualization 1.8.0-beta.7 → 1.8.0-beta.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/render.cjs CHANGED
@@ -34,6 +34,36 @@ const LOOKS = [
34
34
  "wireframe",
35
35
  "night"
36
36
  ];
37
+ // src/render/fit.ts
38
+ /** Room left around a fitted box, as a factor on the tight distance. */
39
+ const FIT_MARGIN = 1.1;
40
+ /**
41
+ * How far from the box's center, along `direction` (target → camera), a perspective camera with
42
+ * vertical field of view `fovDegrees` and `aspect` must sit to see the whole box, with `margin`.
43
+ *
44
+ * Every corner is fitted across both the width and the height of the view. Fitting the box's
45
+ * largest side to the vertical field of view alone left a long part in a wide viewport filling a
46
+ * third of it.
47
+ */
48
+ function fitDistance(box, direction, up, fovDegrees, aspect, margin = FIT_MARGIN) {
49
+ if (box.isEmpty()) return margin;
50
+ const dir = direction.clone().normalize();
51
+ const center = box.getCenter(new three.Vector3());
52
+ const tanV = Math.tan(fovDegrees * Math.PI / 360);
53
+ const tanH = tanV * (aspect > 0 && Number.isFinite(aspect) ? aspect : 1);
54
+ const right = new three.Vector3().crossVectors(up, dir);
55
+ if (right.lengthSq() < 1e-12) right.crossVectors(Math.abs(dir.x) < .9 ? new three.Vector3(1, 0, 0) : new three.Vector3(0, 1, 0), dir);
56
+ right.normalize();
57
+ const viewUp = new three.Vector3().crossVectors(dir, right).normalize();
58
+ const corner = new three.Vector3();
59
+ let distance = 0;
60
+ for (const x of [box.min.x, box.max.x]) for (const y of [box.min.y, box.max.y]) for (const z of [box.min.z, box.max.z]) {
61
+ corner.set(x, y, z).sub(center);
62
+ const depth = corner.dot(dir);
63
+ distance = Math.max(distance, Math.abs(corner.dot(right)) / tanH + depth, Math.abs(corner.dot(viewUp)) / tanV + depth);
64
+ }
65
+ return (distance || 1) * margin;
66
+ }
37
67
  // src/render/scene-ownership.ts
38
68
  /** Geometry produced by a solve. Replaced wholesale on the next one. */
39
69
  const SOURCE_COMPUTE = "compute";
@@ -649,8 +679,7 @@ const CAMERA_CONFIG = {
649
679
  HUGE: 100,
650
680
  LARGE: 50,
651
681
  NORMAL: 20
652
- },
653
- INITIAL_DISTANCE_MULTIPLIER: 4
682
+ }
654
683
  };
655
684
  /** Replaces scene content with `meshes`, rescales the camera frustum to fit, and (first call only) positions the camera/controls. */
656
685
  function updateScene(scene, meshes, camera, controls, initialPositionSet) {
@@ -676,8 +705,9 @@ function updateScene(scene, meshes, camera, controls, initialPositionSet) {
676
705
  }
677
706
  camera.updateProjectionMatrix();
678
707
  if (!initialPositionSet) {
679
- const distance = maxDim * CAMERA_CONFIG.INITIAL_DISTANCE_MULTIPLIER;
680
- camera.position.copy(center).add(isoOffset(camera.up, distance));
708
+ const direction = isoOffset(camera.up, 1).normalize();
709
+ const distance = fitDistance(unionBoundingBox, direction, camera.up, camera.fov, camera.aspect);
710
+ camera.position.copy(center).addScaledVector(direction, distance);
681
711
  controls.target.copy(center);
682
712
  controls.update();
683
713
  }
@@ -817,10 +847,19 @@ function createCameraController(deps) {
817
847
  controls.update();
818
848
  onActiveCameraChange(active());
819
849
  };
820
- const frame = (center, maxDim, direction, animate) => {
821
- const fov = perspective.fov * (Math.PI / 180);
822
- const distance = maxDim / (2 * Math.tan(fov / 2)) * 1.5;
823
- const dir = nudgeOffPole(direction, up);
850
+ let lastFit = null;
851
+ const forgetFit = () => {
852
+ lastFit = null;
853
+ };
854
+ controls.addEventListener?.("start", forgetFit);
855
+ const frame = (box, direction, animate) => {
856
+ const center = box.getCenter(new three.Vector3());
857
+ const dir = nudgeOffPole(direction, up).clone().normalize();
858
+ const distance = fitDistance(box, dir, up, perspective.fov, aspect);
859
+ lastFit = {
860
+ box: box.clone(),
861
+ direction: dir.clone()
862
+ };
824
863
  const toPosition = center.clone().add(dir.clone().multiplyScalar(distance));
825
864
  const cam = active();
826
865
  if (projection === "orthographic") ortho.zoom = 1;
@@ -836,20 +875,15 @@ function createCameraController(deps) {
836
875
  }
837
876
  };
838
877
  const setViewDirection = (direction, animate = true) => {
839
- const box = computeContentBounds(scene);
840
- const center = box.isEmpty() ? controls.target.clone() : box.getCenter(new three.Vector3());
841
- const size = box.isEmpty() ? new three.Vector3(1, 1, 1) : box.getSize(new three.Vector3());
842
- const maxDim = Math.max(size.x, size.y, size.z) || 1;
843
- frame(center, maxDim, direction, animate);
878
+ const content = computeContentBounds(scene);
879
+ const box = content.isEmpty() ? new three.Box3().setFromCenterAndSize(controls.target.clone(), new three.Vector3(1, 1, 1)) : content;
880
+ frame(box, direction, animate);
844
881
  };
845
882
  const frameBounds = (box, animate = true) => {
846
883
  if (box.isEmpty()) return;
847
- const center = box.getCenter(new three.Vector3());
848
- const size = box.getSize(new three.Vector3());
849
- const maxDim = Math.max(size.x, size.y, size.z) || 1;
850
884
  const direction = active().position.clone().sub(controls.target);
851
885
  if (direction.lengthSq() < 1e-12) direction.copy(VIEW_DIRECTIONS.iso);
852
- frame(center, maxDim, direction.normalize(), animate);
886
+ frame(box, direction.normalize(), animate);
853
887
  };
854
888
  const setView = (preset, animate = true) => {
855
889
  setViewDirection(VIEW_DIRECTIONS[preset], animate);
@@ -870,6 +904,7 @@ function createCameraController(deps) {
870
904
  const target = toVector(view.target);
871
905
  if (position && target && position.distanceToSquared(target) > 1e-12) {
872
906
  cancelTween();
907
+ forgetFit();
873
908
  const cam = active();
874
909
  if (projection === "orthographic") ortho.zoom = 1;
875
910
  if (animate) {
@@ -892,7 +927,14 @@ function createCameraController(deps) {
892
927
  controls.enableRotate = enabled;
893
928
  };
894
929
  const updateAspect = (width, height) => {
895
- aspect = height === 0 ? aspect : width / height;
930
+ const next = height === 0 ? aspect : width / height;
931
+ const changed = Math.abs(next - aspect) > 1e-6;
932
+ aspect = next;
933
+ if (changed && lastFit) {
934
+ const { box, direction } = lastFit;
935
+ frame(box, direction, false);
936
+ return;
937
+ }
896
938
  if (projection === "orthographic") syncOrthoFrustum();
897
939
  };
898
940
  return {
@@ -911,7 +953,10 @@ function createCameraController(deps) {
911
953
  setRotateEnabled,
912
954
  isRotateEnabled: () => controls.enableRotate,
913
955
  updateAspect,
914
- dispose: cancelTween
956
+ dispose: () => {
957
+ cancelTween();
958
+ controls.removeEventListener?.("start", forgetFit);
959
+ }
915
960
  };
916
961
  }
917
962
  const rounded = (v) => [