mirror of
https://github.com/tanema/light_world.lua.git
synced 2024-12-11 01:14:26 +00:00
added comments to the new shader and got rid of bad methods
This commit is contained in:
parent
9e59f9ffee
commit
66dd1b65f8
15
lib/init.lua
15
lib/init.lua
@ -29,7 +29,6 @@ local class = require(_PACKAGE..'class')
|
||||
local Light = require(_PACKAGE..'light')
|
||||
local Body = require(_PACKAGE..'body')
|
||||
local util = require(_PACKAGE..'util')
|
||||
local normal_map = require(_PACKAGE..'normal_map')
|
||||
local PostShader = require(_PACKAGE..'postshader')
|
||||
require(_PACKAGE..'postshader')
|
||||
|
||||
@ -323,26 +322,12 @@ function light_world:newRefraction(normal, x, y, width, height)
|
||||
return self:newBody("refraction", normal, x, y, width, height)
|
||||
end
|
||||
|
||||
-- new refraction from height map
|
||||
function light_world:newRefractionHeightMap(heightMap, x, y, strength)
|
||||
local normal = normal_map.fromHeightMap(heightMap, strength)
|
||||
self.isRefraction = true
|
||||
return self.newRefraction(p, normal, x, y)
|
||||
end
|
||||
|
||||
-- new reflection
|
||||
function light_world:newReflection(normal, x, y, width, height)
|
||||
self.isReflection = true
|
||||
return self:newBody("reflection", normal, x, y, width, height)
|
||||
end
|
||||
|
||||
-- new reflection from height map
|
||||
function light_world:newReflectionHeightMap(heightMap, x, y, strength)
|
||||
local normal = normal_map.fromHeightMap(heightMap, strength)
|
||||
self.isReflection = true
|
||||
return self.newReflection(p, normal, x, y)
|
||||
end
|
||||
|
||||
-- new body
|
||||
function light_world:newBody(type, ...)
|
||||
local id = #self.body + 1
|
||||
|
@ -1,16 +1,21 @@
|
||||
/*
|
||||
Copyright (c) 2014 Tim Anema
|
||||
light shadow, shine and normal shader all in one
|
||||
*/
|
||||
#define PI 3.1415926535897932384626433832795
|
||||
|
||||
extern vec2 screenResolution;
|
||||
extern Image shadowMap;
|
||||
extern vec3 lightPosition;
|
||||
extern vec3 lightColor;
|
||||
extern float lightRange;
|
||||
extern float lightSmooth;
|
||||
extern vec2 lightGlow;
|
||||
extern float lightDirection;
|
||||
extern float lightAngle;
|
||||
extern bool invert_normal;
|
||||
extern vec2 screenResolution; //size of the screen
|
||||
extern Image shadowMap; //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
|
||||
extern float lightSmooth; //smoothing of the lights attenuation
|
||||
extern vec2 lightGlow; //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
|
||||
|
||||
//calculate if a pixel is within the light slice
|
||||
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);
|
||||
@ -29,37 +34,47 @@ vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) {
|
||||
}
|
||||
|
||||
vec3 normal;
|
||||
//if on the normal map ie there is normal map data
|
||||
if(pixelColor.a > 0.0) {
|
||||
if(invert_normal == true) {
|
||||
//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);
|
||||
}
|
||||
float dist = distance(lightPosition, vec3(pixel_coords, normal.b));
|
||||
//if the pixel is within this lights range
|
||||
if(dist < lightRange) {
|
||||
//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) {
|
||||
//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) {
|
||||
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(shadowColor.a > 0.0) {
|
||||
pixel.rgb = pixel.rgb * shadowColor.rgb;
|
||||
}
|
||||
return pixel;
|
||||
} else {
|
||||
//on the normal map, draw normal shadows
|
||||
vec3 dir = vec3((lightPosition.xy - pixel_coords.xy) / screenResolution.xy, lightPosition.z);
|
||||
dir.x *= screenResolution.x / screenResolution.y;
|
||||
vec3 diff = lightColor * max(dot(normalize(normal), normalize(dir)), 0.0);
|
||||
//return the light that is effected by the normal and attenuation
|
||||
return vec4(diff * att, 1.0);
|
||||
}
|
||||
} else {
|
||||
//not in range draw in shadows
|
||||
return vec4(0.0, 0.0, 0.0, 1.0);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user