ScreenSpaceLensFlares

im using an include file, its code is like this:
CLREDIT:

 

vec3 ApplyThreshold(vec3 CLR, float Threshold){
	return max(CLR - vec3(Threshold), vec3(0.0));
}

vec3 Saturation(vec3 InputCLR, float Saturation){
	return mix(vec3(dot(InputCLR.rgb, vec3(0.299, 0.587, 0.114))), InputCLR.rgb, Saturation);
}

also the code is based off of some article i found so idrk if its MIT but who really gives a shit, yk?

 

https://john-chapman.github.io/2017/11/05/pseudo-lens-flare.html

 

for all i care its free and open source code and you SHOULD use it in your project

Shader code
shader_type canvas_item;
render_mode blend_add;

#include "res://Assets/Materials/Shaders/Library/CLREDIT.gdshaderinc"

uniform lowp sampler2D Screen_Sample : hint_screen_texture, filter_linear_mipmap_anisotropic;
uniform lowp sampler2D FlareMult;
uniform lowp sampler2D FlareMult2;

uniform float Blur = 2.5;
uniform float FlareThreshold;
uniform int Flares;
uniform float FlareSpacing;

uniform float Intensity;
uniform float Saturation_;

void fragment(){
	vec2 FlippedUV = vec2(1.0) - SCREEN_UV;
	vec2 FlareVector = (vec2(0.5) - SCREEN_UV) * FlareSpacing;
	vec3 FinalFlare = vec3(0.0);
	
	for (int i = 0; i < Flares; ++i){
		vec2 SUV = fract(SCREEN_UV + FlareVector * vec2(float(i)));
		float Dist = distance(SUV, vec2(0.5));
		float Weight = 1.0 - smoothstep(0.0, 0.75, Dist);
		vec3 BlurredScreen = texture(Screen_Sample, SUV, Blur).rgb;
		BlurredScreen = ApplyThreshold(BlurredScreen, FlareThreshold);
		FinalFlare += BlurredScreen * Weight;
	}
	FinalFlare *= texture(FlareMult, SCREEN_UV).rgb;
	FinalFlare *= texture(FlareMult2, SCREEN_UV).rgb;
	COLOR.rgb = FinalFlare * Intensity;
	COLOR.rgb = Saturation(COLOR.rgb, Saturation_);
}
Live Preview
Tags
camera, distortion, fast, flare, fx, lens, lensflare, screen-space
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

More from Unhinged Dev

