dymokomi/luce-render
Source of release 0.1.2
0.1.2 / src / render / materials.lucb
#==============================================================================================
#
# render/materials - OpenPBR Surface parameters in the kernels' records
#
# DESCRIPTION:
# OpenPbr holds the parameters of OpenPBR Surface 1.1 that luce-render shades so
# far, with the specification's defaults (docs/research/RENDERER-STUDY.md §3).
# Colors are linear ACEScg. Each material packs into `material_stride` vec4s
# (shaders/openpbr.glsl has the layout). Every color is stored twice: as its
# spectrum fit (c0, c1, c2, scale) for the spectral build and as RGB for the RGB
# build. Emission is in nits: luminance × color.
#
#==============================================================================================
pub let material_stride: usize = 16
pub struct OpenPbr:
pub var base_weight: f64
pub var base_color: Vector3
pub var base_metalness: f64
pub var base_diffuse_roughness: f64
pub var specular_weight: f64
pub var specular_color: Vector3
pub var specular_roughness: f64
pub var specular_roughness_anisotropy: f64
pub var specular_ior: f64
pub var transmission_weight: f64
pub var transmission_color: Vector3
pub var coat_weight: f64
pub var coat_color: Vector3
pub var coat_roughness: f64
pub var coat_roughness_anisotropy: f64
pub var coat_ior: f64
pub var coat_darkening: f64
pub var emission_luminance: f64
pub var emission_color: Vector3
pub var geometry_opacity: f64
pub var geometry_thin_walled: bool
## Dispersion: 0 none, 1 the Abbe number as it is; its index then varies
## with wavelength (Cauchy's law through specular_ior at 587.6 nm).
pub var transmission_dispersion_scale: f64
pub var transmission_dispersion_abbe_number: f64
## The specification's defaults: a grey dielectric with a slightly rough
## specular layer.
pub static func standard() -> OpenPbr:
let white = Vector3(1.0, 1.0, 1.0)
return OpenPbr(1.0, Vector3(0.8, 0.8, 0.8), 0.0, 0.0,
1.0, white, 0.3, 0.0, 1.5,
0.0, white,
0.0, white, 0.0, 0.0, 1.6, 1.0,
0.0, white,
1.0, false, 0.0, 20.0)
## Material `index` of a library as OpenPbr: its parameters by OpenPBR's names,
## the specification's defaults for the rest. A material for another shader is
## the default.
pub func openpbr_of(materials: const Materials*, index: usize) -> OpenPbr:
var m = OpenPbr.standard()
if materials.shader(index) != "openpbr": return m
for at in 0..<materials.parameter_count(index):
let name = materials.parameter_name(index, at)
let p = materials.parameter_value(index, at)
let v = p.value
let color = Vector3(v[0], v[1], v[2])
if name == "base_weight": m.base_weight = v[0]
elif name == "base_color": m.base_color = color
elif name == "base_metalness": m.base_metalness = v[0]
elif name == "base_diffuse_roughness": m.base_diffuse_roughness = v[0]
elif name == "specular_weight": m.specular_weight = v[0]
elif name == "specular_color": m.specular_color = color
elif name == "specular_roughness": m.specular_roughness = v[0]
elif name == "specular_roughness_anisotropy": m.specular_roughness_anisotropy = v[0]
elif name == "specular_ior": m.specular_ior = v[0]
elif name == "transmission_weight": m.transmission_weight = v[0]
elif name == "transmission_color": m.transmission_color = color
elif name == "coat_weight": m.coat_weight = v[0]
elif name == "coat_color": m.coat_color = color
elif name == "coat_roughness": m.coat_roughness = v[0]
elif name == "coat_roughness_anisotropy": m.coat_roughness_anisotropy = v[0]
elif name == "coat_ior": m.coat_ior = v[0]
elif name == "coat_darkening": m.coat_darkening = v[0]
elif name == "emission_luminance": m.emission_luminance = v[0]
elif name == "emission_color": m.emission_color = color
elif name == "geometry_opacity": m.geometry_opacity = v[0]
elif name == "geometry_thin_walled": m.geometry_thin_walled = v[0] != 0.0
elif name == "transmission_dispersion_scale": m.transmission_dispersion_scale = v[0]
elif name == "transmission_dispersion_abbe_number": m.transmission_dispersion_abbe_number = v[0]
return m
## Cauchy's B (nm²) for the material's dispersion: n(λ) = n_d + B (1/λ² - 1/λ_d²),
## where n_d is specular_ior at λ_d = 587.6 nm and the Abbe number
## V = (n_d - 1) / (n_F - n_C) over the F (486.1 nm) and C (656.3 nm) lines,
## divided by the dispersion scale. 0: none.
func cauchy_b(material: OpenPbr) -> f64:
let scale = material.transmission_dispersion_scale
let abbe = material.transmission_dispersion_abbe_number
if scale <= 0.0 or abbe <= 0.0 or material.transmission_weight <= 0.0: return 0.0
let line_f = 486.1
let line_c = 656.3
return (material.specular_ior - 1.0) * scale / (abbe * (1.0 / (line_f * line_f) - 1.0 / (line_c * line_c)))
## Append `material`'s record: material_stride vec4s; `program` its node
## graph's entry (textures.lucb; -1 none).
pub func pack_material(material: OpenPbr, program: f32, out: Growing[f32]*) -> !:
let colors: Vector3[5] = [material.base_color, material.specular_color, material.coat_color,
material.emission_color.multiply_scalar(material.emission_luminance), material.transmission_color]
let weights: f64[5] = [material.base_weight, material.specular_weight, material.coat_weight, material.emission_luminance, material.transmission_weight]
for at in 0..<(usize)5:
let color = colors[at]
let fit = fit_spectrum(Vector3(math.max(color.x, 0.0), math.max(color.y, 0.0), math.max(color.z, 0.0)))
for value in [fit.c0, fit.c1, fit.c2, fit.scale, color.x, color.y, color.z, weights[at]]: try out.push((f32)value)
let rest: f64[24] = [material.base_metalness, material.base_diffuse_roughness, material.specular_roughness, material.specular_roughness_anisotropy,
material.specular_ior, material.coat_roughness, material.coat_ior, material.coat_darkening,
material.geometry_opacity, 1.0 if material.geometry_thin_walled else 0.0, material.coat_roughness_anisotropy, cauchy_b(material),
(f64)program, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0,
0.0, 0.0, 0.0, 0.0]
for value in rest: try out.push((f32)value)