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