Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.6

0.11.6 / src / scene_projection.luc

## Hierarchical display submission. Group transforms never bake child meshes.
from geometry_data import GeometryData, key_light
from luce_geocore import geocore
from luce_3d.graphics import Group, Mesh, MeshLambertMaterial, Matrix4, Vector3, FogVolume, GaussianSplats
import theme
from overlay_lines import OverlayLines
from luce_ui import ui

pub func append_scene(data: GeometryData, parent: Group, depth: int = 0, mode: int = 3) -> int!:
    if depth > 48:
        error(ui.invalid, "Scene hierarchy exceeds the display depth budget")
    # A tint (a Check's deviation colors) shades in place of the bodies.
    if let tint = data.tint:
        parent.add(display_mesh(tint))
        return 0
    var analytic = 0
    if data.has_cad():
        let preview = data.preview()
        analytic = preview.failed_faces
        for mesh in preview.meshes:
            _ = append_scene(GeometryData(mesh), parent, depth, mode)
    if let mesh = data.mesh:
        if mesh.face_count() > 0:
            parent.add(display_mesh(mesh))
    if let preview = data.field_preview():
        parent.add(display_mesh(preview))
    for index in 0..<data.instance_count():
        if not data.instance_visible(index):
            continue
        let branch = Group()
        branch.set_position(data.instance_translation(index))
        branch.set_rotation(data.instance_rotation(index))
        branch.set_scale(data.instance_scale(index))
        analytic += append_scene(data.instance(index), branch, depth + 1, mode)
        parent.add(branch)
    return analytic

## Like append_scene, for a copy placed by `matrix` (the rest of the scene
## around a context, docs/NETWORK-LEVELS.md): its meshes placed and flat
## under `parent`. Returns the CAD preview patches that failed.
pub func append_placed(data: GeometryData, matrix: Matrix4, parent: Group, depth: int = 0, fade: float = 0.0) -> int!:
    if depth > 48:
        error(ui.invalid, "Scene hierarchy exceeds the display depth budget")
    var analytic = 0
    let meshes: list[geocore.Mesh] = []
    if data.has_cad():
        let preview = data.preview()
        analytic = preview.failed_faces
        for mesh in preview.meshes: meshes.append(mesh)
    if let mesh = data.mesh: meshes.append(mesh)
    if let preview = data.field_preview(): meshes.append(preview)
    for mesh in meshes:
        if mesh.face_count() > 0:
            parent.add(display_mesh(mesh.placed(matrix), fade))
    for index in 0..<data.instance_count():
        if data.instance_visible(index):
            analytic += append_placed(data.instance(index), matrix.multiply(data.instance_matrix(index)), parent, depth + 1, fade)
    return analytic

## Shaded scene object for one polygon mesh, drawn from its GPU arrays; vertex
## colors show at full white.
## `fade` (0..1) draws it toward a see-through grey: the rest of the scene
## around a context (context_display.luc).
pub func display_mesh(mesh: geocore.Mesh, fade: float = 0.0) -> Mesh!:
    let colored = mesh.find_attribute("Cd", 2) >= 0 or mesh.find_attribute("Cd", 1) >= 0 or mesh.find_attribute("Cd", 0) >= 0 or mesh.find_attribute("Cd", 3) >= 0
    let material = MeshLambertMaterial(theme.grey(255.0 if colored else 180.0))
    if fade > 0.0:
        material.set_fade(fade)
    return Mesh.of(mesh, material)

## Overlays in world space, gathered in Base (OverlayLines): CAD
## boundaries into `wires`, CAD normal guides and (with `polygons`) a
## polygon normal guide per face into `normals`. Polygon wires come from the
## renderer, which draws them from each mesh's GPU arrays.
pub func append_overlays(data: GeometryData, matrix: Matrix4, wires: OverlayLines, normals: OverlayLines, depth: int = 0, polygons: bool = true) -> unit!:
    if depth > 48:
        error(ui.invalid, "Scene hierarchy exceeds overlay depth budget")
    if data.has_cad():
        let preview = data.preview()
        preview.lines(0, matrix, wires)
        preview.lines(2, matrix, normals)
    if let mesh = data.mesh if polygons else none:
        normals.add_face_guides(mesh, matrix)
    for index in 0..<data.instance_count():
        if data.instance_visible(index):
            append_overlays(data.instance(index), matrix.multiply(data.instance_matrix(index)), wires, normals, depth + 1, polygons)

