Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.9

0.11.9 / tools / preview_create.luc

## Stage 6's captures for preview.py (docs/research/POLY-MODELING.md):
## primitives, Poly Pen and retopology in Edit Mesh, with wireframes.
##   create_gallery: every primitive made at the origin and placed in rows:
##     box (rounded), grid, plane, sphere, icosphere, quad sphere; cylinder
##     (grid caps), cone, pipe, disc (grid fill), torus (twisted); capsule,
##     dodecahedron, helix.
##   create_box: a box being drawn from viewport events: its base dragged
##     on the ground, the pointer raising it, the value box reading Height.
##   create_surface: a cylinder being drawn on a box's top, its base still
##     held, the value box reading Radius.
##   pen_strip: Poly Pen's strip of three quads on the ground, a corner of
##     the next pair placed, the edge under the pointer lit.
##   retopo_sphere: a sphere in one object as the snap target, an Edit Mesh
##     in another: Poly Pen quads drawn over its top, every point on it.
##   retopo_shrinkwrap: a grid above the sphere, shrinkwrapped onto its top
##     as an Edit Mesh step reading the Reference.
from app import Luce3D
from luce_ui.ui import Event
from luce_window.input import EventKind, Key
from luce_3d.graphics import Vector3
from view_projection import project
import node_commands
import modeling_commands
import retopology
import tool_registry
import preview_support
from luce_std import math

pub func scenes() -> list[str]:
    return ["create_gallery", "create_box", "create_surface", "pen_strip", "retopo_sphere", "retopo_shrinkwrap"]

## 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]]

func send(editor: Luce3D, kind: EventKind, p: Vector3, meta: bool = false) -> unit!:
    let point = at(editor, p)
    editor.app.dispatch(Event(kind = kind, x = point[0], y = point[1], button = 0, meta = meta))
    editor.panels.refresh()

func click(editor: Luce3D, p: Vector3) -> unit!:
    send(editor, EventKind.pointer_moved, p)
    send(editor, EventKind.pointer_down, p)
    send(editor, EventKind.pointer_up, p)
    preview_support.settle(editor.workspace)

## An empty Edit Mesh.
func edit(editor: Luce3D) -> 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)

## Primitive `name` at the origin, its slots `slots` then set to `values`,
## its base moved to `base`.
func made(editor: Luce3D, name: str, base: Vector3, slots: list[int] = [], values: list[float] = []) -> unit!:
    let w = editor.workspace
    modeling_commands.create(w, name)
    preview_support.settle(w)
    let tool = tool_registry.find(name) else trap(name)
    let place = tool.place else trap(name)
    for index in 0..<slots.length:
        modeling_commands.set_last_step(w, slots[index], values[index], -1.0e9, 1.0e9)
    modeling_commands.set_last_step(w, place.position, base.x, -1.0e9, 1.0e9)
    modeling_commands.set_last_step(w, place.position + 2, base.z, -1.0e9, 1.0e9)
    preview_support.settle(w)

## The sphere (radius 2) in Geo 1 and Geo 2 entered with an Edit Mesh; the
## sphere picked as the snap target.
func retopology_scene(editor: Luce3D) -> unit!:
    let w = editor.workspace
    node_commands.delete_selected(w)
    node_commands.add_node(w, "Sphere", 0.0, 0.0)
    node_commands.set_parameter(w, 0, 2.0)
    node_commands.set_parameter(w, 1, 48.0)
    node_commands.set_parameter(w, 2, 24.0)
    node_commands.leave_group(w)
    node_commands.add_node(w, "Geo", 300.0, 0.0)
    node_commands.enter_group(w, w.network.selected)
    node_commands.add_node(w, "Edit Mesh", 0.0, 0.0)
    preview_support.settle(w)

## A point of the radius-2 sphere's top (its center at (0, 2, 0)).
func on_top(x: float, z: float) -> Vector3:
    return Vector3(x, 2.0 + math.sqrt(4.0 - x * x - z * z), z)

