Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.9

0.11.9 / tools / preview_modify.luc

## Stage 5's captures for preview.py (docs/research/POLY-MODELING.md): the
## everyday modify tools in Edit Mesh, shown with their wireframes.
##   modify_knife: the Knife drawing a stroke over a box, its points
##     snapped, the rubber band to the pointer, the hint on the notice line.
##   modify_knife_cut: that stroke cut (Enter): the box's faces split along
##     it, the cut edges selected.
##   modify_bridge: a sphere's middle band gone, its loops bridged with 3
##     cuts and a slight sphere Profile (0.15) closing it round again.
##   modify_bridge_pick: Bridge run on one run: it waits for the second
##     loop, the prompt on the notice line.
##   modify_bridge_runs: a grid without its two middle rows, its two runs
##     bridged with 3 cuts along Blend path: a flat strip, rows even.
##   modify_grid_fill: a cylinder's top cap gone, the hole grid-filled.
##   modify_solidify: a noisy bowl (a sphere's lower half, roughened),
##     made a shell inside it with Even thickness.
##   modify_poke: a box's faces poked out.
##   modify_rip: a row of a grid's edges ripped open.
##   modify_symmetrize: a lopsided sphere symmetrized, -X over +X.
##   modify_screw: a short run screwed into a spring.
##   modify_decimate: a sphere decimated to a third of its triangles.
##   modify_subdivide: an L of a grid's faces subdivided: only they split,
##     the faces around take the new points (the inner corner's cut).
from app import Luce3D
from luce_ui.ui import Event
from luce_window.input import EventKind
from luce_3d.graphics import Vector3
from luce_geocore.geocore import Selection
from view_projection import project
from edit_types import point_level, edge_level, face_level
import node_commands
import modeling_commands
import selection_commands
import placing_commands
import preview_support
from luce_std import math

pub func scenes() -> list[str]:
    return ["modify_knife", "modify_knife_cut", "modify_bridge", "modify_bridge_pick", "modify_bridge_runs", "modify_grid_fill", "modify_solidify", "modify_poke", "modify_rip",
            "modify_symmetrize", "modify_screw", "modify_decimate", "modify_subdivide"]

## An Edit Mesh holding primitive `name`.
func modeled(editor: Luce3D, name: str) -> unit!:
    let w = editor.workspace
    node_commands.delete_selected(w)
    node_commands.add_node(w, "Edit Mesh", 40.0, 40.0)
    preview_support.settle(w)
    modeling_commands.create(w, name)
    preview_support.settle(w)

## Run tool `label` on `expression` (on `domain`: 0 points, 4 edges, 2
## faces) at `level`, then set its numbers `slots` to `values`.
func tool(editor: Luce3D, label: str, level: int, expression: str, domain: int, slots: list[int] = [], values: list[float] = []) -> unit!:
    let w = editor.workspace
    let mesh = preview_support.shown(w) else trap("mesh")
    selection_commands.set_mode(w, level)
    w.selection = Selection.of(mesh, expression, domain)
    modeling_commands.model(w, label)
    preview_support.settle(w)
    for index in 0..<slots.length:
        modeling_commands.set_last_step(w, slots[index], values[index], -1.0e9, 1.0e9)
    preview_support.settle(w)

## The knife's stroke over the box's top and down its front.
func stroke() -> list[Vector3]:
    return [Vector3(-0.6, 2.0, -1.0), Vector3(0.2, 2.0, 0.3), Vector3(0.7, 2.0, 1.0), Vector3(0.7, 1.0, 1.0)]

