@selvajs/visualization 1.8.0-beta.4 → 1.8.0-beta.6

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.d.cts CHANGED
@@ -4,6 +4,7 @@ import { OrbitControls } from "three/addons/controls/OrbitControls.js";
4
4
  import "three/addons/lines/LineSegments2.js";
5
5
  import { CSS2DObject } from "three/addons/renderers/CSS2DRenderer.js";
6
6
  import "three/addons/postprocessing/Pass.js";
7
+ import "three-mesh-bvh";
7
8
  /**
8
9
  * Errors for the visualization package. Replaces `@selvajs/compute`'s `RhinoComputeError`, which
9
10
  * mis-named failures on paths (e.g. the plugin WebSocket) that never touch Rhino.Compute. `code`
@@ -290,6 +291,17 @@ type EventConfig = {
290
291
  */
291
292
  type ViewPreset = 'top' | 'bottom' | 'front' | 'back' | 'left' | 'right' | 'iso';
292
293
  type CameraProjection = 'perspective' | 'orthographic';
294
+ /**
295
+ * A view a host saves and restores, as plain arrays so it serializes as is. Without `position`
296
+ * and `target` (or with either not three numbers) only `direction` counts: the camera looks along
297
+ * it and frames the content, so the view keeps its angle when the model changes size.
298
+ */
299
+ interface CameraView {
300
+ /** From the target towards the camera. */
301
+ direction: number[];
302
+ position?: number[];
303
+ target?: number[];
304
+ }
293
305
  interface CameraController {
294
306
  /** Swaps identity on {@link setProjection}. */
295
307
  getActiveCamera(): THREE.Camera;
@@ -302,6 +314,10 @@ interface CameraController {
302
314
  * named preset — used by the nav-cube, whose clicked axis is a world axis.
303
315
  */
304
316
  setViewDirection(direction: THREE.Vector3, animate?: boolean): void;
317
+ /** The current view: direction, exact position and target. */
318
+ getCameraView(): CameraView;
319
+ /** Restores a {@link CameraView}; one without a usable direction falls back to `iso`. */
320
+ setCameraView(view: CameraView, animate?: boolean): void;
305
321
  /** Frame a world-space box from the current view direction. No-op on an empty box. */
306
322
  frameBounds(box: THREE.Box3, animate?: boolean): void;
307
323
  setRotateEnabled(enabled: boolean): void;
@@ -607,6 +623,11 @@ interface ThreeViewer {
607
623
  clearUserGeometry: (appId?: string) => void;
608
624
  }
609
625
  export declare const initThree: (canvas: HTMLCanvasElement, options?: ThreeInitializerOptions) => ThreeViewer;
626
+ /**
627
+ * Every hit along `raycaster`'s ray over the visible content of `scene`, nearest first. Meshes use
628
+ * the BVH; lines, points and instanced meshes fall back to their own `raycast`.
629
+ */
630
+ export declare function raycastVisible(scene: THREE.Object3D, raycaster: THREE.Raycaster): THREE.Intersection[];
610
631
  /** Geometry produced by a solve. Replaced wholesale on the next one. */
611
632
  export declare const SOURCE_COMPUTE = "compute";
612
633
  /**
@@ -630,5 +651,5 @@ export declare function isHostOwned(object: THREE.Object3D): boolean;
630
651
  export declare function isOwnedBy(object: THREE.Object3D, id?: string): boolean;
631
652
  /** Replaces scene content with `meshes`, rescales the camera frustum to fit, and (first call only) positions the camera/controls. */
632
653
  export declare function updateScene(scene: THREE.Scene, meshes: THREE.Object3D[], camera: THREE.PerspectiveCamera, controls: OrbitControls, initialPositionSet: boolean): void;
633
- export { type CameraConfig, type CameraController, type CameraProjection, type ControlsConfig, type EdgesConfig, type EnvironmentConfig, type ErrorCode, type EventConfig, type FloorConfig, type GizmoConfig, type Grid, type GridConfig, LOOKS, type LabelHandle, type LabelLayer, type LightingConfig, type Logger, type Look, type LookMaterialOverride, type LookPreset, type MaterialAppearanceOptions, type MeasureConfig, type MeasureTool, type PointerTool, type RenderConfig, type SetLookOptions, type ThreeInitializerOptions, type ThreeViewer, type ToolRegistration, type ToolRegistry, type ViewGizmo, type ViewPreset };
654
+ export { type CameraConfig, type CameraController, type CameraProjection, type CameraView, type ControlsConfig, type EdgesConfig, type EnvironmentConfig, type ErrorCode, type EventConfig, type FloorConfig, type GizmoConfig, type Grid, type GridConfig, LOOKS, type LabelHandle, type LabelLayer, type LightingConfig, type Logger, type Look, type LookMaterialOverride, type LookPreset, type MaterialAppearanceOptions, type MeasureConfig, type MeasureTool, type PointerTool, type RenderConfig, type SetLookOptions, type ThreeInitializerOptions, type ThreeViewer, type ToolRegistration, type ToolRegistry, type ViewGizmo, type ViewPreset };
634
655
  //# sourceMappingURL=render.d.cts.map
package/dist/render.d.ts CHANGED
@@ -2,6 +2,7 @@ import { a as LookPreset, i as LookMaterialOverride, n as LOOKS, o as MaterialAp
2
2
  import * as THREE from "three";
3
3
  import { LineSegments2 } from "three/addons/lines/LineSegments2.js";
4
4
  import { CSS2DObject } from "three/addons/renderers/CSS2DRenderer.js";
5
+ import { MeshBVH } from "three-mesh-bvh";
5
6
  import { Pass } from "three/addons/postprocessing/Pass.js";
6
7
  import { OrbitControls } from "three/addons/controls/OrbitControls.js";
7
8
  /**
@@ -290,6 +291,17 @@ type EventConfig = {
290
291
  */
291
292
  type ViewPreset = 'top' | 'bottom' | 'front' | 'back' | 'left' | 'right' | 'iso';
292
293
  type CameraProjection = 'perspective' | 'orthographic';
294
+ /**
295
+ * A view a host saves and restores, as plain arrays so it serializes as is. Without `position`
296
+ * and `target` (or with either not three numbers) only `direction` counts: the camera looks along
297
+ * it and frames the content, so the view keeps its angle when the model changes size.
298
+ */
299
+ interface CameraView {
300
+ /** From the target towards the camera. */
301
+ direction: number[];
302
+ position?: number[];
303
+ target?: number[];
304
+ }
293
305
  interface CameraController {
294
306
  /** Swaps identity on {@link setProjection}. */
295
307
  getActiveCamera(): THREE.Camera;
@@ -302,6 +314,10 @@ interface CameraController {
302
314
  * named preset — used by the nav-cube, whose clicked axis is a world axis.
303
315
  */
304
316
  setViewDirection(direction: THREE.Vector3, animate?: boolean): void;
317
+ /** The current view: direction, exact position and target. */
318
+ getCameraView(): CameraView;
319
+ /** Restores a {@link CameraView}; one without a usable direction falls back to `iso`. */
320
+ setCameraView(view: CameraView, animate?: boolean): void;
305
321
  /** Frame a world-space box from the current view direction. No-op on an empty box. */
306
322
  frameBounds(box: THREE.Box3, animate?: boolean): void;
307
323
  setRotateEnabled(enabled: boolean): void;
@@ -607,6 +623,11 @@ interface ThreeViewer {
607
623
  clearUserGeometry: (appId?: string) => void;
608
624
  }
609
625
  export declare const initThree: (canvas: HTMLCanvasElement, options?: ThreeInitializerOptions) => ThreeViewer;
626
+ /**
627
+ * Every hit along `raycaster`'s ray over the visible content of `scene`, nearest first. Meshes use
628
+ * the BVH; lines, points and instanced meshes fall back to their own `raycast`.
629
+ */
630
+ export declare function raycastVisible(scene: THREE.Object3D, raycaster: THREE.Raycaster): THREE.Intersection[];
610
631
  /** Geometry produced by a solve. Replaced wholesale on the next one. */
611
632
  export declare const SOURCE_COMPUTE = "compute";
612
633
  /**
@@ -630,5 +651,5 @@ export declare function isHostOwned(object: THREE.Object3D): boolean;
630
651
  export declare function isOwnedBy(object: THREE.Object3D, id?: string): boolean;
631
652
  /** Replaces scene content with `meshes`, rescales the camera frustum to fit, and (first call only) positions the camera/controls. */
632
653
  export declare function updateScene(scene: THREE.Scene, meshes: THREE.Object3D[], camera: THREE.PerspectiveCamera, controls: OrbitControls, initialPositionSet: boolean): void;
633
- export { type CameraConfig, type CameraController, type CameraProjection, type ControlsConfig, type EdgesConfig, type EnvironmentConfig, type ErrorCode, type EventConfig, type FloorConfig, type GizmoConfig, type Grid, type GridConfig, LOOKS, type LabelHandle, type LabelLayer, type LightingConfig, type Logger, type Look, type LookMaterialOverride, type LookPreset, type MaterialAppearanceOptions, type MeasureConfig, type MeasureTool, type PointerTool, type RenderConfig, type SetLookOptions, type ThreeInitializerOptions, type ThreeViewer, type ToolRegistration, type ToolRegistry, type ViewGizmo, type ViewPreset };
654
+ export { type CameraConfig, type CameraController, type CameraProjection, type CameraView, type ControlsConfig, type EdgesConfig, type EnvironmentConfig, type ErrorCode, type EventConfig, type FloorConfig, type GizmoConfig, type Grid, type GridConfig, LOOKS, type LabelHandle, type LabelLayer, type LightingConfig, type Logger, type Look, type LookMaterialOverride, type LookPreset, type MaterialAppearanceOptions, type MeasureConfig, type MeasureTool, type PointerTool, type RenderConfig, type SetLookOptions, type ThreeInitializerOptions, type ThreeViewer, type ToolRegistration, type ToolRegistry, type ViewGizmo, type ViewPreset };
634
655
  //# sourceMappingURL=render.d.ts.map
package/dist/render.js CHANGED
@@ -852,6 +852,40 @@ function createCameraController(deps) {
852
852
  const setView = (preset, animate = true) => {
853
853
  setViewDirection(VIEW_DIRECTIONS[preset], animate);
854
854
  };
855
+ const getCameraView = () => {
856
+ const target = controls.target;
857
+ const offset = active().position.clone().sub(target);
858
+ if (offset.lengthSq() < 1e-12) offset.copy(VIEW_DIRECTIONS.iso);
859
+ if (projection === "orthographic") offset.divideScalar(ortho.zoom);
860
+ return {
861
+ direction: rounded(offset.clone().normalize()),
862
+ position: rounded(target.clone().add(offset)),
863
+ target: rounded(target)
864
+ };
865
+ };
866
+ const setCameraView = (view, animate = false) => {
867
+ const position = toVector(view.position);
868
+ const target = toVector(view.target);
869
+ if (position && target && position.distanceToSquared(target) > 1e-12) {
870
+ cancelTween();
871
+ const cam = active();
872
+ if (projection === "orthographic") ortho.zoom = 1;
873
+ if (animate) {
874
+ activeTween = animateMove(cam, controls, position, target, () => {
875
+ if (projection === "orthographic") syncOrthoFrustum();
876
+ });
877
+ return;
878
+ }
879
+ cam.position.copy(position);
880
+ controls.target.copy(target);
881
+ if (projection === "orthographic") syncOrthoFrustum();
882
+ controls.update();
883
+ return;
884
+ }
885
+ const direction = toVector(view.direction);
886
+ if (direction && direction.lengthSq() > 1e-12) setViewDirection(direction.normalize(), animate);
887
+ else setView("iso", animate);
888
+ };
855
889
  const setRotateEnabled = (enabled) => {
856
890
  controls.enableRotate = enabled;
857
891
  };
@@ -869,6 +903,8 @@ function createCameraController(deps) {
869
903
  },
870
904
  setView,
871
905
  setViewDirection,
906
+ getCameraView,
907
+ setCameraView,
872
908
  frameBounds,
873
909
  setRotateEnabled,
874
910
  isRotateEnabled: () => controls.enableRotate,
@@ -876,6 +912,12 @@ function createCameraController(deps) {
876
912
  dispose: cancelTween
877
913
  };
878
914
  }
915
+ const rounded = (v) => [
916
+ v.x,
917
+ v.y,
918
+ v.z
919
+ ].map((n) => Math.round(n * 1e6) / 1e6);
920
+ const toVector = (values) => values?.length === 3 && values.every(Number.isFinite) ? new THREE.Vector3(values[0], values[1], values[2]) : void 0;
879
921
  /**
880
922
  * Nudges a top/bottom view direction a ~0.5° tilt off the up axis; other presets pass through
881
923
  * unchanged. Looking exactly down `up` is an OrbitControls singularity: camera direction coincides
@@ -4572,6 +4614,6 @@ const initThree = function(canvas, options) {
4572
4614
  clearUserGeometry
4573
4615
  };
4574
4616
  };
4575
- export { DEFAULT_LOOK, ErrorCodes, LOOKS, LOOK_PRESETS, SOURCE_COMPUTE, SOURCE_USER, VisualizationError, appIdFromSource, appSource, enableDebugLogging, getLogger, initThree, isHostOwned, isOwnedBy, materialAppearanceForLook, pickThreshold, pointerToNdc, setLogger, snapToVertex, updateScene };
4617
+ export { DEFAULT_LOOK, ErrorCodes, LOOKS, LOOK_PRESETS, SOURCE_COMPUTE, SOURCE_USER, VisualizationError, appIdFromSource, appSource, enableDebugLogging, getLogger, initThree, isHostOwned, isOwnedBy, materialAppearanceForLook, pickThreshold, pointerToNdc, raycastVisible, setLogger, snapToVertex, updateScene };
4576
4618
 
4577
4619
  //# sourceMappingURL=render.js.map