1 /** fragment shader effects with auto-synced uniforms */
2 
3 module re.gfx.effects.frag;
4 
5 import re.ng.scene;
6 import re.gfx.raytypes;
7 import re.gfx.effect;
8 import re.util.hotreload;
9 static import raylib;
10 
11 /// fragment shader effect
12 class FragEffect : Effect {
13     enum shader_uni_resolution = "i_resolution";
14     enum shader_uni_frame = "i_frame";
15     enum shader_uni_time = "i_time";
16     // enum shader_uni_mouse = "i_mouse";
17 
18     Scene scene;
19     int start_frame = 0;
20     float start_time = 0;
21 
22     this(Scene scene, Shader shader) {
23         super(shader, Colors.WHITE);
24         setup(scene);
25     }
26 
27     this(Scene scene, ReloadableShader reloadable_shader) {
28         super(reloadable_shader, Colors.WHITE);
29         setup(scene);
30     }
31 
32     private void setup(Scene scene) {
33         this.scene = scene;
34 
35         // initialize uniforms
36         init_time();
37         sync_uniforms();
38     }
39 
40     public void init_time() {
41         start_frame = Time.frame_count;
42         start_time = Time.total_time;
43     }
44 
45     public void sync_uniforms() {
46         this.set_shader_var_imm(shader_uni_resolution, cast(float[3])[
47                 scene.resolution.x, scene.resolution.y, 1.0
48             ]);
49 
50     }
51 
52     protected override void reload_shader() {
53         super.reload_shader();
54 
55         // if reloading, we need to resync uniforms
56         sync_uniforms();
57     }
58 
59     public override void update() {
60         super.update();
61 
62         this.set_shader_var_imm(shader_uni_frame, cast(int)(Time.frame_count - start_frame));
63         this.set_shader_var_imm(shader_uni_time, Time.total_time - start_time);
64     }
65 }