use std::path::{Path, PathBuf};
use serde_json::{Map, Value, json};
use crate::assets::Assets;
use crate::document::Keep;
use crate::formats::xml::El;
use crate::images::Images;
use crate::mesh::{CLAMP, MIRROR, Material, REPEAT, srgb_to_linear};
const NEAREST: u32 = 9728;
const LINEAR: u32 = 9729;
const NEAREST_MIPMAP_NEAREST: u32 = 9984;
const LINEAR_MIPMAP_NEAREST: u32 = 9985;
const LINEAR_MIPMAP_LINEAR: u32 = 9987;
pub(super) struct Attrs {
list: Vec<(String, String, String)>,
}
impl Attrs {
pub fn of(el: El<'_, '_>) -> Self {
let list = el
.children()
.filter_map(|c| {
let name = c.attr("name")?.into_owned();
let value = c.attr("value").map(std::borrow::Cow::into_owned).unwrap_or_default();
Some((c.name().to_owned(), name, value))
})
.collect();
Self { list }
}
pub fn get(&self, name: &str) -> Option<&str> {
self.list
.iter()
.find(|(_, n, _)| n.eq_ignore_ascii_case(name))
.map(|(_, _, v)| v.as_str())
}
pub fn iter(&self) -> impl Iterator<Item = (&str, &str, &str)> {
self.list.iter().map(|(k, n, v)| (k.as_str(), n.as_str(), v.as_str()))
}
pub fn f32(&self, name: &str, default: f32) -> f32 {
self.get(name)
.and_then(|v| v.trim().parse::<f32>().ok())
.filter(|v| v.is_finite())
.unwrap_or(default)
}
pub fn bool(&self, name: &str, default: bool) -> bool {
self.get(name)
.map_or(default, |v| v.trim().eq_ignore_ascii_case("true"))
}
pub fn vec3(&self, name: &str, default: [f32; 3]) -> [f32; 3] {
self.get(name).map_or(default, floats)
}
pub fn colorf(&self, name: &str, default: [f32; 4]) -> [f32; 4] {
self.get(name).map_or(default, floats)
}
pub fn color(&self, name: &str, default: u32) -> u32 {
self.get(name).map_or(default, argb)
}
}
pub(super) fn floats<const N: usize>(v: &str) -> [f32; N] {
let mut out = [0.0; N];
for (o, t) in out
.iter_mut()
.zip(v.split([',', ' ', '\t', '\n', '\r']).filter(|t| !t.is_empty()))
{
*o = t.parse::<f32>().ok().filter(|x| x.is_finite()).unwrap_or(0.0);
}
out
}
pub(super) fn argb(v: &str) -> u32 {
u32::from_str_radix(v.trim(), 16).unwrap_or(0xFFFF_FFFF)
}
pub(super) fn linear(c: u32) -> [f32; 4] {
let [a, r, g, b] = c.to_be_bytes().map(|x| f32::from(x) / 255.0);
[srgb_to_linear(r), srgb_to_linear(g), srgb_to_linear(b), a]
}
#[derive(Debug, Clone)]
pub(super) struct IrrMaterial {
pub kind: String,
pub diffuse: u32,
pub emissive: u32,
pub specular: u32,
pub shininess: f32,
pub textures: [String; 4],
pub wrap: [[u32; 2]; 4],
pub filter: [[bool; 2]; 4],
pub lighting: bool,
pub backface_culling: bool,
pub wireframe: bool,
}
impl Default for IrrMaterial {
fn default() -> Self {
Self {
kind: "solid".into(),
diffuse: 0xFFFF_FFFF,
emissive: 0,
specular: 0xFFFF_FFFF,
shininess: 0.0,
textures: Default::default(),
wrap: [[REPEAT; 2]; 4],
filter: [[true, false]; 4],
lighting: true,
backface_culling: true,
wireframe: false,
}
}
}
fn wrap(v: &str) -> u32 {
let v = v.trim().to_ascii_lowercase();
let v = v.strip_prefix("texture_clamp_").unwrap_or(&v);
match v {
"repeat" => REPEAT,
"mirror" => MIRROR,
_ => CLAMP,
}
}
impl IrrMaterial {
pub fn parse(a: &Attrs) -> Self {
let mut m = Self::default();
for (_, name, value) in a.iter() {
let n = name.to_ascii_lowercase();
let slot = n
.bytes()
.last()
.and_then(|d| d.checked_sub(b'1'))
.map(usize::from)
.filter(|&s| s < 4);
match (n.as_str(), slot) {
("type", _) => m.kind = value.trim().to_ascii_lowercase(),
("diffuse", _) => m.diffuse = argb(value),
("emissive", _) => m.emissive = argb(value),
("specular", _) => m.specular = argb(value),
("shininess", _) => m.shininess = value.trim().parse().unwrap_or(0.0),
("lighting", _) => m.lighting = value.trim().eq_ignore_ascii_case("true"),
("backfaceculling", _) => m.backface_culling = value.trim().eq_ignore_ascii_case("true"),
("wireframe", _) => m.wireframe = value.trim().eq_ignore_ascii_case("true"),
(t, Some(s)) if t.starts_with("texture") && t.len() == 8 => value.trim().clone_into(&mut m.textures[s]),
(t, Some(s)) if t.starts_with("texturewrapu") => m.wrap[s][0] = wrap(value),
(t, Some(s)) if t.starts_with("texturewrapv") => m.wrap[s][1] = wrap(value),
(t, Some(s)) if t.starts_with("texturewrap") => m.wrap[s] = [wrap(value); 2],
(t, Some(s)) if t.starts_with("bilinearfilter") => {
m.filter[s][0] = value.trim().eq_ignore_ascii_case("true");
}
(t, Some(s)) if t.starts_with("trilinearfilter") => {
m.filter[s][1] = value.trim().eq_ignore_ascii_case("true");
}
_ => {}
}
}
m
}
pub fn vertex_alpha(&self) -> bool {
self.kind.ends_with("trans_vertex_alpha")
}
fn lightmap(&self) -> bool {
self.kind.starts_with("lightmap")
}
fn normal_map(&self) -> bool {
self.kind.starts_with("normalmap") || self.kind.starts_with("parallaxmap")
}
}
#[derive(Debug, Clone, Default)]
pub(super) struct Patch {
pub blend: bool,
pub second_set: bool,
pub kind: Option<String>,
}
pub(super) struct Textures<'a> {
assets: &'a dyn Assets,
pub images: Images,
root: PathBuf,
keep: bool,
}
impl<'a> Textures<'a> {
pub fn new(assets: &'a dyn Assets, keep: Keep, root: &Path) -> Self {
Self {
assets,
images: Images::new(!keep.image_bytes, root),
root: root.to_path_buf(),
keep: keep.textures,
}
}
pub fn get(&mut self, name: &str, dir: &Path) -> Option<usize> {
if !self.keep || name.trim().is_empty() {
return None;
}
let dirs: &[&Path] = if dir == self.root { &[dir] } else { &[dir, &self.root] };
self.images.find(self.assets, &[name], dirs)
}
}
pub(super) fn convert(
m: &IrrMaterial,
name: &str,
tex: &mut Textures<'_>,
dir: &Path,
sets: usize,
) -> (Material, Patch) {
let diffuse = linear(m.diffuse);
let mut out = Material::plain(name, [diffuse[0], diffuse[1], diffuse[2], 1.0]);
let e = linear(m.emissive);
out.emissive = [e[0], e[1], e[2]];
out.roughness = if m.shininess <= 0.0 || m.specular.trailing_zeros() >= 24 {
1.0
} else {
(2.0 / (m.shininess.clamp(0.0, 128.0) + 2.0)).powf(0.25)
};
out.unlit = !m.lighting;
out.double_sided = !m.backface_culling;
let sampling = |s: usize| {
let [bi, tri] = m.filter[s];
crate::mesh::Sampling {
wrap: m.wrap[s],
filter: if tri {
[Some(LINEAR), Some(LINEAR_MIPMAP_LINEAR)]
} else if bi {
[Some(LINEAR), Some(LINEAR_MIPMAP_NEAREST)]
} else {
[Some(NEAREST), Some(NEAREST_MIPMAP_NEAREST)]
},
..Default::default()
}
};
out.color_image = tex.get(&m.textures[0], dir);
out.sampling.color = sampling(0);
let mut patch = Patch::default();
if m.normal_map() {
out.normal_image = tex.get(&m.textures[1], dir);
out.sampling.normal = sampling(1);
} else if m.lightmap() {
out.occlusion_image = tex.get(&m.textures[1], dir);
out.sampling.occlusion = sampling(1);
patch.second_set = sets > 1 && out.occlusion_image.is_some();
}
match m.kind.as_str() {
"trans_alphach_ref" => out.alpha_cutoff = Some(0.5),
"trans_alphach" => patch.blend = true,
k if m.vertex_alpha() || k.is_empty() => {}
"solid" | "lightmap" | "lightmap_m2" | "lightmap_m4" | "lightmap_light" | "lightmap_light_m2"
| "lightmap_light_m4" | "normalmap_solid" | "parallaxmap_solid" => {}
k => patch.kind = Some(k.to_owned()),
}
if m.wireframe {
patch.kind = Some(format!("{} wireframe", m.kind));
}
(out, patch)
}
pub(super) fn patch(json: &mut Map<String, Value>, patches: &[Patch], extras: bool) {
let Some(Value::Array(materials)) = json.get_mut("materials") else {
return;
};
for (m, p) in materials.iter_mut().zip(patches) {
let Some(o) = m.as_object_mut() else { continue };
if p.blend {
o.insert("alphaMode".into(), json!("BLEND"));
}
if p.second_set
&& let Some(Value::Object(t)) = o.get_mut("occlusionTexture")
{
t.insert("texCoord".into(), json!(1));
}
if extras && let Some(k) = &p.kind {
o.insert("extras".into(), json!({ "irrlichtType": k }));
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reads_materials() {
let x = crate::formats::xml::Xml::parse(
"<material><enum name='Type' value='lightmap_m4'/><color name='Diffuse' value='ff808080'/>\
<texture name='Texture2' value='lm.png'/><bool name='BackfaceCulling' value='false'/>\
<enum name='TextureWrap1' value='texture_clamp_mirror'/><enum name='TextureWrapV2' value='texture_clamp_clamp'/>\
<bool name='Lighting' value='false'/></material>",
)
.unwrap();
let m = IrrMaterial::parse(&Attrs::of(x.root()));
assert_eq!(m.kind, "lightmap_m4");
assert_eq!(m.diffuse, 0xFF80_8080);
assert_eq!(m.textures[1], "lm.png");
assert!(!m.backface_culling && !m.lighting && m.lightmap());
assert_eq!(m.wrap[0], [MIRROR; 2]);
assert_eq!(m.wrap[1], [REPEAT, CLAMP]);
assert_eq!(floats::<3>("1, -2.5,3"), [1.0, -2.5, 3.0]);
assert_eq!(linear(0x80FF_0000), [1.0, 0.0, 0.0, 128.0 / 255.0]);
}
}