just a bit of a refactor that should be a bit of an optimizations because I wont be sending a buffer several time for no reason

This commit is contained in:
Tim Anema 2014-12-20 00:21:00 -05:00
parent 7fe549a01a
commit 3cafa72684
2 changed files with 11 additions and 10 deletions

View File

@ -109,6 +109,9 @@ function light_world:drawNormalShading(l,t,w,h,s)
end end
end) end)
self.shadowShader:send('normalMap', self.normalMap)
self.shadowShader:send("invert_normal", self.normalInvert == true)
self.shadow_buffer:clear() self.shadow_buffer:clear()
for i = 1, #self.lights do for i = 1, #self.lights do
local light = self.lights[i] local light = self.lights[i]
@ -131,14 +134,12 @@ function light_world:drawNormalShading(l,t,w,h,s)
end end
end) end)
-- draw scene for this light using normals and shadowmap -- 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('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("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('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) util.drawCanvasToCanvas(self.shadowMap, self.shadow_buffer, {
util.drawCanvasToCanvas(self.normalMap, self.shadow_buffer, {
blendmode = 'additive', blendmode = 'additive',
shader = self.shadowShader, shader = self.shadowShader,
stencil = function() stencil = function()

View File

@ -4,7 +4,7 @@
*/ */
#define PI 3.1415926535897932384626433832795 #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 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
@ -13,8 +13,7 @@ extern vec2 lightGlow; //how brightly the light bulb part glows
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 normalColor = Texel(normalMap, 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
@ -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); return vec4(0.0, 0.0, 0.0, 1.0);
}else{ }else{
vec3 normal; vec3 normal;
if(pixelColor.a > 0.0) { if(normalColor.a > 0.0) {
//if on the normal map ie there is normal map data //if on the normal map ie there is normal map data
//so get the normal data //so get the normal data
if(invert_normal) { 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 { } else {
normal = normalize(pixelColor.rgb * 2.0 - 1.0); normal = normalize(normalColor.rgb * 2.0 - 1.0);
} }
} else { } else {
// not on the normal map so it is the floor with a normal point strait up // 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 //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(normalColor.a == 0.0) {
//start with a dark color and add in the light color and shadow color //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); vec4 pixel = vec4(0.0, 0.0, 0.0, 1.0);
if (lightGlow.x < 1.0 && lightGlow.y > 0.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); 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(texture, texture_coords);
if(shadowColor.a > 0.0) { if(shadowColor.a > 0.0) {
pixel.rgb = pixel.rgb * shadowColor.rgb; pixel.rgb = pixel.rgb * shadowColor.rgb;
} }