Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.6

0.11.6 / src / display_publisher.luc

## Adopts worker results by content key. A key the UI already holds keeps its
## mesh, scene object and renderer cache, so re-publishing an unchanged stamp
## (a rename, a selection, a visibility toggle elsewhere) rebuilds nothing. A
## fog volume's key is its grids' content: a new look (Volume Visualization)
## only sets the held volume's look, uploading nothing. A point cloud's key is
## its columns' (GaussianSplats.key): held, it uploads once. Each displayed
## output's whole set (meshes, clouds, cameras, lights, materials) comes too,
## by the output's stamp, for the viewport's render mode (render_mode.luc).
## A few keys that just left the scene stay held, so toggling a node back on
## or undoing a display change needs no new copy either.
## Until the first result arrives the scene objects and line batches are the
## local cook's (made before the worker started); the first result replaces
## them whatever it holds, even nothing but fog.
## Meshes of the rest of the scene around the context (context_view.luc)
## draw after the context's, faded or not at all as the context display mode
## says; changing the mode restyles the held meshes without a new result.
from luce_geocore.geocore import Mesh, GeometrySet
from luce_3d import graphics as three
from luce_3d.graphics import Group, Vector3, FogVolume, GaussianSplats
from compute_bridge import ComputeService
from network import Network
from geometry_data import GeometryData
from scene_projection import display_mesh
from context_view import is_outside
import context_display

