Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.6

0.11.6 / tests / editor / scene_node_tests.luc

## Camera, Light and Render nodes: objects at the root, never wired, placed
## by a Group, checked by Render, and drawn as viewport helpers.
from network import Network
from data_network import data_network
from luce_3d.graphics import Vector3
from overlay_lines import OverlayLines
from geometry_tests import near
import node_catalog
import node_commands
from workspace import Workspace
from outliner import Outliner

## Why node `id` fails to cook ("" when it cooks).
func failure_of(graph: Network, id: int) -> str!:
    _ = graph.evaluate_data(id) catch failure:
        return failure.message
    return ""

pub func scene_node_tests() -> unit!:
    # A Geo, a Camera and a Light at the root: objects, none wired. Render
    # takes no input: it renders the root's flagged objects.
    let graph = Network(false)
    let geo = graph.add("Geo", 0.0, 0.0)
    graph.scope = geo.id
    _ = graph.add("Box", 0.0, 0.0)
    graph.scope = -1
    let camera = graph.add("Camera", 0.0, 100.0)
    let light = graph.add("Light", 0.0, 200.0)
    let render = graph.add("Render", 0.0, 300.0)
    assert(render.inputs.length == 0 and camera.inputs.length == 0 and light.inputs.length == 0)
    assert(camera.texts[0] == f"camera{camera.id}" and light.texts[0] == f"light{light.id}")
    assert(node_catalog.category("Light") == "Render" and node_catalog.input_count("Camera") == 0 and not node_catalog.optional_input("Light"))
    # The render holds the Geo's cube and both objects.
    let data = graph.evaluate_data(render.id)
    assert(data.mesh != none or data.geometry.instance_count() > 0)
    assert(data.warning == "")
    let scene = data.geometry.scene()
    assert(scene.count() == 2 and scene.is_camera(0) and scene.path(1) == light.texts[0])
    let lens = scene.object(0).camera
    near(lens.focal_length, 50.0)
    near(scene.object(0).matrix.translation.z, 6.0)
    # The default area light hangs at y = 4 shining straight down.
    let key = scene.object(1)
    near(key.matrix.translation.y, 4.0)
    near(key.matrix.transform_direction(Vector3(0.0, 0.0, -1.0)).y, -1.0)
    # Nothing is wired at the root: not into a Light, not out of Render.
    var wired = true
    graph.connect(camera.id, light.id, 0) catch failure:
        wired = false
    assert(not wired)
    graph.connect(render.id, light.id, 0) catch failure:
        wired = false
    assert(not wired)
    # Unconnected, a Light is just its light; Render needs a camera.
    let lights = Network(false)
    let alone = lights.add("Light", 200.0, 0.0)
    let lonely = lights.add("Render", 200.0, 100.0)
    assert(lights.evaluate_data(alone.id).geometry.scene().count() == 1)
    assert(failure_of(lights, lonely.id).contains("Add a Camera node"))
    # A named camera that is missing is an error; no lights is a warning.
    let cameras = Network(false)
    _ = cameras.add("Camera", 0.0, 0.0)
    let lit_off = cameras.add("Render", 400.0, 0.0)
    assert(cameras.evaluate_data(lit_off.id).warning.contains("no lights"))
    lit_off.texts[0] = "/nowhere"
    cameras.invalidate(lit_off.id)
    assert(failure_of(cameras, lit_off.id).contains("No camera at /nowhere"))
    # A Group at the root places its cameras like geometry.
    let group = graph.add("Group", 200.0, 0.0)
    group.values[1] = 10.0
    graph.scope = group.id
    _ = graph.add("Camera", 0.0, 0.0)
    graph.scope = -1
    let moved = graph.evaluate_data(group.id)
    assert(moved.instance_count() == 1)
    let placed = moved.instance(0).geometry.scene()
    assert(placed.count() == 1)
    near(moved.instance_matrix(0).transform_point(placed.object(0).matrix.translation).x, 10.0)
    # Helpers: a perspective frustum is 4 + 4 + 3 segments, an area light 4 + 2 + 1.
    let lines = OverlayLines.make()
    lines.add_scene(data.geometry)
    assert(lines.count() == 18)
    # Absolute paths (as USD writes them) list in the Outliner under their top level.
    let w = Workspace()
    node_commands.leave_group(w)
    node_commands.add_node(w, "Camera", 0.0, 120.0)
    node_commands.set_text(w, 0, "/cameras/hero")
    let outliner = Outliner(w)
    outliner.refresh()
    # Materials: a chain of Material nodes is a library; Assign Material binds
    # the group's faces and passes the library on.
    let shapes = data_network()
    let box = shapes.add("Box", 0.0, 0.0)
    let red = shapes.add("Material", 200.0, 0.0)
    red.texts[0] = "materials/red"
    red.values[0] = 0.9
    let glass = shapes.add("Material", 200.0, 100.0)
    glass.texts[0] = "materials/glass"
    shapes.connect(red.id, glass.id, 0)
    let assign = shapes.add("Assign Material", 0.0, 200.0)
    shapes.connect(box.id, assign.id, 0)
    shapes.connect(glass.id, assign.id, 1)
    assign.texts[0] = "0-1"
    let bound = shapes.evaluate_data(assign.id)
    assert(bound.geometry.material_paths() == "materials/red\nmaterials/glass" and bound.warning == "")
    let mesh = bound.mesh else trap("mesh")
    let attribute = mesh.find_attribute("material", 2)
    assert(attribute >= 0 and mesh.attribute_text(attribute, 0) == "materials/red" and mesh.attribute_text(attribute, 4) == "")
    assign.texts[1] = "materials/glass"
    shapes.invalidate(assign.id)
    let glassy = shapes.evaluate_data(assign.id).mesh else trap("mesh")
    assert(glassy.attribute_text(glassy.find_attribute("material", 2), 1) == "materials/glass")
    assign.texts[1] = "materials/missing"
    shapes.invalidate(assign.id)
    assert(shapes.evaluate_data(assign.id).warning.contains("default material"))
    print("PASS Camera, Light, Render, Material and Assign Material nodes: objects with no inputs, Group placement, render checks, viewport helpers, material binding")