## The fog volumes of `data` and its visible instances, placed by `matrix`,
## each with its grid's look: a volume `held` under its content key is reused
## (a new look keeps its atlas); every volume goes into `kept` by its key.
pub func append_fog(data: GeometryData, matrix: Matrix4, volumes: list[FogVolume], held: map[int, FogVolume], kept: map[int, FogVolume], depth: int = 0) -> unit!:
    if depth > 48:
        error(ui.invalid, "Scene hierarchy exceeds the display depth budget")
    let count = FogVolume.count(data.geometry)
    for index in 0..<count:
        let key = FogVolume.key(data.geometry, index, matrix, key_light())
        let volume = held[key] else FogVolume.of(data.geometry, index, matrix, key_light())
        volume.restyle(data.geometry, index)
        kept[key] = volume
        volumes.append(volume)
    for index in 0..<data.instance_count():
        if data.instance_visible(index):
            append_fog(data.instance(index), matrix.multiply(data.instance_matrix(index)), volumes, held, kept, depth + 1)

## The point clouds of `data` and its visible instances, placed by `matrix`,
## as Gaussian splats (or dots): a cloud `held` under its content key is
## reused, uploading nothing; every cloud goes into `kept` by its key.
pub func append_clouds(data: GeometryData, matrix: Matrix4, clouds: list[GaussianSplats], held: map[int, GaussianSplats], kept: map[int, GaussianSplats], depth: int = 0) -> unit!:
    if depth > 48:
        error(ui.invalid, "Scene hierarchy exceeds the display depth budget")
    if GaussianSplats.count(data.geometry) > 0:
        let key = GaussianSplats.key(data.geometry, matrix)
        let cloud = held[key] else GaussianSplats.of(data.geometry, matrix)
        kept[key] = cloud
        clouds.append(cloud)
    for index in 0..<data.instance_count():
        if data.instance_visible(index):
            append_clouds(data.instance(index), matrix.multiply(data.instance_matrix(index)), clouds, held, kept, depth + 1)

## Analytic patch borders stay visible independently of polygon wire overlays.
pub func append_patch_boundaries(data: GeometryData, matrix: Matrix4, lines: OverlayLines) -> unit!:
    if data.has_cad():
        data.preview().lines(0, matrix, lines)
    for index in 0..<data.instance_count():
        if data.instance_visible(index):
            append_patch_boundaries(data.instance(index), matrix.multiply(data.instance_matrix(index)), lines)

## CAD control nets (the hull), as world-space point pairs: those of
## imported and freeform faces; with `modeled`, those of faces a modeling
## feature made too (Edit CAD's CVs level, where they are edited; Fusion
## shows a modeled body no control frame), and of CAD curves.
pub func append_cad_hulls(data: GeometryData, matrix: Matrix4, lines: OverlayLines, modeled: bool = false, depth: int = 0) -> unit!:
    if depth > 48:
        error(ui.invalid, "Scene hierarchy exceeds overlay depth budget")
    if data.has_cad():
        data.preview().lines(1, matrix, lines)
        if modeled:
            data.preview().lines(3, matrix, lines)
    # CAD curves' control polygons with the nets they are edited beside.
    if modeled and data.has_curves():
        lines.add_cad_curve_hulls(data.geometry, matrix)
    for index in 0..<data.instance_count():
        if data.instance_visible(index):
            append_cad_hulls(data.instance(index), matrix.multiply(data.instance_matrix(index)), lines, modeled, depth + 1)

## Transform bounding-box corners, not geometry, when framing an assembly.
pub func append_bounds(data: GeometryData, matrix: Matrix4, points: list[Vector3], depth: int = 0) -> unit!:
    if depth > 48:
        error(ui.invalid, "Scene hierarchy exceeds the display depth budget")
    if data.has_cad():
        let preview = data.preview()
        for mesh in preview.meshes:
            append_bounds(GeometryData(mesh), matrix, points, depth)
        # Failed preview patches still have visible analytic boundary curves.
        let boundaries = OverlayLines.make()
        preview.lines(0, matrix, boundaries)
        if boundaries.count() > 0:
            let box = boundaries.bounds()
            points.append(box.minimum)
            points.append(box.maximum)
    for shown in [data.mesh, data.field_preview()]:
        let mesh = shown else continue
        if mesh.point_count() > 0:
            let box = mesh.bounds()
            for x in [box.minimum.x, box.maximum.x]:
                for y in [box.minimum.y, box.maximum.y]:
                    for z in [box.minimum.z, box.maximum.z]:
                        points.append(matrix.transform_point(Vector3(x, y, z)))
    for index in 0..<data.instance_count():
        if data.instance_visible(index):
            append_bounds(data.instance(index), matrix.multiply(data.instance_matrix(index)), points, depth + 1)