dymokomi/luced-3d
Source of release 0.11.5
0.11.5 / src / shading_menu.luc
## Modal viewport display flyout. The grouped square keeps its selected mode.
from luce_ui.ui import Widget, Layout, Constraints, Size, Painter, Event
from luce_gpu.gpu import RenderTarget, Rect
from luce_window.input import EventKind, Key
from workspace import Workspace
import theme
import math
from luce_ui import ui
pub func mode_names() -> list[str]:
return ["Wireframe", "Flat + Wireframe", "Flat", "Shaded", "Shaded + Wireframe", "Render", "Show normals", "Zebra", "Isophotes", "Curvature"]
pub func mode_asset(mode: int) -> str:
return ["wireframe", "solid-wireframe", "solid-view", "material", "shaded-wireframe", "render", "surface-normal", "reflection", "light-sun", "gradient-tool"][mode]
func curvature_names() -> list[str]:
return ["Mean", "Gaussian", "Max (k1)", "Min (k2)", "Largest |k|"]
## The viewport's CAD mesh qualities: names and surface divisions.
func quality_names() -> list[str]:
return ["Draft", "Normal", "Fine", "Finest"]
func quality_divisions() -> list[int]:
return [8, 16, 32, 64]
## The CAD mesh row, under every mode's options.
func quality_row(workspace: Workspace) -> str:
let at = quality_divisions().index_of(workspace.view.cad_quality) else 1
return f"CAD mesh: {quality_names()[at]}"
## The Render mode's sample caps a click steps through.
func render_caps() -> list[int]:
return [16, 64, 256, 1024, 4096]
## Fixed ranges a click steps through, in 1/unit; 0 is automatic.
func curvature_ranges() -> list[float]:
return [0.0, 0.1, 1.0, 10.0, 100.0]
## The options shown under the modes: stripes while Zebra or Isophotes is on,
## the value and range while Curvature is; then the CAD mesh quality; then,
## while Gaussian splats are shown, how they draw.
func option_rows(workspace: Workspace) -> list[str]:
let rows = mode_option_rows(workspace)
rows.append(quality_row(workspace))
for text in splat_rows(workspace):
rows.append(text)
return rows
## The alpha culls and size multipliers a click steps through.
func splat_culls() -> list[float]:
return [1.0 / 255.0, 0.02, 0.1, 0.3]
func splat_scales() -> list[float]:
return [1.0, 1.5, 2.0, 0.25, 0.5]
func showing_splats(workspace: Workspace) -> bool:
for cloud in workspace.batches.clouds:
if cloud.is_splats():
return true
return false
## Gaussian splats' rows: centers or shaded, the SH degree cap, the alpha
## cull and the size multiplier (none without splats on screen).
func splat_rows(workspace: Workspace) -> list[str]:
if not showing_splats(workspace):
return []
let v = workspace.view
let cull = "1/255" if v.splat_cull < 0.005 else f"{v.splat_cull}"
return ["Splats: Centers" if v.splat_centers else "Splats: Shaded", f"SH degree: {v.splat_degree}", f"Alpha cull: {cull}", f"Splat scale: {v.splat_scale}"]
func mode_option_rows(workspace: Workspace) -> list[str]:
if workspace.view.display_mode == 5:
return [f"Samples: {workspace.view.render_samples}"]
if workspace.view.display_mode == 9:
let range = "Auto" if workspace.view.curvature_range <= 0.0 else f"{workspace.view.curvature_range} / unit"
return [f"Value: {curvature_names()[workspace.view.curvature_kind]}", f"Range: {range}"]
if not (workspace.view.display_mode in [7, 8]):
return []
let axis = ["X", "Y", "Z", "View up"][workspace.view.zebra_axis]
return [f"Stripes: {workspace.view.zebra_stripes}", f"Dark share: {int(workspace.view.zebra_duty * 100.0 + 0.5)}%", f"Axis: {axis}",
"Model: Planar" if workspace.view.zebra_planar else "Model: Cylinder", "Shading: Off" if workspace.view.zebra_stripes_only else "Shading: On"]
let row: float = 35.0
pub func mode_icon(p: Painter, mode: int, x: float, y: float) -> unit!:
ui.draw_icon(p.target(), mode_asset(mode), Rect(x, y, 24.0, 24.0), theme.text())
pub class ShadingMenu: Widget:
let box: Layout
let workspace: Workspace
var hovered: int = -1
var held: bool = false
pub func init(self, workspace: Workspace) -> unit!:
self.workspace = workspace
self.box = Layout()
self.box.set_popup(true)
self.box.set_visible(false)
self.box.set_focusable(true)
self.box.set_interactive(true)
pub func layout(self) -> Layout:
return self.box
pub func measure(self, available: Constraints) -> Size!:
return Size(268.0, 12.0 + row * float(mode_names().length + option_rows(self.workspace).length))
pub func open(self, x: float, y: float, held: bool) -> unit!:
self.held = held
self.hovered = -1
self.box.set_popup_point(x, y)
self.box.set_visible(true)
pub func choose(self, index: int) -> unit!:
let modes = mode_names().length
if index >= modes:
self.option(index - modes)
return
if index < 0:
return
if index == 6:
self.workspace.toggle_normals()
else:
self.workspace.set_display_mode(index)
if index in [5, 7, 8, 9]:
# Stay open: the options appear below.
self.box.invalidate()
return
self.box.set_visible(false)
## Step an option; the draw state changes, nothing recooks (but the CAD
## mesh quality, which meshes CAD bodies again).
func option(self, index: int) -> unit!:
let w = self.workspace
let splat_index = index - mode_option_rows(w).length - 1
if splat_index >= 0:
self.splat_option(splat_index)
return
if index == mode_option_rows(w).length:
let qualities = quality_divisions()
let at = qualities.index_of(w.view.cad_quality) else 1
w.set_cad_quality(qualities[(at + 1) % qualities.length])
self.box.invalidate()
return
if w.view.display_mode == 5:
# The sample cap steps through 16 to 4096.
if index != 0:
return
let caps = render_caps()
let at = caps.index_of(w.view.render_samples) else 2
w.view.render_samples = caps[(at + 1) % caps.length]
w.revision += 1
self.box.invalidate()
return
if w.view.display_mode == 9:
if index == 0:
w.view.curvature_kind = (w.view.curvature_kind + 1) % curvature_names().length
elif index == 1:
let ranges = curvature_ranges()
let at = ranges.index_of(w.view.curvature_range) else 0
w.view.curvature_range = ranges[(at + 1) % ranges.length]
else:
return
w.revision += 1
self.box.invalidate()
return
if index == 0:
w.view.zebra_stripes = w.view.zebra_stripes * 2 if w.view.zebra_stripes < 64 else 8
elif index == 1:
w.view.zebra_duty = w.view.zebra_duty + 0.1 if w.view.zebra_duty < 0.85 else 0.1
elif index == 2:
w.view.zebra_axis = (w.view.zebra_axis + 1) % 4
elif index == 3:
w.view.zebra_planar = not w.view.zebra_planar
elif index == 4:
w.view.zebra_stripes_only = not w.view.zebra_stripes_only
else:
return
w.revision += 1
self.box.invalidate()
## Step splat option `index` (splat_rows' order); nothing recooks.
func splat_option(self, index: int) -> unit!:
let v = self.workspace.view
if index == 0:
v.splat_centers = not v.splat_centers
elif index == 1:
v.splat_degree = (v.splat_degree + 1) % 4
elif index == 2:
let culls = splat_culls()
var at = 0
for step in 0..<culls.length:
if math.abs(culls[step] - v.splat_cull) < 1.0e-9:
at = step
v.splat_cull = culls[(at + 1) % culls.length]
elif index == 3:
let scales = splat_scales()
let at = scales.index_of(v.splat_scale) else 0
v.splat_scale = scales[(at + 1) % scales.length]
else:
return
self.workspace.revision += 1
self.box.invalidate()
pub func draw(self, target: RenderTarget) -> unit!:
let p = Painter(target)
p.rectangle(Rect(0.0, 0.0, target.size().width, target.size().height), theme.surface())
let names = mode_names()
for index in 0..<names.length:
let y = 6.0 + float(index) * row
let selected = index == self.workspace.view.display_mode or (index == 6 and self.workspace.view.normals_visible)
if self.hovered == index:
p.rectangle(Rect(4.0, y, 260.0, 34.0), theme.grey(72.0))
if selected:
p.rectangle(Rect(4.0, y, 3.0, 34.0), theme.accent())
mode_icon(p, index, 12.0, y + 4.0)
p.text(names[index], 44.0, y + 8.0, theme.text())
let options = option_rows(self.workspace)
for index in 0..<options.length:
let y = 6.0 + float(names.length + index) * row
if self.hovered == names.length + index:
p.rectangle(Rect(4.0, y, 260.0, 34.0), theme.grey(72.0))
p.text(options[index], 44.0, y + 8.0, theme.text_muted())
p.close()
pub func event(self, event: Event) -> unit!:
if event.cancelled or event.kind == EventKind.focus_lost or (event.kind == EventKind.key_down and event.key == Key.escape):
self.box.set_visible(false)
self.held = false
return
if event.kind == EventKind.pointer_moved:
let rows = float(mode_names().length + option_rows(self.workspace).length)
self.hovered = int((event.y - 6.0) / row) if event.inside and event.y >= 6.0 and event.y < 6.0 + row * rows else -1
self.box.invalidate()
if (event.kind == EventKind.pointer_down and not self.held) or (event.kind == EventKind.pointer_up and self.held):
self.held = false
let rows = float(mode_names().length + option_rows(self.workspace).length)
if event.inside and event.y >= 6.0 and event.y < 6.0 + row * rows:
self.choose(int((event.y - 6.0) / row))