Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-3d

Source of release 0.9.3

0.9.3 / tests / splat_pixels / scenes.lucb

## Checks of clouds drawn together (SplatScene), selection marks and level
## of detail: interleaved clouds composite in depth order across clouds,
## marked splats are tinted, and a large cloud far away draws its merged
## twigs and chunks, few of them, looking like the splats they stand for.
from luce_gpu import gpu
import interop
import memory
from luce_std import math
from luce_3d.graphics import GaussianSplats, SplatScene, SplatLook, Vector3, splat_scene_type
from luce_geocore.geocore import Selection, MeshBuilder, Mesh, GeometrySet
import clouds
from clouds import Splat
import views
from views import View, Wall

let width: u32 = 160
let height: u32 = 120

func near(value: f64, wanted: f64, tolerance: f64) -> bool:
    return math.abs(value - wanted) <= tolerance

func new_scene() -> interop.Reference[SplatScene]!:
    return try interop.Reference[SplatScene].adopt(new SplatScene() in memory.heap, splat_scene_type, memory.heap)

## The opacity of a splat of σ 0.3 and opacity 0.6 at view depth `depth`,
## centered, at the middle pixel (see pixels.expected_alpha).
func middle_alpha(depth: f64) -> f64:
    let focal = 1.0 / math.tan(20.0 * math.pi / 180.0) * (f64)height * 0.5
    let spread = focal * 0.3 / depth
    let variance = spread * spread + 0.3
    let d2 = 0.5
    return math.min(0.99, 0.6 * math.exp(-0.5 * d2 / variance))

## Two clouds interleaved in depth on the view axis: the first holds red at
## the origin and green two units toward the eye, the second blue between
## them. Sorted together, green covers blue covers red. Drawn one cloud after
## the other (each sorted alone), the second cloud's blue lands over the first
## cloud's green: the order a combined sort fixes.
pub func interleaved(device: gpu.Device) -> !:
    var view = try View(device, width, height)
    defer view.close()
    let first: Splat[2] = [Splat(center = Vector3(), scale = Vector3(0.3, 0.3, 0.3), opacity = 0.6, color = Vector3(1.0, 0.0, 0.0)),
                           Splat(center = Vector3(0.0, 0.0, 2.0), scale = Vector3(0.3, 0.3, 0.3), opacity = 0.6, color = Vector3(0.0, 1.0, 0.0))]
    let second: Splat[1] = [Splat(center = Vector3(0.0, 0.0, 1.0), scale = Vector3(0.3, 0.3, 0.3), opacity = 0.6, color = Vector3(0.0, 0.0, 1.0))]
    let first_set = try clouds.splat_set(first, 0)
    defer first_set.release()
    let second_set = try clouds.splat_set(second, 0)
    defer second_set.release()
    let a = try GaussianSplats.of(first_set)
    defer a.release()
    let b = try GaussianSplats.of(second_set)
    defer b.release()
    let both: interop.Reference[GaussianSplats][2] = [a, b]
    var walls: Wall[]
    let red = middle_alpha(4.0)
    let blue = middle_alpha(3.0)
    let green = middle_alpha(2.0)
    # One after the other: blue over everything of the first cloud.
    _ = try view.draw(both, walls)
    let apart = Vector3(view.at(80, 60, 0), view.at(80, 60, 1), view.at(80, 60, 2))
    assert(near(apart.z, blue, 0.01) and near(apart.y, green * (1.0 - blue), 0.01))
    # Together: back to front across both clouds.
    let scene = try new_scene()
    defer scene.release()
    _ = try view.draw_scene(scene, both, walls)
    let together = Vector3(view.at(80, 60, 0), view.at(80, 60, 1), view.at(80, 60, 2))
    assert(near(together.y, green, 0.01) and near(together.z, blue * (1.0 - green), 0.01) and near(together.x, red * (1.0 - blue) * (1.0 - green), 0.01))
    # From behind the order turns over: red covers blue covers green.
    try view.look_from(Vector3(0.0, 0.0, -4.0))
    _ = try view.draw_scene(scene, both, walls)
    let behind_red = middle_alpha(4.0)
    let behind_blue = middle_alpha(5.0)
    let behind_green = middle_alpha(6.0)
    assert(near(view.at(80, 60, 0), behind_red, 0.01) and near(view.at(80, 60, 2), behind_blue * (1.0 - behind_red), 0.01)
           and near(view.at(80, 60, 1), behind_green * (1.0 - behind_blue) * (1.0 - behind_red), 0.01))
    # An unchanged view sorts nothing; the camera moved twice.
    _ = try view.draw_scene(scene, both, walls)
    assert(scene.value().sort_count() == 2 and a.value().upload_count() == 1 and b.value().upload_count() == 1)
    let times = try scene.value().timings()
    assert(times.visible == 3)
    print(f"interleaved clouds: apart green {apart.y:.3f} blue {apart.z:.3f}; together green {together.y:.3f} blue {together.z:.3f} red {together.x:.3f}")