pub class DisplayPublisher:
    let objects: Group
    let held: map[int, Mesh]
    ## Fog volumes by key, kept like meshes so each uploads once.
    let held_volumes: map[int, FogVolume]
    ## The fog volumes of the displayed result.
    pub var volumes: list[FogVolume]
    pub var volume_nodes: list[int]
    ## Point clouds (Gaussian splats or dots) by key, kept like meshes so each
    ## uploads once, and those of the displayed result.
    let held_clouds: map[int, GaussianSplats]
    pub var clouds: list[GaussianSplats]
    ## The node each of `clouds` came from.
    pub var cloud_nodes: list[int]
    ## The displayed outputs' sets by key, and those of the displayed result
    ## with their keys.
    let held_scenes: map[int, GeometrySet]
    pub var scenes: list[GeometrySet]
    pub var scene_keys: list[int]
    pub var scene_nodes: list[int]
    ## Scene objects by (key, faded), and the displayed keys with their nodes
    ## and the mode and scope they were styled for.
    let shown: map[int, three.Mesh]
    var shown_keys: list[int]
    var shown_nodes: list[int]
    var shown_style: (int, int) = (-1, -2)
    var product_keys: list[int]
    ## Every key the worker may re-publish without copying: this result's
    ## products, then recently dropped keys, newest first.
    pub var kept: list[int]
    pub var geometry: Mesh? = none
    pub let bounds: list[Vector3]
    ## The meshes on screen and the node each came from (object picking).
    pub var nodes: list[int]
    pub var meshes: list[Mesh]
    ## Meshes adopted as new copies over the session (tests and benchmarks).
    pub var adopted: int = 0
    ## Whether a result has been shown: before it, `objects` holds the local
    ## cook's meshes, which no key of ours names.
    var showing: bool = false

    pub func init(self, objects: Group):
        self.objects = objects
        self.held = {}
        self.held_volumes = {}
        self.volumes = []
        self.volume_nodes = []
        self.held_clouds = {}
        self.clouds = []
        self.cloud_nodes = []
        self.held_scenes = {}
        self.scenes = []
        self.scene_keys = []
        self.scene_nodes = []
        self.shown = {}
        self.shown_keys = []
        self.shown_nodes = []
        self.product_keys = []
        self.kept = []
        self.bounds = []
        self.nodes = []
        self.meshes = []

    ## Adopt the completed result. Returns whether its overlay batches differ
    ## from the previous result's, i.e. whether the caller must fetch them.
    pub func adopt(self, service: ComputeService, network: Network, mode: int) -> bool!:
        let displayed: list[int] = []
        let displayed_nodes: list[int] = []
        let products: list[int] = []
        self.nodes = []
        self.meshes = []
        self.volumes = []
        self.volume_nodes = []
        self.clouds = []
        self.cloud_nodes = []
        self.scenes = []
        self.scene_keys = []
        self.scene_nodes = []
        var fresh = false
        self.geometry = none
        for index in 0..<service.mesh_count():
            let key = service.mesh_key(index)
            let node = service.mesh_node(index)
            products.append(key)
            if service.volume_fresh(index):
                self.held_volumes[key] = service.take_volume(index)
                fresh = true
            if service.volume_product(index):
                if let volume = self.held_volumes[key]:
                    service.style_volume(index, volume)
                    self.volumes.append(volume)
                    self.volume_nodes.append(node)
                continue
            if service.scene_fresh(index):
                self.held_scenes[key] = service.take_scene(index)
            if service.scene_product(index):
                if let whole = self.held_scenes[key]:
                    self.scenes.append(whole)
                    self.scene_keys.append(key)
                    self.scene_nodes.append(node)
                continue
            if service.cloud_fresh(index):
                self.held_clouds[key] = service.take_cloud(index)
                fresh = true
            if service.cloud_product(index):
                if let cloud = self.held_clouds[key]:
                    self.clouds.append(cloud)
                    self.cloud_nodes.append(node)
                continue
            if service.mesh_fresh(index):
                self.held[key] = service.take_mesh(index)
                self.adopted += 1
                fresh = true
            let mesh = self.held[key] else continue
            if service.mesh_display(index):
                displayed.append(key)
                displayed_nodes.append(node)
                self.nodes.append(node)
                self.meshes.append(mesh)
            if service.mesh_inspect(index):
                if let selected = network.find(node):
                    selected.cache = GeometryData(mesh)
                if node == network.pick_node():
                    self.geometry = mesh
        let first = not self.showing
        if first or displayed != self.shown_keys or self.shown_style != (mode, network.scope):
            self.show(displayed, displayed_nodes, network, mode)
        self.retain(products)
        let changed = first or fresh or products != self.product_keys
        self.product_keys = products
        return changed

    ## Style the displayed meshes for context display `mode` again (no new
    ## result): the rest of the scene shows, fades or hides.
    pub func restyle(self, network: Network, mode: int) -> unit!:
        if self.showing:
            self.show(self.shown_keys, self.shown_nodes, network, mode)

    ## The scene objects of the displayed keys, the context's first, then the
    ## rest of the scene's as `mode` says: a key still on screen keeps its
    ## object, a newly shown one gets one.
    func show(self, displayed: list[int], nodes: list[int], network: Network, mode: int) -> unit!:
        self.objects.clear()
        self.bounds.clear()
        let around: list[Vector3] = []
        let shown: map[int, three.Mesh] = {}
        for pass in [false, true]:
            if pass and mode == context_display.hidden:
                break
            let faded = pass and mode == context_display.greyed
            for at in 0..<displayed.length:
                if is_outside(network, nodes[at]) != pass:
                    continue
                let key = displayed[at]
                let mesh = self.held[key] else continue
                let styled = (key, faded).hash()
                if let existing = self.shown[styled]:
                    shown[styled] = existing
                else:
                    shown[styled] = display_mesh(mesh, context_display.fade if faded else 0.0)
                self.objects.add(shown[styled] else continue)
                let corners = around if pass else self.bounds
                corners.append(mesh.bounds().minimum)
                corners.append(mesh.bounds().maximum)
        # Framing takes the context, or the rest when there is none.
        if self.bounds.length == 0:
            for corner in around: self.bounds.append(corner)
        self.shown.clear()
        for key in shown.keys():
            self.shown[key] = shown[key] else continue
        self.shown_keys = displayed
        self.shown_nodes = nodes
        self.shown_style = (mode, network.scope)
        self.showing = true

    ## Keep this result's keys plus the six most recent dropped ones.
    func retain(self, products: list[int]) -> unit:
        let kept = products.copy()
        var extra = 0
        for key in self.kept:
            if extra == 6:
                break
            if not (key in kept):
                kept.append(key)
                extra += 1
        let meshes: map[int, Mesh] = {}
        let volumes: map[int, FogVolume] = {}
        let clouds: map[int, GaussianSplats] = {}
        let scenes: map[int, GeometrySet] = {}
        for key in kept:
            if let whole = self.held_scenes[key]:
                scenes[key] = whole
            if let mesh = self.held[key]:
                meshes[key] = mesh
            if let volume = self.held_volumes[key]:
                volumes[key] = volume
            if let cloud = self.held_clouds[key]:
                clouds[key] = cloud
        self.held.clear()
        for key in meshes.keys():
            self.held[key] = meshes[key] else continue
        self.held_volumes.clear()
        for key in volumes.keys():
            self.held_volumes[key] = volumes[key] else continue
        self.held_clouds.clear()
        for key in clouds.keys():
            self.held_clouds[key] = clouds[key] else continue
        self.held_scenes.clear()
        for key in scenes.keys():
            self.held_scenes[key] = scenes[key] else continue
        self.kept = kept