Comments 13

  1. xShader1374_

    Hey! I'm trying to use this one but don't really know where should i put it, i tried to apply this one into a SubViewportContainer but doesn't really seem to work, any help would be appreciated, thanks in advance!

    0
    1. the way post process shaders are handled in godot is kinda bomboclat, but to use my shader i recommend creating a scene that will contain your post process shaders, it should be a UI scene, add a canvaslayer node (so you can change layer level later if needed) and add a control node, make it full rect and add as a child of it a color rect node, in this nodes inspector there is a "material" setting thingy, make a shader material with a new shader, paste the code in and it should work (you also need the shader include file, dont forget to change the filepath in the shader)

      1
    1. step 1: create a "shader include" file with the first bit of code
      step 2: create a regular shader file with the second code
      step 3: create a color rect where you want the shader magic to happen
      step 4: give it a shader material and load the shader you made
      step5: profit

      0
  2. PIRO_ONTAN TV

    أين أضع الرمز؟أين أضع الرمز؟أين أضع الرمز؟

    0
  3. The shader doesn't seem to work. Nothing happens even if I try to copy the parameters used in the first screenshot. I've even spent time trying to recreate the input textures that are seen in the screenshot.

     

    Could you maybe provide the two textures that you used for the shader?

    0
    1. I just had to restart the scene ....

       

      Would still be good tho if you provided the textures used so that we don't have to create our own.

      0
    2. im not sure why it happened, either way the textures are very simple you could probably google for them, but if i get around to it ill post them here

      0
  4. Great shader, works perfect in desktop! But for some reason doesn't work in Web. Instead it just shows transparent FlareMult texture and no flares. Any way to tweak it for Web?

    1
      1. happy you figured it out when i wasnt around, i have an include file with a bunch of blur functions you might like:

        //slow if BokehSize is too high, best quality
        vec4 BokehBlur(sampler2D Texture, float LOD,float BokehSize, vec2 UVs, vec2 TexturePixSize){
        vec4 c = textureLod(Texture, UVs, LOD);
        for (float x = -BokehSize; x < BokehSize; x++)
        {
        for (float y = -BokehSize; y < BokehSize; y++)
        {
        if (x*x + y*y > BokehSize*BokehSize){continue;}
        vec4 new_c = texture(Texture, UVs+TexturePixSize*vec2(x, y));
        if (length(new_c) >length(c)){
        c = new_c;
        }
        }
        }
        return c;
        }
        
        //multipass,kinda slow but good
        vec4 MonksGaussianMultipass(sampler2D Texture, vec2 UVs, vec2 TexturePixSize, int Iterations, vec2 Direction){
        vec4 blurred_tex = vec4(0.0);
        vec2 resolution = 1.0 / TexturePixSize;
        
        for (int i=0; i < Iterations; i++){
        float size = float(Iterations - i);
        
        vec2 off1 = vec2(1.3846153846) * (Direction * size);
        vec2 off2 = vec2(3.2307692308) * (Direction * size);
        
        blurred_tex += texture(Texture, UVs) * 0.2270270270;
        blurred_tex += texture(Texture, UVs + (off1 / resolution)) * 0.3162162162;
        blurred_tex += texture(Texture, UVs - (off1 / resolution)) * 0.3162162162;
        blurred_tex += texture(Texture, UVs + (off2 / resolution)) * 0.0702702703;
        blurred_tex += texture(Texture, UVs - (off2 / resolution)) * 0.0702702703;
        }
        
        blurred_tex /= float(Iterations) + 1.0;
        
        return blurred_tex;
        }
        
        //fastest
        vec4 NodevGaussian(sampler2D Texture, vec2 UVs, vec2 TexturePixSize, float Blur, float Radius){
        float pi = 3.14;
        float n = 0.0015;
        
        vec4 blurred_tex = vec4(0);
        
        float weight;
        for (float i = -Blur; i <= Blur; i++){
        float d = i / pi;
        vec2 anchor = vec2(cos(d), sin(d)) * Radius * i;
        vec2 directions = UVs + TexturePixSize * anchor;
        blurred_tex += texture(Texture, directions) * n;
        if (i <= 0.0) {n += 0.0015; }
        if (i > 0.0) {n -= 0.0015; }
        weight += n;
        }
        
        float norm = 1.0 / weight;
        blurred_tex *= norm;
        return blurred_tex;
        }
        
        vec3 RadialBlur(sampler2D Texture, vec2 UVs, vec2 Center , int Samples, float BlurAmount){
        vec2 Direction = UVs - Center;
        vec3 C = vec3( 0.0, 0.0, 0.0 );
        float F = 1.0 / float(Samples);
        for(int I=0; I < Samples; I++){
        C += texture(Texture, UVs - BlurAmount * Direction * float(I) ).rgb * F;
        }
        return C;
        }
        
        float WindowCubic(float _x, float _center, float _radius){
        _x = min(abs(_x - _center) / _radius, 1.0);
        return 1.0 - _x * _x * (3.0 - 2.0 * _x);
        }
        
        vec4 DirectionalBlur(sampler2D Texture, vec2 BlurUV, vec2 Angle, int Samples){
          vec3 Acc = vec3(0);
           
          float Delta = 2.0 / float(Samples);
          for(float i = -1.0; i <= 1.0; i += Delta){
            Acc += texture(Texture, BlurUV - vec2(Angle.x * i, Angle.y * i)).rgb * Delta * .5;
          }
           
          return vec4(Acc, 1.0);  
        }
        
        1