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
//! Custom meshes from raw vertex data, for procedural geometry the primitive
//! spawns do not cover. Build vertices and triangle indices, register them, and
//! spawn an entity that renders them.
use nightshade::ecs::mesh::commands::spawn_mesh;
use nightshade::ecs::mesh::components::{Mesh, Vertex};
use nightshade::ecs::prefab::resources::mesh_cache_insert;
use nightshade::prelude::*;
/// Registers a mesh under `name` from raw geometry and spawns an entity
/// rendering it at `position`. Each vertex is a (position, normal, uv) triple,
/// and `indices` lists triangle corners three at a time. Reuse the same `name`
/// to spawn more entities sharing the geometry.
pub fn spawn_custom_mesh(
world: &mut World,
name: &str,
vertices: &[([f32; 3], [f32; 3], [f32; 2])],
indices: &[u32],
position: Vec3,
) -> Entity {
let built: Vec<Vertex> = vertices
.iter()
.map(|(position, normal, uv)| {
Vertex::with_tex_coords(
Vec3::new(position[0], position[1], position[2]),
Vec3::new(normal[0], normal[1], normal[2]),
*uv,
)
})
.collect();
let mesh = Mesh::new(built, indices.to_vec());
mesh_cache_insert(
&mut world.resources.assets.mesh_cache,
name.to_string(),
mesh,
);
spawn_mesh(world, name, position, Vec3::new(1.0, 1.0, 1.0))
}