slimming down and optimization of shadow shader

This commit is contained in:
Tim Anema 2014-12-14 01:14:42 -05:00
parent 81bcb0e347
commit 08627c3c50
2 changed files with 12 additions and 33 deletions

View File

@ -149,7 +149,7 @@ function light_world:drawNormalShading(l,t,w,h,s)
self.shadowShader:send('lightRange',{light.range * s}) self.shadowShader:send('lightRange',{light.range * s})
self.shadowShader:send("lightSmooth", light.smooth) self.shadowShader:send("lightSmooth", light.smooth)
self.shadowShader:send("lightGlow", {1.0 - light.glowSize, light.glowStrength}) self.shadowShader:send("lightGlow", {1.0 - light.glowSize, light.glowStrength})
self.shadowShader:send("invert_normal", self.normalInvert == true) self.shadowShader:send("invert_normal", self.normalInvert)
util.drawCanvasToCanvas(self.normalMap, self.normal2, { util.drawCanvasToCanvas(self.normalMap, self.normal2, {
blendmode = 'additive', blendmode = 'additive',
shader = self.shadowShader shader = self.shadowShader

View File

@ -5,61 +5,40 @@
#define PI 3.1415926535897932384626433832795 #define PI 3.1415926535897932384626433832795
extern Image shadowMap; //a canvas containing shadow data only extern Image shadowMap; //a canvas containing shadow data only
extern vec3 lightPosition; //the light position on the screen(not global) extern vec3 lightPosition; //the light position on the screen(not global)
extern vec3 lightColor; //the rgb color of the light extern vec3 lightColor; //the rgb color of the light
extern float lightRange; //the range of the light extern float lightRange; //the range of the light
extern float lightSmooth; //smoothing of the lights attenuation extern float lightSmooth; //smoothing of the lights attenuation
extern vec2 lightGlow; //how brightly the light bulb part glows extern vec2 lightGlow = vec2(0.5, 0.5); //how brightly the light bulb part glows
extern float lightAngle; //if set, the light becomes directional to a slice lightAngle degrees wide
extern float lightDirection; //which direction to shine the light in if directional in degrees
extern bool invert_normal; //if the light should invert normals extern bool invert_normal; //if the light should invert normals
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) { vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) {
vec4 pixelColor = Texel(texture, texture_coords); vec4 pixelColor = Texel(texture, texture_coords);
vec4 shadowColor = Texel(shadowMap, texture_coords);
float dist = distance(lightPosition, vec3(pixel_coords, 1.0)); float dist = distance(lightPosition, vec3(pixel_coords, 1.0));
//if the pixel is within this lights range //if the pixel is within this lights range
if(dist > lightRange) { if(dist > lightRange) {
//not in range draw in shadows
return vec4(0.0, 0.0, 0.0, 1.0); return vec4(0.0, 0.0, 0.0, 1.0);
}else{ }else{
vec3 normal;
if(pixelColor.a > 0.0) {
//if on the normal map ie there is normal map data
//so get the normal data
if(invert_normal) {
normal = normalize(vec3(pixelColor.r, 1 - pixelColor.g, pixelColor.b) * 2.0 - 1.0);
} else {
normal = normalize(pixelColor.rgb * 2.0 - 1.0);
}
} else {
// not on the normal map so it is the floor with a normal point strait up
normal = vec3(0.0, 0.0, 1.0);
}
//calculater attenuation of light based on the distance //calculater attenuation of light based on the distance
float att = clamp((1.0 - dist / lightRange) / lightSmooth, 0.0, 1.0); float att = clamp((1.0 - dist / lightRange) / lightSmooth, 0.0, 1.0);
// if not on the normal map draw attenuated shadows // if not on the normal map draw attenuated shadows
if(pixelColor.a == 0.0) { if(pixelColor.a == 0.0) {
//start with a dark color and add in the light color and shadow color vec3 pixel = lightColor * pow(att, lightSmooth) + pow(smoothstep(lightGlow.x, 1.0, att), lightSmooth) * lightGlow.y;
vec4 pixel = vec4(0.0, 0.0, 0.0, 1.0);
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);
} else {
pixel.rgb = lightColor * pow(att, lightSmooth);
}
//If on the shadow map add the shadow color //If on the shadow map add the shadow color
vec4 shadowColor = Texel(shadowMap, texture_coords);
if(shadowColor.a > 0.0) { if(shadowColor.a > 0.0) {
pixel.rgb = pixel.rgb * shadowColor.rgb; pixel.rgb = pixel.rgb * shadowColor.rgb;
} }
return pixel; return vec4(pixel, 1.0);
} else { } else {
//on the normal map, draw normal shadows //on the normal map, draw normal shadows
vec3 dir = vec3((lightPosition.xy - pixel_coords.xy) / love_ScreenSize.xy, lightPosition.z); vec3 lightDir = vec3((lightPosition.xy - pixel_coords.xy) / love_ScreenSize.xy, lightPosition.z);
dir.x *= love_ScreenSize.x / love_ScreenSize.y; lightDir.x *= love_ScreenSize.x / love_ScreenSize.y;
vec3 diff = lightColor * max(dot(normalize(normal), normalize(dir)), 0.0); vec3 normal = normalize(vec3(pixelColor.r,(invert_normal ? 1 - pixelColor.g : pixelColor.g), pixelColor.b) * 2.0 - 1.0);
vec3 diffuse = lightColor * max(dot(normalize(normal), normalize(lightDir)), 0.0);
//return the light that is effected by the normal and attenuation //return the light that is effected by the normal and attenuation
return vec4(diff * att, 1.0); return vec4(diffuse * att, 1.0);
} }
} }
} }