use std::error::Error;
use glam::Vec3;
use gltf::texture::WrappingMode;
use super::deformation::{MeshDeformation, Rig};
pub use super::gltf::{
GltfImage, Ignored, ImageSource, LoadOptions, load, load_slice, load_slice_with_options,
load_with_options,
};
pub use super::images::{CompressedFormat, CompressedImage, Image};
use super::material::{AlphaMode, NormalLayer};
pub type Result<T> = std::result::Result<T, Box<dyn Error>>;
#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable)]
pub struct Vertex {
pub position: [f32; 3],
pub normal: [f32; 3],
pub uv: [f32; 2],
pub color: [f32; 4],
pub lightmap_uv: [f32; 2],
pub lightmap_bounds: [f32; 4],
pub tangent: [f32; 4],
}
#[derive(Clone)]
pub struct CpuMesh {
pub vertices: Vec<Vertex>,
pub indices: Vec<u32>,
pub material: usize,
pub deformation: MeshDeformation,
}
#[derive(Clone)]
pub struct Material {
pub name: String,
pub visibility_group: u32,
pub casts_directional_shadow: bool,
pub base: [f32; 4],
pub emissive: [f32; 3],
pub metallic: f32,
pub roughness: f32,
pub ior: f32,
pub specular: f32,
pub specular_color: [f32; 3],
pub clearcoat: f32,
pub coat_roughness: f32,
pub clearcoat_texture: Option<usize>,
pub coat_roughness_texture: Option<usize>,
pub coat_normal_texture: Option<usize>,
pub coat_normal_scale: f32,
pub iridescence: f32,
pub iridescence_ior: f32,
pub iridescence_thickness: [f32; 2],
pub iridescence_texture: Option<usize>,
pub iridescence_thickness_texture: Option<usize>,
pub sheen_color: [f32; 3],
pub sheen_roughness: f32,
pub sheen_color_texture: Option<usize>,
pub sheen_roughness_texture: Option<usize>,
pub diffuse_transmission: f32,
pub diffuse_transmission_color: [f32; 3],
pub diffuse_transmission_texture: Option<usize>,
pub diffuse_transmission_color_texture: Option<usize>,
pub anisotropy_strength: f32,
pub anisotropy_rotation: f32,
pub anisotropy_texture: Option<usize>,
pub base_texture: Option<usize>,
pub mr_texture: Option<usize>,
pub occlusion_texture: Option<usize>,
pub occlusion_strength: f32,
pub emissive_texture: Option<usize>,
pub normal_texture: Option<usize>,
pub normal_scale: f32,
pub normal_layers: Option<[NormalLayer; 2]>,
pub bump_texture: Option<usize>,
pub bump_scale: f32,
pub transmission: f32,
pub transmission_texture: Option<usize>,
pub thickness: f32,
pub thickness_texture: Option<usize>,
pub attenuation_distance: f32,
pub attenuation_color: [f32; 3],
pub dispersion: f32,
pub wrap: [WrappingMode; 2],
pub double_sided: bool,
pub unlit: bool,
pub emits_into_gi: bool,
pub alpha: AlphaMode,
}
impl Material {
pub(crate) fn packed_occlusion(&self) -> bool {
self.occlusion_texture.is_some() && self.occlusion_texture == self.mr_texture
}
pub(crate) fn texture_indices(&self) -> [Option<usize>; 18] {
let Self {
name: _,
visibility_group: _,
casts_directional_shadow: _,
base: _,
emissive: _,
metallic: _,
roughness: _,
ior: _,
specular: _,
specular_color: _,
clearcoat: _,
coat_roughness: _,
clearcoat_texture,
coat_roughness_texture,
coat_normal_texture,
coat_normal_scale: _,
iridescence: _,
iridescence_ior: _,
iridescence_thickness: _,
iridescence_texture,
iridescence_thickness_texture,
sheen_color: _,
sheen_roughness: _,
sheen_color_texture,
sheen_roughness_texture,
diffuse_transmission: _,
diffuse_transmission_color: _,
diffuse_transmission_texture,
diffuse_transmission_color_texture,
anisotropy_strength: _,
anisotropy_rotation: _,
anisotropy_texture,
base_texture,
mr_texture,
occlusion_texture,
occlusion_strength: _,
emissive_texture,
normal_texture,
normal_scale: _,
normal_layers: _,
bump_texture,
bump_scale: _,
transmission: _,
transmission_texture,
thickness: _,
thickness_texture,
attenuation_distance: _,
attenuation_color: _,
dispersion: _,
wrap: _,
double_sided: _,
unlit: _,
emits_into_gi: _,
alpha: _,
} = self;
[
*base_texture,
*mr_texture,
*occlusion_texture,
*emissive_texture,
*normal_texture,
*bump_texture,
*anisotropy_texture,
*clearcoat_texture,
*coat_roughness_texture,
*coat_normal_texture,
*iridescence_texture,
*iridescence_thickness_texture,
*transmission_texture,
*thickness_texture,
*sheen_color_texture,
*sheen_roughness_texture,
*diffuse_transmission_texture,
*diffuse_transmission_color_texture,
]
}
}
impl Default for Material {
fn default() -> Self {
Self {
name: String::new(),
visibility_group: 0,
casts_directional_shadow: true,
base: [1.0; 4],
emissive: [0.0; 3],
metallic: 1.0,
roughness: 1.0,
ior: 1.5,
specular: 1.0,
specular_color: [1.0; 3],
clearcoat: 0.0,
coat_roughness: 0.0,
clearcoat_texture: None,
coat_roughness_texture: None,
coat_normal_texture: None,
coat_normal_scale: 1.0,
iridescence: 0.0,
iridescence_ior: 1.3,
iridescence_thickness: [100.0, 400.0],
iridescence_texture: None,
iridescence_thickness_texture: None,
sheen_color: [0.0; 3],
sheen_roughness: 0.0,
sheen_color_texture: None,
sheen_roughness_texture: None,
diffuse_transmission: 0.0,
diffuse_transmission_color: [1.0; 3],
diffuse_transmission_texture: None,
diffuse_transmission_color_texture: None,
anisotropy_strength: 0.0,
anisotropy_rotation: 0.0,
anisotropy_texture: None,
base_texture: None,
mr_texture: None,
occlusion_texture: None,
occlusion_strength: 1.0,
emissive_texture: None,
normal_texture: None,
normal_scale: 1.0,
normal_layers: None,
bump_texture: None,
bump_scale: 0.0,
transmission: 0.0,
transmission_texture: None,
thickness: 0.0,
thickness_texture: None,
attenuation_distance: f32::INFINITY,
attenuation_color: [1.0; 3],
dispersion: 0.0,
wrap: [WrappingMode::Repeat; 2],
double_sided: false,
unlit: false,
emits_into_gi: true,
alpha: AlphaMode::Opaque,
}
}
}
#[derive(Clone)]
pub struct Asset {
pub meshes: Vec<CpuMesh>,
pub materials: Vec<Material>,
pub images: Vec<Image>,
pub rig: Rig,
pub ignored: Vec<Ignored>,
}
pub(crate) fn tangent_frames(vertices: &[Vertex]) -> bool {
vertices.iter().all(tangent_frame)
}
pub(crate) fn tangent_frame(vertex: &Vertex) -> bool {
tangent_off_normal(
Vec3::from_array(vertex.normal),
Vec3::from_slice(&vertex.tangent[..3]),
)
.is_some()
&& matches!(vertex.tangent[3], -1.0 | 1.0)
}
const ALONG_NORMAL_SIN_SQUARED: f32 = f32::EPSILON;
pub(crate) fn tangent_off_normal(normal: Vec3, tangent: Vec3) -> Option<Vec3> {
let normal = normal.try_normalize()?;
let tangent = tangent.try_normalize()?;
let projected = tangent - normal * normal.dot(tangent);
(projected.length_squared() > ALONG_NORMAL_SIN_SQUARED).then(|| projected.normalize())
}
pub(crate) fn valid_anisotropy(strength: f32, rotation: f32) -> bool {
(0.0..=1.0).contains(&strength) && rotation.is_finite()
}
pub fn empty() -> Asset {
Asset {
meshes: Vec::new(),
materials: Vec::new(),
images: Vec::new(),
rig: Rig::default(),
ignored: Vec::new(),
}
}