diff options
| author | LLLL Colonq <llll@colonq> | 2025-02-17 22:54:50 -0500 |
|---|---|---|
| committer | LLLL Colonq <llll@colonq> | 2025-02-17 22:54:50 -0500 |
| commit | d7c175733194b406990cb0113aab2b26836e8edb (patch) | |
| tree | c95a478ce568eeffd2186548453acb7f86da17ee | |
| parent | 80ae56c9e28454cb351b0ad3217adc2484e67107 (diff) | |
Set the shader from JS
| -rw-r--r-- | crates/throwshade/index.html | 25 | ||||
| -rw-r--r-- | crates/throwshade/src/lib.rs | 21 |
2 files changed, 41 insertions, 5 deletions
diff --git a/crates/throwshade/index.html b/crates/throwshade/index.html index c3d8719..63e07ae 100644 --- a/crates/throwshade/index.html +++ b/crates/throwshade/index.html @@ -1 +1,24 @@ -<link data-trunk rel="rust" data-wasm-opt="2" data-target-name="throwshade" /> +<!DOCTYPE html> +<html> + <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> + <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no"> + <head> + <meta charset="UTF-8"> + <link data-trunk rel="rust" data-wasm-opt="2" data-target-name="throwshade" /> + <base data-trunk-public-url /> + <meta name="theme-color" media="(prefers-color-scheme: light)" content="white"> + <meta name="theme-color" media="(prefers-color-scheme: dark)" content="#404040"> + <link rel="icon" href="data:;base64,iVBORw0KGgo="> + <title>teleia</title> + </head> + <body> + <script> + addEventListener("TrunkApplicationStarted", async (event) => { + console.log("initialized, starting..."); + window.wasmBindings.main_js(); + window.wasmBindings.set_shader("hi"); + }); + </script> + <div id="teleia-parent"></canvas> + </body> +</html> diff --git a/crates/throwshade/src/lib.rs b/crates/throwshade/src/lib.rs index c20b5fb..402be0e 100644 --- a/crates/throwshade/src/lib.rs +++ b/crates/throwshade/src/lib.rs @@ -34,16 +34,25 @@ impl ThrowShade { cfg_if::cfg_if! { if #[cfg(target_arch = "wasm32")] { - struct Game {} + struct Game { + throwshade: ThrowShade, + } impl Game { pub async fn new(_ctx: &context::Context) -> Self { - Self {} + Self { + throwshade: ThrowShade::new(), + } } } impl state::Game for Game { fn render(&mut self, ctx: &context::Context, st: &mut state::State) -> Option<()> { - ctx.clear_color(glam::Vec4::new(1.0, 0.0, 0.0, 1.0)); - ctx.clear(); + if let Some(s) = &self.throwshade.shader { + s.bind(ctx); + s.set_vec2(ctx, "resolution", &glam::Vec2::new(ctx.render_width, ctx.render_height)); + let elapsed = (st.tick - self.throwshade.tickset) as f32 / 60.0; + s.set_f32(ctx, "time", elapsed); + ctx.render_no_geometry(); + } Some(()) } } @@ -57,6 +66,10 @@ cfg_if::cfg_if! { pub async fn set_shader(s: &str) { contextualize(|ctx, st, g: &mut Game| { log::info!("set shader: {}", s); + if let Err(e) = g.throwshade.set(ctx, st, &s) { + log::warn!("error compiling shader: {}", e); + g.throwshade.shader = None; + } }); } } |
