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