use super::{Aabb, Geometry};
use crate::context::WgpuContext;
use crate::core::buffer::{IndexBuffer, VertexBuffer};
use crate::core::vertex::VertexPC;
use glam::Vec3;
pub struct Axes {
vertex_buffer: VertexBuffer,
vertex_count: u32,
size: f32,
}
impl Axes {
pub const X_COLOR: [f32; 4] = [1.0, 0.0, 0.0, 1.0];
pub const Y_COLOR: [f32; 4] = [0.0, 1.0, 0.0, 1.0];
pub const Z_COLOR: [f32; 4] = [0.0, 0.0, 1.0, 1.0];
pub fn new(ctx: &WgpuContext, size: f32) -> Self {
let origin = [0.0, 0.0, 0.0];
let vertices = vec![
VertexPC::new(origin, Self::X_COLOR),
VertexPC::new([size, 0.0, 0.0], Self::X_COLOR),
VertexPC::new(origin, Self::Y_COLOR),
VertexPC::new([0.0, size, 0.0], Self::Y_COLOR),
VertexPC::new(origin, Self::Z_COLOR),
VertexPC::new([0.0, 0.0, size], Self::Z_COLOR),
];
let vertex_buffer = VertexBuffer::new(ctx, &vertices, Some("axes"));
Self {
vertex_buffer,
vertex_count: 6,
size,
}
}
pub fn with_arrows(ctx: &WgpuContext, size: f32, arrow_size: f32) -> Self {
let origin = [0.0, 0.0, 0.0];
let arrow_ratio = arrow_size / size;
let vertices = vec![
VertexPC::new(origin, Self::X_COLOR),
VertexPC::new([size, 0.0, 0.0], Self::X_COLOR),
VertexPC::new([size, 0.0, 0.0], Self::X_COLOR),
VertexPC::new(
[size - arrow_size, arrow_ratio * size * 0.3, 0.0],
Self::X_COLOR,
),
VertexPC::new([size, 0.0, 0.0], Self::X_COLOR),
VertexPC::new(
[size - arrow_size, -arrow_ratio * size * 0.3, 0.0],
Self::X_COLOR,
),
VertexPC::new([size, 0.0, 0.0], Self::X_COLOR),
VertexPC::new(
[size - arrow_size, 0.0, arrow_ratio * size * 0.3],
Self::X_COLOR,
),
VertexPC::new([size, 0.0, 0.0], Self::X_COLOR),
VertexPC::new(
[size - arrow_size, 0.0, -arrow_ratio * size * 0.3],
Self::X_COLOR,
),
VertexPC::new(origin, Self::Y_COLOR),
VertexPC::new([0.0, size, 0.0], Self::Y_COLOR),
VertexPC::new([0.0, size, 0.0], Self::Y_COLOR),
VertexPC::new(
[arrow_ratio * size * 0.3, size - arrow_size, 0.0],
Self::Y_COLOR,
),
VertexPC::new([0.0, size, 0.0], Self::Y_COLOR),
VertexPC::new(
[-arrow_ratio * size * 0.3, size - arrow_size, 0.0],
Self::Y_COLOR,
),
VertexPC::new([0.0, size, 0.0], Self::Y_COLOR),
VertexPC::new(
[0.0, size - arrow_size, arrow_ratio * size * 0.3],
Self::Y_COLOR,
),
VertexPC::new([0.0, size, 0.0], Self::Y_COLOR),
VertexPC::new(
[0.0, size - arrow_size, -arrow_ratio * size * 0.3],
Self::Y_COLOR,
),
VertexPC::new(origin, Self::Z_COLOR),
VertexPC::new([0.0, 0.0, size], Self::Z_COLOR),
VertexPC::new([0.0, 0.0, size], Self::Z_COLOR),
VertexPC::new(
[arrow_ratio * size * 0.3, 0.0, size - arrow_size],
Self::Z_COLOR,
),
VertexPC::new([0.0, 0.0, size], Self::Z_COLOR),
VertexPC::new(
[-arrow_ratio * size * 0.3, 0.0, size - arrow_size],
Self::Z_COLOR,
),
VertexPC::new([0.0, 0.0, size], Self::Z_COLOR),
VertexPC::new(
[0.0, arrow_ratio * size * 0.3, size - arrow_size],
Self::Z_COLOR,
),
VertexPC::new([0.0, 0.0, size], Self::Z_COLOR),
VertexPC::new(
[0.0, -arrow_ratio * size * 0.3, size - arrow_size],
Self::Z_COLOR,
),
];
let vertex_buffer = VertexBuffer::new(ctx, &vertices, Some("axes with arrows"));
Self {
vertex_buffer,
vertex_count: vertices.len() as u32,
size,
}
}
pub fn size(&self) -> f32 {
self.size
}
pub fn vertex_layout() -> wgpu::VertexBufferLayout<'static> {
VertexPC::layout()
}
}
impl Geometry for Axes {
fn vertex_buffer(&self) -> &VertexBuffer {
&self.vertex_buffer
}
fn index_buffer(&self) -> Option<&IndexBuffer> {
None
}
fn draw_count(&self) -> u32 {
self.vertex_count
}
fn aabb(&self) -> Aabb {
Aabb::new(Vec3::ZERO, Vec3::splat(self.size))
}
}