2014-09-26 16:48:46 +00:00
|
|
|
local _PACKAGE = (...):match("^(.+)[%./][^%./]+") or ""
|
|
|
|
local class = require(_PACKAGE.."/class")
|
2014-10-22 02:48:19 +00:00
|
|
|
local util = require(_PACKAGE..'/util')
|
2014-09-26 16:48:46 +00:00
|
|
|
|
2014-09-26 20:52:16 +00:00
|
|
|
local light = class()
|
|
|
|
|
2014-11-29 19:52:05 +00:00
|
|
|
light.shadowShader = love.graphics.newShader(_PACKAGE.."/shaders/shadow.glsl")
|
2014-09-29 14:03:15 +00:00
|
|
|
|
2014-10-03 00:32:31 +00:00
|
|
|
function light:init(x, y, r, g, b, range)
|
2014-09-26 20:52:16 +00:00
|
|
|
self.direction = 0
|
|
|
|
self.angle = math.pi * 2.0
|
|
|
|
self.range = 0
|
|
|
|
self.x = x or 0
|
|
|
|
self.y = y or 0
|
2014-11-28 15:20:00 +00:00
|
|
|
self.z = 1
|
2014-09-26 20:52:16 +00:00
|
|
|
self.red = r or 255
|
|
|
|
self.green = g or 255
|
|
|
|
self.blue = b or 255
|
|
|
|
self.range = range or 300
|
|
|
|
self.smooth = 1.0
|
|
|
|
self.glowSize = 0.1
|
|
|
|
self.glowStrength = 0.0
|
|
|
|
self.visible = true
|
2014-10-08 12:55:05 +00:00
|
|
|
self:refresh()
|
2014-10-03 00:32:31 +00:00
|
|
|
end
|
|
|
|
|
2014-10-08 12:55:05 +00:00
|
|
|
function light:refresh(w, h)
|
2014-12-02 01:41:09 +00:00
|
|
|
self.shadowShader:send('screenResolution', {w or love.window.getWidth(), h or love.window.getHeight()})
|
2014-09-26 20:52:16 +00:00
|
|
|
end
|
2014-09-26 16:48:46 +00:00
|
|
|
|
|
|
|
-- set position
|
|
|
|
function light:setPosition(x, y, z)
|
|
|
|
if x ~= self.x or y ~= self.y or (z and z ~= self.z) then
|
|
|
|
self.x = x
|
|
|
|
self.y = y
|
|
|
|
if z then
|
|
|
|
self.z = z
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-10 21:02:02 +00:00
|
|
|
-- move position
|
|
|
|
function light:move(x, y, z)
|
|
|
|
if x then
|
|
|
|
self.x = self.x + x
|
|
|
|
end
|
|
|
|
if y then
|
|
|
|
self.y = self.y + y
|
|
|
|
end
|
|
|
|
if z then
|
|
|
|
self.z = self.z + z
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-09-26 16:48:46 +00:00
|
|
|
-- get x
|
2014-10-26 19:27:35 +00:00
|
|
|
function light:getPosition()
|
2014-10-28 01:46:27 +00:00
|
|
|
return self.x, self.y, self.z
|
2014-09-26 16:48:46 +00:00
|
|
|
end
|
2014-10-03 00:32:31 +00:00
|
|
|
|
2014-09-26 16:48:46 +00:00
|
|
|
-- set color
|
|
|
|
function light:setColor(red, green, blue)
|
|
|
|
self.red = red
|
|
|
|
self.green = green
|
|
|
|
self.blue = blue
|
|
|
|
end
|
|
|
|
|
|
|
|
-- set range
|
|
|
|
function light:setRange(range)
|
|
|
|
if range ~= self.range then
|
|
|
|
self.range = range
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- set direction
|
|
|
|
function light:setDirection(direction)
|
|
|
|
if direction ~= self.direction then
|
|
|
|
if direction > math.pi * 2 then
|
|
|
|
self.direction = math.mod(direction, math.pi * 2)
|
|
|
|
elseif direction < 0.0 then
|
|
|
|
self.direction = math.pi * 2 - math.mod(math.abs(direction), math.pi * 2)
|
|
|
|
else
|
|
|
|
self.direction = direction
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-09-27 17:46:46 +00:00
|
|
|
|
2014-09-26 16:48:46 +00:00
|
|
|
-- set angle
|
|
|
|
function light:setAngle(angle)
|
|
|
|
if angle ~= self.angle then
|
|
|
|
if angle > math.pi then
|
|
|
|
self.angle = math.mod(angle, math.pi)
|
|
|
|
elseif angle < 0.0 then
|
|
|
|
self.angle = math.pi - math.mod(math.abs(angle), math.pi)
|
|
|
|
else
|
|
|
|
self.angle = angle
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-09-27 17:46:46 +00:00
|
|
|
|
2014-09-26 16:48:46 +00:00
|
|
|
-- set glow size
|
|
|
|
function light:setSmooth(smooth)
|
|
|
|
self.smooth = smooth
|
|
|
|
end
|
2014-09-27 17:46:46 +00:00
|
|
|
|
2014-09-26 16:48:46 +00:00
|
|
|
-- set glow size
|
|
|
|
function light:setGlowSize(size)
|
|
|
|
self.glowSize = size
|
|
|
|
end
|
2014-09-27 17:46:46 +00:00
|
|
|
|
2014-09-26 16:48:46 +00:00
|
|
|
-- set glow strength
|
|
|
|
function light:setGlowStrength(strength)
|
|
|
|
self.glowStrength = strength
|
|
|
|
end
|
|
|
|
|
2014-11-06 04:05:21 +00:00
|
|
|
function light:inRange(l,t,w,h,s)
|
|
|
|
local lx, ly, rs = (self.x + l/s) * s, (self.y + t/s) * s, self.range * s
|
2014-12-03 19:08:44 +00:00
|
|
|
return self.visible and (lx + rs) > 0 and (lx - rs) < w/s and (ly + rs) > 0 and (ly - rs) < h/s
|
2014-09-26 22:11:23 +00:00
|
|
|
end
|
|
|
|
|
2014-11-30 00:10:15 +00:00
|
|
|
function light:drawNormalShading(l,t,w,h,s, normalMap, shadowMap, canvas)
|
2014-12-03 19:08:44 +00:00
|
|
|
self.shadowShader:send('shadowMap', shadowMap)
|
|
|
|
self.shadowShader:send('lightColor', {self.red / 255.0, self.green / 255.0, self.blue / 255.0})
|
|
|
|
self.shadowShader:send("lightPosition", {(self.x + l/s) * s, (h/s - (self.y + t/s)) * s, (self.z * 10) / 255.0})
|
|
|
|
self.shadowShader:send('lightRange',{self.range * s})
|
|
|
|
self.shadowShader:send("lightSmooth", self.smooth)
|
|
|
|
self.shadowShader:send("lightGlow", {1.0 - self.glowSize, self.glowStrength})
|
|
|
|
self.shadowShader:send("lightAngle", math.pi - self.angle / 2.0)
|
|
|
|
self.shadowShader:send("lightDirection", self.direction)
|
|
|
|
self.shadowShader:send("invert_normal", self.normalInvert == true)
|
|
|
|
util.drawCanvasToCanvas(normalMap, canvas, {
|
|
|
|
blendmode = 'additive',
|
|
|
|
shader = self.shadowShader
|
|
|
|
})
|
2014-09-26 22:11:23 +00:00
|
|
|
end
|
|
|
|
|
2014-11-06 04:06:54 +00:00
|
|
|
function light:setVisible(visible)
|
|
|
|
self.visible = visible
|
|
|
|
end
|
|
|
|
|
2014-09-26 16:48:46 +00:00
|
|
|
return light
|