## A selection of a cloud's points tints them toward the highlight; others
## keep their color; clearing it restores the color.
pub func marks(device: gpu.Device) -> !:
    var view = try View(device, width, height)
    defer view.close()
    let splats: Splat[2] = [Splat(center = Vector3(-1.0, 0.0, 0.0), scale = Vector3(0.1, 0.1, 0.1), opacity = 1.0, color = Vector3(0.0, 0.0, 1.0)),
                            Splat(center = Vector3(1.0, 0.0, 0.0), scale = Vector3(0.1, 0.1, 0.1), opacity = 1.0, color = Vector3(0.0, 0.0, 1.0))]
    let set = try clouds.splat_set(splats, 0)
    defer set.release()
    let cloud = try GaussianSplats.of(set)
    defer cloud.release()
    let one: interop.Reference[GaussianSplats][1] = [cloud]
    var walls: Wall[]
    var builder = try MeshBuilder()
    defer builder.close()
    for splat in splats: _ = try builder.point(splat.center)
    let points = try builder.finish()
    defer points.release()
    let nothing = try Selection.empty(points, 0)
    defer nothing.release()
    let chosen = try nothing.value().changed(1, 0)
    defer chosen.release()
    try cloud.value().set_marks(chosen)
    let scene = try new_scene()
    defer scene.release()
    _ = try view.draw_scene(scene, one, walls)
    # (1, 0, 0) is 41 pixels right of the middle: tinted, more red than blue.
    let x = (u32)(80.0 + 1.0 / math.tan(20.0 * math.pi / 180.0) * 60.0 / 4.0)
    let left = (u32)(160 - x)
    assert(view.at(x, 60, 0) > 0.5 and view.at(x, 60, 0) > view.at(x, 60, 2))
    assert(view.at(left, 60, 0) == 0.0 and view.at(left, 60, 2) > 0.9)
    try cloud.value().set_marks(none)
    _ = try view.draw_scene(scene, one, walls)
    assert(view.at(x, 60, 0) == 0.0 and view.at(x, 60, 2) > 0.9)

## Level of detail keeps a cloud's brightness: each merged splat draws its
## members' summed opacity composited as they overlap (1 - exp(-z g), out to
## 3σ). A dense ball of 2^20 splats (radius 1, seen from 15 units, about 22
## pixels across) at 1.5 pixels draws under a fifth of them, its image's
## summed color within 6% of the full cloud's (thousands of splats deep, the
## worst case: a merged splat's Gaussian reaches past the ball's rim). A
## ground of 2^20 flat splats (radius 22, the 20M field's density), seen from
## above at 60, 150 and 400 units with LOD at 1.5 and 4 pixels, stays within
## 6% in every channel (its brightness within 4%): where merged splats
## saturated at opacity 1, it lost up to 11%.
pub func level_of_detail(device: gpu.Device) -> !:
    var view = try View(device, width, height, true, 1000.0)
    defer view.close()
    let scene = try new_scene()
    defer scene.release()
    let ball = try clouds.random_set(1048576, 1.0, 0.002, 0.01, 0, 21)
    defer ball.release()
    try compare_lod(&view, scene, ball, "ball", [Vector3(0.0, 0.0, 15.0)], [1.5], 0.06)
    let ground = try clouds.ground_set(1048576, 22.0, 13)
    defer ground.release()
    try compare_lod(&view, scene, ground, "ground", [Vector3(0.0, 60.0, 1.0), Vector3(0.0, 150.0, 1.0), Vector3(0.0, 400.0, 1.0)], [1.5, 4.0], 0.06)

