1 module play;
2 
3 import re;
4 import re.gfx;
5 import re.gfx.shapes.cube;
6 import re.gfx.shapes.grid;
7 import re.ng.camera;
8 import re.math;
9 static import raylib;
10 
11 /// simple 3d demo scene
12 class PlayScene : Scene3D {
13     private PostProcessor glitch_postproc;
14     private float[2] sample_offset = [0.01, 0];
15 
16     override void on_start() {
17         clear_color = Colors.LIGHTGRAY;
18 
19         // load a shader effect and add it as a postprocessor
20         auto chrm_abr = new Effect(Core.content.load_shader(null,
21                 "shader/chromatic_aberration.frag").front, color_alpha_white(0.8));
22         glitch_postproc = new PostProcessor(resolution, chrm_abr);
23         glitch_postproc.enabled = false;
24         postprocessors ~= glitch_postproc;
25 
26         // set the camera position
27         cam.entity.position = Vector3(0, 10, 10);
28 
29         auto block = create_entity("block", Vector3(0, 0, 0));
30         auto cube = block.add_component(new Cube(Vector3(2, 2, 2)));
31 
32         // point the camera at the block, then orbit it
33         cam.look_at(block.position);
34         cam.entity.add_component(new CameraOrbit(block, 0.5));
35 
36         // enable an example shader on cube
37         auto cross_stitch = new Effect(Core.content.load_shader(null,
38                 "shader/cross_stitch.frag").front, Colors.PURPLE);
39         auto mixAmt = 0.05f;
40         cross_stitch.set_shader_var("mixAmt", mixAmt);
41         cube.effect = cross_stitch;
42 
43         // draw a grid at the origin
44         auto grid = create_entity("grid");
45         grid.add_component(new Grid3D(10, 1));
46     }
47 
48     override void update() {
49         super.update();
50 
51         // allow the postprocessor to be toggled with SPACE
52         if (Input.is_key_pressed(Keys.KEY_SPACE)) {
53             glitch_postproc.enabled = !glitch_postproc.enabled;
54         }
55 
56         if (glitch_postproc.enabled) {
57             // make our postprocess effect fluctuate with time
58             import std.math : sin;
59 
60             sample_offset[0] = 0.010 + 0.005 * sin(Time.total_time / 2);
61             glitch_postproc.effect.set_shader_var("sample_offset", sample_offset);
62         }
63     }
64 }