Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-3d

Source of release 0.9.5

0.9.5 / tests / objects / main.luc

from luce_geocore import geocore
from luce_3d.graphics import Knife, BoxSelect, EditOverlay, SurfaceHits, Scene, Group, Mesh, SphereGeometry, MeshLambertMaterial, Color, Vector3, Camera, AmbientLight, DirectionalLight, Renderer
from luce_gpu.gpu import Frame
from luce_gpu import gpu
from luce_window import window
from luce_std import math

pub func main(arguments: list[str]) -> int!:
    let geometry = SphereGeometry(1.0, 16, 8)
    let material = MeshLambertMaterial(Color(0.1, 0.4, 1.0))
    let mesh = Mesh(geometry, material)
    mesh.set_position(Vector3(0.2, 0.0, 0.0))
    mesh.set_rotation(Vector3(0.1, 0.2, 0.0))
    let group = Group()
    group.set_scale(Vector3(1.0, 0.8, 1.2))
    group.add(mesh)
    let scene = Scene()
    scene.add(group)
    scene.add(AmbientLight(intensity = 0.2))
    scene.add(DirectionalLight(intensity = 0.8))
    let camera = Camera(50.0, 1.0, 0.1, 100.0)
    let renderer = Renderer()
    let frame = Frame(window.Size(200.0, 160.0, 200.0, 160.0, 1.0))
    let target = frame.target()
    renderer.set_wire_overlay(1.0)
    renderer.render(scene, camera, target)
    renderer.set_wire_overlay(0.0)
    # A host view renders into its own region; fitting its aspect leaves the camera's alone.
    renderer.render(scene, camera, target.region(gpu.Rect(8.0, 8.0, 80.0, 60.0)), fit_aspect = true)
    assert(camera.aspect() == 1.0)
    assert(group.remove(mesh))
    scene.add(mesh)
    assert(group.node().child_count() == 0 and scene.node().child_count() == 4)
    edit_overlays(target)
    box_selection()
    cad_edges()
    surface_hits()
    static_reuse(target)
    frame.close()
    print("ok Luce scene objects and region rendering")
    return 0

func screen(camera: Camera, point: Vector3) -> list[float]!:
    let p = camera.projection(200.0 / 160.0)
    let offset = point.subtract(p.position)
    let depth = -offset.dot(p.backward)
    return [(offset.dot(p.right) * p.focal / p.aspect / depth + 1.0) * 100.0, (1.0 - offset.dot(p.up) * p.focal / depth) * 80.0]

