• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
  • entries
    64
  • comments
    178
  • views
    109141

Alpha transparency in deferred shader

Sign in to follow this  
Followers 0
Ashaman73

8043 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 !

2
Sign in to follow this  
Followers 0


7 Comments


Cool stuff =D

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

Share this comment


Link to comment
Quote:
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.

0

Share this comment


Link to comment
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 ?
0

Share this comment


Link to comment
[quote]I missed something in the code that already provides this ?
[/quote]
It is already provided. The basic idea is, that a 2x2 screen area contains two or four background pixels (always opaque) and two pixels can contain an transparent overlay. The transparent overlay consists of a RGB color and an alpha value, this alpha value is actually taken to blend the overlay with the background, therefore it can be 0..1.
0

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now