roast2d_internal 0.4.0

Roast2D internal crate
Documentation
//! Vertex formats for 3D meshes.

use crate::color::Color;

#[repr(C)]
#[derive(Clone, Copy, bytemuck::Pod, bytemuck::Zeroable, Debug)]
pub struct Mesh3DVertex {
    pub position: [f32; 3],
    pub normal: [f32; 3],
    pub uv: [f32; 2],
}

impl Mesh3DVertex {
    /// Create a new vertex with position, normal, and UV coordinates.
    pub fn new(position: glam::Vec3, normal: glam::Vec3, uv: glam::Vec2) -> Self {
        Self {
            position: position.to_array(),
            normal: normal.to_array(),
            uv: uv.to_array(),
        }
    }
}

/// Per-instance data for instanced 3D rendering.
///
/// Contains the model matrix and color for each instance.
/// Used with `Engine::draw3d_instanced()` for efficient rendering
/// of many copies of the same mesh.
#[repr(C)]
#[derive(Clone, Copy, Debug, bytemuck::Pod, bytemuck::Zeroable)]
pub struct InstanceData {
    /// Model matrix columns (4x4 = 16 floats = 64 bytes)
    pub model: [[f32; 4]; 4],
    /// RGBA color (4 floats = 16 bytes)
    pub color: [f32; 4],
}

impl InstanceData {
    /// Create new instance data with the given model matrix and color.
    pub fn new(model: glam::Mat4, color: Color) -> Self {
        Self {
            model: model.to_cols_array_2d(),
            color: [color.r, color.g, color.b, color.a],
        }
    }

    /// Create instance data with just a position (identity rotation/scale, white color).
    pub fn from_position(position: glam::Vec3) -> Self {
        Self::new(glam::Mat4::from_translation(position), crate::color::WHITE)
    }

    /// Create instance data with position and color.
    pub fn from_position_color(position: glam::Vec3, color: Color) -> Self {
        Self::new(glam::Mat4::from_translation(position), color)
    }
}