## Edit overlays keep selection batches in Base and pick through the retained
## screen projection, confirming the nearest candidate with one ray.
func edit_overlays(target: gpu.RenderTarget) -> unit!:
    let cube = geocore.Mesh.cube(2.0)
    let camera = Camera(50.0, 1.0, 0.1, 100.0)
    camera.set_position(Vector3(3.0, 4.0, 5.0))
    camera.look_at(Vector3(0.0, 1.0, 0.0))
    let overlay = EditOverlay()
    let grey = gpu.Color(0.3, 0.3, 0.3)
    let accent = gpu.Color(1.0, 0.5, 0.0)
    let top = EditOverlay.center(cube, 2, [2, 2])
    assert(top.x == 0.0 and top.y == 2.0 and top.z == 0.0)
    assert(EditOverlay.center(cube, 0, []).y == 1.0 and EditOverlay.center(cube, 1, [0]).y == 1.0)
    overlay.update(cube, 2, [0], 1)
    assert(overlay.accent_segments() == 4 and overlay.selected_triangles() == 2)
    let builds = overlay.selection_builds()
    for hovered in [2, 3, -1, 4]:
        overlay.update(cube, 2, [0], hovered)
        overlay.draw(camera, target, grey, accent, grey)
    assert(overlay.selection_builds() == builds)
    overlay.update(cube, 2, [0, 5], -1)
    assert(overlay.selection_builds() == builds + 1 and overlay.selected_triangles() == 4)
    overlay.draw(camera, target, grey, accent, grey)
    # A see-through pass (pick geometry inside a surface, such as SDF
    # proxies) draws the selection over everything first, rebuilding nothing.
    overlay.draw(camera, target, grey, accent, grey, hidden = grey)
    assert(overlay.selection_builds() == builds + 1)
    overlay.update(cube, 1, [0, 1], 3)
    assert(overlay.accent_segments() == 2 and overlay.selected_triangles() == 0)
    overlay.update(cube, 0, [2, 3, 6, 7], -1)
    assert(overlay.accent_segments() == 4)
    overlay.draw(camera, target, grey, accent, grey)
    let near = screen(camera, cube.point(6))
    assert(overlay.pick(camera, near[0] + 2.0, near[1] - 1.0, 200.0, 160.0) == 6)
    let hidden = screen(camera, cube.point(0))
    assert(overlay.pick(camera, hidden[0], hidden[1], 200.0, 160.0) != 0)
    var top_edge = -1
    for edge in 0..<cube.edge_count():
        let a = cube.edge_point(edge, 0)
        let b = cube.edge_point(edge, 1)
        if (a == 6 and b == 7) or (a == 7 and b == 6):
            top_edge = edge
    overlay.update(cube, 1, [], -1)
    let middle = screen(camera, cube.point(6).add(cube.point(7)).multiply_scalar(0.5))
    assert(overlay.pick(camera, middle[0], middle[1] + 1.0, 200.0, 160.0) == top_edge)
    # A new mesh replaces every retained batch.
    let moved = cube.moved_points([6, 7], Vector3(0.0, 1.5, 0.0))
    overlay.update(moved, 1, [], -1)
    overlay.draw(camera, target, grey, accent, grey)
    let raised = screen(camera, moved.point(6).add(moved.point(7)).multiply_scalar(0.5))
    assert(overlay.pick(camera, raised[0], raised[1] + 1.0, 200.0, 160.0) == top_edge)
    assert(overlay.pick(camera, middle[0], middle[1] + 1.0, 200.0, 160.0) != top_edge)
    # A knife stroke across the cube's middle: its plane holds the eye, it
    # passes over the faces under the line, and Shift snaps it to 15°.
    let across = screen(camera, cube.point(0).add(cube.point(6)).multiply_scalar(0.5))
    let stroke = Knife.plane(camera, across[0] - 30.0, across[1], across[0] + 30.0, across[1], 200.0, 160.0)
    assert(math.abs(stroke.normal.length() - 1.0) < 0.000001 and stroke.point.subtract(camera.position()).length() < 0.000001)
    let under = Knife.faces_under(cube, camera, across[0] - 30.0, across[1], across[0] + 30.0, across[1], 200.0, 160.0)
    assert(under.count() >= 2 and under.count() <= 6)
    let level = Knife.snapped(0.0, 0.0, 10.0, 1.0)
    assert(math.abs(level.y) < 0.000001 and math.abs(level.x - math.sqrt(101.0)) < 0.000001)
    # Soft selection tints the points it reaches: point 6 weighs 1, its three
    # neighbours (2 away) 1 - 2 / 2.1 linearly, the rest nothing. A face
    # selection weighs its own points 1 and shows markers in face mode too.
    let corner = geocore.Selection.of(cube, "6", 0)
    overlay.set_soft(2.1, 0)
    overlay.show(cube, 0, corner, -1)
    overlay.draw(camera, target, grey, accent, grey)
    assert(overlay.soft_points() == 4 and overlay.soft_weight(6) == 1.0)
    assert(math.abs(overlay.soft_weight(7) - (1.0 - 2.0 / 2.1)) < 0.0001 or math.abs(overlay.soft_weight(2) - (1.0 - 2.0 / 2.1)) < 0.0001)
    let side = geocore.Selection.of(cube, "0", 2)
    overlay.show(cube, 2, side, -1)
    overlay.draw(camera, target, grey, accent, grey)
    assert(overlay.soft_points() == 8)
    overlay.set_soft(0.0)
    overlay.show(cube, 2, side, -1)
    assert(overlay.soft_points() == 0)
    # A point cloud (CAD control vertices: no faces) picks the nearest point
    # on screen, within reach.
    let cloud = geocore.Mesh.from_corner_data([Vector3(0.0, 0.0, 0.0), Vector3(0.0, 2.0, 0.0)], [], [], [], [])
    overlay.update(cloud, 0, [1], -1)
    overlay.draw(camera, target, grey, accent, grey)
    let high = screen(camera, cloud.point(1))
    assert(overlay.pick(camera, high[0] + 3.0, high[1], 200.0, 160.0) == 1)
    assert(overlay.pick(camera, high[0] + 40.0, high[1], 200.0, 160.0) == -1)

