light_world.lua/lib/shaders/poly_shadow.glsl

41 lines
1.4 KiB
Plaintext
Raw Normal View History

2014-03-16 00:12:18 +00:00
#define PI 3.1415926535897932384626433832795
2014-03-28 02:32:32 +00:00
extern vec3 lightPosition;
2014-03-04 13:52:51 +00:00
extern vec3 lightColor;
2014-03-16 00:12:18 +00:00
extern float lightRange;
2014-03-09 03:22:45 +00:00
extern float lightSmooth;
extern vec2 lightGlow;
2014-03-16 00:12:18 +00:00
extern float lightDirection;
extern float lightAngle;
2014-03-04 13:52:51 +00:00
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords){
vec4 pixel = Texel(texture, texture_coords);
2014-03-28 02:32:32 +00:00
vec3 lightToPixel = vec3(pixel_coords.x, pixel_coords.y, 0.0) - lightPosition;
2014-03-04 13:52:51 +00:00
float distance = length(lightToPixel);
2014-03-16 00:12:18 +00:00
float att = 1 - distance / lightRange;
if(lightAngle > 0.0) {
float angle2 = atan(lightPosition.x - pixel_coords.x, pixel_coords.y - lightPosition.y) + PI;
if(lightDirection - lightAngle > 0 && lightDirection + lightAngle < PI * 2) {
if(angle2 < mod(lightDirection + lightAngle, PI * 2) && angle2 > mod(lightDirection - lightAngle, PI * 2)) {
return vec4(0.0, 0.0, 0.0, 1.0);
}
} else {
if(angle2 < mod(lightDirection + lightAngle, PI * 2) || angle2 > mod(lightDirection - lightAngle, PI * 2)) {
return vec4(0.0, 0.0, 0.0, 1.0);
}
}
}
2014-03-04 13:52:51 +00:00
2014-03-16 00:12:18 +00:00
if (distance <= lightRange) {
2014-03-09 03:22:45 +00:00
if (lightGlow.x < 1.0 && lightGlow.y > 0.0) {
pixel.rgb = clamp(lightColor * pow(att, lightSmooth) + pow(smoothstep(lightGlow.x, 1.0, att), lightSmooth) * lightGlow.y, 0.0, 1.0);
2014-03-04 13:52:51 +00:00
} else {
2014-03-09 03:22:45 +00:00
pixel.rgb = lightColor * pow(att, lightSmooth);
2014-03-04 13:52:51 +00:00
}
} else {
2014-03-16 00:12:18 +00:00
return vec4(0.0, 0.0, 0.0, 1.0);
2014-03-04 13:52:51 +00:00
}
return pixel;
}