## `set` drawn from each of `eyes` whole and with LOD at each of `sizes`:
## LOD draws under a fifth of the splats, its summed color within `within`.
func compare_lod(view: View*, scene: interop.Reference[SplatScene], set: interop.Reference[GeometrySet], name: str, eyes: const Vector3[], sizes: const f64[], within: f64) -> !:
    let cloud = try GaussianSplats.of(set)
    defer cloud.release()
    assert(cloud.value().has_lod())
    let one: interop.Reference[GaussianSplats][1] = [cloud]
    var walls: Wall[]
    for eye in eyes:
        try view.look_from(eye)
        var look = SplatLook()
        look.lod_pixels = 0.0
        try scene.value().set_look(look)
        _ = try view.draw_scene(scene, one, walls)
        let full = try sums(view)
        let full_visible = (try scene.value().timings()).visible
        for pixels in sizes:
            look.lod_pixels = pixels
            look.lod_from = 0
            try scene.value().set_look(look)
            _ = try view.draw_scene(scene, one, walls)
            let merged = try sums(view)
            let merged_visible = (try scene.value().timings()).visible
            print(f"level of detail, {name} from {eye.length():.0f} units at {pixels:.1f} px: {full_visible} splats drawn, {merged_visible} with LOD; summed red {full[0]:.2f} -> {merged[0]:.2f} ({(merged[0] / full[0] - 1.0) * 100.0:+.1f}%), green {full[1]:.2f} -> {merged[1]:.2f}, blue {full[2]:.2f} -> {merged[2]:.2f}")
            assert(merged_visible * 5 < full_visible)
            for channel in 0..<(usize)3:
                assert(math.abs(merged[channel] - full[channel]) <= within * full[channel])

## An antialiased capture (Mip-Splatting's 2D filter): a splat far under a
## pixel keeps its integral through the 0.3 px² low-pass, its opacity scaled
## by √(det Σ / det(Σ + 0.3)) = σ² / (σ² + 0.3) for a round one; the same
## splat without the flag is drawn at its full opacity.
pub func antialiased(device: gpu.Device) -> !:
    var view = try View(device, width, height)
    defer view.close()
    let splats: Splat[1] = [Splat(center = Vector3(), scale = Vector3(0.01, 0.01, 0.01), opacity = 0.8, color = Vector3(1.0, 0.0, 0.0))]
    var walls: Wall[]
    let focal = 1.0 / math.tan(20.0 * math.pi / 180.0) * (f64)height * 0.5
    let spread = focal * 0.01 / 4.0
    let variance = spread * spread + 0.3
    let falloff = math.exp(-0.5 * 0.5 / variance)
    for flagged in [false, true]:
        let set = try clouds.splat_set(splats, 0, none, none, flagged)
        defer set.release()
        let cloud = try GaussianSplats.of(set)
        defer cloud.release()
        let one: interop.Reference[GaussianSplats][1] = [cloud]
        _ = try view.draw(one, walls)
        let opacity = 0.8 * spread * spread / variance if flagged else 0.8
        assert(near(view.at(80, 60, 0), opacity * falloff, 0.005))
        print(f"antialiased {flagged}: middle red {view.at(80, 60, 0):.4f}, expected {opacity * falloff:.4f}")

## The sums of the color channels over the last read.
func sums(view: View*) -> f64[3]!:
    var total: f64[3] = [0.0, 0.0, 0.0]
    for y in 0..<height:
        for x in 0..<width:
            for channel in 0..<(usize)3: total[channel] += view.at(x, y, channel)
    return total