Jump to content

  • Log In with Google      Sign In   
  • Create Account

Alpha transparency in deferred shader

Posted by Ashaman73, 10 June 2010 · 4,866 views

When I switched from forward rendering to deferred rendering sometime ago, I really liked the elegant handling of lights. This is still the main reason
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
float stippleMask()

vec2 stipple = fract( gl_FragCoord.xy * 0.5 );
return step(0.25,abs(stipple.x-stipple.y));

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 !

Cool...that's a neat trick.
Cool stuff =D

How well does the stippled transparency integrate with other parts of your rendering pipe, like unlit/forward-lit particles, bloom, etc?
Original post by Hodgman
Cool stuff =D

How well does the stippled transparency integrate with other parts of your rendering pipe, like unlit/forward-lit particles, bloom, etc?

It works quite well.
- SSAO and shadowing is done before the stipple pass.
- Particles should work as normal, but I need more testing to be sure. The stipples write to the z-buffer so that particles should integrate seemless.
- Bloom works on the stippled image and I can't see any artifacts.

Very interesting!
That was a nice one, thanks !<BR>Let's see if I understand it right so far. <BR>This method provides order independent transparency by rendering an object via screen space checker like grid, where every even pixel has 0 aplha and every odd pixel has 1 alpha value. This is rendered using something like alphatest and filtered to bring one layer of transparency with 50 % "^alpha blend^" <BR>This is pretty nice, but is there a way to overcome the fixed 50% transparency and make it full range. 0-1.0(0-255) or maybe I missed something in the code that already provides this ?

September 2014 »

1415161718 19 20

Recent Entries

Recent Entries