//! Material channels of a texture set. use serde::{Deserialize, Serialize}; /// A material channel. Order is the display order in the UI. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] pub enum Channel { BaseColor, Metallic, Roughness, Normal, Height, Emissive, Opacity, AmbientOcclusion, SpecularLevel, /// Specular/glossiness workflow: the F0 color. Specular, /// Specular/glossiness workflow: 1 − roughness. Glossiness, /// Clear coat: weight of a GGX dielectric layer (IOR 1.5) over the base. CoatWeight, /// Clear coat roughness. CoatRoughness, /// Sheen (cloth fuzz) layer weight. SheenWeight, /// Sheen tint. SheenColor, /// Sheen roughness. SheenRoughness, /// Anisotropy: how much the specular highlight stretches along the tangent direction. AnisotropyLevel, /// Anisotropy direction: 0..1 turns the tangent by 0..π. AnisotropyAngle, /// Subsurface scattering weight (scatter color and radius are project shader settings). ScatterWeight, /// Transmission: how much of the dielectric base lets light through (refraction at the /// project's IOR, tinted by the base color) instead of reflecting it diffusely. Transmission, /// Tangent-space normal of the clear coat (relative to the mesh normal). CoatNormal, /// User channels: free three-component data the shader does not read (masks, packed maps for /// other engines). A set may name them ([`crate::TextureSet::channel_names`]). User0, User1, User2, User3, User4, User5, User6, User7, } /// How a channel's values are stored and interpreted. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ChannelKind { /// Linear RGB color, displayed and exported sRGB-encoded. Color, /// Tangent-space normal packed into 0..1 (`n * 0.5 + 0.5`). NormalMap, /// A 0..1 scalar. Scalar, /// A signed scalar (height). Signed, /// Linear three-component data (user channels): composited, displayed and exported as stored, /// with no color encoding. Data, } impl Channel { pub const ALL: [Channel; 29] = [ Channel::BaseColor, Channel::Metallic, Channel::Roughness, Channel::Normal, Channel::Height, Channel::Emissive, Channel::Opacity, Channel::AmbientOcclusion, Channel::SpecularLevel, Channel::Specular, Channel::Glossiness, Channel::CoatWeight, Channel::CoatRoughness, Channel::SheenWeight, Channel::SheenColor, Channel::SheenRoughness, Channel::AnisotropyLevel, Channel::AnisotropyAngle, Channel::ScatterWeight, Channel::Transmission, Channel::CoatNormal, Channel::User0, Channel::User1, Channel::User2, Channel::User3, Channel::User4, Channel::User5, Channel::User6, Channel::User7, ]; /// The user channels, in order (`user0` … `user7`). pub const USER: [Channel; 8] = [Channel::User0, Channel::User1, Channel::User2, Channel::User3, Channel::User4, Channel::User5, Channel::User6, Channel::User7]; /// User channel `n` (0..8). pub fn user(n: usize) -> Option { Channel::USER.get(n).copied() } /// The number of a user channel (`None` for the others). pub fn user_index(self) -> Option { Channel::USER.iter().position(|c| *c == self) } /// The extra shading lobes (clear coat, sheen, anisotropy, subsurface scattering, /// transmission). Their defaults switch the lobes off, so a set without them shades exactly /// as before. pub const LOBES: [Channel; 10] = [ Channel::CoatWeight, Channel::CoatRoughness, Channel::SheenWeight, Channel::SheenColor, Channel::SheenRoughness, Channel::AnisotropyLevel, Channel::AnisotropyAngle, Channel::ScatterWeight, Channel::Transmission, Channel::CoatNormal, ]; /// The channels of the default "PBR - Metallic Roughness" template. pub const DEFAULT_SET: [Channel; 5] = [Channel::BaseColor, Channel::Metallic, Channel::Roughness, Channel::Normal, Channel::Height]; pub fn kind(self) -> ChannelKind { match self { Channel::BaseColor | Channel::Emissive | Channel::Specular | Channel::SheenColor => ChannelKind::Color, Channel::Normal | Channel::CoatNormal => ChannelKind::NormalMap, Channel::Height => ChannelKind::Signed, Channel::User0 | Channel::User1 | Channel::User2 | Channel::User3 | Channel::User4 | Channel::User5 | Channel::User6 | Channel::User7 => { ChannelKind::Data } _ => ChannelKind::Scalar, } } /// Value components (without coverage): 3 for colors, normals and user data, 1 for scalars. pub fn components(self) -> usize { match self.kind() { ChannelKind::Color | ChannelKind::NormalMap | ChannelKind::Data => 3, ChannelKind::Scalar | ChannelKind::Signed => 1, } } /// The value a texel has when no layer covers it. pub fn default_value(self) -> [f32; 3] { match self { Channel::BaseColor => [0.5, 0.5, 0.5], Channel::Metallic | Channel::Height | Channel::Emissive => [0.0; 3], Channel::CoatWeight | Channel::SheenWeight | Channel::AnisotropyLevel | Channel::AnisotropyAngle | Channel::ScatterWeight | Channel::Transmission => [0.0; 3], Channel::CoatRoughness => [0.1; 3], Channel::SheenColor => [1.0; 3], Channel::SheenRoughness => [0.5; 3], Channel::Roughness => [0.5; 3], Channel::Normal | Channel::CoatNormal => [0.5, 0.5, 1.0], Channel::Opacity | Channel::AmbientOcclusion => [1.0; 3], Channel::SpecularLevel | Channel::Glossiness => [0.5; 3], // F0 of a common dielectric (4 %). Channel::Specular => [0.04; 3], // White, as the reference app shows a user channel no layer covers. Channel::User0 | Channel::User1 | Channel::User2 | Channel::User3 | Channel::User4 | Channel::User5 | Channel::User6 | Channel::User7 => [1.0; 3], } } pub fn label(self) -> &'static str { match self { Channel::BaseColor => "Base color", Channel::Metallic => "Metallic", Channel::Roughness => "Roughness", Channel::Normal => "Normal", Channel::Height => "Height", Channel::Emissive => "Emissive", Channel::Opacity => "Opacity", Channel::AmbientOcclusion => "Ambient occlusion", Channel::SpecularLevel => "Specular level", Channel::Specular => "Specular", Channel::Glossiness => "Glossiness", Channel::CoatWeight => "Coat weight", Channel::CoatRoughness => "Coat roughness", Channel::SheenWeight => "Sheen weight", Channel::SheenColor => "Sheen color", Channel::SheenRoughness => "Sheen roughness", Channel::AnisotropyLevel => "Anisotropy level", Channel::AnisotropyAngle => "Anisotropy angle", Channel::ScatterWeight => "Scattering weight", Channel::Transmission => "Transmission", Channel::CoatNormal => "Coat normal", Channel::User0 => "User 0", Channel::User1 => "User 1", Channel::User2 => "User 2", Channel::User3 => "User 3", Channel::User4 => "User 4", Channel::User5 => "User 5", Channel::User6 => "User 6", Channel::User7 => "User 7", } } /// Short label used on material channel toggle buttons. pub fn short(self) -> &'static str { match self { Channel::BaseColor => "color", Channel::Metallic => "metal", Channel::Roughness => "rough", Channel::Normal => "nrm", Channel::Height => "height", Channel::Emissive => "emiss", Channel::Opacity => "op", Channel::AmbientOcclusion => "ao", Channel::SpecularLevel => "spec", Channel::Specular => "specular", Channel::Glossiness => "gloss", Channel::CoatWeight => "coat", Channel::CoatRoughness => "coat rough", Channel::SheenWeight => "sheen", Channel::SheenColor => "sheen color", Channel::SheenRoughness => "sheen rough", Channel::AnisotropyLevel => "aniso", Channel::AnisotropyAngle => "aniso angle", Channel::ScatterWeight => "scatter", Channel::Transmission => "transm", Channel::CoatNormal => "coat nrm", Channel::User0 => "user 0", Channel::User1 => "user 1", Channel::User2 => "user 2", Channel::User3 => "user 3", Channel::User4 => "user 4", Channel::User5 => "user 5", Channel::User6 => "user 6", Channel::User7 => "user 7", } } /// Stable machine id (commands, export templates, file names). pub fn id(self) -> &'static str { match self { Channel::BaseColor => "baseColor", Channel::Metallic => "metallic", Channel::Roughness => "roughness", Channel::Normal => "normal", Channel::Height => "height", Channel::Emissive => "emissive", Channel::Opacity => "opacity", Channel::AmbientOcclusion => "ambientOcclusion", Channel::SpecularLevel => "specularLevel", Channel::Specular => "specular", Channel::Glossiness => "glossiness", Channel::CoatWeight => "coatWeight", Channel::CoatRoughness => "coatRoughness", Channel::SheenWeight => "sheenWeight", Channel::SheenColor => "sheenColor", Channel::SheenRoughness => "sheenRoughness", Channel::AnisotropyLevel => "anisotropyLevel", Channel::AnisotropyAngle => "anisotropyAngle", Channel::ScatterWeight => "scatterWeight", Channel::Transmission => "transmission", Channel::CoatNormal => "coatNormal", Channel::User0 => "user0", Channel::User1 => "user1", Channel::User2 => "user2", Channel::User3 => "user3", Channel::User4 => "user4", Channel::User5 => "user5", Channel::User6 => "user6", Channel::User7 => "user7", } } /// Parse an id, label or short label (case-insensitive, spaces/underscores ignored). pub fn parse(s: &str) -> Option { let norm = |x: &str| x.to_ascii_lowercase().replace([' ', '_', '-'], ""); let want = norm(s); Channel::ALL.into_iter().find(|c| norm(c.id()) == want || norm(c.label()) == want || norm(c.short()) == want || norm(&format!("{c:?}")) == want) } pub fn index(self) -> usize { Channel::ALL.iter().position(|c| *c == self).unwrap_or(0) } } /// Storage format of a channel in a texture set (what export and the UI report). #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] pub enum ChannelFormat { Srgb8, L8, Rgb8, L16, Rgb16, L16F, Rgb16F, L32F, Rgb32F, } impl ChannelFormat { pub fn default_for(c: Channel) -> ChannelFormat { match c.kind() { ChannelKind::Color => ChannelFormat::Srgb8, ChannelKind::NormalMap => ChannelFormat::Rgb16F, ChannelKind::Scalar => ChannelFormat::L8, ChannelKind::Signed => ChannelFormat::L16F, ChannelKind::Data => ChannelFormat::Rgb8, } } pub fn label(self) -> &'static str { match self { ChannelFormat::Srgb8 => "sRGB8", ChannelFormat::L8 => "L8", ChannelFormat::Rgb8 => "RGB8", ChannelFormat::L16 => "L16", ChannelFormat::Rgb16 => "RGB16", ChannelFormat::L16F => "L16F", ChannelFormat::Rgb16F => "RGB16F", ChannelFormat::L32F => "L32F", ChannelFormat::Rgb32F => "RGB32F", } } } #[cfg(test)] mod tests { use super::*; #[test] fn parse_accepts_ids_labels_and_shorts() { for c in Channel::ALL { assert_eq!(Channel::parse(c.id()), Some(c)); assert_eq!(Channel::parse(c.label()), Some(c)); assert_eq!(Channel::parse(c.short()), Some(c)); } assert_eq!(Channel::parse("base color"), Some(Channel::BaseColor)); assert_eq!(Channel::parse("nope"), None); } #[test] fn names_are_unique_and_lobes_default_off() { let norm = |x: &str| x.to_ascii_lowercase().replace([' ', '_', '-'], ""); let mut seen = std::collections::BTreeMap::new(); for c in Channel::ALL { for name in [c.id(), c.label(), c.short()] { let prev = seen.insert(norm(name), c); assert!(prev.is_none_or(|p| p == c), "`{name}` names both {prev:?} and {c:?}"); } } for c in Channel::LOBES { assert!(Channel::ALL.contains(&c)); // Tints, roughnesses and the flat coat normal only matter once their lobe has weight. let off = matches!(c, Channel::CoatRoughness | Channel::SheenColor | Channel::SheenRoughness | Channel::CoatNormal) || c.default_value() == [0.0; 3]; assert!(off, "{c:?} must default to an inactive lobe"); } assert_eq!(Channel::parse("sheen color"), Some(Channel::SheenColor)); assert_eq!(Channel::parse("coatWeight"), Some(Channel::CoatWeight)); } #[test] fn user_channels_are_white_linear_data() { assert_eq!(Channel::USER.len(), 8); for (n, c) in Channel::USER.into_iter().enumerate() { assert_eq!((Channel::user(n), c.user_index()), (Some(c), Some(n))); assert_eq!(c.id(), format!("user{n}")); assert_eq!((c.kind(), c.components(), c.default_value()), (ChannelKind::Data, 3, [1.0; 3])); assert!(Channel::ALL.contains(&c)); } assert_eq!((Channel::user(8), Channel::BaseColor.user_index()), (None, None)); assert_eq!(Channel::parse("User 3"), Some(Channel::User3)); assert_eq!(ChannelFormat::default_for(Channel::User0), ChannelFormat::Rgb8); // old files never name them; new ones round-trip as plain strings (also as map keys) let json = serde_json::to_string(&std::collections::BTreeMap::from([(Channel::User5, 1)])).unwrap(); assert_eq!(json, r#"{"User5":1}"#); assert_eq!(serde_json::from_str::(r#""User5""#).unwrap(), Channel::User5); } #[test] fn index_matches_all_order() { for (i, c) in Channel::ALL.iter().enumerate() { assert_eq!(c.index(), i); } } }