Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced-3d

Source of release 0.11.9

0.11.9 / src / viewport_objects.luc

## Objects in the viewport: which displayed node's mesh is under a point
## (rays to each mesh's shared query index, in Base), or which ones a box
## touches: any face of a mesh meeting it, hidden or not (luce-3d's
## BoxSelect.touches), a cloud's, volume's or camera's and light's glyph's
## bounds overlapping it on screen.
from luce_3d.graphics import Vector3, BoxSelect
from luce_geocore.geocore import Bounds
from luce_gpu.gpu import Rect
from workspace import Workspace
from context_view import is_outside
from luce_std import math
import view_projection

## The node whose mesh the ray through (x, y) meets first, or -1.
pub func object_at(w: Workspace, x: float, y: float, width: float, height: float) -> int!:
    let (nodes, meshes) = w.on_screen()
    let origin = view_projection.origin(w, x, y, width, height)
    let direction = view_projection.ray(w, x, y, width, height).normalized()
    var nearest = -1.0
    var found = -1
    for at in 0..<meshes.length:
        let distance = meshes[at].ray_distance(origin, direction)
        if distance >= 0.0 and (nearest < 0.0 or distance < nearest):
            nearest = distance
            found = nodes[at]
    for at in 0..<w.batches.scenes.length:
        let geometry = w.batches.scenes[at]
        let node = w.batches.scene_nodes[at]
        # The rest of the scene around a context is drawn, not picked.
        if is_outside(w.network, node): continue
        let scene = geometry.scene()
        let helpers = w.batches.helpers(at)
        if w.display.hulls_visible:
            for index in 0..<helpers.length:
                let distance = helpers[index].pick(w.view.camera, x, y, width, height)
                if distance >= 0.0 and (nearest < 0.0 or distance < nearest):
                    nearest = distance
                    found = helper_node(w, scene.path(index), "Camera" if scene.is_camera(index) else "Light", node)
        # Volumes and clouds are objects without polygon surfaces.
        if not (node in nodes) and helpers.length == 0 and geometry.has_bounds():
            let distance = box_distance(geometry.bounds(), origin, direction)
            if distance >= 0.0 and (nearest < 0.0 or distance < nearest):
                nearest = distance
                found = node
    return found

## Resolve a helper to its authoring node (or enclosing context). A Render
## output may carry a Camera, but clicking that helper selects the Camera.
pub func helper_node(w: Workspace, path: str, kind: str, fallback: int) -> int!:
    var ancestor = w.network.find(authored_node(w, path, kind, fallback)) else return fallback
    while ancestor.parent != w.network.scope:
        ancestor = w.network.find(ancestor.parent) else return fallback
    return ancestor.id

## The `kind` node (Camera or Light) that made the scene object at `path`
## of output node `output`'s scene, or -1.
pub func authored_node(w: Workspace, path: str, kind: str, output: int) -> int!:
    for node in w.network.nodes:
        if node.kind != kind: continue
        let authored = node.texts[0].trim() if node.texts.length > 0 else ""
        let name = authored if authored != "" else kind.lower() + str(node.id)
        if name == path and w.network.depends_on(output, node.id):
            return node.id
    return -1

func box_distance(box: Bounds, origin: Vector3, direction: Vector3) -> float:
    var near = 0.0
    var far = 1.0e30
    for (low, high, start, along) in [(box.minimum.x, box.maximum.x, origin.x, direction.x), (box.minimum.y, box.maximum.y, origin.y, direction.y), (box.minimum.z, box.maximum.z, origin.z, direction.z)]:
        if math.abs(along) < 1e-12:
            if start < low or start > high: return -1.0
            continue
        let a = (low - start) / along
        let b = (high - start) / along
        near = math.max(near, math.min(a, b))
        far = math.min(far, math.max(a, b))
        if far < near: return -1.0
    return near

## The nodes whose objects the box from (x0, y0) to (x1, y1) touches.
pub func objects_in(w: Workspace, x0: float, y0: float, x1: float, y1: float, width: float, height: float) -> list[int]!:
    let found: list[int] = []
    let (nodes, meshes) = w.on_screen()
    for at in 0..<meshes.length:
        if not (nodes[at] in found) and BoxSelect.touches(meshes[at], w.view.camera, x0, y0, x1, y1, width, height):
            found.append(nodes[at])
    let box = Rect(math.min(x0, x1), math.min(y0, y1), math.abs(x1 - x0), math.abs(y1 - y0))
    for at in 0..<w.batches.scenes.length:
        let geometry = w.batches.scenes[at]
        let node = w.batches.scene_nodes[at]
        if is_outside(w.network, node): continue
        let scene = geometry.scene()
        let helpers = w.batches.helpers(at)
        if w.display.hulls_visible:
            for index in 0..<helpers.length:
                let id = helper_node(w, scene.path(index), "Camera" if scene.is_camera(index) else "Light", node)
                if not (id in found) and overlaps(screen_rect(w, [helpers[index].bounds()], width, height), box):
                    found.append(id)
        if not (node in nodes) and not (node in found) and helpers.length == 0 and geometry.has_bounds() and overlaps(screen_rect(w, [geometry.bounds()], width, height), box):
            found.append(node)
    return found

## Whether screen rectangles `a` (none: not on screen) and `b` overlap.
func overlaps(a: Rect?, b: Rect) -> bool:
    let r = a else return false
    return r.x <= b.x + b.width and r.x + r.width >= b.x and r.y <= b.y + b.height and r.y + r.height >= b.y

## Where node `id`'s meshes sit on screen (their bounds' corners in front of
## the camera), or none.
pub func screen_bounds(w: Workspace, id: int, width: float, height: float) -> Rect?!:
    let (nodes, meshes) = w.on_screen()
    let boxes: list[Bounds] = []
    for at in 0..<meshes.length:
        if nodes[at] == id: boxes.append(meshes[at].bounds())
    for at in 0..<w.batches.scenes.length:
        let geometry = w.batches.scenes[at]
        let node = w.batches.scene_nodes[at]
        let scene = geometry.scene()
        let helpers = w.batches.helpers(at)
        if w.display.hulls_visible:
            for index in 0..<helpers.length:
                if helper_node(w, scene.path(index), "Camera" if scene.is_camera(index) else "Light", node) == id:
                    boxes.append(helpers[index].bounds())
        if node == id and not (id in nodes) and helpers.length == 0 and geometry.has_bounds(): boxes.append(geometry.bounds())
    return screen_rect(w, boxes, width, height)

## Where `boxes` sit on screen together (their corners in front of the
## camera), or none.
func screen_rect(w: Workspace, boxes: list[Bounds], width: float, height: float) -> Rect?!:
    var left = 1.0e30
    var top = 1.0e30
    var right = -1.0e30
    var bottom = -1.0e30
    for box in boxes:
        for corner in 0..<8:
            let point = Vector3(box.maximum.x if corner % 2 == 1 else box.minimum.x, box.maximum.y if (corner // 2) % 2 == 1 else box.minimum.y, box.maximum.z if corner >= 4 else box.minimum.z)
            let screen = view_projection.project(w, point, width, height)
            if screen[2] <= 0.0:
                continue
            left = math.min(left, screen[0])
            top = math.min(top, screen[1])
            right = math.max(right, screen[0])
            bottom = math.max(bottom, screen[1])
    if right < left:
        return none
    return Rect(left, top, right - left, bottom - top)