1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
#![deny(missing_docs)]
//! This crate is intended to load [glTF 2.0](https://www.khronos.org/gltf), a
//! file format designed for the efficient transmission of 3D assets.
//!
//! It's base on [gltf](https://github.com/gltf-rs/gltf) crate but has an easy to use output.
//!
//! # Installation
//!
//! ```toml
//! [dependencies]
//! easy-gltf="1.1.2"
//! ```
//!
//! # Example
//!
//! ```
//! let scenes = easy_gltf::load("tests/cube.glb").expect("Failed to load glTF");
//! for scene in scenes {
//! println!(
//! "Cameras: #{} Lights: #{} Models: #{}",
//! scene.cameras.len(),
//! scene.lights.len(),
//! scene.models.len()
//! )
//! }
//! ```
mod scene;
mod utils;
use std::error::Error;
use std::path::Path;
use utils::GltfData;
pub use scene::*;
/// Load scenes from path to a glTF 2.0.
///
/// Note: You can use this function with either a `Gltf` (standard `glTF`) or `Glb` (binary glTF).
///
/// # Example
///
/// ```
/// let scenes = easy_gltf::load("tests/cube.glb").expect("Failed to load glTF");
/// println!("Scenes: #{}", scenes.len()); // Output "Scenes: #1"
/// let scene = &scenes[0]; // Retrieve the first and only scene
/// println!("Cameras: #{}", scene.cameras.len());
/// println!("Lights: #{}", scene.lights.len());
/// println!("Models: #{}", scene.models.len());
/// ```
pub fn load<P>(path: P) -> Result<Vec<Scene>, Box<dyn Error + Send + Sync>>
where
P: AsRef<Path>,
{
// Run gltf
let (doc, buffers, images) = gltf::import(&path)?;
// Init data and collection useful for conversion
let mut data = GltfData::new(buffers, images, &path);
// Convert gltf -> easy_gltf
let mut res = vec![];
for scene in doc.scenes() {
res.push(Scene::load(scene, &mut data));
}
Ok(res)
}
#[cfg(test)]
mod tests {
use crate::model::Mode;
use crate::*;
use cgmath::*;
macro_rules! assert_delta {
($x:expr, $y:expr, $d:expr) => {
if !($x - $y < $d || $y - $x < $d) {
panic!();
}
};
}
#[test]
fn check_cube_glb() {
let scenes = load("tests/cube.glb").unwrap();
assert_eq!(scenes.len(), 1);
let scene = &scenes[0];
assert_eq!(scene.cameras.len(), 1);
assert_eq!(scene.lights.len(), 3);
assert_eq!(scene.models.len(), 1);
}
#[test]
fn check_different_meshes() {
let scenes = load("tests/complete.glb").unwrap();
assert_eq!(scenes.len(), 1);
let scene = &scenes[0];
for model in scene.models.iter() {
match model.mode() {
Mode::Triangles | Mode::TriangleFan | Mode::TriangleStrip => {
assert!(model.triangles().is_ok());
}
Mode::Lines | Mode::LineLoop | Mode::LineStrip => {
assert!(model.lines().is_ok());
}
Mode::Points => {
assert!(model.points().is_ok());
}
}
}
}
#[test]
fn check_cube_gltf() {
let _ = load("tests/cube_classic.gltf").unwrap();
}
#[test]
fn check_default_texture() {
let _ = load("tests/box_sparse.glb").unwrap();
}
#[test]
fn check_camera() {
let scenes = load("tests/cube.glb").unwrap();
let scene = &scenes[0];
let cam = &scene.cameras[0];
assert!((cam.position() - Vector3::new(7.3589, 4.9583, 6.9258)).magnitude() < 0.1);
}
#[test]
fn check_lights() {
let scenes = load("tests/cube.glb").unwrap();
let scene = &scenes[0];
for light in scene.lights.iter() {
match light {
Light::Directional {
direction,
color: _,
intensity,
..
} => {
assert!((direction - Vector3::new(0.6068, -0.7568, -0.2427)).magnitude() < 0.1);
assert_delta!(intensity, 542., 0.01);
}
Light::Point {
position,
color: _,
intensity,
..
} => {
assert!((position - Vector3::new(4.0762, 5.9039, -1.0055)).magnitude() < 0.1);
assert_delta!(intensity, 1000., 0.01);
}
Light::Spot {
position,
direction,
color: _,
intensity,
inner_cone_angle: _,
outer_cone_angle,
..
} => {
assert!((position - Vector3::new(4.337, 15.541, -8.106)).magnitude() < 0.1);
assert!(
(direction - Vector3::new(-0.0959, -0.98623, 0.1346)).magnitude() < 0.1
);
assert_delta!(intensity, 42., 0.01);
assert_delta!(outer_cone_angle, 40., 0.01);
}
}
}
}
#[test]
fn check_model() {
let scenes = load("tests/cube.glb").unwrap();
let scene = &scenes[0];
let model = &scene.models[0];
assert!(model.has_normals());
assert!(model.has_tex_coords());
assert!(model.has_tangents());
for t in model.triangles().unwrap().iter().flatten() {
let pos = t.position;
assert!(pos.x > -0.01 && pos.x < 1.01);
assert!(pos.y > -0.01 && pos.y < 1.01);
assert!(pos.z > -0.01 && pos.z < 1.01);
// Check that the tangent w component is 1 or -1
assert_eq!(t.tangent.w.abs(), 1.);
}
}
#[test]
fn check_material() {
let scenes = load("tests/head.glb").unwrap();
let scene = &scenes[0];
let mat = &scene.models[0].material;
assert!(mat.pbr.base_color_texture.is_some());
assert_eq!(mat.pbr.metallic_factor, 0.);
}
#[test]
fn check_invalid_path() {
assert!(load("tests/invalid.glb").is_err());
}
}