Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-3d

Source of release 0.9.4

0.9.4 / src / renderers / wire_batch.lucb

## Retained native line endpoints avoid rebuilding Luce lists on camera frames.
from luce_gpu import gpu
import interop
import memory
from luce_std import math
import errors
from luce_geocore.geocore import Vector3, Bounds
from cameras.perspective import PerspectiveCamera
from renderers.wire import WireRenderer
from renderers.projection import projection_matrix
from renderers.retained import RetainedBatch

pub struct WireBatch:
    let points: Vector3[]
    var retained: RetainedBatch

    pub func init(points: const Vector3[]) -> !:
        self.points = try new Vector3[points.length] --- in memory.heap
        self.retained = RetainedBatch()
        for index in 0..<points.length:
            self.points[index] = points[index]

    pub func count() -> i64:
        return (i64)self.points.length // 2

    ## The box around every endpoint (framing); fails when there are none.
    pub func bounds() -> Bounds!:
        if self.points.length == 0: error(errors.invalid, "an empty wire batch has no bounds")
        var low = self.points[0]
        var high = low
        for p in self.points:
            low = Vector3(math.min(low.x, p.x), math.min(low.y, p.y), math.min(low.z, p.z))
            high = Vector3(math.max(high.x, p.x), math.max(high.y, p.y), math.max(high.z, p.z))
        return Bounds(low, high)

    ## Draw the lines, depth tested. With `writes_depth` off they leave the depth
    ## buffer alone (the ground grid: splats and fog drawn later cover it) and
    ## are built each frame instead of retained.
    pub func draw(camera: interop.Reference[PerspectiveCamera], target: interop.View[gpu.RenderTarget], color: gpu.Color, width: f64 = 1.0, writes_depth: bool = true) -> !:
        if not writes_depth:
            try WireRenderer.lines(self.points, camera, target, color, width, writes_depth = false)
            return
        let native = try target.get()
        var device = gpu.device_of(native) catch failure:
            if failure.code != gpu.unavailable: error(failure.code, failure.message)
            recover gpu.Device()
        defer device.destroy()
        if device.is_open() and self.points.length > 0:
            if not self.retained.is_open():
                let vertices = try new gpu.Vertex[self.points.length // 2] --- in memory.heap
                defer free(vertices) in memory.heap
                for at in 0..<vertices.length:
                    let a = self.points[at * 2]
                    let b = self.points[at * 2 + 1]
                    vertices[at] = gpu.Vertex(x = (f32)a.x, y = (f32)a.y, z = (f32)a.z, red = (f32)b.x, green = (f32)b.y, blue = (f32)b.z)
                try self.retained.upload(device, vertices, true)
            let size = try native.size()
            let matrix = projection_matrix(try camera.value().projection(size.width / size.height))
            try self.retained.draw(native, matrix, color, width)
            return
        try WireRenderer.lines(self.points, camera, target, color, width)

    pub func close():
        self.retained.close()
        free(self.points) in memory.heap

pub let wire_batch_type: interop.Type[WireBatch] = interop.Type[WireBatch]("WireBatch", WireBatch.close)