## Scene `scene` before its first frame.
pub func setup(editor: Luce3D, scene: str) -> unit!:
    let w = editor.workspace
    var target = Vector3(0.0, 0.5, 0.0)
    var distance = 7.0
    var pitch = 0.55
    if scene == "create_gallery":
        edit(editor)
        made(editor, "Box", Vector3(-6.0, 0.0, -4.0), [6, 7], [0.3, 3.0])
        made(editor, "Grid", Vector3(-3.0, 0.0, -4.0))
        made(editor, "Plane", Vector3(0.0, 0.0, -4.0))
        made(editor, "Sphere", Vector3(3.0, 0.0, -4.0))
        made(editor, "Icosphere", Vector3(6.0, 0.0, -4.0))
        made(editor, "Quad Sphere", Vector3(-6.0, 0.0, 0.0))
        made(editor, "Cylinder", Vector3(-3.0, 0.0, 0.0), [4], [3.0])
        made(editor, "Cone", Vector3(0.0, 0.0, 0.0), [4], [2.0])
        made(editor, "Pipe", Vector3(3.0, 0.0, 0.0))
        made(editor, "Disc", Vector3(6.0, 0.0, 0.0), [2, 3], [2.0, 2.0])
        made(editor, "Torus", Vector3(-6.0, 0.0, 4.0), [4], [3.0])
        made(editor, "Capsule", Vector3(-3.0, 0.0, 4.0))
        made(editor, "Platonic Solid", Vector3(0.0, 0.0, 4.0), [0], [3.0])
        made(editor, "Helix", Vector3(3.0, 0.0, 4.0))
        target = Vector3(0.0, 0.5, 0.0)
        distance = 19.0
        pitch = 0.75
    elif scene == "create_box" or scene == "create_surface":
        edit(editor)
        if scene == "create_surface":
            made(editor, "Box", Vector3(), [0, 1, 2], [3.0, 1.0, 3.0])
        target = Vector3(0.0, 0.5, 0.0)
        distance = 8.0
    elif scene == "pen_strip":
        edit(editor)
        target = Vector3(0.5, 0.0, 1.0)
        distance = 6.0
        pitch = 0.9
    elif scene == "retopo_sphere" or scene == "retopo_shrinkwrap":
        retopology_scene(editor)
        target = Vector3(1.0, 2.0, 0.0)
        distance = 9.0
        pitch = 0.7
    w.view.yaw = 0.6
    w.view.pitch = pitch
    w.view.distance = distance
    w.view.target = target
    w.view.update_camera()
    editor.panels.refresh()

## Frame `frame` of scene `scene` (from 2; `last` is the frame captured):
## the gestures, driven by viewport events.
pub func act(editor: Luce3D, scene: str, frame: int, last: int) -> unit!:
    let w = editor.workspace
    editor.app.focus(editor.panels.view.layout())
    if scene == "create_box":
        if frame == 2:
            modeling_commands.model(w, "Box")
            send(editor, EventKind.pointer_moved, Vector3(-1.0, 0.0, -0.5))
            send(editor, EventKind.pointer_down, Vector3(-1.0, 0.0, -0.5))
            send(editor, EventKind.pointer_moved, Vector3(0.0, 0.0, 0.2))
            send(editor, EventKind.pointer_moved, Vector3(1.0, 0.0, 0.8))
            send(editor, EventKind.pointer_up, Vector3(1.0, 0.0, 0.8))
        elif frame == 3:
            send(editor, EventKind.pointer_moved, Vector3(0.0, 1.6, 0.15))
    elif scene == "create_surface":
        if frame == 2:
            modeling_commands.model(w, "Cylinder")
            send(editor, EventKind.pointer_moved, Vector3(0.3, 1.0, 0.2))
            send(editor, EventKind.pointer_down, Vector3(0.3, 1.0, 0.2))
            send(editor, EventKind.pointer_moved, Vector3(0.7, 1.0, 0.4))
            send(editor, EventKind.pointer_moved, Vector3(1.1, 1.0, 0.6))
    elif scene == "pen_strip" and frame == 2:
        modeling_commands.model(w, "Poly Pen")
        for _ in 0..<3:
            editor.app.dispatch(Event(kind = EventKind.key_down, key = Key.m))
        for z in 0..<4:
            click(editor, Vector3(0.0, 0.0, float(z)))
            click(editor, Vector3(1.0, 0.0, float(z)))
        click(editor, Vector3(0.0, 0.0, 4.0))
        send(editor, EventKind.pointer_moved, Vector3(0.5, 0.0, 3.0))
    elif scene == "retopo_sphere" and frame == 2:
        retopology.pick(w)
        click(editor, on_top(0.0, 0.0))
        modeling_commands.model(w, "Poly Pen")
        for x in 0..<3:
            for z in 0..<2:
                let a = -0.9 + 0.6 * float(x)
                let b = -0.6 + 0.6 * float(z)
                for p in [on_top(a, b), on_top(a + 0.6, b), on_top(a + 0.6, b + 0.6), on_top(a, b + 0.6)]:
                    click(editor, p)
        send(editor, EventKind.pointer_moved, on_top(1.2, 0.5))
    elif scene == "retopo_shrinkwrap" and frame == 2:
        retopology.pick(w)
        click(editor, on_top(0.0, 0.0))
        modeling_commands.create(w, "Grid")
        preview_support.settle(w)
        modeling_commands.set_last_step(w, 2, 8.0, 1.0, 100.0)
        modeling_commands.set_last_step(w, 3, 8.0, 1.0, 100.0)
        modeling_commands.set_last_step(w, 0, 3.0, 0.001, 100.0)
        modeling_commands.set_last_step(w, 1, 3.0, 0.001, 100.0)
        modeling_commands.set_last_step(w, 5, 5.0, -100.0, 100.0)
        preview_support.settle(w)
        modeling_commands.model(w, "Shrinkwrap")
        preview_support.settle(w)
    editor.panels.refresh()