2020-10-18 20:48:15 +00:00
|
|
|
use anyhow::Result;
|
2020-12-31 20:57:15 +00:00
|
|
|
use bevy_asset::{AssetIoError, AssetLoader, AssetPath, Handle, LoadContext, LoadedAsset};
|
2021-01-01 21:30:18 +00:00
|
|
|
use bevy_core::Name;
|
2020-10-20 00:29:31 +00:00
|
|
|
use bevy_ecs::{bevy_utils::BoxedFuture, World, WorldBuilderSource};
|
2020-10-18 20:48:15 +00:00
|
|
|
use bevy_math::Mat4;
|
2020-11-16 04:32:23 +00:00
|
|
|
use bevy_pbr::prelude::{PbrBundle, StandardMaterial};
|
2020-05-15 23:55:44 +00:00
|
|
|
use bevy_render::{
|
2020-11-07 01:08:15 +00:00
|
|
|
camera::{
|
|
|
|
Camera, CameraProjection, OrthographicProjection, PerspectiveProjection, VisibleEntities,
|
|
|
|
},
|
2020-10-31 02:21:53 +00:00
|
|
|
mesh::{Indices, Mesh, VertexAttributeValues},
|
2020-07-17 01:26:21 +00:00
|
|
|
pipeline::PrimitiveTopology,
|
2020-10-18 20:48:15 +00:00
|
|
|
prelude::{Color, Texture},
|
2020-11-07 01:08:15 +00:00
|
|
|
render_graph::base,
|
2020-11-22 20:04:47 +00:00
|
|
|
texture::{
|
|
|
|
AddressMode, Extent3d, FilterMode, SamplerDescriptor, TextureDimension, TextureFormat,
|
|
|
|
},
|
2020-05-15 23:55:44 +00:00
|
|
|
};
|
2020-10-18 20:48:15 +00:00
|
|
|
use bevy_scene::Scene;
|
|
|
|
use bevy_transform::{
|
|
|
|
hierarchy::{BuildWorldChildren, WorldChildBuilder},
|
|
|
|
prelude::{GlobalTransform, Transform},
|
|
|
|
};
|
2020-10-29 21:22:45 +00:00
|
|
|
use gltf::{
|
|
|
|
mesh::Mode,
|
|
|
|
texture::{MagFilter, MinFilter, WrappingMode},
|
2020-12-09 03:36:41 +00:00
|
|
|
Material, Primitive,
|
2020-10-29 21:22:45 +00:00
|
|
|
};
|
2020-10-18 20:48:15 +00:00
|
|
|
use image::{GenericImageView, ImageFormat};
|
2020-12-31 20:57:15 +00:00
|
|
|
use std::{collections::HashMap, path::Path};
|
2020-05-15 23:55:44 +00:00
|
|
|
use thiserror::Error;
|
|
|
|
|
2020-12-31 20:57:15 +00:00
|
|
|
use crate::{Gltf, GltfNode};
|
|
|
|
|
2020-08-09 23:13:04 +00:00
|
|
|
/// An error that occurs when loading a GLTF file
|
2020-05-15 23:55:44 +00:00
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum GltfError {
|
2020-12-02 19:31:16 +00:00
|
|
|
#[error("unsupported primitive mode")]
|
2020-05-15 23:55:44 +00:00
|
|
|
UnsupportedPrimitive { mode: Mode },
|
2020-12-02 19:31:16 +00:00
|
|
|
#[error("unsupported min filter")]
|
2020-10-29 21:22:45 +00:00
|
|
|
UnsupportedMinFilter { filter: MinFilter },
|
2020-12-02 19:31:16 +00:00
|
|
|
#[error("invalid GLTF file")]
|
2020-05-15 23:55:44 +00:00
|
|
|
Gltf(#[from] gltf::Error),
|
2020-12-02 19:31:16 +00:00
|
|
|
#[error("binary blob is missing")]
|
2020-08-25 23:55:08 +00:00
|
|
|
MissingBlob,
|
2020-12-02 19:31:16 +00:00
|
|
|
#[error("failed to decode base64 mesh data")]
|
2020-08-12 17:23:54 +00:00
|
|
|
Base64Decode(#[from] base64::DecodeError),
|
2020-12-02 19:31:16 +00:00
|
|
|
#[error("unsupported buffer format")]
|
2020-08-12 17:23:54 +00:00
|
|
|
BufferFormatUnsupported,
|
2020-12-02 19:31:16 +00:00
|
|
|
#[error("invalid image mime type")]
|
2020-10-18 20:48:15 +00:00
|
|
|
InvalidImageMimeType(String),
|
2020-12-02 19:31:16 +00:00
|
|
|
#[error("failed to load an image")]
|
2020-10-18 20:48:15 +00:00
|
|
|
ImageError(#[from] image::ImageError),
|
2020-12-02 19:31:16 +00:00
|
|
|
#[error("failed to load an asset path")]
|
2020-10-18 20:48:15 +00:00
|
|
|
AssetIoError(#[from] AssetIoError),
|
2020-05-15 23:55:44 +00:00
|
|
|
}
|
|
|
|
|
2020-10-18 20:48:15 +00:00
|
|
|
/// Loads meshes from GLTF files into Mesh assets
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct GltfLoader;
|
|
|
|
|
|
|
|
impl AssetLoader for GltfLoader {
|
2020-10-20 00:29:31 +00:00
|
|
|
fn load<'a>(
|
|
|
|
&'a self,
|
|
|
|
bytes: &'a [u8],
|
|
|
|
load_context: &'a mut LoadContext,
|
|
|
|
) -> BoxedFuture<'a, Result<()>> {
|
|
|
|
Box::pin(async move { Ok(load_gltf(bytes, load_context).await?) })
|
2020-10-18 20:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn extensions(&self) -> &[&str] {
|
2020-11-17 21:40:18 +00:00
|
|
|
&["gltf", "glb"]
|
2020-05-15 23:55:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-20 00:29:31 +00:00
|
|
|
async fn load_gltf<'a, 'b>(
|
|
|
|
bytes: &'a [u8],
|
|
|
|
load_context: &'a mut LoadContext<'b>,
|
|
|
|
) -> Result<(), GltfError> {
|
2020-10-18 20:48:15 +00:00
|
|
|
let gltf = gltf::Gltf::from_slice(bytes)?;
|
2020-10-20 00:29:31 +00:00
|
|
|
let buffer_data = load_buffers(&gltf, load_context, load_context.path()).await?;
|
2020-10-18 20:48:15 +00:00
|
|
|
|
2020-12-31 20:57:15 +00:00
|
|
|
let mut materials = vec![];
|
|
|
|
let mut named_materials = HashMap::new();
|
|
|
|
for material in gltf.materials() {
|
|
|
|
let handle = load_material(&material, load_context);
|
|
|
|
if let Some(name) = material.name() {
|
|
|
|
named_materials.insert(name.to_string(), handle.clone());
|
|
|
|
}
|
|
|
|
materials.push(handle);
|
|
|
|
}
|
2020-10-18 20:48:15 +00:00
|
|
|
|
2020-12-31 20:57:15 +00:00
|
|
|
let mut meshes = vec![];
|
|
|
|
let mut named_meshes = HashMap::new();
|
2020-10-18 20:48:15 +00:00
|
|
|
for mesh in gltf.meshes() {
|
2020-12-31 20:57:15 +00:00
|
|
|
let mut primitives = vec![];
|
2020-10-18 20:48:15 +00:00
|
|
|
for primitive in mesh.primitives() {
|
|
|
|
let primitive_label = primitive_label(&mesh, &primitive);
|
2020-12-31 20:57:15 +00:00
|
|
|
let reader = primitive.reader(|buffer| Some(&buffer_data[buffer.index()]));
|
|
|
|
let primitive_topology = get_primitive_topology(primitive.mode())?;
|
2020-10-18 20:48:15 +00:00
|
|
|
|
2020-12-31 20:57:15 +00:00
|
|
|
let mut mesh = Mesh::new(primitive_topology);
|
2020-10-18 20:48:15 +00:00
|
|
|
|
2020-12-31 20:57:15 +00:00
|
|
|
if let Some(vertex_attribute) = reader
|
|
|
|
.read_positions()
|
|
|
|
.map(|v| VertexAttributeValues::Float3(v.collect()))
|
|
|
|
{
|
|
|
|
mesh.set_attribute(Mesh::ATTRIBUTE_POSITION, vertex_attribute);
|
|
|
|
}
|
2020-10-18 20:48:15 +00:00
|
|
|
|
2020-12-31 20:57:15 +00:00
|
|
|
if let Some(vertex_attribute) = reader
|
|
|
|
.read_normals()
|
|
|
|
.map(|v| VertexAttributeValues::Float3(v.collect()))
|
|
|
|
{
|
|
|
|
mesh.set_attribute(Mesh::ATTRIBUTE_NORMAL, vertex_attribute);
|
|
|
|
}
|
2020-10-18 20:48:15 +00:00
|
|
|
|
2020-12-31 20:57:15 +00:00
|
|
|
if let Some(vertex_attribute) = reader
|
|
|
|
.read_tex_coords(0)
|
|
|
|
.map(|v| VertexAttributeValues::Float2(v.into_f32().collect()))
|
|
|
|
{
|
|
|
|
mesh.set_attribute(Mesh::ATTRIBUTE_UV_0, vertex_attribute);
|
|
|
|
}
|
2020-10-18 20:48:15 +00:00
|
|
|
|
2020-12-31 20:57:15 +00:00
|
|
|
if let Some(indices) = reader.read_indices() {
|
|
|
|
mesh.set_indices(Some(Indices::U32(indices.into_u32().collect())));
|
2020-10-18 20:48:15 +00:00
|
|
|
};
|
2020-12-31 20:57:15 +00:00
|
|
|
|
|
|
|
let mesh = load_context.set_labeled_asset(&primitive_label, LoadedAsset::new(mesh));
|
|
|
|
primitives.push(super::GltfPrimitive {
|
|
|
|
mesh,
|
|
|
|
material: primitive
|
|
|
|
.material()
|
|
|
|
.index()
|
|
|
|
.and_then(|i| materials.get(i).cloned()),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
let handle = load_context.set_labeled_asset(
|
|
|
|
&mesh_label(&mesh),
|
|
|
|
LoadedAsset::new(super::GltfMesh { primitives }),
|
|
|
|
);
|
|
|
|
if let Some(name) = mesh.name() {
|
|
|
|
named_meshes.insert(name.to_string(), handle.clone());
|
|
|
|
}
|
|
|
|
meshes.push(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut nodes_intermediate = vec![];
|
|
|
|
let mut named_nodes_intermediate = HashMap::new();
|
|
|
|
for node in gltf.nodes() {
|
|
|
|
let node_label = node_label(&node);
|
|
|
|
nodes_intermediate.push((
|
|
|
|
node_label,
|
|
|
|
GltfNode {
|
|
|
|
children: vec![],
|
|
|
|
mesh: node
|
|
|
|
.mesh()
|
|
|
|
.map(|mesh| mesh.index())
|
|
|
|
.and_then(|i| meshes.get(i).cloned()),
|
|
|
|
transform: match node.transform() {
|
|
|
|
gltf::scene::Transform::Matrix { matrix } => {
|
|
|
|
Transform::from_matrix(bevy_math::Mat4::from_cols_array_2d(&matrix))
|
|
|
|
}
|
|
|
|
gltf::scene::Transform::Decomposed {
|
|
|
|
translation,
|
|
|
|
rotation,
|
|
|
|
scale,
|
|
|
|
} => Transform {
|
|
|
|
translation: bevy_math::Vec3::from(translation),
|
|
|
|
rotation: bevy_math::Quat::from(rotation),
|
|
|
|
scale: bevy_math::Vec3::from(scale),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
node.children()
|
|
|
|
.map(|child| child.index())
|
|
|
|
.collect::<Vec<_>>(),
|
|
|
|
));
|
|
|
|
if let Some(name) = node.name() {
|
|
|
|
named_nodes_intermediate.insert(name, node.index());
|
2020-05-15 23:55:44 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-31 20:57:15 +00:00
|
|
|
let nodes = resolve_node_hierarchy(nodes_intermediate)
|
|
|
|
.into_iter()
|
|
|
|
.map(|(label, node)| load_context.set_labeled_asset(&label, LoadedAsset::new(node)))
|
|
|
|
.collect::<Vec<bevy_asset::Handle<GltfNode>>>();
|
|
|
|
let named_nodes = named_nodes_intermediate
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|(name, index)| {
|
|
|
|
nodes
|
|
|
|
.get(index)
|
|
|
|
.map(|handle| (name.to_string(), handle.clone()))
|
|
|
|
})
|
|
|
|
.collect();
|
2020-05-15 23:55:44 +00:00
|
|
|
|
2020-10-18 20:48:15 +00:00
|
|
|
for texture in gltf.textures() {
|
|
|
|
if let gltf::image::Source::View { view, mime_type } = texture.source().source() {
|
|
|
|
let start = view.offset() as usize;
|
|
|
|
let end = (view.offset() + view.length()) as usize;
|
|
|
|
let buffer = &buffer_data[view.buffer().index()][start..end];
|
|
|
|
let format = match mime_type {
|
|
|
|
"image/png" => Ok(ImageFormat::Png),
|
|
|
|
"image/jpeg" => Ok(ImageFormat::Jpeg),
|
|
|
|
_ => Err(GltfError::InvalidImageMimeType(mime_type.to_string())),
|
|
|
|
}?;
|
|
|
|
let image = image::load_from_memory_with_format(buffer, format)?;
|
|
|
|
let size = image.dimensions();
|
2020-11-22 03:08:50 +00:00
|
|
|
let image = image.into_rgba8();
|
2020-05-15 23:55:44 +00:00
|
|
|
|
2020-10-18 20:48:15 +00:00
|
|
|
let texture_label = texture_label(&texture);
|
2020-12-31 20:57:15 +00:00
|
|
|
load_context.set_labeled_asset::<Texture>(
|
2020-10-18 20:48:15 +00:00
|
|
|
&texture_label,
|
|
|
|
LoadedAsset::new(Texture {
|
|
|
|
data: image.clone().into_vec(),
|
2020-11-22 20:04:47 +00:00
|
|
|
size: Extent3d::new(size.0, size.1, 1),
|
|
|
|
dimension: TextureDimension::D2,
|
2020-10-18 20:48:15 +00:00
|
|
|
format: TextureFormat::Rgba8Unorm,
|
2020-10-29 21:22:45 +00:00
|
|
|
sampler: texture_sampler(&texture)?,
|
2020-10-18 20:48:15 +00:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-08-16 07:30:04 +00:00
|
|
|
|
2020-12-31 20:57:15 +00:00
|
|
|
let mut scenes = vec![];
|
|
|
|
let mut named_scenes = HashMap::new();
|
2020-10-18 20:48:15 +00:00
|
|
|
for scene in gltf.scenes() {
|
|
|
|
let mut err = None;
|
2020-12-31 20:57:15 +00:00
|
|
|
let mut world = World::default();
|
|
|
|
let world_builder = &mut world.build();
|
2020-10-18 20:48:15 +00:00
|
|
|
world_builder
|
|
|
|
.spawn((Transform::default(), GlobalTransform::default()))
|
|
|
|
.with_children(|parent| {
|
|
|
|
for node in scene.nodes() {
|
|
|
|
let result = load_node(&node, parent, load_context, &buffer_data);
|
|
|
|
if result.is_err() {
|
|
|
|
err = Some(result);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if let Some(Err(err)) = err {
|
|
|
|
return Err(err);
|
|
|
|
}
|
2020-12-31 20:57:15 +00:00
|
|
|
let scene_handle = load_context
|
|
|
|
.set_labeled_asset(&scene_label(&scene), LoadedAsset::new(Scene::new(world)));
|
|
|
|
|
|
|
|
if let Some(name) = scene.name() {
|
|
|
|
named_scenes.insert(name.to_string(), scene_handle.clone());
|
|
|
|
}
|
|
|
|
scenes.push(scene_handle);
|
2020-10-18 20:48:15 +00:00
|
|
|
}
|
|
|
|
|
2020-12-31 20:57:15 +00:00
|
|
|
load_context.set_default_asset(LoadedAsset::new(Gltf {
|
|
|
|
default_scene: gltf
|
|
|
|
.default_scene()
|
|
|
|
.and_then(|scene| scenes.get(scene.index()))
|
|
|
|
.cloned(),
|
|
|
|
scenes,
|
|
|
|
named_scenes,
|
|
|
|
meshes,
|
|
|
|
named_meshes,
|
|
|
|
materials,
|
|
|
|
named_materials,
|
|
|
|
nodes,
|
|
|
|
named_nodes,
|
|
|
|
}));
|
2020-10-18 20:48:15 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-08-16 07:30:04 +00:00
|
|
|
|
2020-12-31 20:57:15 +00:00
|
|
|
fn load_material(material: &Material, load_context: &mut LoadContext) -> Handle<StandardMaterial> {
|
2020-12-09 03:36:41 +00:00
|
|
|
let material_label = material_label(&material);
|
|
|
|
let pbr = material.pbr_metallic_roughness();
|
|
|
|
let mut dependencies = Vec::new();
|
|
|
|
let texture_handle = if let Some(info) = pbr.base_color_texture() {
|
|
|
|
match info.texture().source().source() {
|
|
|
|
gltf::image::Source::View { .. } => {
|
|
|
|
let label = texture_label(&info.texture());
|
|
|
|
let path = AssetPath::new_ref(load_context.path(), Some(&label));
|
|
|
|
Some(load_context.get_handle(path))
|
|
|
|
}
|
|
|
|
gltf::image::Source::Uri { uri, .. } => {
|
|
|
|
let parent = load_context.path().parent().unwrap();
|
|
|
|
let image_path = parent.join(uri);
|
|
|
|
let asset_path = AssetPath::new(image_path, None);
|
|
|
|
let handle = load_context.get_handle(asset_path.clone());
|
|
|
|
dependencies.push(asset_path);
|
|
|
|
Some(handle)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let color = pbr.base_color_factor();
|
|
|
|
load_context.set_labeled_asset(
|
|
|
|
&material_label,
|
|
|
|
LoadedAsset::new(StandardMaterial {
|
|
|
|
albedo: Color::rgba(color[0], color[1], color[2], color[3]),
|
|
|
|
albedo_texture: texture_handle,
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
.with_dependencies(dependencies),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-10-18 20:48:15 +00:00
|
|
|
fn load_node(
|
2020-11-07 01:08:15 +00:00
|
|
|
gltf_node: &gltf::Node,
|
2020-10-18 20:48:15 +00:00
|
|
|
world_builder: &mut WorldChildBuilder,
|
|
|
|
load_context: &mut LoadContext,
|
|
|
|
buffer_data: &[Vec<u8>],
|
|
|
|
) -> Result<(), GltfError> {
|
2020-11-07 01:08:15 +00:00
|
|
|
let transform = gltf_node.transform();
|
2020-10-18 20:48:15 +00:00
|
|
|
let mut gltf_error = None;
|
2020-11-07 01:08:15 +00:00
|
|
|
let node = world_builder.spawn((
|
|
|
|
Transform::from_matrix(Mat4::from_cols_array_2d(&transform.matrix())),
|
|
|
|
GlobalTransform::default(),
|
|
|
|
));
|
|
|
|
|
2020-12-31 20:57:15 +00:00
|
|
|
if let Some(name) = gltf_node.name() {
|
2021-01-01 21:30:18 +00:00
|
|
|
node.with(Name::new(name.to_string()));
|
2020-12-31 20:57:15 +00:00
|
|
|
}
|
|
|
|
|
2020-11-07 01:08:15 +00:00
|
|
|
// create camera node
|
|
|
|
if let Some(camera) = gltf_node.camera() {
|
|
|
|
node.with(VisibleEntities {
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
|
|
|
|
match camera.projection() {
|
|
|
|
gltf::camera::Projection::Orthographic(orthographic) => {
|
|
|
|
let xmag = orthographic.xmag();
|
|
|
|
let ymag = orthographic.ymag();
|
2020-11-09 22:12:42 +00:00
|
|
|
let orthographic_projection: OrthographicProjection = OrthographicProjection {
|
|
|
|
left: -xmag,
|
|
|
|
right: xmag,
|
|
|
|
top: ymag,
|
|
|
|
bottom: -ymag,
|
|
|
|
far: orthographic.zfar(),
|
|
|
|
near: orthographic.znear(),
|
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
|
2020-11-07 01:08:15 +00:00
|
|
|
node.with(Camera {
|
2020-12-03 21:46:15 +00:00
|
|
|
name: Some(base::camera::CAMERA_2D.to_owned()),
|
2020-11-07 01:08:15 +00:00
|
|
|
projection_matrix: orthographic_projection.get_projection_matrix(),
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
node.with(orthographic_projection);
|
|
|
|
}
|
|
|
|
gltf::camera::Projection::Perspective(perspective) => {
|
2020-11-09 22:12:42 +00:00
|
|
|
let mut perspective_projection: PerspectiveProjection = PerspectiveProjection {
|
|
|
|
fov: perspective.yfov(),
|
|
|
|
near: perspective.znear(),
|
|
|
|
..Default::default()
|
|
|
|
};
|
2020-11-07 01:08:15 +00:00
|
|
|
if let Some(zfar) = perspective.zfar() {
|
|
|
|
perspective_projection.far = zfar;
|
|
|
|
}
|
|
|
|
if let Some(aspect_ratio) = perspective.aspect_ratio() {
|
|
|
|
perspective_projection.aspect_ratio = aspect_ratio;
|
2020-10-18 20:48:15 +00:00
|
|
|
}
|
2020-11-07 01:08:15 +00:00
|
|
|
node.with(Camera {
|
2020-12-03 21:46:15 +00:00
|
|
|
name: Some(base::camera::CAMERA_3D.to_owned()),
|
2020-11-07 01:08:15 +00:00
|
|
|
projection_matrix: perspective_projection.get_projection_matrix(),
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
node.with(perspective_projection);
|
2020-08-16 07:30:04 +00:00
|
|
|
}
|
2020-11-07 01:08:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-16 07:30:04 +00:00
|
|
|
|
2020-11-07 01:08:15 +00:00
|
|
|
node.with_children(|parent| {
|
|
|
|
if let Some(mesh) = gltf_node.mesh() {
|
|
|
|
// append primitives
|
|
|
|
for primitive in mesh.primitives() {
|
2020-12-09 03:36:41 +00:00
|
|
|
let material = primitive.material();
|
|
|
|
let material_label = material_label(&material);
|
|
|
|
|
|
|
|
// This will make sure we load the default material now since it would not have been
|
|
|
|
// added when iterating over all the gltf materials (since the default material is
|
|
|
|
// not explicitly listed in the gltf).
|
|
|
|
if !load_context.has_labeled_asset(&material_label) {
|
|
|
|
load_material(&material, load_context);
|
|
|
|
}
|
|
|
|
|
2020-11-07 01:08:15 +00:00
|
|
|
let primitive_label = primitive_label(&mesh, &primitive);
|
|
|
|
let mesh_asset_path =
|
|
|
|
AssetPath::new_ref(load_context.path(), Some(&primitive_label));
|
|
|
|
let material_asset_path =
|
|
|
|
AssetPath::new_ref(load_context.path(), Some(&material_label));
|
2020-12-09 03:36:41 +00:00
|
|
|
|
2020-11-16 04:32:23 +00:00
|
|
|
parent.spawn(PbrBundle {
|
2020-11-07 01:08:15 +00:00
|
|
|
mesh: load_context.get_handle(mesh_asset_path),
|
|
|
|
material: load_context.get_handle(material_asset_path),
|
|
|
|
..Default::default()
|
|
|
|
});
|
2020-09-25 21:29:30 +00:00
|
|
|
}
|
2020-11-07 01:08:15 +00:00
|
|
|
}
|
2020-05-15 23:55:44 +00:00
|
|
|
|
2020-11-07 01:08:15 +00:00
|
|
|
// append other nodes
|
|
|
|
for child in gltf_node.children() {
|
|
|
|
if let Err(err) = load_node(&child, parent, load_context, buffer_data) {
|
|
|
|
gltf_error = Some(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-10-18 20:48:15 +00:00
|
|
|
if let Some(err) = gltf_error {
|
|
|
|
Err(err)
|
|
|
|
} else {
|
|
|
|
Ok(())
|
2020-05-15 23:55:44 +00:00
|
|
|
}
|
2020-10-18 20:48:15 +00:00
|
|
|
}
|
2020-05-15 23:55:44 +00:00
|
|
|
|
2020-12-31 20:57:15 +00:00
|
|
|
fn mesh_label(mesh: &gltf::Mesh) -> String {
|
|
|
|
format!("Mesh{}", mesh.index())
|
|
|
|
}
|
|
|
|
|
2020-10-18 20:48:15 +00:00
|
|
|
fn primitive_label(mesh: &gltf::Mesh, primitive: &Primitive) -> String {
|
|
|
|
format!("Mesh{}/Primitive{}", mesh.index(), primitive.index())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn material_label(material: &gltf::Material) -> String {
|
|
|
|
if let Some(index) = material.index() {
|
|
|
|
format!("Material{}", index)
|
|
|
|
} else {
|
|
|
|
"MaterialDefault".to_string()
|
2020-05-15 23:55:44 +00:00
|
|
|
}
|
2020-10-18 20:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn texture_label(texture: &gltf::Texture) -> String {
|
|
|
|
format!("Texture{}", texture.index())
|
|
|
|
}
|
2020-05-15 23:55:44 +00:00
|
|
|
|
2020-12-31 20:57:15 +00:00
|
|
|
fn node_label(node: &gltf::Node) -> String {
|
|
|
|
format!("Node{}", node.index())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn scene_label(scene: &gltf::Scene) -> String {
|
|
|
|
format!("Scene{}", scene.index())
|
|
|
|
}
|
|
|
|
|
2020-10-29 21:22:45 +00:00
|
|
|
fn texture_sampler(texture: &gltf::Texture) -> Result<SamplerDescriptor, GltfError> {
|
|
|
|
let gltf_sampler = texture.sampler();
|
|
|
|
|
|
|
|
Ok(SamplerDescriptor {
|
|
|
|
address_mode_u: texture_address_mode(&gltf_sampler.wrap_s()),
|
|
|
|
address_mode_v: texture_address_mode(&gltf_sampler.wrap_t()),
|
|
|
|
|
|
|
|
mag_filter: gltf_sampler
|
|
|
|
.mag_filter()
|
|
|
|
.map(|mf| match mf {
|
|
|
|
MagFilter::Nearest => FilterMode::Nearest,
|
|
|
|
MagFilter::Linear => FilterMode::Linear,
|
|
|
|
})
|
|
|
|
.unwrap_or(SamplerDescriptor::default().mag_filter),
|
|
|
|
|
|
|
|
min_filter: gltf_sampler
|
|
|
|
.min_filter()
|
|
|
|
.map(|mf| match mf {
|
|
|
|
MinFilter::Nearest => Ok(FilterMode::Nearest),
|
|
|
|
MinFilter::Linear => Ok(FilterMode::Linear),
|
|
|
|
filter => Err(GltfError::UnsupportedMinFilter { filter }),
|
|
|
|
})
|
|
|
|
.transpose()?
|
|
|
|
.unwrap_or(SamplerDescriptor::default().min_filter),
|
|
|
|
|
|
|
|
..Default::default()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn texture_address_mode(gltf_address_mode: &gltf::texture::WrappingMode) -> AddressMode {
|
|
|
|
match gltf_address_mode {
|
|
|
|
WrappingMode::ClampToEdge => AddressMode::ClampToEdge,
|
|
|
|
WrappingMode::Repeat => AddressMode::Repeat,
|
|
|
|
WrappingMode::MirroredRepeat => AddressMode::MirrorRepeat,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-18 20:48:15 +00:00
|
|
|
fn get_primitive_topology(mode: Mode) -> Result<PrimitiveTopology, GltfError> {
|
|
|
|
match mode {
|
|
|
|
Mode::Points => Ok(PrimitiveTopology::PointList),
|
|
|
|
Mode::Lines => Ok(PrimitiveTopology::LineList),
|
|
|
|
Mode::LineStrip => Ok(PrimitiveTopology::LineStrip),
|
|
|
|
Mode::Triangles => Ok(PrimitiveTopology::TriangleList),
|
|
|
|
Mode::TriangleStrip => Ok(PrimitiveTopology::TriangleStrip),
|
|
|
|
mode => Err(GltfError::UnsupportedPrimitive { mode }),
|
|
|
|
}
|
2020-05-15 23:55:44 +00:00
|
|
|
}
|
|
|
|
|
2020-10-20 00:29:31 +00:00
|
|
|
async fn load_buffers(
|
2020-10-18 20:48:15 +00:00
|
|
|
gltf: &gltf::Gltf,
|
2020-10-20 00:29:31 +00:00
|
|
|
load_context: &LoadContext<'_>,
|
2020-10-18 20:48:15 +00:00
|
|
|
asset_path: &Path,
|
|
|
|
) -> Result<Vec<Vec<u8>>, GltfError> {
|
2020-08-12 17:23:54 +00:00
|
|
|
const OCTET_STREAM_URI: &str = "data:application/octet-stream;base64,";
|
|
|
|
|
2020-05-15 23:55:44 +00:00
|
|
|
let mut buffer_data = Vec::new();
|
2020-08-25 23:55:08 +00:00
|
|
|
for buffer in gltf.buffers() {
|
2020-05-15 23:55:44 +00:00
|
|
|
match buffer.source() {
|
2020-10-18 20:48:15 +00:00
|
|
|
gltf::buffer::Source::Uri(uri) => {
|
2020-05-15 23:55:44 +00:00
|
|
|
if uri.starts_with("data:") {
|
2020-08-12 17:23:54 +00:00
|
|
|
if uri.starts_with(OCTET_STREAM_URI) {
|
|
|
|
buffer_data.push(base64::decode(&uri[OCTET_STREAM_URI.len()..])?);
|
|
|
|
} else {
|
|
|
|
return Err(GltfError::BufferFormatUnsupported);
|
|
|
|
}
|
2020-05-15 23:55:44 +00:00
|
|
|
} else {
|
2020-10-18 20:48:15 +00:00
|
|
|
// TODO: Remove this and add dep
|
2020-05-17 03:18:30 +00:00
|
|
|
let buffer_path = asset_path.parent().unwrap().join(uri);
|
2020-10-20 00:29:31 +00:00
|
|
|
let buffer_bytes = load_context.read_asset_bytes(buffer_path).await?;
|
2020-05-15 23:55:44 +00:00
|
|
|
buffer_data.push(buffer_bytes);
|
|
|
|
}
|
|
|
|
}
|
2020-10-18 20:48:15 +00:00
|
|
|
gltf::buffer::Source::Bin => {
|
2020-08-25 23:55:08 +00:00
|
|
|
if let Some(blob) = gltf.blob.as_deref() {
|
|
|
|
buffer_data.push(blob.into());
|
|
|
|
} else {
|
|
|
|
return Err(GltfError::MissingBlob);
|
|
|
|
}
|
|
|
|
}
|
2020-05-15 23:55:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(buffer_data)
|
|
|
|
}
|
2020-12-31 20:57:15 +00:00
|
|
|
|
|
|
|
fn resolve_node_hierarchy(
|
|
|
|
nodes_intermediate: Vec<(String, GltfNode, Vec<usize>)>,
|
|
|
|
) -> Vec<(String, GltfNode)> {
|
|
|
|
let mut max_steps = nodes_intermediate.len();
|
|
|
|
let mut nodes_step = nodes_intermediate
|
|
|
|
.into_iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, (label, node, children))| (i, label, node, children))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
let mut nodes = std::collections::HashMap::<usize, (String, GltfNode)>::new();
|
|
|
|
while max_steps > 0 && !nodes_step.is_empty() {
|
|
|
|
if let Some((index, label, node, _)) = nodes_step
|
|
|
|
.iter()
|
|
|
|
.find(|(_, _, _, children)| children.is_empty())
|
|
|
|
.cloned()
|
|
|
|
{
|
|
|
|
nodes.insert(index, (label, node));
|
|
|
|
for (_, _, node, children) in nodes_step.iter_mut() {
|
|
|
|
if let Some((i, _)) = children
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.find(|(_, child_index)| **child_index == index)
|
|
|
|
{
|
|
|
|
children.remove(i);
|
|
|
|
|
|
|
|
if let Some((_, child_node)) = nodes.get(&index) {
|
|
|
|
node.children.push(child_node.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nodes_step = nodes_step
|
|
|
|
.into_iter()
|
|
|
|
.filter(|(i, _, _, _)| *i != index)
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
max_steps -= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut nodes_to_sort = nodes.into_iter().collect::<Vec<_>>();
|
|
|
|
nodes_to_sort.sort_by_key(|(i, _)| *i);
|
|
|
|
nodes_to_sort
|
|
|
|
.into_iter()
|
|
|
|
.map(|(_, resolved)| resolved)
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::resolve_node_hierarchy;
|
|
|
|
use crate::GltfNode;
|
|
|
|
|
|
|
|
impl GltfNode {
|
|
|
|
fn empty() -> Self {
|
|
|
|
GltfNode {
|
|
|
|
children: vec![],
|
|
|
|
mesh: None,
|
|
|
|
transform: bevy_transform::prelude::Transform::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn node_hierarchy_single_node() {
|
|
|
|
let result = resolve_node_hierarchy(vec![("l1".to_string(), GltfNode::empty(), vec![])]);
|
|
|
|
|
|
|
|
assert_eq!(result.len(), 1);
|
|
|
|
assert_eq!(result[0].0, "l1");
|
|
|
|
assert_eq!(result[0].1.children.len(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn node_hierarchy_no_hierarchy() {
|
|
|
|
let result = resolve_node_hierarchy(vec![
|
|
|
|
("l1".to_string(), GltfNode::empty(), vec![]),
|
|
|
|
("l2".to_string(), GltfNode::empty(), vec![]),
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert_eq!(result.len(), 2);
|
|
|
|
assert_eq!(result[0].0, "l1");
|
|
|
|
assert_eq!(result[0].1.children.len(), 0);
|
|
|
|
assert_eq!(result[1].0, "l2");
|
|
|
|
assert_eq!(result[1].1.children.len(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn node_hierarchy_simple_hierarchy() {
|
|
|
|
let result = resolve_node_hierarchy(vec![
|
|
|
|
("l1".to_string(), GltfNode::empty(), vec![1]),
|
|
|
|
("l2".to_string(), GltfNode::empty(), vec![]),
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert_eq!(result.len(), 2);
|
|
|
|
assert_eq!(result[0].0, "l1");
|
|
|
|
assert_eq!(result[0].1.children.len(), 1);
|
|
|
|
assert_eq!(result[1].0, "l2");
|
|
|
|
assert_eq!(result[1].1.children.len(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn node_hierarchy_hierarchy() {
|
|
|
|
let result = resolve_node_hierarchy(vec![
|
|
|
|
("l1".to_string(), GltfNode::empty(), vec![1]),
|
|
|
|
("l2".to_string(), GltfNode::empty(), vec![2]),
|
|
|
|
("l3".to_string(), GltfNode::empty(), vec![3, 4, 5]),
|
|
|
|
("l4".to_string(), GltfNode::empty(), vec![6]),
|
|
|
|
("l5".to_string(), GltfNode::empty(), vec![]),
|
|
|
|
("l6".to_string(), GltfNode::empty(), vec![]),
|
|
|
|
("l7".to_string(), GltfNode::empty(), vec![]),
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert_eq!(result.len(), 7);
|
|
|
|
assert_eq!(result[0].0, "l1");
|
|
|
|
assert_eq!(result[0].1.children.len(), 1);
|
|
|
|
assert_eq!(result[1].0, "l2");
|
|
|
|
assert_eq!(result[1].1.children.len(), 1);
|
|
|
|
assert_eq!(result[2].0, "l3");
|
|
|
|
assert_eq!(result[2].1.children.len(), 3);
|
|
|
|
assert_eq!(result[3].0, "l4");
|
|
|
|
assert_eq!(result[3].1.children.len(), 1);
|
|
|
|
assert_eq!(result[4].0, "l5");
|
|
|
|
assert_eq!(result[4].1.children.len(), 0);
|
|
|
|
assert_eq!(result[5].0, "l6");
|
|
|
|
assert_eq!(result[5].1.children.len(), 0);
|
|
|
|
assert_eq!(result[6].0, "l7");
|
|
|
|
assert_eq!(result[6].1.children.len(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn node_hierarchy_cyclic() {
|
|
|
|
let result = resolve_node_hierarchy(vec![
|
|
|
|
("l1".to_string(), GltfNode::empty(), vec![1]),
|
|
|
|
("l2".to_string(), GltfNode::empty(), vec![0]),
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert_eq!(result.len(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn node_hierarchy_missing_node() {
|
|
|
|
let result = resolve_node_hierarchy(vec![
|
|
|
|
("l1".to_string(), GltfNode::empty(), vec![2]),
|
|
|
|
("l2".to_string(), GltfNode::empty(), vec![]),
|
|
|
|
]);
|
|
|
|
|
|
|
|
assert_eq!(result.len(), 1);
|
|
|
|
assert_eq!(result[0].0, "l2");
|
|
|
|
assert_eq!(result[0].1.children.len(), 0);
|
|
|
|
}
|
|
|
|
}
|