Skip to content

Commit 73e81e9

Browse files
committed
Added postprocess example
1 parent fcde5dc commit 73e81e9

31 files changed

+970
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
path_settings {
2+
path: "**"
3+
profile: "Default"
4+
}
5+
profiles {
6+
name: "Default"
7+
platforms {
8+
os: OS_ID_GENERIC
9+
formats {
10+
format: TEXTURE_FORMAT_RGBA
11+
compression_level: BEST
12+
compression_type: COMPRESSION_TYPE_DEFAULT
13+
}
14+
mipmaps: false
15+
max_texture_size: 0
16+
premultiply_alpha: true
17+
}
18+
}
146 KB
Binary file not shown.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: "model"
2+
tags: "model"
3+
vertex_program: "/builtins/materials/model.vp"
4+
fragment_program: "/builtins/materials/model.fp"
5+
vertex_constants {
6+
name: "mtx_worldview"
7+
type: CONSTANT_TYPE_WORLDVIEW
8+
}
9+
vertex_constants {
10+
name: "mtx_view"
11+
type: CONSTANT_TYPE_VIEW
12+
}
13+
vertex_constants {
14+
name: "mtx_proj"
15+
type: CONSTANT_TYPE_PROJECTION
16+
}
17+
vertex_constants {
18+
name: "mtx_normal"
19+
type: CONSTANT_TYPE_NORMAL
20+
}
21+
vertex_constants {
22+
name: "light"
23+
type: CONSTANT_TYPE_USER
24+
value {
25+
x: 1.0
26+
y: 1.0
27+
z: 10.0
28+
w: 1.0
29+
}
30+
}
31+
fragment_constants {
32+
name: "tint"
33+
type: CONSTANT_TYPE_USER
34+
value {
35+
x: 1.0
36+
y: 1.0
37+
z: 1.0
38+
w: 1.0
39+
}
40+
}
41+
samplers {
42+
name: "tex0"
43+
wrap_u: WRAP_MODE_CLAMP_TO_EDGE
44+
wrap_v: WRAP_MODE_CLAMP_TO_EDGE
45+
filter_min: FILTER_MODE_MIN_LINEAR
46+
filter_mag: FILTER_MODE_MAG_LINEAR
47+
}
3.49 MB
Binary file not shown.
15.4 KB
Loading
27.4 KB
Binary file not shown.
13.8 KB
Loading
51.9 KB
Binary file not shown.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
font: "/assets/SourceSansPro-Semibold.ttf"
2+
material: "/builtins/fonts/font.material"
3+
size: 32
4+
characters: " !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"

render/post_processing/example.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
![quad](quad.png)
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+
![invert_material](invert_material.png)
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+
![material_properties](material_properties.png)
104+

0 commit comments

Comments
 (0)