Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.3

0.11.3 / src / local_cook.luc

## Cooking the displayed outputs on the UI thread, as tests and scripts do
## before a worker runs (the worker's path is compute_worker.luc): each
## visible output's scene objects go into `objects` and its lines into the
## viewport batches; the displayed node's result comes back for picking.
from luce_3d.graphics import Group, Matrix4, CurveLines
from luce_geocore.geocore import Mesh
from network import Network, Node
from geometry_data import GeometryData
from overlay_lines import OverlayLines
from scene_projection import append_scene, append_overlays, append_patch_boundaries, append_cad_hulls, append_fog, append_clouds
from viewport_batches import ViewportBatches

pub class LocalCook:
    ## The node picked on (Network.pick_node) and its result, when it cooked.
    pub var node: Node? = none
    pub var data: GeometryData? = none
    ## The first failure, or how many CAD patches could not be meshed.
    pub var notice: str = ""
    ## The meshes on screen and the node each came from (object picking).
    pub var nodes: list[int]
    pub var meshes: list[Mesh]

    pub func init(self):
        self.nodes = []
        self.meshes = []

## Cook `graph`'s visible outputs (the displayed node alone when previewing)
## into `objects` and `batches`, meshes prepared for display `mode`.
pub func cook_outputs(graph: Network, objects: Group, batches: ViewportBatches, mode: int) -> LocalCook!:
    let result = LocalCook()
    objects.clear()
    batches.clear_curves()
    batches.volumes.clear()
    let held = batches.held_volumes
    batches.held_volumes = {}
    batches.clouds.clear()
    batches.cloud_nodes.clear()
    let held_clouds = batches.held_clouds
    batches.held_clouds = {}
    var outputs = graph.outputs(graph.scope)
    if graph.preview:
        if let node = graph.find(graph.display):
            outputs = [node]
    var analytic = 0
    let wires = OverlayLines.make()
    let normals = OverlayLines.make()
    let patches = OverlayLines.make()
    let hulls = OverlayLines.make()
    for node in outputs:
        if not node.visible:
            continue
        let data = graph.evaluate_data(node.id) catch failure:
            result.notice = failure.message
            continue
        if node.id == graph.pick_node():
            result.node = node
            result.data = data
        let branch = Group()
        analytic += append_scene(data, branch, mode = mode) catch failure:
            result.notice = failure.message
            continue
        objects.add(branch)
        if data.instance_count() > 0 or data.mesh != none:
            # CAD without a tessellation is not pickable.
            var mesh: Mesh? = none
            mesh = data.require_mesh() catch failure:
                recover none
            if let shown = mesh:
                result.nodes.append(node.id)
                result.meshes.append(shown)
        # Overlays are display only: one that cannot be drawn says so and
        # leaves the cook (and the edit that made it) standing.
        append_overlays(data, Matrix4(), wires, normals) catch failure:
            result.notice = f"{node.name}: guides not drawn: {failure.message}"
        append_cad_hulls(data, Matrix4(), hulls) catch failure:
            result.notice = f"{node.name}: hulls not drawn: {failure.message}"
        if data.geometry.has_scene():
            hulls.add_scene(data.geometry)
        if CurveLines.count(data.geometry) > 0:
            batches.curve_batches.append(CurveLines.batch(data.geometry))
            batches.hull_batches.append(CurveLines.batch(data.geometry, true))
        append_patch_boundaries(data, Matrix4(), patches)
        append_fog(data, Matrix4(), batches.volumes, held, batches.held_volumes) catch failure:
            result.notice = f"{node.name}: fog not drawn: {failure.message}"
        append_clouds(data, Matrix4(), batches.clouds, held_clouds, batches.held_clouds) catch failure:
            result.notice = f"{node.name}: points not drawn: {failure.message}"
        while batches.cloud_nodes.length < batches.clouds.length:
            batches.cloud_nodes.append(node.id)
    # A tool prompt picks on its input, which is not shown.
    if result.node == none:
        if let node = graph.find(graph.picking):
            result.data = graph.evaluate_data(node.id) catch failure:
                result.notice = failure.message
                recover none
            if result.data != none:
                result.node = node
    if analytic > 0 and result.notice == "":
        result.notice = f"CAD preview: {analytic} patches could not be meshed; boundaries remain visible"
    batches.publish(wires, normals, patches, hulls)
    return result