Lens Flare 2D Filter

Hi! youle did recently convert this to BGE 2D Filter system. I fixed several issues and improved it.

Use mouse to look around.
In the script you can adjust sun scale threshold to change size of sun(the bigger the number, the smaller sun).


Enjoy!:slight_smile:

Note! Enable GLSL in properties panel(unless you’re in UPBGE)!

Attachments


lensflare.blend (542 KB)

Hi. Thanks for sharing. Not working on mine (default 2.77) Whats version of Blender are you using?

Also doesn’t work here, 2.77a.

Looks great, though.

Using UPBGE 0.06.
You must enable GLSL on the right in the “Properties” panel.

UPBGE! I totally forgot that existed! I should be using that.

In regular BGE, it does not work, even with GLSL enabled. Here’s the console output:

Blender Game Engine Started
2D Filter GLSL Shader: compile error:
 1  uniform sampler2D bgl_RenderedTexture;
 2  uniform float bgl_RenderedTextureWidth;
 3  uniform float bgl_RenderedTextureHeight;
 4  uniform float sunX;
 5  uniform float sunY;
 6  uniform float timer;
 7  uniform float sundirect;
 8
 9  const float sunScale = 0.2; // Normalized sun size between 0.001 and 1.0
10
11  float noise1( float n )
12  {
13      return fract(sin(n));
14  }
15
16  float noise2(vec2 co){
17      return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
18  }
19
20  vec3 lensflare(vec2 uv,vec2 pos,float sunscale)
21  {
22      vec2 main = uv-pos;
23      vec2 uvd = uv*(length(uv));
24
25      float ang = atan(main.y, main.x);
26      float dist=length(main); dist = pow(dist,.1);
27      float n = noise(vec2((ang-timer/9.0)*16.0,dist*32.0));
28
29      float f0 = 1.0/(length(uv-pos)/sunscale+1.0);
30
31      f0 = f0+f0*(sin((ang+timer/18.0 + noise(abs(ang)+n/2.0)*2.0)*12.0)*.1+dist*.1+.8);
32
33      float f2 = max(1.0/(1.0+32.0*pow(length(uvd+0.8*pos),2.0)),.0)*00.25;
34      float f22 = max(1.0/(1.0+32.0*pow(length(uvd+0.85*pos),2.0)),.0)*00.23;
35      float f23 = max(1.0/(1.0+32.0*pow(length(uvd+0.9*pos),2.0)),.0)*00.21;
36
37      vec2 uvx = mix(uv,uvd,-0.5);
38
39      float f4 = max(0.01-pow(length(uvx+0.4*pos),2.4),.0)*6.0;
40      float f42 = max(0.01-pow(length(uvx+0.45*pos),2.4),.0)*5.0;
41      float f43 = max(0.01-pow(length(uvx+0.5*pos),2.4),.0)*3.0;
42
43      uvx = mix(uv,uvd,-.4);
44
45      float f5 = max(0.01-pow(length(uvx+0.2*pos),5.5),.0)*2.0;
46      float f52 = max(0.01-pow(length(uvx+0.4*pos),5.5),.0)*2.0;
47      float f53 = max(0.01-pow(length(uvx+0.6*pos),5.5),.0)*2.0;
48
49      uvx = mix(uv,uvd,-0.5);
50
51      float f6 = max(0.01-pow(length(uvx-0.3*pos),1.6),.0)*6.0;
52      float f62 = max(0.01-pow(length(uvx-0.325*pos),1.6),.0)*3.0;
53      float f63 = max(0.01-pow(length(uvx-0.35*pos),1.6),.0)*5.0;
54
55      vec3 c = vec3(.0);
56
57      c.r+=f2+f4+f5+f6; c.g+=f22+f42+f52+f62; c.b+=f23+f43+f53+f63;
58      c+=vec3(f0);
59
60      return c;
61  }
62
63  void main()
64  {
65      vec4 renderedTex = texture2D(bgl_RenderedTexture, gl_TexCoord[0].st);
66      float xconv = bgl_RenderedTextureWidth / bgl_RenderedTextureHeight;
67      vec2 sunPos = vec2((sunX - 0.5) * xconv, sunY - 0.5);
68      vec2 uv = vec2((gl_TexCoord[0].s - 0.5) * xconv, gl_TexCoord[0].t - 0.5);
69      vec3 lens = lensflare(uv, sunPos, sunScale);
70      renderedTex.rgb += lens * sundirect;
71      gl_FragColor = renderedTex;
72  }

