diff --git a/lib/init.lua b/lib/init.lua index c20de2e..52349fb 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -109,6 +109,9 @@ function light_world:drawNormalShading(l,t,w,h,s) end end) + self.shadowShader:send('normalMap', self.normalMap) + self.shadowShader:send("invert_normal", self.normalInvert == true) + self.shadow_buffer:clear() for i = 1, #self.lights do local light = self.lights[i] @@ -131,14 +134,12 @@ function light_world:drawNormalShading(l,t,w,h,s) end end) -- draw scene for this light using normals and shadowmap - self.shadowShader:send('shadowMap', self.shadowMap) self.shadowShader:send('lightColor', {light.red / 255.0, light.green / 255.0, light.blue / 255.0}) self.shadowShader:send("lightPosition", {(light.x + l/s) * s, (h/s - (light.y + t/s)) * s, (light.z * 10) / 255.0}) self.shadowShader:send('lightRange',{light.range * s}) self.shadowShader:send("lightSmooth", light.smooth) self.shadowShader:send("lightGlow", {1.0 - light.glowSize, light.glowStrength}) - self.shadowShader:send("invert_normal", self.normalInvert == true) - util.drawCanvasToCanvas(self.normalMap, self.shadow_buffer, { + util.drawCanvasToCanvas(self.shadowMap, self.shadow_buffer, { blendmode = 'additive', shader = self.shadowShader, stencil = function() diff --git a/lib/shaders/shadow.glsl b/lib/shaders/shadow.glsl index 312a887..a4351f0 100644 --- a/lib/shaders/shadow.glsl +++ b/lib/shaders/shadow.glsl @@ -4,7 +4,7 @@ */ #define PI 3.1415926535897932384626433832795 -extern Image shadowMap; //a canvas containing shadow data only +extern Image normalMap; //a canvas containing shadow data only extern vec3 lightPosition; //the light position on the screen(not global) extern vec3 lightColor; //the rgb color of the light extern float lightRange; //the range of the light @@ -13,8 +13,7 @@ extern vec2 lightGlow; //how brightly the light bulb part glows extern bool invert_normal; //if the light should invert normals vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) { - vec4 pixelColor = Texel(texture, texture_coords); - vec4 shadowColor = Texel(shadowMap, texture_coords); + vec4 normalColor = Texel(normalMap, texture_coords); float dist = distance(lightPosition, vec3(pixel_coords, 1.0)); //if the pixel is within this lights range @@ -23,13 +22,13 @@ vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) { return vec4(0.0, 0.0, 0.0, 1.0); }else{ vec3 normal; - if(pixelColor.a > 0.0) { + if(normalColor.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); + normal = normalize(vec3(normalColor.r, 1 - normalColor.g, normalColor.b) * 2.0 - 1.0); } else { - normal = normalize(pixelColor.rgb * 2.0 - 1.0); + normal = normalize(normalColor.rgb * 2.0 - 1.0); } } else { // not on the normal map so it is the floor with a normal point strait up @@ -38,7 +37,7 @@ vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) { //calculater attenuation of light based on the distance float att = clamp((1.0 - dist / lightRange) / lightSmooth, 0.0, 1.0); // if not on the normal map draw attenuated shadows - if(pixelColor.a == 0.0) { + if(normalColor.a == 0.0) { //start with a dark color and add in the light color and shadow color vec4 pixel = vec4(0.0, 0.0, 0.0, 1.0); if (lightGlow.x < 1.0 && lightGlow.y > 0.0) { @@ -47,6 +46,7 @@ vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) { pixel.rgb = lightColor * pow(att, lightSmooth); } //If on the shadow map add the shadow color + vec4 shadowColor = Texel(texture, texture_coords); if(shadowColor.a > 0.0) { pixel.rgb = pixel.rgb * shadowColor.rgb; }