From d7c175733194b406990cb0113aab2b26836e8edb Mon Sep 17 00:00:00 2001 From: LLLL Colonq Date: Mon, 17 Feb 2025 22:54:50 -0500 Subject: Set the shader from JS --- crates/throwshade/src/lib.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'crates/throwshade/src/lib.rs') 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; + } }); } } -- cgit v1.2.3