## A box selects the points inside it and the edges and faces it touches,
## through the depth or only what view rays reach; objects show in a box
## when any face meets it.
func box_selection() -> unit!:
    let cube = geocore.Mesh.cube(2.0)
    let camera = Camera(50.0, 1.0, 0.1, 100.0)
    camera.set_position(Vector3(3.0, 4.0, 5.0))
    camera.look_at(Vector3(0.0, 1.0, 0.0))
    # The whole view: from here 7 corners, 9 edges and 3 faces show.
    for (domain, all, seen) in [(0, 8, 7), (4, 12, 9), (2, 6, 3)]:
        let none_yet = geocore.Selection.empty(cube, domain)
        let through = BoxSelect.select(cube, camera, none_yet, 0.0, 0.0, 200.0, 160.0, 200.0, 160.0, 0, visible_only = false)
        assert(through.count() == all, f"domain {domain}: {through.count()} through")
        let visible = BoxSelect.select(cube, camera, none_yet, 200.0, 160.0, 0.0, 0.0, 200.0, 160.0, 0)
        assert(visible.count() == seen, f"domain {domain}: {visible.count()} visible")
        # Taking the visible from everything leaves the hidden; adding them back all.
        let hidden = BoxSelect.select(cube, camera, through, 0.0, 0.0, 200.0, 160.0, 200.0, 160.0, 2)
        assert(hidden.count() == all - seen)
        assert(BoxSelect.select(cube, camera, hidden, 0.0, 0.0, 200.0, 160.0, 200.0, 160.0, 1).count() == all)
    # A small box around the top corner nearest the camera (point 6) takes
    # just it; a box inside the top face, no corner in it, touches that face.
    let corner = screen(camera, cube.point(6))
    let points = BoxSelect.select(cube, camera, geocore.Selection.empty(cube, 0), corner[0] - 3.0, corner[1] - 3.0, corner[0] + 3.0, corner[1] + 3.0, 200.0, 160.0, 0)
    assert(points.count() == 1 and points.contains(6))
    let top = screen(camera, Vector3(0.0, 2.0, 0.0))
    let faces = BoxSelect.select(cube, camera, geocore.Selection.empty(cube, 2), top[0] - 2.0, top[1] - 2.0, top[0] + 2.0, top[1] + 2.0, 200.0, 160.0, 0)
    assert(faces.count() == 1 and faces.contains(2), f"{faces.count()} faces in the top face's middle")
    assert(BoxSelect.touches(cube, camera, top[0] - 2.0, top[1] - 2.0, top[0] + 2.0, top[1] + 2.0, 200.0, 160.0))
    assert(BoxSelect.touches(cube, camera, -50.0, -50.0, 250.0, 250.0, 200.0, 160.0))
    assert(not BoxSelect.touches(cube, camera, 0.0, 0.0, 6.0, 6.0, 200.0, 160.0))

