Skip to main content

ranim_items/mesh/render/
mod.rs

1//! Render-world data and GPU implementation for mesh items.
2
3mod buffer;
4mod pipeline;
5
6use ranim_core::{
7    components::rgba::Rgba,
8    glam::{Mat4, Vec3},
9};
10
11pub(super) fn install(app: &mut bevy_app::App) {
12    use bevy_ecs::schedule::IntoScheduleConfigs;
13    use ranim_render::{
14        extract::ExtractPlugin,
15        schedule::{RenderSchedule, ViewPhase},
16        utils::WgpuContext,
17    };
18
19    use super::{MeshItem, Sphere, Surface};
20
21    let buffer = buffer::MeshItemsBuffer::new(app.world().resource::<WgpuContext>());
22    app.insert_resource(buffer).add_plugins((
23        ExtractPlugin::<MeshItem>::default(),
24        ExtractPlugin::<Sphere>::default(),
25        ExtractPlugin::<Surface>::default(),
26    ));
27    app.add_systems(RenderSchedule::Prepare, pipeline::prepare_mesh_items)
28        .add_systems(
29            RenderSchedule::View,
30            (
31                pipeline::depth.in_set(ViewPhase::Depth),
32                pipeline::color.in_set(ViewPhase::Color),
33            ),
34        );
35}
36
37/// A primitive for rendering a mesh item.
38#[derive(bevy_ecs::component::Component, Debug, Clone, PartialEq)]
39pub struct RenderMeshItem {
40    /// The vertices of the mesh
41    pub points: Vec<Vec3>,
42    /// The triangle indices
43    pub triangle_indices: Vec<u32>,
44    /// The transform matrix
45    pub transform: Mat4,
46    /// Per-vertex colors
47    pub vertex_colors: Vec<Rgba>,
48    /// Per-vertex normals for smooth shading.
49    /// All-zero (or empty) → shader falls back to flat shading via `dpdx`/`dpdy`.
50    pub vertex_normals: Vec<Vec3>,
51}
52
53impl Default for RenderMeshItem {
54    fn default() -> Self {
55        Self {
56            points: vec![Vec3::ZERO; 3],
57            triangle_indices: vec![0, 1, 2],
58            transform: Mat4::IDENTITY,
59            vertex_colors: vec![Rgba::default(); 3],
60            vertex_normals: vec![Vec3::ZERO; 3],
61        }
62    }
63}