Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-render

Source of release 0.1.1

0.1.1 / src / render / lights.lucb

#==============================================================================================
#
#   render/lights - UsdLux lights in the kernels' records
#
#   DESCRIPTION:
#       Each light becomes `light_stride` vec4s (shaders/lights.glsl has the layout). Its
#       radiance is color × intensity × 2^exposure, divided by its area when it
#       normalizes, as UsdLux defines it. A color temperature multiplies by the blackbody
#       of that temperature at luminance 1. A distant light's intensity is the irradiance
#       it delivers, spread over its angular size (a delta when the angle is 0).
#       Light samples choose distant and dome lights by power (their shares among
#       them are recorded here); every other light is an emitter of the light tree
#       (light_tree.lucb), its index recorded here in the order the tree numbers them.
#       A dome's radiance is color × intensity × 2^exposure (never normalized), times
#       its image (environment.lucb) where it has one; its rows 1-3 hold its axes.
#
#==============================================================================================

pub let light_stride: usize = 7

## ACEScg luminance weights (the Y row of ACEScg to XYZ).
func luminance(rgb: Vector3) -> f64:
    return 0.2722287168 * rgb.x + 0.6740817658 * rgb.y + 0.0536895174 * rgb.z

## The records of the scene's lights: their count, the distant and dome
## lights' power, by how much of the scene (radius `radius`) they light, and
## the light index of the dome `environment` (its image) belongs to: the
## first dome with a texture (-1: none).
pub func pack_lights(objects: const SceneObjects*, radius: f64, out: Growing[f32]*, environment: const Environment*?) -> (usize, f64, i64)!:
    out.clear()
    var powers = Growing[f64]()
    defer powers.close()
    let to_acescg = film_matrix()
    var textured: i64 = -1
    for index in 0..<objects.count():
        let item = objects.object(index)
        if item.kind != SceneKind.light: continue
        let light = item.light
        let m = item.matrix
        let position = m.translation
        var emits = try m.z.multiply_scalar(-1.0).normalized()
        var u = Vector3()
        var v = Vector3()
        var size: f64 = 0.0
        var area: f64 = 0.0
        var cos_half: f64 = 1.0
        var kind: f64 = 0.0
        if light.shape == LightShape.rect:
            u = m.transform_direction(Vector3(0.5 * light.width, 0.0, 0.0))
            v = m.transform_direction(Vector3(0.0, 0.5 * light.height, 0.0))
            area = 4.0 * u.cross(v).length()
        elif light.shape == LightShape.disk:
            kind = 1.0
            u = m.transform_direction(Vector3(light.radius, 0.0, 0.0))
            v = m.transform_direction(Vector3(0.0, light.radius, 0.0))
            area = math.pi * u.cross(v).length()
        elif light.shape == LightShape.sphere:
            kind = 2.0
            size = light.radius * math.cbrt(math.abs(m.determinant()))
            area = 4.0 * math.pi * size * size
        elif light.shape == LightShape.dome:
            kind = 4.0
            u = try m.x.normalized()
            v = try m.y.normalized()
        else:
            kind = 3.0
            cos_half = math.cos(0.5 * light.angle * math.pi / 180.0)
        var scale = light.intensity * math.exp2(light.exposure)
        if kind == 4.0:
            # Rows 1-3 are the dome's axes (X, Y, Z): world to its frame.
            emits = try m.z.normalized()
        if kind < 3.0 and light.normalize:
            if area <= 0.0: continue
            scale = scale / area
        if kind == 3.0 and cos_half < 1.0 - 1e-7:
            scale = scale / (2.0 * math.pi * (1.0 - cos_half))
        var rgb = light.color.multiply_scalar(scale)
        var fit = fit_spectrum(light.color)
        fit.scale = fit.scale * scale
        var flags: f64 = 1.0 if light.visible else 0.0
        if light.temperature > 0.0:
            let tint = blackbody_xyz(light.temperature)
            # Luminance 1: the kernels' blackbody is peak-normalized.
            fit.scale = fit.scale / tint.y
            let tinted = to_acescg.apply(tint.multiply_scalar(1.0 / tint.y))
            rgb = Vector3(rgb.x * tinted.x, rgb.y * tinted.y, rgb.z * tinted.z)
            flags += 2.0
        # A dome's power: what a sphere of its mean radiance pours into the
        # scene's disk (π × the mean, as irradiance, over π r²).
        var power = -1.0
        # A distant light's radiance is its irradiance over its solid angle.
        if kind == 3.0: power = math.max(0.0, luminance(rgb)) * (2.0 * math.pi * (1.0 - cos_half) if cos_half < 1.0 - 1e-7 else 1.0) * math.pi * radius * radius
        if kind == 4.0:
            var mean: f64 = 1.0
            if let image = environment:
                if textured < 0 and objects.texture(index).length > 0:
                    textured = (i64)(powers.count)
                    mean = image.average
            power = math.max(0.0, luminance(rgb)) * mean * math.pi * math.pi * radius * radius
        try powers.push(power)
        let rows: f64[28] = [position.x, position.y, position.z, kind,
                             u.x, u.y, u.z, size,
                             v.x, v.y, v.z, cos_half,
                             emits.x, emits.y, emits.z, area,
                             fit.c0, fit.c1, fit.c2, fit.scale,
                             rgb.x, rgb.y, rgb.z, flags,
                             light.temperature, 0.0, 0.0, 0.0]
        for value in rows: try out.push((f32)value)
    # Distant lights: shares of their power and the running share; the others
    # take the next emitter index.
    let count = powers.count
    var total: f64 = 0.0
    var distant: usize = 0
    for power in powers.view():
        if power >= 0.0:
            total += power
            distant += 1
    var running: f64 = 0.0
    var emitter: usize = 0
    for index in 0..<count:
        let row = (index * light_stride + 6) * 4
        let power = powers.items[index]
        if power < 0.0:
            out.items[row + 3] = (f32)emitter
            emitter += 1
            continue
        let share = power / total if total > 0.0 else 1.0 / (f64)distant
        running += share
        out.items[row + 1] = (f32)share
        out.items[row + 2] = (f32)running
    return (count, total, textured)