Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-3d

Source of release 0.9.1

0.9.1 / src / renderers / edit_markers.lucb

## Depth-tested point markers for Edit mode, thinned to one per marker-sized
## screen cell. Visibility comes from the depth buffer, not a ray per point.
from luce_gpu import gpu
from luce_std import math
from cameras.perspective import Projection
from luce_geocore.core.growing import Growing
from renderers.edit_screen import Screen

let cell: f64 = 6.0

## Depth-tested square markers, at most one marker per class per screen
## cell (the nearest point wins): selected points in the accent, points a
## soft selection reaches in the falloff ramp, then the rest (left out when
## `ordinary_too` is false). Rebuilt when the camera, the selection or the
## weights change; the hovered marker is drawn on its own.
pub struct Markers:
    var vertices: Growing[gpu.Vertex]
    var nearest: Growing[i64]
    var version: u64 = 0
    var valid: bool = false

    pub func init():
        self.vertices = Growing[gpu.Vertex]()
        self.nearest = Growing[i64]()
        self.version = 0
        self.valid = false

    pub func invalidate():
        self.valid = false

    ## `selected` and `weights` are per point or empty (none selected, soft
    ## selection off).
    pub func draw(target: const gpu.RenderTarget*, screen: const Screen*, selected: const bool[], weights: const f32[], ordinary_too: bool,
                  hovered: i64, p: Projection, width: f64, height: f64, ordinary: gpu.Color, accent: gpu.Color) -> !:
        if not self.valid or self.version != screen.version:
            self.vertices.clear()
            # Selected markers first: at equal depth the first one drawn wins.
            for round in 0..<(usize)3:
                if round == 2 and not ordinary_too: break
                if round == 1 and weights.length == 0: continue
                try self.nearest_per_cell(screen, selected, weights, round, width, height)
                for id in self.nearest.view():
                    if id < 0: continue
                    var color = accent
                    if round == 1: color = ramp((f64)weights[(usize)id], accent)
                    elif round == 2: color = ordinary
                    try self.quad(screen, (usize)id, 3.0, p, width, height, color)
            self.version = screen.version
            self.valid = true
        let count = self.vertices.count
        if hovered >= 0 and (usize)hovered < screen.depths.count and screen.depths.items[(usize)hovered] > 0.0:
            try self.quad(screen, (usize)hovered, 4.5, p, width, height, accent)
        if self.vertices.count > 0:
            try target.allow_vertices(8388608)
            try target.triangles(self.vertices.view(), true)
        self.vertices.count = count

    ## The nearest point of marker class `round` (0 selected, 1 soft, 2 the
    ## rest) in each screen cell.
    func nearest_per_cell(screen: const Screen*, selected: const bool[], weights: const f32[], round: usize, width: f64, height: f64) -> !:
        let columns = (usize)math.ceil(width / cell)
        let rows = (usize)math.ceil(height / cell)
        try self.nearest.fill(columns * rows, -1)
        for id in 0..<screen.depths.count:
            let depth = screen.depths.items[id]
            if depth <= 0.0 or class_of(selected, weights, id) != round: continue
            let x = (f64)screen.xs.items[id]
            let y = (f64)screen.ys.items[id]
            if x < 0.0 or y < 0.0 or x >= width or y >= height: continue
            let slot = (usize)(y / cell) * columns + (usize)(x / cell)
            let other = self.nearest.items[slot]
            if other < 0 or screen.depths.items[(usize)other] > depth:
                self.nearest.items[slot] = (i64)id

    func quad(screen: const Screen*, id: usize, radius: f64, p: Projection, width: f64, height: f64, color: gpu.Color) -> !:
        let w = (f64)screen.depths.items[id]
        let x = ((f64)screen.xs.items[id] / (width * 0.5) - 1.0) * w
        let y = (1.0 - (f64)screen.ys.items[id] / (height * 0.5)) * w
        # Pull the marker toward the eye by twice its own world size so the
        # surface it sits on does not clip it.
        let z = p.far * (w - p.near) / (p.far - p.near) - 4.0 * radius * p.far * p.near / (p.focal * height * (p.far - p.near))
        let rx = radius * 2.0 / width * w
        let ry = radius * 2.0 / height * w
        let corners: f64[12] = [-rx, -ry, rx, -ry, rx, ry, -rx, -ry, rx, ry, -rx, ry]
        for at in 0..<(usize)6:
            try self.vertices.push(gpu.Vertex(x = (f32)(x + corners[at * 2]), y = (f32)(y + corners[at * 2 + 1]), z = (f32)z, w = (f32)w,
                                              red = (f32)color.red, green = (f32)color.green, blue = (f32)color.blue))

    pub func close():
        self.vertices.close()
        self.nearest.close()

## A point's marker class: 0 selected (or weighing 1), 1 reached by the soft
## selection, 2 the rest.
func class_of(selected: const bool[], weights: const f32[], id: usize) -> usize:
    if id < selected.length and selected[id]: return 0
    if id >= weights.length or weights[id] <= 0.0: return 2
    return 0 if weights[id] >= 1.0 else 1

## The soft-selection falloff ramp: cool blue far out, through teal and
## yellow, to the accent at full weight.
pub func ramp(weight: f64, accent: gpu.Color) -> gpu.Color:
    let stops: f64[9] = [0.25, 0.45, 0.95, 0.3, 0.8, 0.75, 1.0, 0.88, 0.25]
    let t = math.max(0.0, math.min(1.0, weight)) * 3.0
    let band = (usize)math.min(2.0, math.floor(t))
    let f = t - (f64)band
    let top = band + 1
    let end_red = stops[top * 3] if top < 3 else accent.red
    let end_green = stops[top * 3 + 1] if top < 3 else accent.green
    let end_blue = stops[top * 3 + 2] if top < 3 else accent.blue
    return gpu.Color(stops[band * 3] + (end_red - stops[band * 3]) * f, stops[band * 3 + 1] + (end_green - stops[band * 3 + 1]) * f,
                     stops[band * 3 + 2] + (end_blue - stops[band * 3 + 2]) * f, 1.0)