bevy_chair 0.9.1

Chair mesh asset loader plugin
Documentation
use crate::{
	atlas::*,
	vertex::*
};

use anyhow::{Context, Result};
use bevy_asset::*;
use bevy_render::{
	mesh::{Indices, Mesh},
	render_resource::PrimitiveTopology
};
use bevy_utils::BoxedFuture;
use chair::{MeshFeature, MeshReader};

#[derive(Default)]
pub struct ChairLoader;

impl AssetLoader for ChairLoader {
	fn load<'a>(&'a self, bytes: &'a [u8], load_context: &'a mut LoadContext) -> BoxedFuture<'a, Result<()>> {
		Box::pin(async move {
			let atlas = DummyAtlas;
			let reader = MeshReader::new::<FatVertex>(&atlas, Some(""));
			let (features, vertices, indices) = reader.read::<FatVertex>(bytes)
				.context("Failed to load mesh")?;

			// assemble the mesh struct depending on what features were enabled
			let mut mesh = Mesh::new(PrimitiveTopology::TriangleList);
			mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, vertices.iter()
				.map(|v| v.pos)
				.collect::<Vec<_>>());
			if features.contains(MeshFeature::Coloured) {
				mesh.insert_attribute(Mesh::ATTRIBUTE_COLOR, vertices.iter()
					.map(|v| v.col)
					.collect::<Vec<_>>());
			}
			if features.contains(MeshFeature::Normals) {
				mesh.insert_attribute(Mesh::ATTRIBUTE_NORMAL, vertices.iter()
					.map(|v| v.normals)
					.collect::<Vec<_>>());
			}
			if features.contains(MeshFeature::Textured) {
				mesh.insert_attribute(Mesh::ATTRIBUTE_UV_0, vertices.iter()
					.map(|v| v.uvs)
					.collect::<Vec<_>>());
			}

			mesh.set_indices(Some(Indices::U32(indices)));

			load_context.set_default_asset(LoadedAsset::new(mesh));

			Ok(())
		})
	}

	fn extensions(&self) -> &[&str] {
		&["chr"]
	}
}