use crate::materials::MaterialTextureSlots;
use crate::textures::Texture;
use std::fmt::Write as _;
use std::sync::Arc;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[repr(u8)]
pub enum MapRole {
Map,
AlphaMap,
NormalMap,
RoughnessMap,
MetalnessMap,
AoMap,
EmissiveMap,
Matcap,
Cloud,
IridescenceThicknessMap,
DisplacementMap,
LightMap,
BumpMap,
SpecularMap,
SpecularIntensityMap,
SpecularColorMap,
ClearcoatMap,
ClearcoatRoughnessMap,
ClearcoatNormalMap,
IridescenceMap,
SheenColorMap,
SheenRoughnessMap,
TransmissionMap,
ThicknessMap,
AnisotropyMap,
GradientMap,
}
impl MapRole {
pub const ALL: [MapRole; 26] = [
MapRole::Map,
MapRole::AlphaMap,
MapRole::NormalMap,
MapRole::RoughnessMap,
MapRole::MetalnessMap,
MapRole::AoMap,
MapRole::EmissiveMap,
MapRole::Matcap,
MapRole::Cloud,
MapRole::IridescenceThicknessMap,
MapRole::DisplacementMap,
MapRole::LightMap,
MapRole::BumpMap,
MapRole::SpecularMap,
MapRole::SpecularIntensityMap,
MapRole::SpecularColorMap,
MapRole::ClearcoatMap,
MapRole::ClearcoatRoughnessMap,
MapRole::ClearcoatNormalMap,
MapRole::IridescenceMap,
MapRole::SheenColorMap,
MapRole::SheenRoughnessMap,
MapRole::TransmissionMap,
MapRole::ThicknessMap,
MapRole::AnisotropyMap,
MapRole::GradientMap,
];
pub fn ident(self) -> &'static str {
match self {
MapRole::Map => "map",
MapRole::AlphaMap => "alpha_map",
MapRole::NormalMap => "normal_map",
MapRole::RoughnessMap => "roughness_map",
MapRole::MetalnessMap => "metalness_map",
MapRole::AoMap => "ao_map",
MapRole::EmissiveMap => "emissive_map",
MapRole::Matcap => "matcap",
MapRole::Cloud => "cloud",
MapRole::IridescenceThicknessMap => "iridescence_thickness_map",
MapRole::DisplacementMap => "displacement_map",
MapRole::LightMap => "light_map",
MapRole::BumpMap => "bump_map",
MapRole::SpecularMap => "specular_map",
MapRole::SpecularIntensityMap => "specular_intensity_map",
MapRole::SpecularColorMap => "specular_color_map",
MapRole::ClearcoatMap => "clearcoat_map",
MapRole::ClearcoatRoughnessMap => "clearcoat_roughness_map",
MapRole::ClearcoatNormalMap => "clearcoat_normal_map",
MapRole::IridescenceMap => "iridescence_map",
MapRole::SheenColorMap => "sheen_color_map",
MapRole::SheenRoughnessMap => "sheen_roughness_map",
MapRole::TransmissionMap => "transmission_map",
MapRole::ThicknessMap => "thickness_map",
MapRole::AnisotropyMap => "anisotropy_map",
MapRole::GradientMap => "gradient_map",
}
}
pub fn has_const(self) -> String {
format!("HAS_{}", self.ident().to_ascii_uppercase())
}
fn absent(self) -> &'static str {
match self {
MapRole::NormalMap | MapRole::ClearcoatNormalMap => "vec4<f32>(0.5, 0.5, 1.0, 1.0)",
MapRole::DisplacementMap | MapRole::LightMap | MapRole::Cloud | MapRole::BumpMap => {
"vec4<f32>(0.0, 0.0, 0.0, 0.0)"
}
_ => "vec4<f32>(1.0, 1.0, 1.0, 1.0)",
}
}
pub fn vertex(self) -> bool {
self == MapRole::DisplacementMap
}
pub fn legacy(self) -> bool {
matches!(
self,
MapRole::Map
| MapRole::NormalMap
| MapRole::RoughnessMap
| MapRole::MetalnessMap
| MapRole::AoMap
| MapRole::EmissiveMap
| MapRole::Matcap
| MapRole::Cloud
)
}
pub fn colour(self) -> bool {
matches!(
self,
MapRole::Map
| MapRole::EmissiveMap
| MapRole::Matcap
| MapRole::LightMap
| MapRole::SpecularColorMap
| MapRole::SheenColorMap
)
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default)]
pub struct MapVariant(u32);
impl MapVariant {
pub fn with(self, role: MapRole) -> Self {
MapVariant(self.0 | 1 << role as u32)
}
pub fn contains(self, role: MapRole) -> bool {
self.0 & (1 << role as u32) != 0
}
pub fn roles(self) -> impl Iterator<Item = MapRole> {
MapRole::ALL.into_iter().filter(move |r| self.contains(*r))
}
pub fn is_empty(self) -> bool {
self.0 == 0
}
const ENV: u32 = 1 << 31;
pub fn with_env(self) -> Self {
MapVariant(self.0 | Self::ENV)
}
pub fn has_env(self) -> bool {
self.0 & Self::ENV != 0
}
pub fn key(self) -> u64 {
0x6d61_7073_0000_0000 | self.0 as u64
}
pub fn of(slots: &MaterialTextureSlots) -> Self {
let mut v = MapVariant::default();
for (role, tex) in role_textures(slots) {
if tex.is_some() {
v = v.with(role);
}
}
if slots.env_map.is_some() {
v = v.with_env();
}
v
}
}
pub fn role_textures(slots: &MaterialTextureSlots) -> [(MapRole, Option<&Arc<Texture>>); 26] {
[
(MapRole::Map, slots.map.as_ref()),
(MapRole::AlphaMap, slots.alpha_map.as_ref()),
(MapRole::NormalMap, slots.normal_map.as_ref()),
(MapRole::RoughnessMap, slots.roughness_map.as_ref()),
(MapRole::MetalnessMap, slots.metalness_map.as_ref()),
(MapRole::AoMap, slots.ao_map.as_ref()),
(MapRole::EmissiveMap, slots.emissive_map.as_ref()),
(MapRole::Matcap, slots.matcap_map.as_ref()),
(MapRole::Cloud, slots.cloud_shadow_map.as_ref()),
(
MapRole::IridescenceThicknessMap,
slots.iridescence_thickness_map.as_ref(),
),
(MapRole::DisplacementMap, slots.displacement_map.as_ref()),
(MapRole::LightMap, slots.light_map.as_ref()),
(MapRole::BumpMap, slots.bump_map.as_ref()),
(MapRole::SpecularMap, slots.specular_map.as_ref()),
(
MapRole::SpecularIntensityMap,
slots.specular_intensity_map.as_ref(),
),
(MapRole::SpecularColorMap, slots.specular_color_map.as_ref()),
(MapRole::ClearcoatMap, slots.clearcoat_map.as_ref()),
(
MapRole::ClearcoatRoughnessMap,
slots.clearcoat_roughness_map.as_ref(),
),
(
MapRole::ClearcoatNormalMap,
slots.clearcoat_normal_map.as_ref(),
),
(MapRole::IridescenceMap, slots.iridescence_map.as_ref()),
(MapRole::SheenColorMap, slots.sheen_color_map.as_ref()),
(
MapRole::SheenRoughnessMap,
slots.sheen_roughness_map.as_ref(),
),
(MapRole::TransmissionMap, slots.transmission_map.as_ref()),
(MapRole::ThicknessMap, slots.thickness_map.as_ref()),
(MapRole::AnisotropyMap, slots.anisotropy_map.as_ref()),
(MapRole::GradientMap, slots.gradient_map.as_ref()),
]
}
pub fn needs_variant(slots: &MaterialTextureSlots) -> bool {
if slots.env_map.is_some() {
return true;
}
let legacy = legacy_uv_rows(slots);
let mut first: Option<&Arc<Texture>> = None;
for (role, tex) in role_textures(slots) {
let Some(t) = tex else { continue };
if !role.legacy() {
return true;
}
if matches!(role, MapRole::Matcap | MapRole::Cloud) {
continue;
}
if t.channel != 0 || Some(t.uv_transform_rows()) != legacy {
return true;
}
match first {
None => first = Some(t),
Some(f) => {
if f.mag_filter != t.mag_filter
|| f.min_filter != t.min_filter
|| f.wrap_s != t.wrap_s
|| f.wrap_t != t.wrap_t
{
return true;
}
}
}
}
false
}
pub fn legacy_uv_rows(slots: &MaterialTextureSlots) -> Option<[[f32; 3]; 2]> {
match slots.map.as_ref().or(slots.matcap_map.as_ref()) {
Some(t) => {
let rows = t.uv_transform_rows();
(rows[0][1] == 0.0 && rows[1][0] == 0.0).then_some(rows)
}
None => Some([[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]),
}
}
pub fn legacy_uv_transform(slots: &MaterialTextureSlots) -> [f32; 4] {
match legacy_uv_rows(slots) {
Some([r0, r1]) => [r0[2], r1[2], r0[0], r1[1]],
None => [0.0, 0.0, 1.0, 1.0],
}
}
pub const FIRST_BINDING: u32 = 11;
pub struct VariantBindings {
pub maps: Vec<(MapRole, u32, u32)>,
pub env: Option<(u32, u32)>,
pub xforms: u32,
}
pub fn bindings(variant: MapVariant) -> VariantBindings {
let mut maps = Vec::new();
let mut b = FIRST_BINDING;
for role in variant.roles() {
maps.push((role, b, b + 1));
b += 2;
}
let env = variant.has_env().then(|| {
b += 2;
(b - 2, b - 1)
});
VariantBindings {
maps,
env,
xforms: b,
}
}
pub fn prelude(variant: MapVariant) -> String {
let b = bindings(variant);
let mut s = String::new();
let n = b.maps.len().max(1);
let _ = writeln!(
s,
"// Material maps for this feature set: each with its own sampler and its\n\
// three.js uv transform (two rows of a 3×3; row 0's w its `channel`),\n\
// applied here to the raw uv or the uv set the channel names."
);
let _ = writeln!(
s,
"// `Material::map_uniforms`: `params` [lightMapIntensity, bumpScale,\n\
// iridescenceThicknessRange[0], 0]; `params2` [clearcoatNormalScale, 0, 0];\n\
// `env` [combine, reflectivity, refractionRatio, refraction mapping];\n\
// `env_dir` the rows of envMapRotation · diag(flipEnvMap, 1, 1).\n\
struct MapXforms {{ params : vec4<f32>, env : vec4<f32>, env_dir : array<vec4<f32>, 3>, params2 : vec4<f32>, rows : array<vec4<f32>, {}> }};",
2 * n
);
let _ = writeln!(
s,
"@group(2) @binding({}) var<uniform> map_xforms : MapXforms;",
b.xforms
);
for (role, t, smp) in &b.maps {
let id = role.ident();
let _ = writeln!(s, "@group(2) @binding({t}) var t_{id} : texture_2d<f32>;");
let _ = writeln!(s, "@group(2) @binding({smp}) var s_{id} : sampler;");
}
let _ = writeln!(
s,
"fn map_xform(i : u32, uv : vec2<f32>) -> vec2<f32> {{\n \
let p = vec3<f32>(map_uv_set(map_xforms.rows[2u * i].w, uv), 1.0);\n \
return vec2<f32>(dot(map_xforms.rows[2u * i].xyz, p), dot(map_xforms.rows[2u * i + 1u].xyz, p));\n\
}}"
);
let _ = writeln!(
s,
"fn map_params() -> vec4<f32> {{ return map_xforms.params; }}"
);
let _ = writeln!(
s,
"fn map_params2() -> vec4<f32> {{ return map_xforms.params2; }}"
);
let _ = writeln!(
s,
"fn env_params() -> vec4<f32> {{ return map_xforms.env; }}"
);
let _ = writeln!(
s,
"fn env_lookup(r : vec3<f32>) -> vec3<f32> {{\n \
return vec3<f32>(dot(map_xforms.env_dir[0].xyz, r), dot(map_xforms.env_dir[1].xyz, r), dot(map_xforms.env_dir[2].xyz, r));\n\
}}"
);
for role in MapRole::ALL {
let _ = writeln!(
s,
"const {} : bool = {};",
role.has_const(),
variant.contains(role)
);
}
match b.env {
Some((t, smp)) => {
let _ = writeln!(
s,
"@group(2) @binding({t}) var t_env_map : texture_cube<f32>;\n\
@group(2) @binding({smp}) var s_env_map : sampler;\n\
const HAS_ENV_MAP : bool = true;\n\
fn sample_env_map(dir : vec3<f32>) -> vec4<f32> {{ return textureSample(t_env_map, s_env_map, env_lookup(dir)); }}"
);
}
None => {
let _ = writeln!(s, "{}", ABSENT_ENV);
}
}
for role in MapRole::ALL {
let id = role.ident();
let index = b.maps.iter().position(|(r, _, _)| *r == role);
let body = match index {
Some(i) if role.vertex() => {
format!("return textureSampleLevel(t_{id}, s_{id}, map_xform({i}u, uv), 0.0);")
}
Some(i) => format!("return textureSample(t_{id}, s_{id}, map_xform({i}u, uv));"),
None => format!("return {};", role.absent()),
};
let _ = writeln!(
s,
"fn sample_{id}(uv : vec2<f32>) -> vec4<f32> {{ {body} }}"
);
let level = match index {
Some(i) => {
format!("return textureSampleLevel(t_{id}, s_{id}, map_xform({i}u, uv), 0.0);")
}
None => format!("return {};", role.absent()),
};
let _ = writeln!(
s,
"fn sample_{id}_level(uv : vec2<f32>) -> vec4<f32> {{ {level} }}"
);
}
let gradient = match b.maps.iter().any(|(r, _, _)| *r == MapRole::GradientMap) {
true => "return textureSampleGrad(t_gradient_map, s_gradient_map, vec2<f32>(coord, 0.0), vec2<f32>(dx, 0.0), vec2<f32>(dy, 0.0));",
false => "return vec4<f32>(1.0, 1.0, 1.0, 1.0);",
};
let _ = writeln!(
s,
"fn sample_gradient_map_grad(coord : f32, dx : f32, dy : f32) -> vec4<f32> {{ {gradient} }}"
);
let mirror = match b.maps.iter().any(|(r, _, _)| *r == MapRole::Map) {
true => "return textureSampleLevel(t_map, s_map, uv, 0.0);",
false => "return vec4<f32>(1.0);",
};
let _ = writeln!(
s,
"fn sample_mirror(uv : vec2<f32>) -> vec4<f32> {{ {mirror} }}"
);
s
}
const ABSENT_ENV: &str = "const HAS_ENV_MAP : bool = false;\n\
fn sample_env_map(dir : vec3<f32>) -> vec4<f32> { return vec4<f32>(0.0); }";
pub fn legacy_extras() -> String {
let mut s = String::new();
for role in MapRole::ALL {
let _ = writeln!(s, "const {} : bool = false;", role.has_const());
}
let _ = writeln!(
s,
"fn map_params() -> vec4<f32> {{ return vec4<f32>(1.0, 1.0, 0.0, 0.0); }}"
);
let _ = writeln!(
s,
"fn map_params2() -> vec4<f32> {{ return vec4<f32>(1.0, 1.0, 0.0, 0.0); }}"
);
let _ = writeln!(
s,
"fn env_params() -> vec4<f32> {{ return vec4<f32>(0.0, 1.0, 0.98, 0.0); }}"
);
let _ = writeln!(s, "{}", ABSENT_ENV);
for role in MapRole::ALL.into_iter().filter(|r| !r.legacy()) {
let (id, absent) = (role.ident(), role.absent());
let _ = writeln!(
s,
"fn sample_{id}(uv : vec2<f32>) -> vec4<f32> {{ return {absent}; }}\n\
fn sample_{id}_level(uv : vec2<f32>) -> vec4<f32> {{ return {absent}; }}"
);
}
let _ = writeln!(
s,
"fn sample_gradient_map_grad(coord : f32, dx : f32, dy : f32) -> vec4<f32> {{ return vec4<f32>(1.0, 1.0, 1.0, 1.0); }}"
);
s
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn bindings_follow_the_morph_texture_in_role_order() {
let v = MapVariant::default()
.with(MapRole::BumpMap)
.with(MapRole::Map);
let b = bindings(v);
assert_eq!(
b.maps,
vec![(MapRole::Map, 11, 12), (MapRole::BumpMap, 13, 14)]
);
assert_eq!(b.xforms, 15);
assert!(prelude(v).contains("const HAS_BUMP_MAP : bool = true;"));
assert!(prelude(v).contains("const HAS_ALPHA_MAP : bool = false;"));
}
#[test]
fn every_role_has_a_three_js_slot_name() {
use crate::materials::{
MatcapMaterial, Material, PhongMaterial, PhysicalMaterial, ToonMaterial,
};
let mut owners: [Material; 4] = [
PhysicalMaterial::default().into(),
PhongMaterial::default().into(),
MatcapMaterial::default().into(),
ToonMaterial::default().into(),
];
for role in MapRole::ALL {
let name = match role {
MapRole::Cloud => "cloudShadowMap".to_string(),
r => {
let mut out = String::new();
let mut up = false;
for c in r.ident().chars() {
if c == '_' {
up = true;
} else if up {
out.push(c.to_ascii_uppercase());
up = false;
} else {
out.push(c);
}
}
out
}
};
assert!(
owners.iter_mut().any(|m| m.map_slot_mut(&name).is_some()),
"no material has a '{name}' slot"
);
}
}
fn texel() -> Texture {
Texture::solid([255; 4], crate::textures::TextureFormat::Rgba8Unorm)
}
#[test]
fn transforms_the_legacy_layout_cannot_express_need_a_variant() {
let tiled = || {
let mut t = texel();
t.repeat = crate::math::Vector2::new(4.0, 2.0);
t.offset = crate::math::Vector2::new(0.25, 0.0);
Arc::new(t)
};
let both = MaterialTextureSlots {
map: Some(tiled()),
normal_map: Some(tiled()),
..Default::default()
};
assert!(!needs_variant(&both), "colour and normal map tiled alike");
let alone = MaterialTextureSlots {
normal_map: Some(tiled()),
..Default::default()
};
assert!(
needs_variant(&alone),
"a tiled normal map with no colour map"
);
let mut turned = texel();
turned.rotation = 0.5;
let turned = MaterialTextureSlots {
map: Some(Arc::new(turned)),
..Default::default()
};
assert!(needs_variant(&turned), "a rotated colour map");
let plain = MaterialTextureSlots {
map: Some(Arc::new(texel())),
..Default::default()
};
assert!(!needs_variant(&plain), "an untransformed colour map");
}
}