## View rays meet the nearest visible polygon face, placed by its parents;
## hidden objects are skipped; a construction plane catches the rest.
func surface_hits() -> unit!:
    let root = Group()
    let near = Mesh.of(geocore.Mesh.cube(2.0), MeshLambertMaterial(Color(0.5, 0.5, 0.5)))
    near.set_position(Vector3(0.0, 0.0, 3.0))
    let far = Mesh.of(geocore.Mesh.cube(2.0), MeshLambertMaterial(Color(0.5, 0.5, 0.5)))
    let holder = Group()
    holder.set_scale(Vector3(2.0, 2.0, 2.0))
    holder.add(far)
    root.add(near)
    root.add(holder)
    # Straight down onto the near cube's top (y 2 at z 3).
    let top = SurfaceHits.cast(root, Vector3(0.0, 10.0, 3.0), Vector3(0.0, -1.0, 0.0))
    assert(top.found and math.abs(top.point.y - 2.0) < 0.00001 and top.normal.y > 0.999)
    # Down onto the scaled cube: its top is at y 4.
    let scaled = SurfaceHits.cast(root, Vector3(0.0, 10.0, 0.0), Vector3(0.0, -1.0, 0.0))
    assert(scaled.found and math.abs(scaled.point.y - 4.0) < 0.00001)
    # Along Z through both: the near cube's front face first.
    let front = SurfaceHits.cast(root, Vector3(0.0, 1.0, 10.0), Vector3(0.0, 0.0, -1.0))
    assert(front.found and math.abs(front.point.z - 4.0) < 0.00001 and front.normal.z > 0.999)
    near.node().set_visible(false)
    let behind = SurfaceHits.cast(root, Vector3(0.0, 1.0, 10.0), Vector3(0.0, 0.0, -1.0))
    assert(behind.found and math.abs(behind.point.z - 2.0) < 0.00001)
    let missed = SurfaceHits.cast(root, Vector3(50.0, 1.0, 10.0), Vector3(0.0, 0.0, -1.0))
    assert(not missed.found)
    let ground = SurfaceHits.plane(Vector3(1.0, 5.0, 1.0), Vector3(0.0, -1.0, 1.0), Vector3(), Vector3(0.0, 1.0, 0.0))
    assert(ground.found and math.abs(ground.point.z - 6.0) < 0.00001 and math.abs(ground.point.y) < 0.00001)
    assert(not SurfaceHits.plane(Vector3(0.0, 5.0, 0.0), Vector3(0.0, 1.0, 0.0), Vector3(), Vector3(0.0, 1.0, 0.0)).found)

## A static scene re-prepares only changed objects: adding a mesh keeps the
## other's data, and a hidden mesh shown again right away reuses its own.
func static_reuse(target: gpu.RenderTarget) -> unit!:
    let renderer = Renderer(true)
    let scene = Scene()
    let first = Mesh.of(geocore.Mesh.cube(2.0), MeshLambertMaterial(Color(0.6, 0.6, 0.6)))
    let second = Mesh.of(geocore.Mesh.cube(1.0), MeshLambertMaterial(Color(0.9, 0.5, 0.1)))
    scene.add(first)
    scene.add(DirectionalLight(intensity = 0.8))
    let camera = Camera(50.0, 1.0, 0.1, 100.0)
    camera.set_position(Vector3(3.0, 4.0, 5.0))
    camera.look_at(Vector3(0.0, 1.0, 0.0))
    renderer.render(scene, camera, target)
    scene.add(second)
    renderer.render(scene, camera, target)
    assert(renderer.reused_count() == 1)
    assert(scene.remove(first))
    renderer.render(scene, camera, target)
    scene.add(first)
    renderer.render(scene, camera, target)
    assert(renderer.reused_count() == 4)

## Analytic edge tags filter interior tessellation edges and expand hover
## only within the same model, even when another model reuses the edge ID.
func cad_edges() -> unit!:
    let cube = geocore.Mesh.cube(2.0)
    let ids: list[float] = []
    let owners: list[float] = []
    for edge in 0..<cube.edge_count():
        ids.append(7.0 if edge < 3 else -1.0)
        owners.append(1.0 if edge == 2 else 0.0)
    let tagged = cube.with_attribute("cad_edge", 4, 1, ids, true).with_attribute("cad_edge_model", 4, 1, owners, true)
    let overlay = EditOverlay()
    overlay.update(tagged, 1, [], 0)
    assert(overlay.hover_segments() == 2)
    overlay.update(tagged, 1, [], 2)
    assert(overlay.hover_segments() == 1)
    overlay.update(tagged, 1, [], 3)
    assert(overlay.hover_segments() == 0)
    # No analytic edges: the display mesh's edges cannot be picked.
    let hidden: list[float] = []
    for edge in 0..<cube.edge_count(): hidden.append(-1.0)
    let interiors = cube.with_attribute("cad_edge", 4, 1, hidden, true)
    let camera = Camera(50.0, 1.0, 0.1, 100.0)
    camera.set_position(Vector3(3.0, 4.0, 5.0))
    camera.look_at(Vector3(0.0, 1.0, 0.0))
    overlay.update(interiors, 1, [], -1)
    let at = screen(camera, cube.point(6))
    assert(overlay.pick(camera, at[0], at[1], 200.0, 160.0) == -1)
    print("PASS analytic edge filters and complete model-local hover")