nightshade-api 0.44.0

Procedural high level API for the nightshade game engine
Documentation
//! 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))
}