added in another example for using just the postshader, and added a readme for github

This commit is contained in:
Tim Anema 2014-10-24 14:32:12 -04:00
parent c9fac640d7
commit 00331a51c1
3 changed files with 180 additions and 0 deletions

55
README.md Normal file
View File

@ -0,0 +1,55 @@
# light_world.lua
This is the light modeling done by Priorblue [here](https://bitbucket.org/PriorBlue/love2d-light-and-shadow-engine),
only it has been largely refactored and edited to allow for scaling and proper translation.
## Features ##
* **[Preview (Video)](https://www.youtube.com/watch?v=6V5Dtsa6Nd4)**
* polygon shadow calculation [Preview](http://onepixelahead.de/love2d_polyshadow.png)
* circle shadow calculation
* image shadow calculation [Preview](http://onepixelahead.de/love2d_polyshadow18.png)
* shadow blur
* light color, range, smooth and glow [Preview](http://onepixelahead.de/love2d_polyshadow2.png)
* ambient light
* self shadowing on images with normal maps [Preview](http://onepixelahead.de/love2d_polyshadow_pixelshadow.png)
* dynamic glow effect on images and circle/poly objects [Preview](http://onepixelahead.de/love2d_polyshadow_glow.png) [Preview](http://onepixelahead.de/love2d_polyshadow15.gif)
* generate flat or gradient normal maps [Preview](http://onepixelahead.de/love2d_polyshadow7.png)
* convert height maps to normal maps [Preview](http://onepixelahead.de/love2d_polyshadow8.png)
* generate a normal map directly from the image (usually gives poor results)
* shadow color and alpha (glass) [Preview](http://onepixelahead.de/love2d_polyshadow9.png)
* directional light [Preview](http://onepixelahead.de/love2d_polyshadow12.png)
* refractions (moveable) [Preview](http://onepixelahead.de/love2d_polyshadow13.gif)
* chromatic aberration [Preview](http://onepixelahead.de/love2d_polyshadow16.gif)
* postshader with many included postshaders, plus easy to extend
## Installation
copy the lib folder into your project at any path
## How to use
```lua
local LightWorld = require "lib/light_world"
-- create light world
lightWorld = LightWorld({
drawBackground = drawBackground, //the callback to use for drawing the background
drawForground = drawForground, //the callback to use for drawing the foreground
ambient = {55,55,55}, //the general ambient light in the environment
})
function love.draw()
love.graphics.push()
love.graphics.translate(x, y)
love.graphics.scale(scale)
lightWorld:draw(x,y,scale)
love.graphics.pop()
end
```
please see the examples directory to see how it is fully used. also this project can
be run to show you the deomnstrations.
## License
A License has been included in this project

119
examples/postshaders.lua Normal file
View File

@ -0,0 +1,119 @@
-- Example: Only Postshader Example
local PostShader = require "lib/postshader"
function love.load()
testShader = 0
x = 0
y = 0
scale = 1
colorAberration = 0.0
-- load images
image = love.graphics.newImage("gfx/machine2.png")
quadScreen = love.graphics.newQuad(0, 0, love.window.getWidth() + 32, love.window.getHeight() + 24, 32, 24)
imgFloor = love.graphics.newImage("gfx/floor.png")
imgFloor:setWrap("repeat", "repeat")
post_shader = PostShader()
render_buffer = love.graphics.newCanvas(love.window.getWidth(), love.window.getHeight())
end
function love.keypressed(k)
if k == "1" then
post_shader:toggleEffect("four_colors", {15, 56, 15}, {48, 98, 48}, {139, 172, 15}, {155, 188, 15})
elseif k == "2" then
post_shader:toggleEffect("monochrome")
elseif k == "3" then
post_shader:toggleEffect("scanlines")
elseif k == "4" then
post_shader:toggleEffect("tilt_shift", 4.0)
elseif k == "5" then
post_shader:toggleEffect("bloom", 2.0, 0.25)
elseif k == "6" then
post_shader:toggleEffect("blur", 2.0, 2.0)
elseif k == "7" then
post_shader:toggleEffect("black_and_white")
elseif k == "8" then
post_shader:toggleEffect("curvature")
elseif k == "9" then
post_shader:toggleEffect("edges")
elseif k == "0" then
post_shader:toggleEffect("hdr_tv")
elseif k == "q" then
post_shader:toggleEffect("phosphor")
elseif k == "w" then
post_shader:toggleEffect("phosphorish")
elseif k == "e" then
post_shader:toggleEffect("pip")
elseif k == "r" then
post_shader:toggleEffect("pixellate")
elseif k == "t" then
post_shader:toggleEffect("radialblur")
elseif k == "y" then
post_shader:toggleEffect("waterpaint")
elseif k == "c" then
if colorAberration == 0.0 then
colorAberration = 3.0
end
end
end
function love.update(dt)
love.window.setTitle("Light vs. Shadow Engine (FPS:" .. love.timer.getFPS() .. ")")
if love.keyboard.isDown("up") then
y = y - dt * 200
elseif love.keyboard.isDown("down") then
y = y + dt * 200
end
if love.keyboard.isDown("left") then
x = x - dt * 200
elseif love.keyboard.isDown("right") then
x = x + dt * 200
end
if love.keyboard.isDown("-") then
scale = scale - 0.01
elseif love.keyboard.isDown("=") then
scale = scale + 0.01
end
colorAberration = math.max(0.0, colorAberration - dt * 10.0)
if colorAberration > 0.0 then
post_shader:addEffect("blur", 2.0, 2.0)
post_shader:addEffect("chromatic_aberration")
else
post_shader:removeEffect("blur")
post_shader:removeEffect("chromatic_aberration")
end
lightMouse:setPosition(love.mouse.getX()/scale, love.mouse.getY()/scale)
end
function love.draw()
local w, h = love.graphics.getWidth(), love.graphics.getHeight()
render_buffer:clear()
love.graphics.push()
love.graphics.setCanvas(render_buffer)
love.graphics.translate(x, y)
love.graphics.scale(scale)
love.graphics.setColor(255, 255, 255)
love.graphics.draw(imgFloor, quadScreen, 0,0)
love.graphics.setColor(63, 255, 127)
love.graphics.circle("fill", 256, 256, 16)
love.graphics.rectangle("fill", 512, 512, 64, 64)
love.graphics.setColor(255, 255, 255)
love.graphics.draw(image, 64 - image:getWidth() * 0.5, 64 - image:getHeight() * 0.5)
love.graphics.pop()
love.graphics.setCanvas()
post_shader:drawWith(render_buffer)
love.graphics.setBlendMode("alpha")
love.graphics.setColor(0, 0, 0, 191)
love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), 24)
love.graphics.setColor(0, 255, 0)
love.graphics.print("To toggle postshaders, use 0-9 and q->y, to scale use - and =, and to translate use arrows")
end

View File

@ -119,6 +119,12 @@ function love.draw()
love.graphics.scale(scale)
lightWorld:draw(x,y,scale)
love.graphics.pop()
love.graphics.setBlendMode("alpha")
love.graphics.setColor(0, 0, 0, 191)
love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), 24)
love.graphics.setColor(0, 255, 0)
love.graphics.print("To toggle postshaders, use 0-9 and q->y, to scale use - and =, and to translate use arrows")
end
function drawBackground(l,t,w,h)