2020-04-06 23:15:59 +00:00
|
|
|
use crate::Vertex;
|
2020-04-06 03:19:02 +00:00
|
|
|
use bevy_asset::Asset;
|
|
|
|
use glam::*;
|
2019-12-02 09:31:07 +00:00
|
|
|
|
|
|
|
pub enum MeshType {
|
|
|
|
Cube,
|
2020-04-04 19:43:16 +00:00
|
|
|
Plane { size: f32 },
|
|
|
|
Quad { size: Vec2 },
|
2019-12-02 09:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Mesh {
|
|
|
|
pub vertices: Vec<Vertex>,
|
|
|
|
pub indices: Vec<u16>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Asset<MeshType> for Mesh {
|
|
|
|
fn load(descriptor: MeshType) -> Self {
|
|
|
|
let (vertices, indices) = match descriptor {
|
|
|
|
MeshType::Cube => create_cube(),
|
|
|
|
MeshType::Plane { size } => create_plane(size),
|
2020-04-04 19:43:16 +00:00
|
|
|
MeshType::Quad { size } => create_quad(size),
|
2019-12-02 09:31:07 +00:00
|
|
|
};
|
|
|
|
|
2020-03-10 07:53:07 +00:00
|
|
|
Mesh { vertices, indices }
|
2019-12-02 09:31:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 22:44:29 +00:00
|
|
|
pub fn create_quad_from_vertices(
|
2020-01-11 10:11:27 +00:00
|
|
|
north_west: Vec2,
|
|
|
|
north_east: Vec2,
|
|
|
|
south_west: Vec2,
|
|
|
|
south_east: Vec2,
|
|
|
|
) -> (Vec<Vertex>, Vec<u16>) {
|
2020-01-09 03:17:11 +00:00
|
|
|
let vertex_data = [
|
2020-01-15 06:23:00 +00:00
|
|
|
Vertex::from((
|
|
|
|
[south_west.x(), south_west.y(), 0.0],
|
|
|
|
[0.0, 0.0, 1.0],
|
2020-03-30 22:44:29 +00:00
|
|
|
[0.0, 1.0],
|
2020-01-15 06:23:00 +00:00
|
|
|
)),
|
|
|
|
Vertex::from((
|
|
|
|
[north_west.x(), north_west.y(), 0.0],
|
|
|
|
[0.0, 0.0, 1.0],
|
2020-03-30 22:44:29 +00:00
|
|
|
[0.0, 0.0],
|
2020-01-15 06:23:00 +00:00
|
|
|
)),
|
|
|
|
Vertex::from((
|
|
|
|
[north_east.x(), north_east.y(), 0.0],
|
|
|
|
[0.0, 0.0, 1.0],
|
2020-03-30 22:44:29 +00:00
|
|
|
[1.0, 0.0],
|
2020-01-15 06:23:00 +00:00
|
|
|
)),
|
|
|
|
Vertex::from((
|
|
|
|
[south_east.x(), south_east.y(), 0.0],
|
|
|
|
[0.0, 0.0, 1.0],
|
2020-03-30 22:44:29 +00:00
|
|
|
[1.0, 1.0],
|
2020-01-15 06:23:00 +00:00
|
|
|
)),
|
2020-01-09 03:17:11 +00:00
|
|
|
];
|
|
|
|
|
2020-01-11 19:51:46 +00:00
|
|
|
let index_data: &[u16] = &[0, 2, 1, 0, 3, 2];
|
2020-01-09 03:17:11 +00:00
|
|
|
return (vertex_data.to_vec(), index_data.to_vec());
|
|
|
|
}
|
|
|
|
|
2020-03-30 22:44:29 +00:00
|
|
|
pub fn create_quad(dimensions: Vec2) -> (Vec<Vertex>, Vec<u16>) {
|
|
|
|
let extent_x = dimensions.x() / 2.0;
|
|
|
|
let extent_y = dimensions.y() / 2.0;
|
|
|
|
create_quad_from_vertices(
|
|
|
|
vec2(-extent_x, extent_y),
|
|
|
|
vec2(extent_x, extent_y),
|
|
|
|
vec2(-extent_x, -extent_y),
|
|
|
|
vec2(extent_x, -extent_y),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-12-02 09:31:07 +00:00
|
|
|
pub fn create_cube() -> (Vec<Vertex>, Vec<u16>) {
|
|
|
|
let vertex_data = [
|
|
|
|
// top (0, 0, 1)
|
2020-01-14 01:35:30 +00:00
|
|
|
Vertex::from(([-1, -1, 1], [0, 0, 1], [0, 0])),
|
|
|
|
Vertex::from(([1, -1, 1], [0, 0, 1], [1, 0])),
|
|
|
|
Vertex::from(([1, 1, 1], [0, 0, 1], [1, 1])),
|
|
|
|
Vertex::from(([-1, 1, 1], [0, 0, 1], [0, 1])),
|
2019-12-02 09:31:07 +00:00
|
|
|
// bottom (0, 0, -1)
|
2020-01-14 01:35:30 +00:00
|
|
|
Vertex::from(([-1, 1, -1], [0, 0, -1], [1, 0])),
|
|
|
|
Vertex::from(([1, 1, -1], [0, 0, -1], [0, 0])),
|
|
|
|
Vertex::from(([1, -1, -1], [0, 0, -1], [0, 1])),
|
|
|
|
Vertex::from(([-1, -1, -1], [0, 0, -1], [1, 1])),
|
2019-12-02 09:31:07 +00:00
|
|
|
// right (1, 0, 0)
|
2020-01-14 01:35:30 +00:00
|
|
|
Vertex::from(([1, -1, -1], [1, 0, 0], [0, 0])),
|
|
|
|
Vertex::from(([1, 1, -1], [1, 0, 0], [1, 0])),
|
|
|
|
Vertex::from(([1, 1, 1], [1, 0, 0], [1, 1])),
|
|
|
|
Vertex::from(([1, -1, 1], [1, 0, 0], [0, 1])),
|
2019-12-02 09:31:07 +00:00
|
|
|
// left (-1, 0, 0)
|
2020-01-14 01:35:30 +00:00
|
|
|
Vertex::from(([-1, -1, 1], [-1, 0, 0], [1, 0])),
|
|
|
|
Vertex::from(([-1, 1, 1], [-1, 0, 0], [0, 0])),
|
|
|
|
Vertex::from(([-1, 1, -1], [-1, 0, 0], [0, 1])),
|
|
|
|
Vertex::from(([-1, -1, -1], [-1, 0, 0], [1, 1])),
|
2019-12-02 09:31:07 +00:00
|
|
|
// front (0, 1, 0)
|
2020-01-14 01:35:30 +00:00
|
|
|
Vertex::from(([1, 1, -1], [0, 1, 0], [1, 0])),
|
|
|
|
Vertex::from(([-1, 1, -1], [0, 1, 0], [0, 0])),
|
|
|
|
Vertex::from(([-1, 1, 1], [0, 1, 0], [0, 1])),
|
|
|
|
Vertex::from(([1, 1, 1], [0, 1, 0], [1, 1])),
|
2019-12-02 09:31:07 +00:00
|
|
|
// back (0, -1, 0)
|
2020-01-14 01:35:30 +00:00
|
|
|
Vertex::from(([1, -1, 1], [0, -1, 0], [0, 0])),
|
|
|
|
Vertex::from(([-1, -1, 1], [0, -1, 0], [1, 0])),
|
|
|
|
Vertex::from(([-1, -1, -1], [0, -1, 0], [1, 1])),
|
|
|
|
Vertex::from(([1, -1, -1], [0, -1, 0], [0, 1])),
|
2019-12-02 09:31:07 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let index_data: &[u16] = &[
|
|
|
|
0, 1, 2, 2, 3, 0, // top
|
|
|
|
4, 5, 6, 6, 7, 4, // bottom
|
|
|
|
8, 9, 10, 10, 11, 8, // right
|
|
|
|
12, 13, 14, 14, 15, 12, // left
|
|
|
|
16, 17, 18, 18, 19, 16, // front
|
|
|
|
20, 21, 22, 22, 23, 20, // back
|
|
|
|
];
|
|
|
|
|
|
|
|
(vertex_data.to_vec(), index_data.to_vec())
|
|
|
|
}
|
|
|
|
|
2019-12-10 07:12:50 +00:00
|
|
|
pub fn create_plane(size: f32) -> (Vec<Vertex>, Vec<u16>) {
|
2020-03-30 22:44:29 +00:00
|
|
|
create_quad(vec2(size, size))
|
2020-01-11 10:11:27 +00:00
|
|
|
}
|