1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
use teleia::*;
const VERT: &'static str = include_str!("assets/shaders/throwshade/vert.glsl");
const FRAG: &'static str = include_str!("assets/shaders/throwshade/frag.glsl");
pub struct ThrowShade {
pub tickset: u64,
pub timeset: f64,
pub shader: Option<shader::Shader>,
}
impl ThrowShade {
pub fn new() -> Self {
Self {
tickset: 0,
timeset: 0.0,
shader: None,
}
}
pub fn set(&mut self, ctx: &context::Context, st: &state::State, src: &str) -> Result<(), String> {
let fsrc = format!("{}\n{}\n", FRAG, src);
self.tickset = st.tick;
self.timeset = 0.0;
#[cfg(not(target_arch = "wasm32"))]
if let Ok(dur) = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH) {
self.timeset = dur.as_secs_f64();
}
if let Some(s) = &mut self.shader {
s.replace(ctx, VERT, &fsrc)?;
} else {
self.shader = Some(shader::Shader::new_helper(ctx, VERT, &fsrc)?);
}
Ok(())
}
}
cfg_if::cfg_if! {
if #[cfg(target_arch = "wasm32")] {
struct Game {
throwshade: ThrowShade,
}
impl Game {
pub async fn new(_ctx: &context::Context) -> Self {
Self {
throwshade: ThrowShade::new(),
}
}
}
impl state::Game for Game {
fn render(&mut self, ctx: &context::Context, st: &mut state::State) -> Option<()> {
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(())
}
}
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub async fn main_js() {
teleia::run(1920, 1080, teleia::Options::NORESIZE, Game::new).await;
}
#[wasm_bindgen]
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;
}
});
}
}
}
|