|
| 1 | +--- |
| 2 | +tags: render |
| 3 | +title: Post-processing |
| 4 | +brief: This example shows how to apply a post-processing effect by drawing to a render target and then to a fullscreen quad using a post processing shader. |
| 5 | +author: Defold Foundation |
| 6 | +scripts: postprocess.script, invert.fp, invert.vp |
| 7 | +thumbnail: postprocess_thumb.png |
| 8 | +--- |
| 9 | + |
| 10 | +The basic principle of a full screen post processing effect is to first draw the entire game to a render target, then draw this render target to a full screen quad using a post processing shader to apply some kind of effect. This example shows a color invert effect and a CRT scanline effect. |
| 11 | + |
| 12 | +The setup in this example consists of a custom render script and a game object containing a model component with a fullscreen quad (ie rectangle). |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | +The model uses a material with a render predicate/tag named `postprocess`. The material uses a basic shader program in `invert.fp` to invert the color of anything drawn with the material. |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | +The render script is a copy of the default render script with added code to create a fullscreen render target. The render target has a color and depth buffer, and it will be resized if the screen resolution changes: |
| 21 | + |
| 22 | +```lua |
| 23 | +local function create_postprocess_rt(self, width, height) |
| 24 | + local color_params = { |
| 25 | + format = graphics.TEXTURE_FORMAT_RGBA, |
| 26 | + width = width, |
| 27 | + height = height, |
| 28 | + min_filter = render.FILTER_LINEAR, |
| 29 | + mag_filter = render.FILTER_LINEAR, |
| 30 | + u_wrap = render.WRAP_CLAMP_TO_EDGE, |
| 31 | + v_wrap = render.WRAP_CLAMP_TO_EDGE |
| 32 | + } |
| 33 | + local depth_params = { |
| 34 | + format = graphics.TEXTURE_FORMAT_DEPTH, |
| 35 | + width = width, |
| 36 | + height = height, |
| 37 | + } |
| 38 | + self.postprocess_rt = render.render_target("postprocess_rt", { [render.BUFFER_COLOR_BIT] = color_params, [render.BUFFER_DEPTH_BIT] = depth_params } ) |
| 39 | + self.postprocess_rt_width = width |
| 40 | + self.postprocess_rt_height = height |
| 41 | +end |
| 42 | + |
| 43 | +local function update_postprocess_rt(self) |
| 44 | + local w = render.get_window_width() |
| 45 | + local h = render.get_window_height() |
| 46 | + |
| 47 | + -- keep render target if size is the same |
| 48 | + if self.postprocess_rt_width == w and self.postprocess_rt_height == h then |
| 49 | + return |
| 50 | + end |
| 51 | + |
| 52 | + render.delete_render_target(self.postprocess_rt) |
| 53 | + create_postprocess_rt(self, w, h) |
| 54 | +end |
| 55 | + |
| 56 | +function init(self) |
| 57 | + -- create the postprocess predicate and all of the default predicates |
| 58 | + self.predicates = create_predicates("postprocess", "tile", "gui", "particle", "model", "debug_text") |
| 59 | + |
| 60 | + create_postprocess_rt(self, render.get_window_width(), render.get_window_height()) |
| 61 | +end |
| 62 | + |
| 63 | +function update(self) |
| 64 | + update_postprocess_rt(self) |
| 65 | +end |
| 66 | +``` |
| 67 | + |
| 68 | +The render script is additionally modified so that all content is drawn to the render target instead of directly to the screen. In a separate step at the end, the render target is used as a texture and drawn to the fullscreen quad with the `postprocess` predicate using the post processing shader assigned to the model quad: |
| 69 | + |
| 70 | + |
| 71 | +```lua |
| 72 | +function update(self) |
| 73 | + update_postprocess_rt(self) |
| 74 | + |
| 75 | + -- enable postprecssing render target |
| 76 | + -- subsequent draw operations will be done to the render target |
| 77 | + -- |
| 78 | + render.set_render_target(self.postprocess_rt) |
| 79 | + |
| 80 | + -- note: some render code removed from this snippet to make it readable |
| 81 | + render.draw(predicates.model, draw_options_world) |
| 82 | + render.draw(predicates.tile, draw_options_world) |
| 83 | + render.draw(predicates.particle, draw_options_world) |
| 84 | + render.draw(predicates.gui, camera_gui.options) |
| 85 | + render.draw(predicates.debug_text, camera_gui.options) |
| 86 | + |
| 87 | + -- revert to the default render target |
| 88 | + -- |
| 89 | + render.set_render_target(render.RENDER_TARGET_DEFAULT) |
| 90 | + |
| 91 | + -- render post processing render target to quad with predicate 'postprocess' |
| 92 | + -- |
| 93 | + render.set_view(vmath.matrix4()) |
| 94 | + render.set_projection(vmath.matrix4()) |
| 95 | + render.enable_texture(0, self.postprocess_rt, render.BUFFER_COLOR_BIT) |
| 96 | + render.draw(predicates.postprocess) |
| 97 | + render.disable_texture(0) |
| 98 | +end |
| 99 | +``` |
| 100 | + |
| 101 | +Additionally the example shows in `postprocess.script` how to change material using material resource properties at runtime. |
| 102 | + |
| 103 | + |
| 104 | + |
0 commit comments