2D sdf lighting shader without shadows or light occlusion + (optional) pixelated lighting

it is a 2d light without shadows or light occlusion.

it uses sdf to add onto sdf occluders its own light.

it also can and should be used with itself for darkness by turning on the “night”.

Only areas w/ SDF occluder will be occluded(will block light)

It should be used w/ ColorRect

the scene tree should be made like:

–rest of the game

-the darkening node 

–all of the lights

also

if you turn on the night then set color alpha to 0

hdr 2d needs to be turned off otherwise it will break 

for pixeletion effect you will need to turn it on and adjust the setting the pixel_size is defult to 128 so it is pixel to pixel

note:

when the “night” is true for lights overall color should be a lot darker and for white color should be black and energy should be 1.0

Shader code
shader_type canvas_item;
render_mode unshaded;

uniform sampler2D SCREEN_TEXTURE : hint_screen_texture, filter_linear_mipmap;
uniform vec2 middle=vec2(0.5,0.5);
uniform float energy=.7;
uniform vec4 color: source_color;
uniform float size=1.0;
uniform float light_lenght=0.9;
//uniform vec4 shadow_darkness: source_color; 
// basically turns screen dark
uniform bool dark=false;
uniform bool light=true;

uniform bool pixelated_light = false;
uniform float pixel_size=128;

void fragment(){
	if (light==true){
		vec2 sdf_pos = screen_uv_to_sdf(SCREEN_UV);
		float sdf_tex=texture_sdf(sdf_pos);
		float inside=0.0;
		//
		vec2 pixel_uv = floor(UV *pixel_size) / pixel_size;
		float dist;
		if (pixelated_light==true)
			 dist=distance(middle*2.0/size,(pixel_uv*2.0/size));
		if (pixelated_light==false)
			 dist=distance(middle*2.0/size,(UV*2.0/size));
		vec4 acc_col_wout_sh=vec4(0.0);
		//
		vec4 screen_col=texture(SCREEN_TEXTURE,SCREEN_UV);
		//defining the inside
		if (sdf_tex<=-0.1)
			inside=sdf_tex;
		vec3 col = (color.rgb+screen_col.rgb);
		float sdf_light=((inside-inside*light_lenght));
		float fade_away=color.a-(dist);
		acc_col_wout_sh += (vec4(col,((fade_away)+(sdf_light))))*energy;
		//
		//
		if (dark==false){
			COLOR = vec4(acc_col_wout_sh.rgb,acc_col_wout_sh.a);
			}
		if (dark==true)
			COLOR = vec4(0.0,0.0,0.0,1.0);
		// for (int i = 0; i < 10; i++)
		}
}
Live Preview
Tags
2d, light, pixel, SDF
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.

Related shaders

guest

0 Comments
Oldest
Newest Most Voted