#! Contains MTL's materials: their colors and numbers, the physically based #! ones some tools add, and the textures each maps, with the options they're #! given; and reading them from an MTL file's text. import malunal::linear; module malunal::obj; #! What a texture of a material is a map of, as the statement naming it says. pub enum map_slot: u8 { ambient, #! `map_Ka` diffuse, #! `map_Kd` specular, #! `map_Ks` emissive, #! `map_Ke` shininess, #! `map_Ns` dissolve, #! `map_d` bump, #! `bump` or `map_bump` displacement, #! `disp` decal, #! `decal` reflection, #! `refl` normal, #! `norm` roughness, #! `map_Pr` metallic, #! `map_Pm` sheen, #! `map_Ps` } #! The statements naming each slot's texture, in `map_slot`'s order, the first #! of each written. const slot_names: strview[14] = ["map_Ka", "map_Kd", "map_Ks", "map_Ke", "map_Ns", "map_d", "bump", "disp", "decal", "refl", "norm", "map_Pr", "map_Pm", "map_Ps"]; const slot_kinds: map_slot[14] = [ map_slot::ambient, map_slot::diffuse, map_slot::specular, map_slot::emissive, map_slot::shininess, map_slot::dissolve, map_slot::bump, map_slot::displacement, map_slot::decal, map_slot::reflection, map_slot::normal, map_slot::roughness, map_slot::metallic, map_slot::sheen, ]; # The statement naming a slot's texture, as it's written. def slot_name(slot: map_slot): strview { for k in 0..14 { return slot_names[k] if slot_kinds[k] == slot; } return "map_Kd"; } #! A texture a material maps, and the options it's given. pub struct texture_map { pub var slot: map_slot; #! The image's file, as the MTL file names it. pub var file: string; #! `-o`, `-s`, and `-t`: where on the texture it begins, how much of it, and #! how much it's turbulent. pub var offset := vec3::zero; pub var scale := vec3::one; pub var turbulence := vec3::zero; #! `-bm`: how much a bump map bumps. pub var bump_multiplier: f32 = 1; #! `-boost`, and `-mm`'s base and gain. pub var boost: f32; pub var base: f32; pub var gain: f32 = 1; #! `-clamp`, `-blendu`, `-blendv`, and `-cc`. pub var clamp: bool; pub var blend_u: bool = true; pub var blend_v: bool = true; pub var color_correct: bool; #! `-texres`, or 0 where it's none. pub var resolution: u32; #! `-imfchan`'s channel, `r`, `g`, `b`, `m`, `l`, or `z`, or 0 where it's none. pub var channel: u8; #! `-type`, a reflection map's: `sphere`, or a side of a cube, as `cube_top`. pub var kind: string; } #! A material of an MTL file. Where the file leaves one out, a color is black, #! but diffuse, white, and a number 0, but `dissolve` and `ior`, 1. pub struct material { pub var name: string; #! `Ka`, `Kd`, `Ks`, `Ke`, and `Tf`: ambient, diffuse, specular, emitted, and #! let through. pub var ambient := vec3::zero; pub var diffuse := vec3::one; pub var specular := vec3::zero; pub var emissive := vec3::zero; pub var transmission := vec3::zero; #! `Ns`, the specular exponent, from 0 to 1000. pub var shininess: f32; #! `Ni`, the index of refraction. pub var ior: f32 = 1; #! `d`, how opaque it is, or `Tr`, how transparent, 1 less it. pub var dissolve: f32 = 1; #! `illum`, the illumination model, from 0 to 10. pub var illumination: u32; #! `sharpness`, of reflections. pub var sharpness: f32; #! `Pr`, `Pm`, `Ps`, `Pc`, `Pcr`, `aniso`, and `anisor`: physically based, #! as Blender and others write them. pub var roughness: f32; pub var metallic: f32; pub var sheen: f32; pub var clearcoat: f32; pub var clearcoat_roughness: f32; pub var anisotropy: f32; pub var anisotropy_rotation: f32; #! The textures it maps, in the order the file names them. pub var maps: vector; } #! The texture it maps for a slot, where it maps one. pub def material::map(&this, slot: map_slot): maybe<&texture_map> { for i in 0..this.maps.length { return &this.maps[i] if this.maps[i].slot == slot; } return nothing{}; } #! A material of a name, of the defaults. pub def material::named(name: strview): material { return material{ name = string::from(name, nothing{}) }; } #! Reads the materials of an MTL file's text. #! @param text The text. #! @returns Its materials, in its order, or why they couldn't be read. pub def decode_materials(text: strview): result, obj_error> { var made := vector::::new(); var lines := statements_of(text); while lines.next() { var words := words_of(lines.current); val keyword := words.next(); if keyword == "newmtl" { made.append(material::named(words.rest())); continue; } return at_line(lines.line, $"`{keyword}` comes before any `newmtl`.".span) if made.length == 0; val held := &mut made[made.length - 1]; read_statement(held, keyword, &mut words, lines.line) else (e) { return e; }; } return made; } # Reads a statement of a material. def read_statement(held: &mut material, keyword: strview, words: &mut words, line: u64): result { if keyword == "Ka" || keyword == "Kd" || keyword == "Ks" || keyword == "Ke" || keyword == "Tf" { # `spectral` and `xyz` colors are read as their numbers, where they give # three, and passed over otherwise. var color := vec3::zero; var count: u64 = 0; while count < 3 { val word := words.next(); break if word.length == 0; continue if word == "xyz"; return if word == "spectral"; val value := number_of(word) else { return at_line(line, $"`{word}` isn't a number.".span); }; if count == 0 { color = vec3{ value as f32, value as f32, value as f32 }; } else if count == 1 { color.y = value as f32; } else { color.z = value as f32; } count++; } return at_line(line, $"`{keyword}` gives no color.".span) if count == 0; if keyword == "Ka" { held.ambient = color; } else if keyword == "Kd" { held.diffuse = color; } else if keyword == "Ks" { held.specular = color; } else if keyword == "Ke" { held.emissive = color; } else { held.transmission = color; } return; } if keyword == "illum" { val word := words.next(); val value := integer_of(word) else { return at_line(line, $"`{word}` isn't an illumination model.".span); }; held.illumination = value as u32; return; } if keyword == "d" || keyword == "Tr" { var word := words.next(); if word == "-halo" { word = words.next(); } val value := number_of(word) else { return at_line(line, $"`{word}` isn't a number.".span); }; held.dissolve = keyword == "d" ? value as f32 : 1 - value as f32; return; } val numbers: strview[10] = ["Ns", "Ni", "sharpness", "Pr", "Pm", "Ps", "Pc", "Pcr", "aniso", "anisor"]; for k in 0..10 { continue if keyword != numbers[k]; val word := words.next(); val value := number_of(word) else { return at_line(line, $"`{word}` isn't a number.".span); }; val n := value as f32; if k == 0 { held.shininess = n; } else if k == 1 { held.ior = n; } else if k == 2 { held.sharpness = n; } else if k == 3 { held.roughness = n; } else if k == 4 { held.metallic = n; } else if k == 5 { held.sheen = n; } else if k == 6 { held.clearcoat = n; } else if k == 7 { held.clearcoat_roughness = n; } else if k == 8 { held.anisotropy = n; } else { held.anisotropy_rotation = n; } return; } val slot := slot_of(keyword) else { return; }; var texture := read_map(slot, words, line) else (e) { return e; }; held.maps.append(move(texture)); return; } # The slot a statement maps a texture for, where it's one that does. def slot_of(keyword: strview): maybe { for k in 0..14 { return slot_kinds[k] if keyword == slot_names[k]; } return map_slot::bump if keyword == "map_bump" || keyword == "map_Bump"; return map_slot::displacement if keyword == "map_disp"; return map_slot::reflection if keyword == "map_refl"; return map_slot::normal if keyword == "map_Kn" || keyword == "map_norm"; return nothing{}; } # Reads a texture's options, then its file, the rest of the statement. def read_map(slot: map_slot, words: &mut words, line: u64): result { var made := texture_map{ slot = slot }; while !words.ended() { val before := words.at; val option := words.next(); if option.length < 2 || option[0] != '-' || is_digit(option[1]) { words.at = before; break; } if option == "-o" || option == "-s" || option == "-t" { var v := option == "-s" ? vec3::one : vec3::zero; for k in 0..3 { val at := words.at; val word := words.next(); var value: f32 = 0; if number_of(word) is some(n) { value = n as f32; } else { words.at = at; break; } if k == 0 { v.x = value; } else if k == 1 { v.y = value; } else { v.z = value; } } if option == "-o" { made.offset = v; } else if option == "-s" { made.scale = v; } else { made.turbulence = v; } } else if option == "-bm" || option == "-boost" { val word := words.next(); val value := number_of(word) else { return at_line(line, $"`{option}` needs a number, not `{word}`.".span); }; if option == "-bm" { made.bump_multiplier = value as f32; } else { made.boost = value as f32; } } else if option == "-mm" { val base_word := words.next(); val gain_word := words.next(); val base := number_of(base_word) else { return at_line(line, "`-mm` needs a base and a gain."); }; val gain := number_of(gain_word) else { return at_line(line, "`-mm` needs a base and a gain."); }; made.base = base as f32; made.gain = gain as f32; } else if option == "-clamp" || option == "-blendu" || option == "-blendv" || option == "-cc" { val word := words.next(); return at_line(line, $"`{option}` is `on` or `off`, not `{word}`.".span) if word != "on" && word != "off"; val on := word == "on"; if option == "-clamp" { made.clamp = on; } else if option == "-blendu" { made.blend_u = on; } else if option == "-blendv" { made.blend_v = on; } else { made.color_correct = on; } } else if option == "-texres" { val word := words.next(); val value := integer_of(word) else { return at_line(line, $"`-texres` needs a whole number, not `{word}`.".span); }; made.resolution = value as u32; } else if option == "-imfchan" { val word := words.next(); return at_line(line, "`-imfchan` needs a channel.") if word.length == 0; made.channel = word[0]; } else if option == "-type" { made.kind = string::from(words.next(), nothing{}); } else { # An option of another tool's, with a value, passed over. _ = words.next(); } } val file := words.rest(); return at_line(line, "A texture map names no file.") if file.length == 0; made.file = string::from(file, nothing{}); return made; } # Whether a byte is a digit. def is_digit(c: u8): bool { return c >= '0' && c <= '9'; }