Skip to main content

material_system/
material_system.rs

1//! Material System Demo - Shader Parameter Management
2//!
3//! This example demonstrates the Material API for managing shader parameters:
4//! - Setting shader parameters (floats, vectors, matrices, colors)
5//! - Binding textures to materials
6//! - Material instancing
7//! - Parameter updates and hot-reloading
8//!
9//! Materials provide a high-level API for shader parameter management without
10//! dealing with low-level buffer binding and layout details.
11//!
12//! **Note:** This is a placeholder example demonstrating the Material API structure.
13//! Full rendering integration with custom shaders is in development.
14
15use astrelis_core::logging;
16use astrelis_render::{Color, GraphicsContext, RenderWindow, RenderWindowBuilder, wgpu};
17use astrelis_winit::{
18    FrameTime, WindowId,
19    app::{App, AppCtx, run_app},
20    event::EventBatch,
21    window::{WindowDescriptor, WinitPhysicalSize},
22};
23use std::sync::Arc;
24
25struct MaterialSystemDemo {
26    _context: Arc<GraphicsContext>,
27    window: RenderWindow,
28    window_id: WindowId,
29    time: f32,
30}
31
32fn main() {
33    logging::init();
34
35    run_app(|ctx| {
36        let graphics_ctx =
37            GraphicsContext::new_owned_sync().expect("Failed to create graphics context");
38
39        let window = ctx
40            .create_window(WindowDescriptor {
41                title: "Material System Demo - Shader Parameters".to_string(),
42                size: Some(WinitPhysicalSize::new(1024.0, 768.0)),
43                ..Default::default()
44            })
45            .expect("Failed to create window");
46
47        let window = RenderWindowBuilder::new()
48            .color_format(wgpu::TextureFormat::Bgra8UnormSrgb)
49            .with_depth_default()
50            .build(window, graphics_ctx.clone())
51            .expect("Failed to create render window");
52
53        let window_id = window.id();
54
55        // Demonstration of Material API usage
56        // In actual use, you would create materials with shaders:
57        //
58        // let material = Material::new(shader, graphics_ctx.clone());
59        // material.set_parameter("base_color", MaterialParameter::Color(Color::RED));
60        // material.set_parameter("roughness", MaterialParameter::Float(0.5));
61        // material.set_parameter("view_matrix", MaterialParameter::Matrix4(view_matrix));
62        // material.set_texture("albedo", texture_handle);
63        // material.bind(&mut render_pass);
64
65        println!("\n═══════════════════════════════════════════════════════");
66        println!("  🎨 MATERIAL SYSTEM DEMO - Shader Parameters");
67        println!("═══════════════════════════════════════════════════════");
68        println!("\n  MATERIAL API FEATURES:");
69        println!("    • Type-safe parameter setting (float, vec, matrix, color)");
70        println!("    • Texture binding and management");
71        println!("    • Automatic buffer creation and updates");
72        println!("    • Material instancing for performance");
73        println!("    • Hot-reloadable shader parameters");
74        println!("\n  EXAMPLE MATERIAL TYPES:");
75        println!("    1. Color Material - PBR properties (color, roughness, metallic)");
76        println!("    2. Textured Material - UV transforms (offset, scale, tint)");
77        println!("    3. Animated Material - Time-based effects (frequency, amplitude)");
78        println!("    4. Transform Material - View/projection matrices");
79        println!("\n  Material API Usage:");
80        println!("    material.set_parameter(\"color\", MaterialParameter::Color(..))");
81        println!("    material.set_parameter(\"time\", MaterialParameter::Float(..))");
82        println!("    material.set_parameter(\"matrix\", MaterialParameter::Matrix4(..))");
83        println!("    material.set_texture(\"albedo\", texture_handle)");
84        println!("    material.bind(&mut render_pass)");
85        println!("\n  Materials abstract shader parameter management,");
86        println!("  eliminating manual buffer binding boilerplate.");
87        println!("═══════════════════════════════════════════════════════\n");
88
89        tracing::info!("Material system demo initialized");
90
91        Box::new(MaterialSystemDemo {
92            _context: graphics_ctx,
93            window,
94            window_id,
95            time: 0.0,
96        })
97    });
98}
99
100impl App for MaterialSystemDemo {
101    fn update(&mut self, _ctx: &mut AppCtx, _time: &FrameTime) {
102        self.time += 0.016; // 60 FPS
103    }
104
105    fn render(&mut self, _ctx: &mut AppCtx, window_id: WindowId, events: &mut EventBatch) {
106        if window_id != self.window_id {
107            return;
108        }
109
110        // Handle resize
111        events.dispatch(|event| {
112            if let astrelis_winit::event::Event::WindowResized(size) = event {
113                self.window.resized(*size);
114                astrelis_winit::event::HandleStatus::consumed()
115            } else {
116                astrelis_winit::event::HandleStatus::ignored()
117            }
118        });
119
120        // In a real application, materials would be bound during rendering:
121        // material.bind(&mut render_pass);
122        // draw_mesh(&mesh);
123
124        // Begin frame
125        let Some(frame) = self.window.begin_frame() else {
126            return; // Surface not available
127        };
128
129        {
130            let _pass = frame
131                .render_pass()
132                .clear_color(Color::from_rgb_u8(20, 30, 40))
133                .label("material_system_pass")
134                .build();
135            // Materials would be applied here in actual rendering
136            // This is a conceptual demonstration
137        }
138        // Frame auto-submits on drop
139    }
140}