From 9e59f9ffeec9a98e9fd97747d224c4046123a850 Mon Sep 17 00:00:00 2001 From: Tim Anema Date: Sun, 30 Nov 2014 14:35:18 -0500 Subject: [PATCH] colored shadows now work as expected with attenuated brightness and constant brightness not matter how many lights are present --- lib/init.lua | 17 +++++------------ lib/shaders/shadow.glsl | 33 +++++++++++++++------------------ 2 files changed, 20 insertions(+), 30 deletions(-) diff --git a/lib/init.lua b/lib/init.lua index 6bfd69a..5d0cc2c 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -108,17 +108,6 @@ function light_world:draw(l,t,s) self.post_shader:drawWith(self.render_buffer, l, t, s) end -function light_world:drawBlur(blendmode, blur, canvas, canvas2, l, t, w, h, s) - if blur <= 0 then - return - end - canvas2:clear() - self.blurv:send("steps", blur) - self.blurh:send("steps", blur) - util.drawCanvasToCanvas(canvas, canvas2, {shader = self.blurv, blendmode = blendmode}) - util.drawCanvasToCanvas(canvas2, canvas, {shader = self.blurh, blendmode = blendmode}) -end - -- draw normal shading function light_world:drawNormalShading(l,t,w,h,s) if not self.isShadows then @@ -187,7 +176,11 @@ function light_world:drawGlow(l,t,w,h,s) end end) - light_world:drawBlur("alpha", self.glowBlur, self.glowMap, self.glowMap2, l, t, w, h, s) + self.glowMap2:clear() + self.blurv:send("steps", self.glowBlur) + self.blurh:send("steps", self.glowBlur) + util.drawCanvasToCanvas(self.glowMap, self.glowMap2, {shader = self.blurv, blendmode = "alpha"}) + util.drawCanvasToCanvas(self.glowMap2, self.glowMap, {shader = self.blurh, blendmode = "alpha"}) util.drawCanvasToCanvas(self.glowMap, self.render_buffer, {blendmode = "additive"}) end -- draw refraction diff --git a/lib/shaders/shadow.glsl b/lib/shaders/shadow.glsl index 0c83dfa..f54c117 100644 --- a/lib/shaders/shadow.glsl +++ b/lib/shaders/shadow.glsl @@ -11,22 +11,21 @@ extern float lightDirection; extern float lightAngle; extern bool invert_normal; +bool not_in_slice(vec2 pixel_coords){ + float angle = atan(lightPosition.x - pixel_coords.x, pixel_coords.y - lightPosition.y) + PI; + bool pastRightSide = angle < mod(lightDirection + lightAngle, PI * 2); + bool pastLeftSide = angle > mod(lightDirection - lightAngle, PI * 2); + bool lightUp = lightDirection - lightAngle > 0 && lightDirection + lightAngle < PI * 2; + return (lightUp && (pastRightSide && pastLeftSide)) || (!lightUp && (pastRightSide || pastLeftSide)); +} + vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) { vec4 pixelColor = Texel(texture, texture_coords); vec4 shadowColor = Texel(shadowMap, texture_coords); //if the light is a slice and the pixel is not inside - 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); - } - } + if(lightAngle > 0.0 && not_in_slice(pixel_coords)) { + return vec4(0.0, 0.0, 0.0, 1.0); } vec3 normal; @@ -44,18 +43,16 @@ vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) { if(dist < lightRange) { float att = clamp((1.0 - dist / lightRange) / lightSmooth, 0.0, 1.0); if(pixelColor.a == 0.0) { - vec4 val = pixelColor; - val.a = 1.0; + vec4 pixel = vec4(0.0, 0.0, 0.0, 1.0); if (lightGlow.x < 1.0 && lightGlow.y > 0.0) { - val.rgb = clamp(lightColor * pow(att, lightSmooth) + pow(smoothstep(lightGlow.x, 1.0, att), lightSmooth) * lightGlow.y, 0.0, 1.0); + pixel.rgb = clamp(lightColor * pow(att, lightSmooth) + pow(smoothstep(lightGlow.x, 1.0, att), lightSmooth) * lightGlow.y, 0.0, 1.0); } else { - val.rgb = lightColor * pow(att, lightSmooth); + pixel.rgb = lightColor * pow(att, lightSmooth); } if(shadowColor.a > 0.0) { - shadowColor.a = 1.0 - shadowColor.a; - val = val * shadowColor; + pixel.rgb = pixel.rgb * shadowColor.rgb; } - return val; + return pixel; } else { vec3 dir = vec3((lightPosition.xy - pixel_coords.xy) / screenResolution.xy, lightPosition.z); dir.x *= screenResolution.x / screenResolution.y;