Soft Particles

Delayed light shafts because this was easier. Soft particles are done by fading parts of the particle that are close to solid objects. This is done by comparing the depth of the scene with the depth of the particle. By doing deferred rendering i already got the depth buffer in a texture (how convenient 😀 ) so no extra work done there. So the only work i had to do really was to ammend my old particle shader with the comparison and clipping functions (i have to do my own depth testing here). If the depth of the particle is greater than that of the scene at that pixel then the ‘discard’ statement discards the pixel.


the fading out part is done by a nifty little line of code i found here.


Here are comparison images:

Probably doesnt seem like much, so i did it with a solid texture so you can get a better look at what’s going on


I think its more than obvious that i didnt sort the particles, i’ll do it later. I can get soft particles with only a few extra lines of code. I actually didnt think it would be this easy at first. Turning it on and off is just a matter of me toggling a boolean value in the shader, its that simple 😀

That’s all for today


5 responses to “Soft Particles

  1. Dear Nick,
    I am struggling to make my particles like smoke using texture and shader programming. The particles seems to be white but not like smoke I have attached my fragment shader code here.Could you plz help me what is my problem over here.

    uniform sampler2D tex;
    uniform sampler2D depthInfo;
    uniform float power;
    varying float Depth;
    varying vec2 pos;

    float Contrast(float d)
    float val = clamp( 2.0*((d > 0.5) ? 1.0-d : d ), 0.0, 1.0);

    float a = 0.5 * pow(val, power);

    return (d > 0.5) ? 1.0 – a : a;

    void main()
    float d = texture2D(depthInfo, pos.xy).x; // Scene depth
    vec4 c = texture2D(tex, gl_TexCoord[0].st) * gl_Color;

    // Computes alpha based on the particles distance to the rest of the scene
    //c.a = c.a * Contrast(d – Depth);

    gl_FragColor = c;

    if I turn on this line “c.a = c.a * Contrast(d – Depth);” my whole particles became black ..Please suggest me .

  2. the simple solution is to change your Contrast function to this:
    float Contrast(float d)
    return clamp(d*scale,0.0,1.0);

    ‘scale’ should be a uniform float. You’re going to have to play with that value until you get the look you are aiming for. For the best results, make scale’s value be dependent on the viewport’s draw distance.

    Also, ensure that your depth is in clip space.

  3. Dear Nick,
    Sorry to bother you once again,
    As told by you I changed the constrast function like this in fragment shader:
    float Contrast(float Input)
    return clamp(d*scale, 0.0, 1.0);
    void main()
    float d = texture2D(depthInfo, pos.xy).x;
    vec4 c = texture2D(tex, gl_TexCoord[0].st) * gl_Color;
    c.a = c.a * Contrast(d – Depth);
    gl_FragColor = c;

    and in c++ i bind the shader code as follow:

    GLint texloc = glGetUniformLocationARB(particleShader, “tex”);
    GLint depthTexloc = glGetUniformLocationARB(particleShader, “depthInfo”);
    GLint powerloc = glGetUniformLocationARB(particleShader, “power”);
    GLint scalevalue = glGetUniformLocationARB(particleShader, “scale”);

    glUniform1iARB(powerloc, 2.0);

    glBindTexture(GL_TEXTURE_2D, TextureID());
    glUniform1iARB(texloc, 0);

    glBindTexture(GL_TEXTURE_2D, depthTex);
    glUniform1iARB(depthTexloc, 1);

    But it didnt work.all the particles became black .
    Is there any error in my code.Please once again help me.

    • try higher values for scalevalue, like 5 or 10 or 20. Keep playing with higher and higher values until the particles become visible.
      You say that the particles became black, but they should be transparent when the fragment’s alpha is closer to zero. Is alpha blending turned on?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s