use core::fmt;
use color::OpaqueColor;
use crate::LinearRgb;
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum Param {
BaseWeight,
BaseColor,
BaseMetalness,
BaseDiffuseRoughness,
SpecularWeight,
SpecularColor,
SpecularRoughness,
SpecularRoughnessAnisotropy,
SpecularIor,
TransmissionWeight,
TransmissionColor,
TransmissionDepth,
TransmissionScatter,
TransmissionScatterAnisotropy,
TransmissionDispersionScale,
TransmissionDispersionAbbeNumber,
SubsurfaceWeight,
SubsurfaceColor,
SubsurfaceRadius,
SubsurfaceRadiusScale,
SubsurfaceScatterAnisotropy,
CoatWeight,
CoatColor,
CoatRoughness,
CoatRoughnessAnisotropy,
CoatIor,
CoatDarkening,
FuzzWeight,
FuzzColor,
FuzzRoughness,
EmissionLuminance,
EmissionColor,
ThinFilmWeight,
ThinFilmThickness,
ThinFilmIor,
GeometryOpacity,
GeometryThinWalled,
GeometryNormal,
GeometryTangent,
GeometryCoatNormal,
GeometryCoatTangent,
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
#[non_exhaustive]
pub enum Group {
Base,
Specular,
Transmission,
Subsurface,
Coat,
Fuzz,
Emission,
ThinFilm,
Geometry,
}
impl Group {
pub const ALL: [Self; 9] = [
Self::Base,
Self::Specular,
Self::Transmission,
Self::Subsurface,
Self::Coat,
Self::Fuzz,
Self::Emission,
Self::ThinFilm,
Self::Geometry,
];
#[must_use]
pub const fn prefix(self) -> &'static str {
match self {
Self::Base => "base",
Self::Specular => "specular",
Self::Transmission => "transmission",
Self::Subsurface => "subsurface",
Self::Coat => "coat",
Self::Fuzz => "fuzz",
Self::Emission => "emission",
Self::ThinFilm => "thin_film",
Self::Geometry => "geometry",
}
}
#[must_use]
pub const fn label(self) -> &'static str {
match self {
Self::Base => "Base",
Self::Specular => "Specular",
Self::Transmission => "Transmission",
Self::Subsurface => "Subsurface",
Self::Coat => "Coat",
Self::Fuzz => "Fuzz",
Self::Emission => "Emission",
Self::ThinFilm => "Thin-film",
Self::Geometry => "Geometry",
}
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
#[non_exhaustive]
pub enum Kind {
Float,
Boolean,
Color3,
Vector3,
}
impl Kind {
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Float => "float",
Self::Boolean => "boolean",
Self::Color3 => "color3",
Self::Vector3 => "vector3",
}
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Bound {
Inclusive(f32),
Exclusive(f32),
Unbounded,
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Range {
pub min: Bound,
pub max: Bound,
}
impl Range {
#[must_use]
pub const fn closed(min: f32, max: f32) -> Self {
Self {
min: Bound::Inclusive(min),
max: Bound::Inclusive(max),
}
}
#[must_use]
pub const fn at_least(min: f32) -> Self {
Self {
min: Bound::Inclusive(min),
max: Bound::Unbounded,
}
}
#[must_use]
pub const fn greater_than(min: f32) -> Self {
Self {
min: Bound::Exclusive(min),
max: Bound::Unbounded,
}
}
#[must_use]
pub fn contains(self, value: f32) -> bool {
if !value.is_finite() {
return false;
}
let above = match self.min {
Bound::Inclusive(min) => value >= min,
Bound::Exclusive(min) => value > min,
Bound::Unbounded => true,
};
let below = match self.max {
Bound::Inclusive(max) => value <= max,
Bound::Exclusive(max) => value < max,
Bound::Unbounded => true,
};
above && below
}
}
impl fmt::Display for Range {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.min {
Bound::Inclusive(v) => write!(f, "[{v}, ")?,
Bound::Exclusive(v) => write!(f, "({v}, ")?,
Bound::Unbounded => f.write_str("(-inf, ")?,
}
match self.max {
Bound::Inclusive(v) => write!(f, "{v}]"),
Bound::Exclusive(v) => write!(f, "{v})"),
Bound::Unbounded => f.write_str("inf)"),
}
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
#[non_exhaustive]
pub enum Unit {
None,
Length,
Nits,
Micrometers,
}
#[derive(Copy, Clone)]
pub enum Value<CS: LinearRgb> {
Float(f32),
Boolean(bool),
Color(OpaqueColor<CS>),
Channels([f32; 3]),
}
impl<CS: LinearRgb> fmt::Debug for Value<CS> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Float(v) => f.debug_tuple("Float").field(v).finish(),
Self::Boolean(b) => f.debug_tuple("Boolean").field(b).finish(),
Self::Color(c) => f.debug_tuple("Color").field(&c.components).finish(),
Self::Channels(c) => f.debug_tuple("Channels").field(c).finish(),
}
}
}
impl<CS: LinearRgb> PartialEq for Value<CS> {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(Self::Float(a), Self::Float(b)) => a == b,
(Self::Boolean(a), Self::Boolean(b)) => a == b,
(Self::Color(a), Self::Color(b)) => a == b,
(Self::Channels(a), Self::Channels(b)) => a == b,
_ => false,
}
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum ParamDefault {
Float(f32),
Boolean(bool),
Color3([f32; 3]),
UnperturbedNormal,
UnperturbedTangent,
}
#[derive(Copy, Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct ParamInfo {
pub param: Param,
pub identifier: &'static str,
pub label: &'static str,
pub group: Group,
pub kind: Kind,
pub range: Option<Range>,
pub norm: Option<Range>,
pub default: ParamDefault,
pub unit: Unit,
pub color: bool,
pub uniform: bool,
}
const fn float(
param: Param,
identifier: &'static str,
label: &'static str,
group: Group,
range: Range,
norm: Option<Range>,
default: f32,
unit: Unit,
) -> ParamInfo {
ParamInfo {
param,
identifier,
label,
group,
kind: Kind::Float,
range: Some(range),
norm,
default: ParamDefault::Float(default),
unit,
color: false,
uniform: false,
}
}
const fn color(
param: Param,
identifier: &'static str,
label: &'static str,
group: Group,
range: Range,
default: [f32; 3],
) -> ParamInfo {
ParamInfo {
param,
identifier,
label,
group,
kind: Kind::Color3,
range: Some(range),
norm: None,
default: ParamDefault::Color3(default),
unit: Unit::None,
color: true,
uniform: false,
}
}
const fn vector(
param: Param,
identifier: &'static str,
label: &'static str,
default: ParamDefault,
) -> ParamInfo {
ParamInfo {
param,
identifier,
label,
group: Geometry,
kind: Kind::Vector3,
range: None,
norm: None,
default,
unit: Unit::None,
color: false,
uniform: false,
}
}
const UNIT: Range = Range::closed(0.0, 1.0);
const SIGNED_UNIT: Range = Range::closed(-1.0, 1.0);
const NON_NEGATIVE: Range = Range::at_least(0.0);
const POSITIVE: Range = Range::greater_than(0.0);
const IOR_NORM: Option<Range> = Some(Range::closed(1.0, 3.0));
use Group::{Base, Coat, Emission, Fuzz, Geometry, Specular, Subsurface, ThinFilm, Transmission};
use Param as P;
static TABLE: [ParamInfo; Param::COUNT] = [
float(
P::BaseWeight,
"base_weight",
"Weight",
Base,
UNIT,
None,
1.0,
Unit::None,
),
color(P::BaseColor, "base_color", "Color", Base, UNIT, [0.8; 3]),
float(
P::BaseMetalness,
"base_metalness",
"Metalness",
Base,
UNIT,
None,
0.0,
Unit::None,
),
float(
P::BaseDiffuseRoughness,
"base_diffuse_roughness",
"Diffuse Roughness",
Base,
UNIT,
None,
0.0,
Unit::None,
),
float(
P::SpecularWeight,
"specular_weight",
"Weight",
Specular,
NON_NEGATIVE,
Some(UNIT),
1.0,
Unit::None,
),
color(
P::SpecularColor,
"specular_color",
"Color",
Specular,
UNIT,
[1.0; 3],
),
float(
P::SpecularRoughness,
"specular_roughness",
"Roughness",
Specular,
UNIT,
None,
0.3,
Unit::None,
),
float(
P::SpecularRoughnessAnisotropy,
"specular_roughness_anisotropy",
"Anisotropy",
Specular,
UNIT,
None,
0.0,
Unit::None,
),
float(
P::SpecularIor,
"specular_ior",
"IOR",
Specular,
POSITIVE,
IOR_NORM,
1.5,
Unit::None,
),
float(
P::TransmissionWeight,
"transmission_weight",
"Weight",
Transmission,
UNIT,
None,
0.0,
Unit::None,
),
color(
P::TransmissionColor,
"transmission_color",
"Color",
Transmission,
UNIT,
[1.0; 3],
),
float(
P::TransmissionDepth,
"transmission_depth",
"Depth",
Transmission,
NON_NEGATIVE,
Some(UNIT),
0.0,
Unit::Length,
),
color(
P::TransmissionScatter,
"transmission_scatter",
"Scatter",
Transmission,
UNIT,
[0.0; 3],
),
float(
P::TransmissionScatterAnisotropy,
"transmission_scatter_anisotropy",
"Anisotropy",
Transmission,
SIGNED_UNIT,
None,
0.0,
Unit::None,
),
float(
P::TransmissionDispersionScale,
"transmission_dispersion_scale",
"Dispersion scale",
Transmission,
UNIT,
None,
0.0,
Unit::None,
),
float(
P::TransmissionDispersionAbbeNumber,
"transmission_dispersion_abbe_number",
"Abbe number",
Transmission,
POSITIVE,
Some(Range::closed(9.0, 91.0)),
20.0,
Unit::None,
),
float(
P::SubsurfaceWeight,
"subsurface_weight",
"Weight",
Subsurface,
UNIT,
None,
0.0,
Unit::None,
),
color(
P::SubsurfaceColor,
"subsurface_color",
"Color",
Subsurface,
UNIT,
[0.8; 3],
),
float(
P::SubsurfaceRadius,
"subsurface_radius",
"Radius",
Subsurface,
NON_NEGATIVE,
Some(UNIT),
1.0,
Unit::Length,
),
ParamInfo {
color: false,
..color(
P::SubsurfaceRadiusScale,
"subsurface_radius_scale",
"Radius scale",
Subsurface,
UNIT,
[1.0, 0.5, 0.25],
)
},
float(
P::SubsurfaceScatterAnisotropy,
"subsurface_scatter_anisotropy",
"Anisotropy",
Subsurface,
SIGNED_UNIT,
None,
0.0,
Unit::None,
),
float(
P::CoatWeight,
"coat_weight",
"Weight",
Coat,
UNIT,
None,
0.0,
Unit::None,
),
color(P::CoatColor, "coat_color", "Color", Coat, UNIT, [1.0; 3]),
float(
P::CoatRoughness,
"coat_roughness",
"Roughness",
Coat,
UNIT,
None,
0.0,
Unit::None,
),
float(
P::CoatRoughnessAnisotropy,
"coat_roughness_anisotropy",
"Anisotropy",
Coat,
UNIT,
None,
0.0,
Unit::None,
),
float(
P::CoatIor,
"coat_ior",
"IOR",
Coat,
POSITIVE,
IOR_NORM,
1.6,
Unit::None,
),
float(
P::CoatDarkening,
"coat_darkening",
"Darkening",
Coat,
UNIT,
None,
1.0,
Unit::None,
),
float(
P::FuzzWeight,
"fuzz_weight",
"Weight",
Fuzz,
UNIT,
None,
0.0,
Unit::None,
),
color(P::FuzzColor, "fuzz_color", "Color", Fuzz, UNIT, [1.0; 3]),
float(
P::FuzzRoughness,
"fuzz_roughness",
"Roughness",
Fuzz,
UNIT,
None,
0.5,
Unit::None,
),
float(
P::EmissionLuminance,
"emission_luminance",
"Luminance",
Emission,
NON_NEGATIVE,
Some(Range::closed(0.0, 1000.0)),
0.0,
Unit::Nits,
),
color(
P::EmissionColor,
"emission_color",
"Color",
Emission,
NON_NEGATIVE,
[1.0; 3],
),
float(
P::ThinFilmWeight,
"thin_film_weight",
"Weight",
ThinFilm,
UNIT,
None,
0.0,
Unit::None,
),
float(
P::ThinFilmThickness,
"thin_film_thickness",
"Thickness",
ThinFilm,
NON_NEGATIVE,
Some(UNIT),
0.5,
Unit::Micrometers,
),
float(
P::ThinFilmIor,
"thin_film_ior",
"IOR",
ThinFilm,
POSITIVE,
IOR_NORM,
1.4,
Unit::None,
),
float(
P::GeometryOpacity,
"geometry_opacity",
"Opacity",
Geometry,
UNIT,
None,
1.0,
Unit::None,
),
ParamInfo {
param: P::GeometryThinWalled,
identifier: "geometry_thin_walled",
label: "Thin walled",
group: Geometry,
kind: Kind::Boolean,
range: None,
norm: None,
default: ParamDefault::Boolean(false),
unit: Unit::None,
color: false,
uniform: true,
},
vector(
P::GeometryNormal,
"geometry_normal",
"Normal",
ParamDefault::UnperturbedNormal,
),
vector(
P::GeometryTangent,
"geometry_tangent",
"Tangent",
ParamDefault::UnperturbedTangent,
),
vector(
P::GeometryCoatNormal,
"geometry_coat_normal",
"Coat Normal",
ParamDefault::UnperturbedNormal,
),
vector(
P::GeometryCoatTangent,
"geometry_coat_tangent",
"Coat Tangent",
ParamDefault::UnperturbedTangent,
),
];
impl Param {
pub const COUNT: usize = 41;
pub const ALL: [Self; Self::COUNT] = [
Self::BaseWeight,
Self::BaseColor,
Self::BaseMetalness,
Self::BaseDiffuseRoughness,
Self::SpecularWeight,
Self::SpecularColor,
Self::SpecularRoughness,
Self::SpecularRoughnessAnisotropy,
Self::SpecularIor,
Self::TransmissionWeight,
Self::TransmissionColor,
Self::TransmissionDepth,
Self::TransmissionScatter,
Self::TransmissionScatterAnisotropy,
Self::TransmissionDispersionScale,
Self::TransmissionDispersionAbbeNumber,
Self::SubsurfaceWeight,
Self::SubsurfaceColor,
Self::SubsurfaceRadius,
Self::SubsurfaceRadiusScale,
Self::SubsurfaceScatterAnisotropy,
Self::CoatWeight,
Self::CoatColor,
Self::CoatRoughness,
Self::CoatRoughnessAnisotropy,
Self::CoatIor,
Self::CoatDarkening,
Self::FuzzWeight,
Self::FuzzColor,
Self::FuzzRoughness,
Self::EmissionLuminance,
Self::EmissionColor,
Self::ThinFilmWeight,
Self::ThinFilmThickness,
Self::ThinFilmIor,
Self::GeometryOpacity,
Self::GeometryThinWalled,
Self::GeometryNormal,
Self::GeometryTangent,
Self::GeometryCoatNormal,
Self::GeometryCoatTangent,
];
pub const COLORS: [Self; 8] = [
Self::BaseColor,
Self::SpecularColor,
Self::TransmissionColor,
Self::TransmissionScatter,
Self::SubsurfaceColor,
Self::CoatColor,
Self::FuzzColor,
Self::EmissionColor,
];
#[must_use]
pub fn info(self) -> &'static ParamInfo {
&TABLE[self as usize]
}
#[must_use]
pub fn identifier(self) -> &'static str {
self.info().identifier
}
#[must_use]
pub fn from_identifier(identifier: &str) -> Option<Self> {
TABLE
.iter()
.find(|info| info.identifier == identifier)
.map(|info| info.param)
}
}
impl fmt::Display for Param {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.identifier())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn table_is_indexed_by_discriminant() {
for (index, param) in Param::ALL.into_iter().enumerate() {
assert_eq!(param as usize, index, "{param:?} is out of order in ALL");
assert_eq!(
param.info().param,
param,
"{param:?} has the wrong table row"
);
}
}
#[test]
fn identifiers_are_unique_prefixed_and_round_trip() {
for param in Param::ALL {
let info = param.info();
assert!(
info.identifier
.strip_prefix(info.group.prefix())
.is_some_and(|rest| rest.starts_with('_')),
"{} is not prefixed by its group",
info.identifier
);
assert_eq!(Param::from_identifier(info.identifier), Some(param));
}
assert_eq!(Param::from_identifier("specular_anisotropy_rotation"), None);
}
#[test]
fn groups_appear_in_specification_order() {
let groups: alloc::vec::Vec<Group> = Param::ALL.iter().map(|p| p.info().group).collect();
let mut expected = groups.clone();
expected.sort();
assert_eq!(groups, expected, "parameters must be grouped in spec order");
for group in Group::ALL {
assert!(groups.contains(&group), "{group:?} has no parameters");
}
}
#[test]
fn ranges_match_kinds_and_contain_defaults() {
for param in Param::ALL {
let info = param.info();
match (info.kind, info.range, info.default) {
(Kind::Float, Some(range), ParamDefault::Float(v)) => {
assert!(range.contains(v), "{param} default {v} outside {range}");
}
(Kind::Color3, Some(range), ParamDefault::Color3(c)) => {
assert!(c.iter().all(|&v| range.contains(v)), "{param} default");
}
(Kind::Boolean, None, ParamDefault::Boolean(_))
| (
Kind::Vector3,
None,
ParamDefault::UnperturbedNormal | ParamDefault::UnperturbedTangent,
) => {}
other => panic!("{param} has inconsistent metadata: {other:?}"),
}
if let (Some(norm), ParamDefault::Float(v)) = (info.norm, info.default) {
assert!(norm.contains(v), "{param} default {v} outside norm {norm}");
}
}
}
#[test]
fn colors_are_the_color_flagged_color3_parameters() {
let flagged: alloc::vec::Vec<Param> =
Param::ALL.into_iter().filter(|p| p.info().color).collect();
assert_eq!(flagged, Param::COLORS);
for param in Param::ALL {
let info = param.info();
let expected = info.kind == Kind::Color3 && param != Param::SubsurfaceRadiusScale;
assert_eq!(info.color, expected, "{param}");
}
}
#[test]
fn ranges_follow_their_bounds() {
assert!(POSITIVE.contains(f32::MIN_POSITIVE));
assert!(!POSITIVE.contains(0.0));
assert!(NON_NEGATIVE.contains(0.0));
assert!(!NON_NEGATIVE.contains(f32::INFINITY));
assert!(!NON_NEGATIVE.contains(f32::NEG_INFINITY));
assert!(NON_NEGATIVE.contains(f32::MAX));
assert!(!NON_NEGATIVE.contains(-0.1));
assert!(!UNIT.contains(f32::NAN));
assert!(!NON_NEGATIVE.contains(f32::NAN));
assert_eq!(alloc::format!("{POSITIVE}"), "(0, inf)");
assert_eq!(alloc::format!("{UNIT}"), "[0, 1]");
}
}