simvx.editor.panels.viewport_math¶
Pure math helpers for 3D viewport projection.
Stateless functions for projecting 3D world coordinates to 2D screen coordinates via a view-projection matrix. Used by Viewport3DPanel but contain no panel state or rendering calls.
Module Contents¶
Functions¶
Project a 3D world point to 2D screen coordinates. |
|
Project an axis segment (origin -> origin + direction * axis_length). |
|
Compute a world-space axis length that appears target_px pixels on screen. |
|
Rotate a world-space axis direction by camera yaw/pitch for the axis indicator. |
|
Snap grid origin to major-step increments for visual stability. |
API¶
- simvx.editor.panels.viewport_math.project_point(vp_matrix: numpy.ndarray, world_pos, vx: float, vy: float, vw: float, vh: float) tuple[float, float, float] | None¶
Project a 3D world point to 2D screen coordinates.
Args: vp_matrix: Combined view-projection matrix (4x4). world_pos: World-space position (Vec3, tuple, or array-like). vx, vy, vw, vh: Viewport rectangle in screen pixels.
Returns: (screen_x, screen_y, ndc_depth) or None if behind camera. ndc_depth is in [-1, 1]; values outside indicate clipping.
- simvx.editor.panels.viewport_math.project_direction(vp_matrix: numpy.ndarray, origin: simvx.core.Vec3, direction: simvx.core.Vec3, axis_length: float, vx: float, vy: float, vw: float, vh: float) tuple[tuple[float, float], tuple[float, float]] | None¶
Project an axis segment (origin -> origin + direction * axis_length).
Args: vp_matrix: Combined view-projection matrix (4x4). origin: Segment start in world space. direction: Unit direction vector. axis_length: Length of the segment in world units. vx, vy, vw, vh: Viewport rectangle in screen pixels.
Returns: ((sx0, sy0), (sx1, sy1)) or None if either endpoint fails.
- simvx.editor.panels.viewport_math.gizmo_screen_length(vp_matrix: numpy.ndarray, world_origin: simvx.core.Vec3, vx: float, vy: float, vw: float, vh: float, target_px: float = 100.0) float¶
Compute a world-space axis length that appears target_px pixels on screen.
Uses a simple linear estimate: project a 1-unit test offset along X, measure the screen-pixel distance, then scale to reach target_px.
Args: vp_matrix: Combined view-projection matrix (4x4). world_origin: Origin of the gizmo in world space. vx, vy, vw, vh: Viewport rectangle in screen pixels. target_px: Desired screen-pixel length.
Returns: World-space axis length (fallback 1.5 if projection fails).
- simvx.editor.panels.viewport_math.rotate_axis_to_screen(world_dir: simvx.core.Vec3, yaw_rad: float, pitch_rad: float) tuple[float, float]¶
Rotate a world-space axis direction by camera yaw/pitch for the axis indicator.
Returns (screen_x_factor, screen_y_factor) in [-1, 1] range, suitable for multiplying by the indicator size to get pixel offsets from center.
- simvx.editor.panels.viewport_math.grid_snap_origin(pivot_x: float, pivot_z: float, major_step: float) tuple[float, float]¶
Snap grid origin to major-step increments for visual stability.