I use a deferred renderer, but I always missed the ability to render transparent surfaces like water or "fog". Well, I succeeded in adding transparent
surface support to my deferred renderer again, yeah, but only for one layer of transparency. Still, this is more than enough for the start and I'm quite
happy with the result.
Here's what I've done. The basic idea is to use stipple transparency and a simple shader to get rid of the stipple pattern in a final run. My approach is
a simplified version of the more common 3 layer model stipple transparency model.
Here's an overview of the technique:
1. Render g-buffer as always.
2. Render transparent surfaces with stipple pattern, write an alpha value greater 0 to indicate the tranparency factor.
3. Light pass (just one is needed!)
4. Final composition, combine transparent pixel with background pixels.
The stipple pattern can be generated by the following shader fragment (GLSL):
// returns 0 or 1
vec2 stipple = fract( gl_FragCoord.xy * 0.5 );
In figure 1 you can see my test level with and without a (stipple) transparent surface. The stipple pattern is clearly visible, but this is already some kind
of transparency effect, even if it is fixed at 50%.
We want to get rid of the pattern and we want to use a fullrange transparency value of 0..1. This is done in a final composition step. In my engine I use
this step to do tonemapping and adding bloom.
First I sample four samples of the framebuffer. Remember that we have used the alpha channel to store the transparancy value, so we check if any of
the four texels have an alpha value greater 0. If this is NOT the case, we will ignore all neighbor texels. This way we will not introduce unnecessary
loss of quality.
When we got the atleast one alpha value greater 0 we have to average the texel pairs in a cross fashion. Knowing that only one texel pair contains
alpha values >0 we can add all alpha values to get the final value. Now we can simply alpha blend the background texel pair with the transparent surface
texel pair, with one exception: we don't know which pair is the background and which is the transparent pair. But this is not a problem, just
use the stipple pattern function to determine the background texel pair and do the blending.
Here's a small GLSL fragment shader demonstrating the process:
// collect color texels
vec4 color_texel_a1 = texture2D(color_depth_map, texture_coord);
vec4 color_texel_a2 = texture2D(color_depth_map, texture_coord + screen_size.zw*vec2(-1,-1));
vec4 color_texel_b1 = texture2D(color_depth_map, texture_coord + screen_size.zw*vec2( 0,-1));
vec4 color_texel_b2 = texture2D(color_depth_map, texture_coord + screen_size.zw*vec2(-1, 0));
// average texel color for pair a and b
vec4 avg_a = (color_texel_a1+color_texel_a2)*0.5;
vec4 avg_b = (color_texel_b1+color_texel_b2)*0.5;
// get final alpha transparency value
float alpha = max(avg_a.a,avg_b.a);
// transparent mask
float transparent_mask = step(0.01, alpha);
// update alpha
alpha = stippleMask()>0 ? alpha : 1.0-alpha;
// blend background and foreground color
vec3 color = mix(avg_a.rgb,avg_b.rgb,alpha);
// apply transparent mask, if none transparency is present take the center texel
color = mix(color_texel_a1.rgb,tmp_color,transparent_mask);
As you can see in the close up view, we half the resolution when we encounter a transparent surface. This can result in artifacts when we use transparent
surfaces with high frequent highlights, but on the other hand it is very simple, fast and it enables transparency in a deferred shader !