Lens flare postprocess filter

My interpretation of technique from this article for Panda3D FilterManager.
The advantage of this approach is that any bright objects in the scene would create a lens flare without any special configuration of the scene.

Tested on Panda3D 1.8.0
Radeon HD 4850 and GF 7300 GS

Sample with resources

Screenshots:

Looks cool, unfortunately on a Radeon HD 3850 I got a pink screen and

:gobj(error): Cg program too complex for driver: invert_threshold_r_blur.sha. Try choosing a different profile. 

Try to set basic-shaders-only #f in the Config.prc.

EDIT:
Hm, it’s strange, but seems that problem caused by input variables. Try this variant of invert_threshold_r_blur.sha with hardcoded radial blur parameters.

//Cg
// First part of lens flare postprocess filter from this article http://www.john-chapman.net/content.php?id=18
// Panda3d interpretation by 09th (rupanda3d@gmail.com)
void vshader(
    float4 vtx_position : POSITION,
    float2 vtx_texcoord0 : TEXCOORD0,
    out float4 l_position : POSITION,
    out float2 l_texcoord0 : TEXCOORD0,
    uniform float4 texpad_tex1,
    uniform float4x4 mat_modelproj)
{
    l_position=mul(mat_modelproj, vtx_position);
    // invert texture coord
    l_texcoord0 = - (vtx_position.xz * texpad_tex1.xy*2);
}
 
void fshader(float2 l_texcoord0 : TEXCOORD0,
             out float4 o_color : COLOR,
             uniform float4 texpad_tex1,
             uniform sampler2D k_tex1 : TEXUNIT0,
             uniform float3 k_threshold)
{

    int NSAMPLES = 3;
    float BlurStart = 0.5;
    float BlurWidth = -0.1;
    float BRIGHTNESS = 1.0;
    
    // threshold + radial blur
    float4 c = 0;
    float3 tmp = 0;
    for(int i=0; i <NSAMPLES; i++) {
        float scale = BlurStart + BlurWidth*(i/(float) (NSAMPLES-1));
        tmp = tex2D(k_tex1, l_texcoord0 * scale + texpad_tex1.xy ).xyz;
        tmp = saturate((((tmp - k_threshold)/(float3(1.0) - k_threshold))) * BRIGHTNESS);
        c += float4(tmp, 1.0);
    }
    c /= NSAMPLES;
    
    o_color = c;

}

Now it works…and I must say it looks SPECTACULAR! Good job!

Can I use it in my game(-s)? :smiley:

Thanks ) Of course you can use it )

ADD: I fixed shader in the sample archive

ADD2: ATI video cards (or drivers) are not too frendly with the input variables, so I had to transfer most of parameters to the shader code. Archive updated.

Woah! Man, this is one of the best looking things I’ve seen in Panda in 2012! Really, the effect is amazing and it runs perfectly smooth on my Intel GMA 4500 (your initial package, not the modified version; didn’t try that).

Keep up the great work!
You make me want using Panda again.

Thanks. Nice to hear it )

Hello ninth,

This looks like a very nice filter. I was preliminarily discussing with rdb the possibility of integrating it into CommonFilters.py in Panda.

What is the license for your code, and would you mind if this filter was made part of a basic install of Panda?

In case it’s ok, I can do the integration work, unless you specifically want to do it yourself. Using a similar approach as for the other filters, it would be possible to expose the compile-time parameters to the user, as CommonFilters already has the infrastructure to regenerate the filter code when certain parts of its configuration change.

(Note that there will probably be some changes to CommonFilters.py from my cartoon shader patch; e.g. the new version automatically numbers the TEXCOORDn semantics requested by the filters. Integration should be easier with the new version.)

Sure. I would be glad if this filter became the part of the Panda.
About license - I think something like WTFPL or Public Domain.

Ok. I’ll integrate it. Thanks!

I now have an initial integrated version ready. It was pretty straightforward. Thanks ninth!

The way I did the integration depends on some of the changes to CommonFilters in my cartoon shader patch, so likely that one will have to be processed first.

I made a new thread, in which I’ll post about any updates to the situation: