62b5cac11b
Étape 7 (DRAFT): the PipelineCache moves into the Scene (SceneGpu holds
device + format + cache, wired via Scene::init_gpu in AppRunner::resumed),
so App no longer owns a cache field. Mesh now holds Option<Arc<Material>>
(with material()/set_material()/with_material()); Entity drops material_id
({mesh_id, transform}); iter_entities yields (label, &Mesh, &Transform);
Renderer::render_scene resolves the material from the mesh or the Scene's
lazy default_material(). New declarative Scene helpers: register_shader,
add_material_shader, create_mesh. cube/simple examples migrated; manual
remains low-level and unchanged.
72 lines
2.9 KiB
Rust
72 lines
2.9 KiB
Rust
//! Workflow déclaratif minimal, sans manipulation WGPU explicite dans ce fichier.
|
|
//! `AppBuilder` crée l'event loop puis `App::run` ouvre la fenêtre, construit le `Context`/`Renderer`
|
|
//! et fait tourner la boucle update → render → present. Depuis la migration winit 0.30, le GPU n'existe
|
|
//! qu'après `resumed` : c'est pourquoi l'enregistrement shader + la création mesh/matériau/entité vivent
|
|
//! dans le hook `AppHandler::setup`, appelé une fois le contexte prêt. Depuis l'Étape 7 le PipelineCache
|
|
//! vit dans la scène (`Scene::init_gpu`, appelé dans `resumed`) : on passe par `register_shader` +
|
|
//! `add_material_shader` + `create_mesh` + `add_entity`, le matériau étant lié au mesh. La scène se rend
|
|
//! automatiquement : la méthode `render()` par défaut appelle `app.render_scene(frame.view())`.
|
|
use wsg_lib::AppHandler;
|
|
use wsg_lib::app::AppBuilder;
|
|
use wsg_lib::resources::Vertex;
|
|
use wsg_lib::utils::WsgError;
|
|
|
|
struct MonQuad;
|
|
|
|
impl AppHandler for MonQuad {
|
|
fn setup(&mut self, app: &mut wsg_lib::App) {
|
|
// Exemple 2D plat : le shader `standard` en mode **unlit** (options.x = 1) renvoie la couleur
|
|
// du vertex telle quelle. Ainsi le 2D est un cas particulier du 3D — un seul pipeline pour tous.
|
|
app.renderer_mut().set_unlit(true);
|
|
app.scene
|
|
.register_shader("standard", wsg_lib::utils::STANDARD_SHADER_PATH)
|
|
.unwrap();
|
|
let vertices = [
|
|
Vertex {
|
|
position: [-0.5, 0.5, 0.0],
|
|
normal: [0.0, 0.0, 1.0],
|
|
uv: [0.0, 0.0],
|
|
color: [1.0, 0.0, 0.0, 1.0],
|
|
}, // Haut-Gauche (Rouge)
|
|
Vertex {
|
|
position: [0.5, 0.5, 0.0],
|
|
normal: [0.0, 0.0, 1.0],
|
|
uv: [1.0, 0.0],
|
|
color: [0.0, 1.0, 0.0, 1.0],
|
|
}, // Haut-Droite (Vert)
|
|
Vertex {
|
|
position: [0.5, -0.5, 0.0],
|
|
normal: [0.0, 0.0, 1.0],
|
|
uv: [1.0, 1.0],
|
|
color: [0.0, 0.0, 1.0, 1.0],
|
|
}, // Bas-Droite (Bleu)
|
|
Vertex {
|
|
position: [-0.5, -0.5, 0.0],
|
|
normal: [0.0, 0.0, 1.0],
|
|
uv: [0.0, 1.0],
|
|
color: [1.0, 1.0, 0.0, 1.0],
|
|
}, // Bas-Gauche (Jaune)
|
|
];
|
|
let indices: [u16; 6] = [0, 1, 2, 0, 2, 3];
|
|
|
|
app.scene
|
|
.add_material_shader("standard_material", "standard")
|
|
.unwrap();
|
|
app.scene
|
|
.create_mesh(
|
|
"quad_mesh",
|
|
&vertices,
|
|
Some(&indices),
|
|
Some("standard_material"),
|
|
)
|
|
.unwrap();
|
|
app.scene.add_entity("quad", "quad_mesh").unwrap();
|
|
}
|
|
}
|
|
|
|
#[pollster::main]
|
|
async fn main() -> Result<(), WsgError> {
|
|
let app = AppBuilder::new().title("WSG Simple").build().await?;
|
|
app.run(MonQuad)
|
|
}
|