## Scene `scene` before its first frame.
pub func setup(editor: Luce3D, scene: str) -> unit!:
    let w = editor.workspace
    var target = Vector3(0.0, 1.0, 0.0)
    var distance = 5.2
    var pitch = 0.55
    if scene == "modify_knife" or scene == "modify_knife_cut":
        modeled(editor, "Box")
        modeling_commands.model(w, "Knife")
        let points = stroke()
        for point in points:
            placing_commands.add_corner(w, point, Vector3(-0.35, -0.6, -0.7))
        if scene == "modify_knife_cut":
            placing_commands.finish_placing(w)
            preview_support.settle(w)
    elif scene == "modify_bridge":
        # A sphere's middle band gone; its two loops bridged with 3 cuts and
        # a slight sphere Profile closing it round again.
        modeled(editor, "Sphere")
        let sphere = preview_support.shown(w) else trap("sphere")
        let box = sphere.bounds()
        let middle = (box.minimum.y + box.maximum.y) * 0.5
        let band: list[str] = []
        for face in 0..<sphere.face_count():
            if math.abs(sphere.face_center(face).y - middle) < 0.3:
                band.append(f"{face}")
        tool(editor, "Delete", face_level, band.join(" "), 2)
        tool(editor, "Bridge", edge_level, "*", 4, [0, 2, 4, 5], [3.0, 1.0, 0.15, 1.0])
    elif scene == "modify_bridge_pick":
        modeled(editor, "Grid")
        modeling_commands.set_last_step(w, 2, 6.0, 1.0, 1000.0)
        modeling_commands.set_last_step(w, 3, 6.0, 1.0, 1000.0)
        preview_support.settle(w)
        tool(editor, "Delete", face_level, "12-23", 2)
        tool(editor, "Bridge", edge_level, "p14-15-16-17-18-19-20", 4)
        target = Vector3(0.0, 0.3, 0.0)
    elif scene == "modify_bridge_runs":
        modeled(editor, "Grid")
        modeling_commands.set_last_step(w, 2, 6.0, 1.0, 1000.0)
        modeling_commands.set_last_step(w, 3, 6.0, 1.0, 1000.0)
        preview_support.settle(w)
        tool(editor, "Delete", face_level, "12-23", 2)
        tool(editor, "Bridge", edge_level, "p14-15-16-17-18-19-20 p21-22-23-24-25-26-27", 4, [0], [3.0])
        target = Vector3(0.0, 0.0, 0.0)
    elif scene == "modify_grid_fill":
        modeled(editor, "Cylinder")
        tool(editor, "Delete", face_level, "1", 2)
        tool(editor, "Grid Fill", edge_level, "*", 4)
    elif scene == "modify_solidify":
        # A sphere's lower half, roughened: a noisy bowl, its shell inside.
        modeled(editor, "Sphere")
        let ball = preview_support.shown(w) else trap("sphere")
        let bounds = ball.bounds()
        let equator = (bounds.minimum.y + bounds.maximum.y) * 0.5
        let upper: list[str] = []
        for face in 0..<ball.face_count():
            if ball.face_center(face).y > equator:
                upper.append(f"{face}")
        tool(editor, "Delete", face_level, upper.join(" "), 2)
        tool(editor, "Noise", point_level, "*", 0, [0], [0.05])
        tool(editor, "Solidify", face_level, "*", 2, [0, 2], [0.15, 1.0])
        target = Vector3(0.0, 0.6, 0.0)
        pitch = 0.75
    elif scene == "modify_poke":
        modeled(editor, "Box")
        tool(editor, "Poke", face_level, "*", 2, [0], [0.6])
    elif scene == "modify_rip":
        modeled(editor, "Grid")
        modeling_commands.set_last_step(w, 2, 6.0, 1.0, 1000.0)
        modeling_commands.set_last_step(w, 3, 6.0, 1.0, 1000.0)
        preview_support.settle(w)
        tool(editor, "Rip", edge_level, "p22-23-24-25-26", 4, [0], [0.25])
        target = Vector3(0.0, 0.0, 0.0)
    elif scene == "modify_symmetrize":
        modeled(editor, "Sphere")
        tool(editor, "Noise", point_level, "@P.x>0.01", 0, [0], [0.35])
        tool(editor, "Symmetrize", face_level, "*", 2)
    elif scene == "modify_screw":
        modeled(editor, "Grid")
        modeling_commands.set_last_step(w, 0, 0.6, 0.0, 1000.0)
        modeling_commands.set_last_step(w, 1, 0.6, 0.0, 1000.0)
        modeling_commands.set_last_step(w, 2, 1.0, 1.0, 1000.0)
        modeling_commands.set_last_step(w, 3, 1.0, 1.0, 1000.0)
        preview_support.settle(w)
        tool(editor, "Screw", edge_level, "p0-1", 4, [0, 1, 2, 3, 4, 5, 6], [1.0, 1.2, 0.0, 0.0, 4.0, 24.0, 0.8])
        target = Vector3(0.0, 1.5, 0.0)
        distance = 7.5
    elif scene == "modify_decimate":
        modeled(editor, "Sphere")
        tool(editor, "Triangulate", face_level, "*", 2)
        tool(editor, "Decimate", face_level, "*", 2, [1], [0.33])
    elif scene == "modify_subdivide":
        modeled(editor, "Grid")
        tool(editor, "Subdivide", face_level, "5 6 9", 2, [1], [0.0])
        target = Vector3(0.0, 0.0, 0.0)
        distance = 4.2
    w.view.yaw = 0.6
    w.view.pitch = pitch
    w.view.distance = distance
    w.view.target = target
    w.view.update_camera()
    editor.panels.refresh()

## Window point of world point `p`.
func at(editor: Luce3D, p: Vector3) -> list[float]!:
    let area = editor.panels.view.layout().bounds()
    let on = project(editor.workspace, p, area.width, area.height)
    return [area.x + on[0], area.y + on[1]]

## Frame `frame` of scene `scene` (from 2; `last` is the frame captured):
## the Knife's pointer rests past its last point.
pub func act(editor: Luce3D, scene: str, frame: int, last: int) -> unit!:
    editor.app.focus(editor.panels.view.layout())
    if scene == "modify_knife":
        let end = at(editor, Vector3(0.7, 0.4, 1.0))
        editor.app.dispatch(Event(kind = EventKind.pointer_moved, x = end[0], y = end[1]))
    editor.panels.refresh()