* Render: one sRGB transfer function, geom::srgb, for the hand-written copies (#7161) Twelve places wrote the sRGB encode or decode formula out by hand (doc's glass recipe, engine's render library, glTF import, the PNG film, photographic and presentation output, the ray tracer's image, the stage, the surface decoder, texture normalisation and the theme's contrast check). They now call solidcraft_geom::srgb: encode/decode on 0..1 values, encode8/decode8 for 8-bit codes. render::surface::srgb8_to_linear stays as a re-export, so its callers are unchanged. Same results: the module's tests compare it bit for bit with the formula as the callers wrote it, over all 256 codes and a dense sweep of linear values plus the knees, out-of-range values, infinities and NaN, for each way the callers clamped (after encoding, before it, or not at all), and decoded values either side of the decode knee, where no 8-bit code falls. One change: glTF import encoded in f32. Over every f32 in 0..1, 218 of 1,065,353,217 values sit on a rounding tie that f32 got wrong and now give the neighbouring code (f64 gives the nearer one). Left as they are: render/job/image.rs (replaced by exact tables in its own change), the tests and the starter-pack validator that keep their own copy as an independent reference, and the WGSL shaders (they can't call Rust). Fixes #7161 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Tests: the sRGB codes of five callers that had no test of their output (#7161) The glass swatch, a library surface applied as a plain appearance, glTF's fallback for a colour channel that isn't a number, a stage panel and an empty pixel in presentation output each get an assertion with their sRGB codes worked out by hand. A planted wrong conversion at each is now caught by that test. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
184 lines
6.0 KiB
Rust
184 lines
6.0 KiB
Rust
//! Original visual authoring choices; never engineering material grades or a catalog codec.
|
|
use serde::Serialize;
|
|
|
|
use crate::{DocError, Result, appearance::Look, pbr_appearance::PbrParameters};
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
|
|
#[serde(rename_all = "snake_case")]
|
|
pub enum Category {
|
|
Wood,
|
|
Metal,
|
|
Plastic,
|
|
Glass,
|
|
Solar,
|
|
Regolith,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
|
|
#[serde(rename_all = "snake_case")]
|
|
pub enum Requirement {
|
|
OpaquePbr,
|
|
Refraction,
|
|
CertifiedThinOrClosed,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
|
|
#[serde(rename_all = "snake_case")]
|
|
pub enum TextureAxis {
|
|
V,
|
|
}
|
|
|
|
/// Planned original procedural resources, not a loaded texture or a saved binding.
|
|
/// Albedo is sRGB; roughness and positive-Y tangent normals are linear data.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
|
|
pub struct TextureRecipe {
|
|
pub id: &'static str,
|
|
pub repeat_mm: [f64; 2],
|
|
pub grain_axis: Option<TextureAxis>,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
|
|
pub struct Provenance {
|
|
pub author: &'static str,
|
|
pub license: &'static str,
|
|
pub source: &'static str,
|
|
pub kind: &'static str,
|
|
}
|
|
pub const ORIGINAL: Provenance = Provenance {
|
|
author: "SolidCraft contributors",
|
|
license: "MIT OR Apache-2.0",
|
|
source: "assets/appearances/starter_pack.rs",
|
|
kind: "original illustrative optical authoring; not measured material data",
|
|
};
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
|
|
pub struct OpaqueRecipe {
|
|
pub color: [u8; 3],
|
|
pub metallic: f64,
|
|
pub roughness: f64,
|
|
pub dielectric_ior: f64,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
|
|
#[serde(tag = "mode", rename_all = "snake_case")]
|
|
pub enum GlassBoundary {
|
|
Thin { thickness_mm: f64 },
|
|
Closed,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
|
|
pub struct GlassRecipe {
|
|
pub dielectric_ior: f64,
|
|
/// Linear transmittance over reference_path_mm, not sRGB tint or alpha.
|
|
pub transmission_linear_rgb: [f64; 3],
|
|
pub reference_path_mm: f64,
|
|
pub boundary: GlassBoundary,
|
|
}
|
|
impl GlassRecipe {
|
|
pub fn validate(&self) -> Result<()> {
|
|
if !self.dielectric_ior.is_finite()
|
|
|| !(1.0..=3.0).contains(&self.dielectric_ior)
|
|
|| self.transmission_linear_rgb.iter().any(|v| !v.is_finite() || *v <= 0.0 || *v > 1.0)
|
|
|| !self.reference_path_mm.is_finite()
|
|
|| self.reference_path_mm <= 0.0
|
|
|| self.reference_path_mm > 1e6
|
|
|| matches!(self.boundary,GlassBoundary::Thin {thickness_mm} if !thickness_mm.is_finite() || thickness_mm<=0.0 || thickness_mm>10000.0)
|
|
{
|
|
return Err(DocError::Invalid(
|
|
"invalid illustrative glass recipe: finite IOR1..3, linear transmission0<T<=1, bounded positive reference path/thickness required"
|
|
.into(),
|
|
));
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
|
|
#[serde(tag = "kind", rename_all = "snake_case")]
|
|
pub enum Recipe {
|
|
Opaque(OpaqueRecipe),
|
|
Glass(GlassRecipe),
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Serialize)]
|
|
pub struct VisualPreset {
|
|
pub id: &'static str,
|
|
pub name: &'static str,
|
|
pub category: Category,
|
|
pub revision: u32,
|
|
pub provenance: Provenance,
|
|
pub requirements: &'static [Requirement],
|
|
pub recipe: Recipe,
|
|
pub preview_note: &'static str,
|
|
pub future_texture_recipe: Option<TextureRecipe>,
|
|
}
|
|
|
|
impl VisualPreset {
|
|
pub const fn opaque(
|
|
id: &'static str,
|
|
name: &'static str,
|
|
category: Category,
|
|
recipe: OpaqueRecipe,
|
|
preview_note: &'static str,
|
|
future_texture_recipe: Option<TextureRecipe>,
|
|
) -> Self {
|
|
Self {
|
|
id,
|
|
name,
|
|
category,
|
|
revision: 1,
|
|
provenance: ORIGINAL,
|
|
requirements: &[Requirement::OpaquePbr],
|
|
recipe: Recipe::Opaque(recipe),
|
|
preview_note,
|
|
future_texture_recipe,
|
|
}
|
|
}
|
|
pub const fn glass(id: &'static str, name: &'static str, recipe: GlassRecipe) -> Self {
|
|
Self {
|
|
id,
|
|
name,
|
|
category: Category::Glass,
|
|
revision: 1,
|
|
provenance: ORIGINAL,
|
|
requirements: &[Requirement::Refraction, Requirement::CertifiedThinOrClosed],
|
|
recipe: Recipe::Glass(recipe),
|
|
preview_note: "Recipe only: requires actual refraction and a certified thin/closed geometry path; no alpha substitute",
|
|
future_texture_recipe: None,
|
|
}
|
|
}
|
|
pub fn is_opaque(&self) -> bool {
|
|
matches!(self.recipe, Recipe::Opaque(_))
|
|
}
|
|
pub fn preview_color(&self) -> [u8; 3] {
|
|
match self.recipe {
|
|
Recipe::Opaque(r) => r.color,
|
|
// A UI swatch of the transmission values, not rendered glass or absorption.
|
|
Recipe::Glass(r) => r.transmission_linear_rgb.map(solidcraft_geom::srgb::encode8),
|
|
}
|
|
}
|
|
|
|
/// All opaque presets share the existing finite PBR constructor and owned Look.
|
|
/// Refraction recipes cannot be smuggled into a scalar/alpha appearance.
|
|
pub fn opaque_look(&self) -> Result<Look> {
|
|
let Recipe::Opaque(recipe) = self.recipe else {
|
|
return Err(DocError::Invalid(
|
|
"glass recipe unavailable: actual refraction and certified thin/closed geometry are required; alpha transparency is not a substitute"
|
|
.into(),
|
|
));
|
|
};
|
|
let pbr = PbrParameters::new(recipe.metallic, recipe.roughness, recipe.dielectric_ior).map_err(|e| DocError::Invalid(e.to_string()))?;
|
|
Ok(Look { name: self.name.into(), color: recipe.color, opacity: 1.0, pbr: Some(pbr) })
|
|
}
|
|
}
|
|
|
|
#[path = "../../../assets/appearances/starter_pack.rs"]
|
|
mod data;
|
|
pub use data::STARTER_PACK;
|
|
|
|
/// Stable IDs are preferred for commands; display-name lookup is unambiguous.
|
|
pub fn find(id_or_name: &str) -> Option<&'static VisualPreset> {
|
|
let key = id_or_name.trim();
|
|
STARTER_PACK.iter().find(|p| p.id.eq_ignore_ascii_case(key) || p.name.eq_ignore_ascii_case(key))
|
|
}
|