use crate::{Hex, HexLayout};
use glam::{Vec2, Vec3};
#[derive(Debug, Clone)]
pub struct MeshInfo<const LEN: usize> {
pub vertices: [[f32; 3]; LEN],
pub normals: [[f32; 3]; LEN],
pub uvs: [[f32; 2]; LEN],
pub indices: Vec<u16>,
}
impl MeshInfo<7> {
#[must_use]
pub fn hexagonal_plane(layout: &HexLayout, hex: Hex) -> Self {
let center = layout.hex_to_world_pos(hex);
let center = [center.x, 0., center.y];
let corners = layout.hex_corners(hex);
let corners_arr = corners.map(|p| [p.x, 0., p.y]);
let uv_delta = Vec2::splat(0.5);
Self {
vertices: [
center,
corners_arr[0],
corners_arr[1],
corners_arr[2],
corners_arr[3],
corners_arr[4],
corners_arr[5],
],
uvs: [
uv_delta.to_array(),
(corners[0] + uv_delta).to_array(),
(corners[1] + uv_delta).to_array(),
(corners[2] + uv_delta).to_array(),
(corners[3] + uv_delta).to_array(),
(corners[4] + uv_delta).to_array(),
(corners[5] + uv_delta).to_array(),
],
normals: [[0., 1., 0.]; 7],
indices: vec![
1, 0, 2, 2, 0, 3, 3, 0, 4, 4, 0, 5, 5, 0, 6, 6, 0, 1, ],
}
}
}
impl MeshInfo<31> {
#[must_use]
#[allow(clippy::too_many_lines)]
pub fn partial_hexagonal_column(layout: &HexLayout, hex: Hex, column_height: f32) -> Self {
let center = layout.hex_to_world_pos(hex);
let center_top = Vec3::new(center.x, column_height, center.y);
let corners = layout.hex_corners(hex);
let (top_corners, bot_corners) = (
corners.map(|p| Vec3::new(p.x, column_height, p.y)),
corners.map(|p| Vec3::new(p.x, 0., p.y)),
);
let quad_normals = [
((top_corners[0] - center_top) + (top_corners[1] - center_top)).to_array(),
((top_corners[1] - center_top) + (top_corners[2] - center_top)).to_array(),
((top_corners[2] - center_top) + (top_corners[3] - center_top)).to_array(),
((top_corners[3] - center_top) + (top_corners[4] - center_top)).to_array(),
((top_corners[4] - center_top) + (top_corners[5] - center_top)).to_array(),
((top_corners[5] - center_top) + (top_corners[0] - center_top)).to_array(),
];
let indices = vec![
1, 0, 2, 2, 0, 3, 3, 0, 4, 4, 0, 5, 5, 0, 6, 6, 0, 1, 7, 8, 10, 10, 9, 7, 11, 12, 14, 14, 13, 11, 15, 16, 18, 18, 17, 15, 19, 20, 22, 22, 21, 19, 23, 24, 26, 26, 25, 23, 27, 28, 30, 30, 29, 27, ];
let uv_delta = Vec2::splat(0.5);
Self {
vertices: [
center_top.to_array(), top_corners[0].to_array(), top_corners[1].to_array(), top_corners[2].to_array(), top_corners[3].to_array(), top_corners[4].to_array(), top_corners[5].to_array(), top_corners[0].to_array(), top_corners[1].to_array(), bot_corners[0].to_array(), bot_corners[1].to_array(), top_corners[1].to_array(), top_corners[2].to_array(), bot_corners[1].to_array(), bot_corners[2].to_array(), top_corners[2].to_array(), top_corners[3].to_array(), bot_corners[2].to_array(), bot_corners[3].to_array(), top_corners[3].to_array(), top_corners[4].to_array(), bot_corners[3].to_array(), bot_corners[4].to_array(), top_corners[4].to_array(), top_corners[5].to_array(), bot_corners[4].to_array(), bot_corners[5].to_array(), top_corners[5].to_array(), top_corners[0].to_array(), bot_corners[5].to_array(), bot_corners[0].to_array(), ],
uvs: [
uv_delta.to_array(),
(corners[0] + uv_delta).to_array(),
(corners[1] + uv_delta).to_array(),
(corners[2] + uv_delta).to_array(),
(corners[3] + uv_delta).to_array(),
(corners[4] + uv_delta).to_array(),
(corners[5] + uv_delta).to_array(),
[0., column_height],
[1., column_height],
[0., 1.],
[1., 1.],
[0., column_height],
[1., column_height],
[0., 1.],
[1., 1.],
[0., column_height],
[1., column_height],
[0., 1.],
[1., 1.],
[0., column_height],
[1., column_height],
[0., 1.],
[1., 1.],
[0., column_height],
[1., column_height],
[0., 1.],
[1., 1.],
[0., column_height],
[1., column_height],
[0., 1.],
[1., 1.],
],
normals: [
[0., 1., 0.],
[0., 1., 0.],
[0., 1., 0.],
[0., 1., 0.],
[0., 1., 0.],
[0., 1., 0.],
[0., 1., 0.],
quad_normals[0],
quad_normals[0],
quad_normals[0],
quad_normals[0],
quad_normals[1],
quad_normals[1],
quad_normals[1],
quad_normals[1],
quad_normals[2],
quad_normals[2],
quad_normals[2],
quad_normals[2],
quad_normals[3],
quad_normals[3],
quad_normals[3],
quad_normals[3],
quad_normals[4],
quad_normals[4],
quad_normals[4],
quad_normals[4],
quad_normals[5],
quad_normals[5],
quad_normals[5],
quad_normals[5],
],
indices,
}
}
}
impl MeshInfo<13> {
#[must_use]
pub fn cheap_hexagonal_column(layout: &HexLayout, hex: Hex, column_height: f32) -> Self {
let center = layout.hex_to_world_pos(hex);
let center_top = Vec3::new(center.x, column_height, center.y);
let corners = layout.hex_corners(hex);
let (top_corners, bot_corners) = (
corners.map(|p| Vec3::new(p.x, column_height, p.y)),
corners.map(|p| Vec3::new(p.x, 0., p.y)),
);
let quad_normals = [
(top_corners[0] - center_top).to_array(),
(top_corners[1] - center_top).to_array(),
(top_corners[2] - center_top).to_array(),
(top_corners[3] - center_top).to_array(),
(top_corners[4] - center_top).to_array(),
(top_corners[5] - center_top).to_array(),
];
let vertices = [
center_top.to_array(), top_corners[0].to_array(), top_corners[1].to_array(), top_corners[2].to_array(), top_corners[3].to_array(), top_corners[4].to_array(), top_corners[5].to_array(), bot_corners[0].to_array(), bot_corners[1].to_array(), bot_corners[2].to_array(), bot_corners[3].to_array(), bot_corners[4].to_array(), bot_corners[5].to_array(), ];
let indices = vec![
1, 0, 2, 2, 0, 3, 3, 0, 4, 4, 0, 5, 5, 0, 6, 6, 0, 1, 1, 2, 8, 8, 7, 1, 2, 3, 9, 9, 8, 2, 3, 4, 10, 10, 9, 3, 4, 5, 11, 11, 10, 4, 5, 6, 12, 12, 11, 5, 6, 1, 7, 7, 12, 6, ];
Self {
vertices,
normals: [
[0., 1., 0.],
quad_normals[0],
quad_normals[1],
quad_normals[2],
quad_normals[3],
quad_normals[4],
quad_normals[5],
quad_normals[0],
quad_normals[1],
quad_normals[2],
quad_normals[3],
quad_normals[4],
quad_normals[5],
],
uvs: [[0., 1.]; 13], indices,
}
}
}
impl MeshInfo<38> {
#[must_use]
#[allow(clippy::too_many_lines)]
pub fn hexagonal_column(layout: &HexLayout, hex: Hex, column_height: f32) -> Self {
let center = layout.hex_to_world_pos(hex);
let center_top = Vec3::new(center.x, column_height, center.y);
let center_bot = Vec3::new(center.x, 0., center.y);
let corners = layout.hex_corners(hex);
let (top_corners, bot_corners) = (
corners.map(|p| Vec3::new(p.x, column_height, p.y)),
corners.map(|p| Vec3::new(p.x, 0., p.y)),
);
let quad_normals = [
((top_corners[0] - center_top) + (top_corners[1] - center_top)).to_array(),
((top_corners[1] - center_top) + (top_corners[2] - center_top)).to_array(),
((top_corners[2] - center_top) + (top_corners[3] - center_top)).to_array(),
((top_corners[3] - center_top) + (top_corners[4] - center_top)).to_array(),
((top_corners[4] - center_top) + (top_corners[5] - center_top)).to_array(),
((top_corners[5] - center_top) + (top_corners[0] - center_top)).to_array(),
];
let indices = vec![
1, 0, 2, 2, 0, 3, 3, 0, 4, 4, 0, 5, 5, 0, 6, 6, 0, 1, 7, 8, 10, 10, 9, 7, 11, 12, 14, 14, 13, 11, 15, 16, 18, 18, 17, 15, 19, 20, 22, 22, 21, 19, 23, 24, 26, 26, 25, 23, 27, 28, 30, 30, 29, 27, 31, 32, 33, 31, 33, 34, 31, 34, 35, 31, 35, 36, 31, 36, 37, 31, 37, 32, ];
let uv_delta = Vec2::splat(0.5);
Self {
vertices: [
center_top.to_array(), top_corners[0].to_array(), top_corners[1].to_array(), top_corners[2].to_array(), top_corners[3].to_array(), top_corners[4].to_array(), top_corners[5].to_array(), top_corners[0].to_array(), top_corners[1].to_array(), bot_corners[0].to_array(), bot_corners[1].to_array(), top_corners[1].to_array(), top_corners[2].to_array(), bot_corners[1].to_array(), bot_corners[2].to_array(), top_corners[2].to_array(), top_corners[3].to_array(), bot_corners[2].to_array(), bot_corners[3].to_array(), top_corners[3].to_array(), top_corners[4].to_array(), bot_corners[3].to_array(), bot_corners[4].to_array(), top_corners[4].to_array(), top_corners[5].to_array(), bot_corners[4].to_array(), bot_corners[5].to_array(), top_corners[5].to_array(), top_corners[0].to_array(), bot_corners[5].to_array(), bot_corners[0].to_array(), center_bot.to_array(), bot_corners[0].to_array(), bot_corners[1].to_array(), bot_corners[2].to_array(), bot_corners[3].to_array(), bot_corners[4].to_array(), bot_corners[5].to_array(), ],
uvs: [
uv_delta.to_array(),
(corners[0] + uv_delta).to_array(),
(corners[1] + uv_delta).to_array(),
(corners[2] + uv_delta).to_array(),
(corners[3] + uv_delta).to_array(),
(corners[4] + uv_delta).to_array(),
(corners[5] + uv_delta).to_array(),
[0., column_height],
[1., column_height],
[0., 1.],
[1., 1.],
[0., column_height],
[1., column_height],
[0., 1.],
[1., 1.],
[0., column_height],
[1., column_height],
[0., 1.],
[1., 1.],
[0., column_height],
[1., column_height],
[0., 1.],
[1., 1.],
[0., column_height],
[1., column_height],
[0., 1.],
[1., 1.],
[0., column_height],
[1., column_height],
[0., 1.],
[1., 1.],
uv_delta.to_array(),
(corners[0] + uv_delta).to_array(),
(corners[1] + uv_delta).to_array(),
(corners[2] + uv_delta).to_array(),
(corners[3] + uv_delta).to_array(),
(corners[4] + uv_delta).to_array(),
(corners[5] + uv_delta).to_array(),
],
normals: [
[0., 1., 0.],
[0., 1., 0.],
[0., 1., 0.],
[0., 1., 0.],
[0., 1., 0.],
[0., 1., 0.],
[0., 1., 0.],
quad_normals[0],
quad_normals[0],
quad_normals[0],
quad_normals[0],
quad_normals[1],
quad_normals[1],
quad_normals[1],
quad_normals[1],
quad_normals[2],
quad_normals[2],
quad_normals[2],
quad_normals[2],
quad_normals[3],
quad_normals[3],
quad_normals[3],
quad_normals[3],
quad_normals[4],
quad_normals[4],
quad_normals[4],
quad_normals[4],
quad_normals[5],
quad_normals[5],
quad_normals[5],
quad_normals[5],
[0., -1., 0.],
[0., -1., 0.],
[0., -1., 0.],
[0., -1., 0.],
[0., -1., 0.],
[0., -1., 0.],
[0., -1., 0.],
],
indices,
}
}
}