use std::error::Error;
use glam::Vec3;
use gltf::texture::WrappingMode;
use super::deformation::{MeshDeformation, Rig};
pub use super::gltf::{
GltfImage, 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 clearcoat: f32,
pub coat_roughness: f32,
pub anisotropy_strength: f32,
pub anisotropy_rotation: f32,
pub anisotropy_texture: Option<usize>,
pub base_texture: Option<usize>,
pub mr_texture: Option<usize>,
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 wrap: [WrappingMode; 2],
pub double_sided: bool,
pub unlit: bool,
pub emits_into_gi: bool,
pub alpha: AlphaMode,
}
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,
clearcoat: 0.0,
coat_roughness: 0.0,
anisotropy_strength: 0.0,
anisotropy_rotation: 0.0,
anisotropy_texture: None,
base_texture: None,
mr_texture: None,
emissive_texture: None,
normal_texture: None,
normal_scale: 1.0,
normal_layers: None,
bump_texture: None,
bump_scale: 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(crate) fn tangent_frames(vertices: &[Vertex]) -> bool {
vertices.iter().all(tangent_frame)
}
pub(crate) fn tangent_frame(vertex: &Vertex) -> bool {
let n = Vec3::from_array(vertex.normal);
let t = Vec3::from_slice(&vertex.tangent[..3]);
n.try_normalize().is_some()
&& t.try_normalize().is_some()
&& n.cross(t).try_normalize().is_some()
&& matches!(vertex.tangent[3], -1.0 | 1.0)
}
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(),
}
}