From 6f23c384bb07db5e0c4bdaf0a0340d0af47475d8 Mon Sep 17 00:00:00 2001 From: drumsetmonkey Date: Wed, 8 Jun 2016 08:41:44 -0400 Subject: Lighting! --- shaders/world.frag | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) (limited to 'shaders/world.frag') diff --git a/shaders/world.frag b/shaders/world.frag index 3177795..bde5fa5 100644 --- a/shaders/world.frag +++ b/shaders/world.frag @@ -3,7 +3,7 @@ uniform sampler2D normalTex; varying vec2 texCoord; varying vec4 color; -varying vec3 fragCoord; +varying vec4 fragCoord; uniform vec4 ambientLight; uniform vec4 light[128]; @@ -11,26 +11,25 @@ uniform vec4 lightColor[128]; uniform float lightImpact; uniform int lightSize; -float b = .0005f; -float minLight = .05f; -float radius = sqrt(1.0f / (b * minLight)); - void main() { - vec4 pixTex = texture2D(texture, vec2(texCoord.x, 1-texCoord.y)); + vec2 texLoc = vec2(texCoord.x, 1-texCoord.y); + vec4 pixTex = texture2D(texture, texLoc); if (pixTex.a < 0.1f) discard; - + vec4 shadeColor = vec4(0.0f, 0.0f, 0.0f, 0.0f); - for (int i = 0; i < lightSize; i++) { - vec2 loc = light[i].xy; - float dist = length(loc - fragCoord.xy); - float attenuation = clamp(1.0f - dist*dist/(radius*radius), 0.0f, 1.0f); - attenuation *= attenuation; - - shadeColor += vec4(attenuation, attenuation, attenuation, 1.0f) * vec4(lightColor[i]); + if (lightImpact > 0.0f) { + for (int i = 0; i < lightSize; i++) { + vec2 loc = light[i].xy; + float dist = length(loc - fragCoord.xy); + float attenuation = clamp(1.0f - dist*dist/(light[i].w*light[i].w), 0.0f, 1.0f); + attenuation *= attenuation; + + shadeColor += (vec4(attenuation, attenuation, attenuation, 0.0f) * vec4(lightColor[i])) * lightImpact; + } } - shadeColor += ambientLight; + shadeColor += ambientLight; gl_FragColor = pixTex * color * shadeColor; } -- cgit v1.2.3