dymokomi/luce-3d
Source of release 0.9.6
0.9.6 / 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.
## The squares are laid out once per projection (one pass over the points)
## and kept on the GPU; a frame draws them from there, and the hovered marker
## on its own.
from luce_gpu import gpu
import memory
from luce_std import math
from cameras.camera import Projection
from luce_geocore.core.growing import Growing
from renderers.edit_screen import Screen
from renderers.overlay_layers import Storage
from renderers.mesh_batch import draw_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). Laid out again when the projection, the
## selection or the weights change.
pub struct Markers:
## Each cell's nearest point per class (selected, soft, the rest), -1
## for none.
var nearest: Growing[i64]
## The same over the points a drag leaves in place: every point but
## `still_first..<still_end` (the moved range), as of the screen's whole
## projection `still_version`; a drag's frame merges its moved points
## into a copy instead of passing over every point.
var still: Growing[i64]
var still_version: u64 = 0
var still_first: usize = 0
var still_end: usize = 0
var still_valid: bool = false
## The squares: six vertices each, in normalized device coordinates.
var vertices: Growing[gpu.Vertex]
var version: u64 = 0
var valid: bool = false
var stale: bool = true
var shape: Storage
var tints: Storage
var order: Storage
var ordered: usize
var hover_shape: Storage
var hover_tints: Storage
pub func init():
self.nearest = Growing[i64]()
self.still = Growing[i64]()
self.still_version = 0
self.still_first = 0
self.still_end = 0
self.still_valid = false
self.vertices = Growing[gpu.Vertex]()
self.version = 0
self.valid = false
self.stale = true
self.shape = Storage()
self.tints = Storage()
self.order = Storage()
self.ordered = 0
self.hover_shape = Storage()
self.hover_tints = Storage()
pub func invalidate():
self.valid = false
self.still_valid = false
## Forget the GPU copies (another device).
pub func drop():
self.shape.close()
self.tints.close()
self.order.close()
self.ordered = 0
self.hover_shape.close()
self.hover_tints.close()
self.stale = true
## `selected` and `weights` are per point or empty (none selected, soft
## selection off). `device` is the target's (closed for a frame without
## one, whose markers go through the frame's vertices).
pub func draw(target: const gpu.RenderTarget*, device: gpu.Device, 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:
try self.lay_out(screen, selected, weights, ordinary_too, p, width, height, ordinary, accent)
self.version = screen.version
self.valid = true
self.stale = true
let shown = hovered >= 0 and (usize)hovered < screen.depths.count and screen.depths.items[(usize)hovered] > 0.0
if not device.is_open():
let count = self.vertices.count
if shown: 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
return
if self.stale:
try self.upload(device, self.vertices.view(), &self.shape, &self.tints)
self.stale = false
try self.ensure_order(device, self.vertices.count)
try draw_screen(target, self.shape.buffer, self.tints.buffer, self.order.buffer, self.vertices.count)
if shown:
let count = self.vertices.count
try self.quad(screen, (usize)hovered, 4.5, p, width, height, accent)
try self.upload(device, self.vertices.view()[count..], &self.hover_shape, &self.hover_tints)
self.vertices.count = count
try draw_screen(target, self.hover_shape.buffer, self.hover_tints.buffer, self.order.buffer, 6)
## The squares of every class: the nearest points per cell of the
## points that stayed put (one pass over them per whole projection),
## with the moved ones merged in each frame.
func lay_out(screen: const Screen*, selected: const bool[], weights: const f32[], ordinary_too: bool, p: Projection, width: f64, height: f64,
ordinary: gpu.Color, accent: gpu.Color) -> !:
self.vertices.clear()
let columns = (usize)math.ceil(width / cell)
let rows = (usize)math.ceil(height / cell)
let cells = columns * rows * 3
let within = screen.moved_first == screen.moved_end or (screen.moved_first >= self.still_first and screen.moved_end <= self.still_end)
let fits = self.still_valid and self.still_version == screen.whole_version and self.still.count == cells and within
if not fits:
self.still_first = screen.moved_first
self.still_end = screen.moved_end
try self.still.fill(cells, -1)
self.nearest_of(screen, selected, weights, ordinary_too, 0, self.still_first, columns, width, height, &self.still)
self.nearest_of(screen, selected, weights, ordinary_too, self.still_end, screen.depths.count, columns, width, height, &self.still)
self.still_version = screen.whole_version
self.still_valid = true
try self.nearest.assign(self.still.view())
self.nearest_of(screen, selected, weights, ordinary_too, self.still_first, self.still_end, columns, width, height, &self.nearest)
# Selected markers first: at equal depth the first one drawn wins.
for round in 0..<(usize)3:
for at in 0..<columns * rows:
let id = self.nearest.items[at * 3 + round]
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)
## Points `first..<end` into `cells`, the nearest of each class per cell.
func nearest_of(screen: const Screen*, selected: const bool[], weights: const f32[], ordinary_too: bool, first: usize, end: usize, columns: usize,
width: f64, height: f64, cells: Growing[i64]*):
for id in first..<end:
let depth = screen.depths.items[id]
if depth <= 0.0: continue
let round = class_of(selected, weights, id)
if round == 2 and not ordinary_too: 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)) * 3 + round
let other = cells.items[slot]
if other < 0 or screen.depths.items[(usize)other] > depth:
cells.items[slot] = (i64)id
## Pack `vertices`' positions and colors into `shape` and `tints`.
func upload(device: gpu.Device, vertices: const gpu.Vertex[], shape: Storage*, tints: Storage*) -> !:
if vertices.length == 0: return
let packed = try new f32[vertices.length * 6] --- in memory.heap
defer free(packed) in memory.heap
for at in 0..<vertices.length:
let vertex = vertices[at]
packed[at * 3] = vertex.x
packed[at * 3 + 1] = vertex.y
packed[at * 3 + 2] = vertex.z
packed[(vertices.length + at) * 3] = vertex.red
packed[(vertices.length + at) * 3 + 1] = vertex.green
packed[(vertices.length + at) * 3 + 2] = vertex.blue
let bytes = u8[]((u8*)packed.data, packed.length * 4)
_ = try shape.put(device, bytes[..<vertices.length * 12])
_ = try tints.put(device, bytes[vertices.length * 12..])
## 0, 1, 2, ... for at least `count` vertices.
func ensure_order(device: gpu.Device, count: usize) -> !:
if count <= self.ordered and self.order.buffer.is_open(): return
var wanted: usize = 1024
while wanted < count: wanted *= 2
let values = try new u32[wanted] --- in memory.heap
defer free(values) in memory.heap
for at in 0..<wanted: values[at] = (u32)at
_ = try self.order.put(device, u8[]((u8*)values.data, wanted * 4))
self.ordered = wanted
## A square around point `id`, `radius` pixels, in normalized device
## coordinates.
func quad(screen: const Screen*, id: usize, radius: f64, p: Projection, width: f64, height: f64, color: gpu.Color) -> !:
let depth = (f64)screen.depths.items[id]
# Clip space: w is the depth in perspective, 1 in orthographic.
let w = 1.0 if p.orthographic() else depth
let x = (f64)screen.xs.items[id] / (width * 0.5) - 1.0
let y = 1.0 - (f64)screen.ys.items[id] / (height * 0.5)
# Pull the marker toward the eye by twice its own world size so the
# surface it sits on does not clip it.
let pull = 2.0 * radius * p.pixel(depth, height)
let z = (depth - pull - p.near) / (p.far - p.near) if p.orthographic() else (p.far * (w - p.near) / (p.far - p.near) - 4.0 * radius * p.far * p.near / (p.focal * height * (p.far - p.near))) / w
let rx = radius * 2.0 / width
let ry = radius * 2.0 / height
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 = 1.0,
red = (f32)color.red, green = (f32)color.green, blue = (f32)color.blue))
pub func close():
self.vertices.close()
self.nearest.close()
self.still.close()
self.drop()
## 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)