dymokomi/luced-3d
Source of release 0.11.4
## The 3D viewport: the scene drawn by luce-3d's renderer (faces, wires,
## curves, hulls, normals, the ground grid, point clouds as Gaussian splats
## or dots, fog volumes ray-marched over them), the node tools over it (viewport_tools.luc), the sketch editor's
## and profile picker's drawings, the marking menus (hold A, S or D) and the
## shading button. Its events go to the marking menu, the chrome, the sketch
## editor and the tools before the camera's navigation.
from luce_ui.ui import Widget, Layout, Constraints, Size, Event, Painter, SceneView
from luce_3d.graphics import WireRenderer, Vector3, Renderer, SplatScene, GaussianSplats, FogScene
from luce_3d import graphics as three
from luce_gpu.gpu import RenderTarget, Rect
from luce_window.input import EventKind, Key, ScrollUnit
from workspace import Workspace
from viewport_tools import ViewportTools
from viewport_chrome import shading_button, on_shading_button, snap_row_end
from snapping import grid_step
from view_grid import draw_grid
from luce_std import math
from sketch_view import SketchView
from profile_pick import ProfilePick
from actions import Actions
from marking_menu import MarkingMenu, MarkItem
import marking_items
import theme
import units
import time
from shading_menu import mode_icon
import modeling_commands
import placing_commands
from luce_geocore.geocore import Domain, Mesh, Selection
import tool_prompts
pub class SceneViewport: Widget:
let view: SceneView
let workspace: Workspace
pub let tools: ViewportTools
## A Sketch node's editor, while one is selected and displayed.
pub let sketch: SketchView
## Pick profiles, while an Extrude or Revolve takes them.
pub let profiles: ProfilePick
## Hold A (create), S (modify) or D (a sketch's constraints).
pub let marking: MarkingMenu
## Every displayed point cloud, sorted together each view (interleaved
## clouds and instances blend in depth order).
pub let splats: SplatScene
## Every displayed fog drawing, marched together each view.
pub let fog: FogScene
let actions: Actions
## A marking menu's node entry: add that node after the selected one.
pub var on_add: func(str) -> unit!? = none
var dragging: bool = false
var navigation_button: int = 0
var pending_right: bool = false
var last_x: float = 0.0
var last_y: float = 0.0
var mode_press: int = -1
pub var on_shading: func(bool) -> unit!? = none
## Tab: the node menu, at the pointer.
pub var on_search: func() -> unit!? = none
pub var pointer_x: float = 100.0
pub var pointer_y: float = 100.0
pub func poll_hold(self) -> unit!:
if self.marking.poll(time.now()):
self.layout().invalidate()
if self.mode_press >= 0 and time.now() - self.mode_press >= 350000000:
self.mode_press = -1
if let open = self.on_shading:
open(true)
pub func init(self, workspace: Workspace, actions: Actions) -> unit!:
self.workspace = workspace
self.tools = ViewportTools(workspace, actions)
self.sketch = SketchView(workspace)
self.profiles = ProfilePick(workspace)
self.marking = MarkingMenu()
self.splats = SplatScene()
self.fog = FogScene()
self.actions = actions
self.tools.chrome.on_reuse = self.reuse_tool
self.view = SceneView(workspace.scene, workspace.view.camera, Renderer(true))
self.tools.picker.renderer = self.view.renderer()
self.view.layout().set_interactive(true)
self.view.layout().set_focusable(true)
self.view.layout().set_accepts_tab(true)
pub func layout(self) -> Layout:
return self.view.layout()
pub func measure(self, available: Constraints) -> Size!:
return self.view.measure(available)
pub func draw(self, target: RenderTarget) -> unit!:
let size = target.size()
let painter = Painter(target)
painter.rectangle(Rect(0.0, 0.0, size.width, size.height), theme.backdrop())
painter.close()
let w = self.workspace
let renderer = self.view.renderer()
# Components are shown while editing, prompting, or with a selection.
let editing = w.editing() or w.prompt != none or w.selected_count() > 0
let wired = w.view.display_mode in [0, 1, 4]
renderer.set_flat(w.view.display_mode in [1, 2])
renderer.set_shading(2 if w.view.display_mode == 7 else (3 if w.view.display_mode == 8 else (6 if w.view.display_mode == 9 else 0)), float(w.view.zebra_stripes), w.view.zebra_duty,
self.zebra_axis(), w.view.zebra_planar, w.view.zebra_stripes_only)
renderer.set_curvature(w.view.curvature_kind, w.view.curvature_range)
# Selected faces are tinted in the surface pass: one bit per face.
let accent = theme.accent()
let tinted = w.geometry != none and w.selection != none and w.on_domain(Domain.face)
if tinted:
renderer.set_selected_faces(w.geometry else trap("geometry"), w.selection else trap("selection"), three.Color(accent.red, accent.green, accent.blue), 0.45)
else:
renderer.clear_face_selection()
# Polygon wires come from each mesh's GPU arrays; Edit mode keeps them
# on, but for a CAD body, whose B-rep edges are what is picked.
let polygons = editing and not w.picking_brep()
let wire_color = theme.grey(90.0) if polygons else (theme.grey(175.0) if w.view.display_mode == 0 else theme.grey(55.0))
renderer.set_edges(1.0 if wired or polygons else 0.0, wire_color, w.view.display_mode != 0)
renderer.set_wire_overlay(1.0 if wired or polygons or w.batches.patch_batch != none else 0.0)
self.view.draw(target)
if wired:
if let batch = w.batches.wire_batch:
batch.draw(w.view.camera, target, theme.grey(175.0) if w.view.display_mode == 0 else theme.grey(55.0))
else:
if let batch = w.batches.patch_batch:
batch.draw(w.view.camera, target, theme.grey(55.0))
# Curves draw in every mode, over their dimmer control hulls. A sketch
# being edited draws its own curves (live as they move), no hulls.
let sketching = self.sketch.editor.active()
if w.view.hulls_visible and not sketching:
for batch in w.batches.hull_batches:
batch.draw(w.view.camera, target, theme.grey(110.0))
if not sketching:
for batch in w.batches.curve_batches:
batch.draw(w.view.camera, target, theme.grey(225.0), 1.5)
self.sketch.draw_lines(target)
self.profiles.draw_lines(target)
if w.view.normals_visible:
if let batch = w.batches.normal_batch:
batch.draw(w.view.camera, target, theme.accent())
if self.workspace.view.grid_visible and not sketching:
# The ground grid at the snapping step round where the camera
# looks, its axes in the accent.
let step = grid_step(w.view.distance)
let major = step * 5.0
let cx = math.round(w.view.target.x / major) * major
let cz = math.round(w.view.target.z / major) * major
let half = step * 30.0
draw_grid(w.view.camera, target, Vector3(cx, -0.008, cz), Vector3(1.0, 0.0, 0.0), Vector3(0.0, 0.0, 1.0), step, theme.grey(58.0), theme.grey(80.0))
let axes: list[Vector3] = []
if math.abs(cz) <= half:
axes.append(Vector3(cx - half, -0.007, 0.0))
axes.append(Vector3(cx + half, -0.007, 0.0))
if math.abs(cx) <= half:
axes.append(Vector3(0.0, -0.007, cz - half))
axes.append(Vector3(0.0, -0.007, cz + half))
if axes.length > 0:
WireRenderer.lines(axes, w.view.camera, target, theme.accent(), 1.0, 0.0, writes_depth = false)
# Point clouds after the opaque scene, all sorted together back to
# front: they veil what lies behind them and hide behind what is in
# front.
self.mark_selection()
self.splats.set_look(w.view.splat_look())
self.splats.draw(w.batches.clouds, w.view.camera, target)
# Fog last of the 3D, every grid in one march: it veils the meshes,
# lines and grid behind it and ends at those in front.
self.fog.draw(w.batches.volumes, w.view.camera, target)
let overlay = Painter(target)
# Where we are, centered along the top: the network's path (and
# whether a node is previewed alone).
let where = self.workspace.network.path() + (" / PREVIEW" if self.workspace.network.preview else "")
overlay.text(where, (size.width - float(where.length) * 7.5) * 0.5, 14.0, theme.text_muted())
overlay.text(f"Y up / grid: {units.length_text(grid_step(w.view.distance), units.document_unit(w))}", snap_row_end() + 10.0, size.height - 32.0, theme.text_muted())
self.tools.draw(overlay, size.width, size.height)
self.sketch.draw(overlay, size.width, size.height)
self.marking.draw(overlay, size.width, size.height)
# The shading button, left of the view buttons.
let shading = shading_button(size.width)
overlay.rounded_rectangle(shading, theme.surface(), 4.0)
mode_icon(overlay, w.view.display_mode, shading.x + 4.0, 11.0)
overlay.rectangle(Rect(shading.x + 28.0, 34.0, 3.0, 3.0), theme.accent())
overlay.close()
## While Edit GSplats is edited, its cloud shows the selected splats
## tinted; others show none. Its cloud is the one the edited (displayed)
## node drew from the very points the pick mesh shares: same node, same
## positions id. Another cloud of the same size, even an earlier version
## of this one shown by another node, is left alone.
pub func mark_selection(self) -> unit!:
let w = self.workspace
var mesh: Mesh? = none
if let kind = w.active_kind():
if kind.tints and w.editing():
mesh = w.geometry
for at in 0..<w.batches.clouds.length:
let cloud = w.batches.clouds[at]
var marks: Selection? = none
if let picked = mesh:
if is_edited_cloud(cloud, w.batches.cloud_nodes, at, w.network.display, picked):
if let chosen = w.selection:
if chosen.domain() == 0 and chosen.fits(picked):
marks = chosen
cloud.set_marks(marks)
## The tool column's pick: in a sketch the editor's tool, else the
## workspace's.
func reuse_tool(self, name: str) -> unit!:
if self.workspace.sketching():
self.sketch.editor.choose(name)
elif name != "":
modeling_commands.choose_tool(self.workspace, name)
self.layout().invalidate()
## A held A, S or (in a sketch) D opens its marking menu at the pointer;
## a tap leaves it open to click.
func open_marking(self, event: Event) -> bool!:
if event.kind != EventKind.key_down or event.repeated or event.meta or event.control or event.alt:
return false
if not (event.key in [Key.a, Key.s, Key.d]):
return false
let w = self.workspace
if event.key == Key.d and not w.sketching():
return false
let add = self.on_add else return false
let editor = self.sketch.editor
var items: list[MarkItem] = []
var title = "Create"
if event.key == Key.a:
items = marking_items.create_items(w, self.actions, add, editor)
elif event.key == Key.s:
title = "Modify"
items = marking_items.modify_items(w, self.actions, add, editor)
else:
title = "Constraints"
items = marking_items.constraint_items(editor)
self.marking.show(title, items, event.key, self.pointer_x, self.pointer_y)
return true
## The world axis zebra and isophote stripes run around: X, Y, Z or the view's up.
func zebra_axis(self) -> Vector3!:
let axis = self.workspace.view.zebra_axis
if axis == 3:
return self.workspace.view.camera.projection(1.0).up
return Vector3(1.0 if axis == 0 else 0.0, 1.0 if axis == 1 else 0.0, 1.0 if axis == 2 else 0.0)
pub func event(self, event: Event) -> unit!:
let bounds = self.layout().bounds()
if event.kind == EventKind.pointer_moved or event.kind == EventKind.pointer_down:
self.pointer_x = event.x
self.pointer_y = event.y
# A tool that snaps marks where this move landed afresh.
self.workspace.snap_mark = none
if event.kind == EventKind.key_down and event.key == Key.tab:
if let search = self.on_search:
search()
return
if self.marking.event(event, bounds.width, bounds.height):
self.layout().invalidate()
return
if self.open_marking(event):
self.layout().invalidate()
return
if event.cancelled or event.kind == EventKind.focus_lost:
self.mode_press = -1
# Shift or Cmd held during a tool prompt peeks at its input.
if event.kind in [EventKind.modifiers_changed, EventKind.key_down, EventKind.key_up, EventKind.pointer_moved, EventKind.pointer_down]:
tool_prompts.set_peek(self.workspace, event.shift or event.meta)
elif event.kind == EventKind.focus_lost:
tool_prompts.set_peek(self.workspace, false)
if event.kind == EventKind.pointer_down and event.inside and not event.alt and on_shading_button(event.x, event.y, bounds.width):
if event.button == 1:
if let open = self.on_shading:
open(false)
else:
self.mode_press = time.now()
return
if event.kind == EventKind.pointer_up and self.mode_press >= 0:
self.mode_press = -1
if let open = self.on_shading:
open(false)
return
if event.cancelled:
self.dragging = false
self.pending_right = false
# A click away from the snapping options closes them.
if event.kind == EventKind.pointer_down and self.tools.chrome.snaps_open and self.tools.chrome.button_at(event.x, event.y, bounds.width, bounds.height) < 400:
self.tools.chrome.snaps_open = false
self.layout().invalidate()
# The viewport's buttons keep their clicks from every tool.
if event.kind in [EventKind.pointer_down, EventKind.pointer_moved] and event.inside and not event.alt and self.tools.chrome.button_at(event.x, event.y, bounds.width, bounds.height) >= 0:
_ = self.tools.event(event, bounds.width, bounds.height)
self.layout().invalidate()
return
# The sketch editor, then Pick profiles, before other tools.
if self.sketch.editor.event(event, bounds.width, bounds.height) or self.profiles.event(event, bounds.width, bounds.height):
self.layout().invalidate()
return
if self.tools.event(event, bounds.width, bounds.height):
self.layout().invalidate()
return
# luce-ui currently drops modifiers on its special right-press route.
# Arm only; the first move must confirm Alt before any camera change.
if event.kind == EventKind.pointer_down and event.inside and event.button == 1:
self.pending_right = true
self.last_x = event.x
self.last_y = event.y
if event.kind == EventKind.pointer_moved and self.pending_right:
self.pending_right = false
if event.alt:
self.dragging = true
self.navigation_button = 1
if event.kind == EventKind.pointer_down and event.inside and event.alt and (event.button == 0 or event.button == 1 or event.button == 2):
self.dragging = true
self.navigation_button = event.button
self.last_x = event.x
self.last_y = event.y
elif event.kind == EventKind.pointer_moved and self.dragging:
let dx = event.x - self.last_x
let dy = event.y - self.last_y
if self.navigation_button == 2:
self.workspace.view.pan(dx, dy, self.layout().bounds().height)
elif self.navigation_button == 1:
self.workspace.view.zoom((dx - dy) * 0.1)
else:
self.workspace.view.orbit(dx, dy)
self.last_x = event.x
self.last_y = event.y
elif event.kind == EventKind.pointer_up or event.kind == EventKind.focus_lost or event.kind == EventKind.overflow:
self.dragging = false
self.pending_right = false
elif event.kind == EventKind.scroll:
self.workspace.view.zoom(event.scroll_y * (0.05 if event.scroll_unit == ScrollUnit.points else 1.0))
elif event.kind == EventKind.magnify:
self.workspace.view.zoom(event.scroll_y * 12.0)
elif event.kind == EventKind.key_down:
if event.key == Key.escape:
self.dragging = false
if self.workspace.placing != none: self.workspace.cancel_placing()
elif self.workspace.prompt != none: self.workspace.cancel_prompt()
elif event.key == Key.enter and self.workspace.placing != none:
placing_commands.finish_placing(self.workspace)
elif event.key == Key.enter and self.workspace.prompt != none:
tool_prompts.accept_prompt(self.workspace)
# Hover over the same component changes nothing on screen.
if event.kind == EventKind.pointer_moved and not self.dragging and not self.tools.hover_changed:
return
self.layout().invalidate()
## Whether drawn cloud `at` is the one an Edit node displayed as `node`
## picks on with `picked`: it came from that node and was packed from the
## positions the pick mesh shares.
pub func is_edited_cloud(cloud: GaussianSplats, nodes: list[int], at: int, node: int, picked: Mesh) -> bool:
if at >= nodes.length or nodes[at] != node:
return false
return cloud.source_id() == picked.positions_id() and cloud.size() == picked.point_count()