WARNING: 0:2: '' :  #version directive missing
ERROR: 0:16: 'float' : overloaded functions must have the same return type
ERROR: 0:27: 'noise' : undeclared identifier
ERROR: 0:28: 'noise' : function name expected
ERROR: 0:31: 'noise' : function name expected




Blender Game Engine Finished

EDIT: Same error in UPBGE

Awesome job adriansnetlis. Seems some graphic cards have noise(float x) and noise(vec2 x) integrated in builtin GLSL functions. As your hardware seems to not support builtin noise, you can use noise1 and noise2 instead at the lines where the error is, but this doesn’t give the same output (less pretty). These noise1 and noise2 functions should be rewrited to give a correct output and maybe a noise texture could be used.

Awesome job adriansnetlis. Seems some graphic cards have noise(float x) and noise(vec2 x) integrated in builtin GLSL functions. As your hardware seems to not support builtin noise, you can use noise1 and noise2 instead at the lines where the error is, but this doesn’t give the same output (less pretty). These noise1 and noise2 functions should be rewrited to give a correct output and maybe a noise texture could be used.

Awesome job adriansnetlis. Seems some graphic cards have noise(float x) and noise(vec2 x) integrated in builtin GLSL functions. As your hardware seems to not support builtin noise, you can use noise1 and noise2 instead at the lines where the error is, but this doesn’t give the same output (less pretty). These noise1 and noise2 functions should be rewrited to give a correct output and maybe a noise texture could be used.

Awesome job adriansnetlis. Seems some graphic cards have noise(float x) and noise(vec2 x) integrated in builtin GLSL functions. As your hardware seems to not support builtin noise, you can use noise1 and noise2 instead at the lines where the error is, but this doesn’t give the same output (less pretty). These noise1 and noise2 functions should be rewrited to give a correct output and maybe a noise texture could be used.

Thank you, youle. I kept getting an error with nosie2 saying “overloaded functions must have the same return type” so I merely commented it out and used noise1 in both places. Result:

Extremely beautiful. Will definitely be using this.

Attachments



Cool! Maybe you can post the .blend and if it works for everyone (we have to wait for feedbacks), adriansnetlis could update his resource with your .blend.

Nice filter @youle.
@ezio160324 noise2 removal works perfectly!
Thanks.

lensflare_updated.blend (545 KB)

I hope adriansnetlis won’t mind. I also removed the need for the property “sunobstacle” in sunScreenPos.py and added a cube (move the cube left\right with A\D respectively) that will block out the sun, allowing you to see the effect of the lens flare disappearing.

It’s mainly adriansnetlis job (who adapted the code from shadertoy). Thanks to him for this awesome filter (and thanks to the original authors "musk’s lense flare, modified by icecool. See the original at: https://www.shadertoy.com/view/4sX3Rs) . Such stuff was missing in blender resources :slight_smile:

EDIT: arf: @ezio160324: your .blend doesn’t work on my computer… (GTX 970) (So you didn’t use noise texture?) or you uploaded the wrong file? It would be cool to find something which work on all types of hardwares…

I just re-downloaded it and it works just fine. I don’t know what the problem could be.

EDIT: I’m currently using a laptop with an Intel chip. Not my main PC, but it does work fine.

I do notice a problem. When trying to incorporate the lens flare into my a separate .blend, I get no errors, but the lens flare does not appear. :spin:

EDIT2: Try removing the #version at the top of the new filter. My PC made me put that in for some reason.

ezio160324: removing #version 120 makes your filter work but the result is not the same as in adriansnetlis filter. Nevermind, it looks cool too :slight_smile: (look at the sun rays, they look different). Using noise texture, I get the same result as in adriansnetlis filter but if it doesn’t work for everyone…)

It doesn’t look the same because it doesn’t use the same code. noise2 is removed completely and noise1 is used in its place, because noise2 was giving an error.

However, my code gives something similar to the sun in The Elder Scrolls IV: Oblivion. Glad you like it. :slight_smile:

hehe yes, this is fun :slight_smile:

Still get nothing in my own separate .blend file. Hmm…

EDIT: I totally missed the .blend file you posted above. My apologies. Does not work. Same error as the original script, referring to noise2:

ERROR: 0:18: ‘float’ : overloaded functions must have the same return type

Doesn’t work for me.

2D Filter GLSL Shader: compile error.0(29) : error C7623: implicit narrowing of type from "vec2" to "float"