mirror of
https://github.com/tanema/light_world.lua.git
synced 2024-12-24 20:24:19 +00:00
refactoring,
taking out unused variables and redundant code, also took out debug variables. also moved the shaders in to the lib folder for better portablility of the library
This commit is contained in:
parent
9579dc0b15
commit
45be0c56fa
18
lib/body.lua
18
lib/body.lua
@ -4,6 +4,9 @@ local height_map_conv = require(_PACKAGE..'/height_map_conv')
|
||||
|
||||
local body = class()
|
||||
|
||||
body.glowShader = love.graphics.newShader(_PACKAGE.."/shaders/glow.glsl")
|
||||
body.materialShader = love.graphics.newShader(_PACKAGE.."/shaders/material.glsl")
|
||||
|
||||
function body:init(world, id, type, ...)
|
||||
local args = {...}
|
||||
self.id = id
|
||||
@ -13,11 +16,6 @@ function body:init(world, id, type, ...)
|
||||
self.glow = nil
|
||||
self.world = world
|
||||
|
||||
self.reflection = false
|
||||
self.reflective = false
|
||||
self.refraction = false
|
||||
self.refractive = false
|
||||
|
||||
self.shine = true
|
||||
self.red = 0
|
||||
self.green = 0
|
||||
@ -600,9 +598,9 @@ function body:drawGlow()
|
||||
love.graphics.polygon("fill", unpack(self.data))
|
||||
elseif self.type == "image" and self.img then
|
||||
if self.glowStrength > 0.0 and self.glow then
|
||||
love.graphics.setShader(self.world.glowShader)
|
||||
self.world.glowShader:send("glowImage", self.glow)
|
||||
self.world.glowShader:send("glowTime", love.timer.getTime() * 0.5)
|
||||
love.graphics.setShader(self.glowShader)
|
||||
self.glowShader:send("glowImage", self.glow)
|
||||
self.glowShader:send("glowTime", love.timer.getTime() * 0.5)
|
||||
love.graphics.setColor(255, 255, 255)
|
||||
else
|
||||
love.graphics.setShader()
|
||||
@ -657,9 +655,11 @@ end
|
||||
|
||||
function body:drawMaterial()
|
||||
if self.material and self.normal then
|
||||
love.graphics.setShader(self.materialShader)
|
||||
love.graphics.setColor(255, 255, 255)
|
||||
self.world.materialShader:send("material", self.material)
|
||||
self.materialShader:send("material", self.material)
|
||||
love.graphics.draw(self.normal, self.x - self.nx + self.world.translate_x, self.y - self.ny + self.world.translate_y)
|
||||
love.graphics.setShader()
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -5,6 +5,10 @@ local vector = require(_PACKAGE..'/vector')
|
||||
|
||||
local light = class()
|
||||
|
||||
light.shader = love.graphics.newShader(_PACKAGE.."/shaders/poly_shadow.glsl")
|
||||
light.normalShader = love.graphics.newShader(_PACKAGE.."/shaders/normal.glsl")
|
||||
light.normalInvertShader = love.graphics.newShader(_PACKAGE.."/shaders/normal_invert.glsl")
|
||||
|
||||
function light:init(world, x, y, r, g, b, range)
|
||||
self.world = world
|
||||
self.direction = 0
|
||||
@ -115,18 +119,18 @@ function light:setGlowStrength(strength)
|
||||
end
|
||||
|
||||
function light:updateShadow()
|
||||
love.graphics.setShader(self.shader)
|
||||
if self.x + self.range > self.world.translate_x and self.x - self.range < love.graphics.getWidth() + self.world.translate_x
|
||||
and self.y + self.range > self.world.translate_y and self.y - self.range < love.graphics.getHeight() + self.world.translate_y
|
||||
then
|
||||
local lightposrange = {self.x, love.graphics.getHeight() - self.y, self.range}
|
||||
self.world.direction = self.world.direction + 0.002
|
||||
self.world.shader:send("lightPosition", {self.x - self.world.translate_x, love.graphics.getHeight() - (self.y - self.world.translate_y), self.z})
|
||||
self.world.shader:send("lightRange", self.range)
|
||||
self.world.shader:send("lightColor", {self.red / 255.0, self.green / 255.0, self.blue / 255.0})
|
||||
self.world.shader:send("lightSmooth", self.smooth)
|
||||
self.world.shader:send("lightGlow", {1.0 - self.glowSize, self.glowStrength})
|
||||
self.world.shader:send("lightAngle", math.pi - self.angle / 2.0)
|
||||
self.world.shader:send("lightDirection", self.direction)
|
||||
self.shader:send("lightPosition", {self.x - self.world.translate_x, love.graphics.getHeight() - (self.y - self.world.translate_y), self.z})
|
||||
self.shader:send("lightRange", self.range)
|
||||
self.shader:send("lightColor", {self.red / 255.0, self.green / 255.0, self.blue / 255.0})
|
||||
self.shader:send("lightSmooth", self.smooth)
|
||||
self.shader:send("lightGlow", {1.0 - self.glowSize, self.glowStrength})
|
||||
self.shader:send("lightAngle", math.pi - self.angle / 2.0)
|
||||
self.shader:send("lightDirection", self.direction)
|
||||
|
||||
love.graphics.setCanvas(self.shadow)
|
||||
love.graphics.clear()
|
||||
@ -158,7 +162,7 @@ function light:updateShadow()
|
||||
self.world.body[k]:drawShadow(self)
|
||||
end
|
||||
|
||||
love.graphics.setShader(self.world.shader)
|
||||
love.graphics.setShader(self.shader)
|
||||
|
||||
-- draw shine
|
||||
love.graphics.setCanvas(self.shine)
|
||||
@ -172,6 +176,7 @@ function light:updateShadow()
|
||||
else
|
||||
self.visible = false
|
||||
end
|
||||
love.graphics.setShader()
|
||||
end
|
||||
|
||||
function light:drawShadow()
|
||||
@ -301,23 +306,23 @@ end
|
||||
function light:drawPixelShadow()
|
||||
if self.visible then
|
||||
if self.normalInvert then
|
||||
self.world.normalInvertShader:send('screenResolution', {love.graphics.getWidth(), love.graphics.getHeight()})
|
||||
self.world.normalInvertShader:send('lightColor', {self.red / 255.0, self.green / 255.0, self.blue / 255.0})
|
||||
self.world.normalInvertShader:send('lightPosition',{self.x, love.graphics.getHeight() - self.y, self.z / 255.0})
|
||||
self.world.normalInvertShader:send('lightRange',{self.range})
|
||||
self.world.normalInvertShader:send("lightSmooth", self.smooth)
|
||||
self.world.normalInvertShader:send("lightAngle", math.pi - self.angle / 2.0)
|
||||
self.world.normalInvertShader:send("lightDirection", self.direction)
|
||||
love.graphics.setShader(self.world.normalInvertShader)
|
||||
self.normalInvertShader:send('screenResolution', {love.graphics.getWidth(), love.graphics.getHeight()})
|
||||
self.normalInvertShader:send('lightColor', {self.red / 255.0, self.green / 255.0, self.blue / 255.0})
|
||||
self.normalInvertShader:send('lightPosition',{self.x, love.graphics.getHeight() - self.y, self.z / 255.0})
|
||||
self.normalInvertShader:send('lightRange',{self.range})
|
||||
self.normalInvertShader:send("lightSmooth", self.smooth)
|
||||
self.normalInvertShader:send("lightAngle", math.pi - self.angle / 2.0)
|
||||
self.normalInvertShader:send("lightDirection", self.direction)
|
||||
love.graphics.setShader(self.normalInvertShader)
|
||||
else
|
||||
self.world.normalShader:send('screenResolution', {love.graphics.getWidth(), love.graphics.getHeight()})
|
||||
self.world.normalShader:send('lightColor', {self.red / 255.0, self.green / 255.0, self.blue / 255.0})
|
||||
self.world.normalShader:send('lightPosition',{self.x, love.graphics.getHeight() - self.y, self.z / 255.0})
|
||||
self.world.normalShader:send('lightRange',{self.range})
|
||||
self.world.normalShader:send("lightSmooth", self.smooth)
|
||||
self.world.normalShader:send("lightAngle", math.pi - self.angle / 2.0)
|
||||
self.world.normalShader:send("lightDirection", self.direction)
|
||||
love.graphics.setShader(self.world.normalShader)
|
||||
self.normalShader:send('screenResolution', {love.graphics.getWidth(), love.graphics.getHeight()})
|
||||
self.normalShader:send('lightColor', {self.red / 255.0, self.green / 255.0, self.blue / 255.0})
|
||||
self.normalShader:send('lightPosition',{self.x, love.graphics.getHeight() - self.y, self.z / 255.0})
|
||||
self.normalShader:send('lightRange',{self.range})
|
||||
self.normalShader:send("lightSmooth", self.smooth)
|
||||
self.normalShader:send("lightAngle", math.pi - self.angle / 2.0)
|
||||
self.normalShader:send("lightDirection", self.direction)
|
||||
love.graphics.setShader(self.normalShader)
|
||||
end
|
||||
love.graphics.draw(self.world.normalMap, self.world.translate_x, self.world.translate_y)
|
||||
end
|
||||
|
@ -29,82 +29,49 @@ local height_map_conv = require(_PACKAGE..'/height_map_conv')
|
||||
|
||||
local light_world = class()
|
||||
|
||||
light_world.blurv = love.graphics.newShader(_PACKAGE.."/shaders/blurv.glsl")
|
||||
light_world.blurh = love.graphics.newShader(_PACKAGE.."/shaders/blurh.glsl")
|
||||
light_world.refractionShader = love.graphics.newShader(_PACKAGE.."/shaders/refraction.glsl")
|
||||
light_world.reflectionShader = love.graphics.newShader(_PACKAGE.."/shaders/reflection.glsl")
|
||||
|
||||
light_world.blurv:send("screen", {love.window.getWidth(), love.window.getHeight()})
|
||||
light_world.blurh:send("screen", {love.window.getWidth(), love.window.getHeight()})
|
||||
light_world.refractionShader:send("screen", {love.window.getWidth(), love.window.getHeight()})
|
||||
light_world.reflectionShader:send("screen", {love.window.getWidth(), love.window.getHeight()})
|
||||
|
||||
function light_world:init()
|
||||
self.translate_x = 0
|
||||
self.translate_y = 0
|
||||
self.direction = 0
|
||||
|
||||
self.last_buffer = nil
|
||||
|
||||
self.lights = {}
|
||||
self.ambient = {0, 0, 0}
|
||||
self.body = {}
|
||||
self.refraction = {}
|
||||
self.shadow = love.graphics.newCanvas()
|
||||
self.shadow2 = love.graphics.newCanvas()
|
||||
self.shine = love.graphics.newCanvas()
|
||||
self.shine2 = love.graphics.newCanvas()
|
||||
self.normalMap = love.graphics.newCanvas()
|
||||
self.glowMap = love.graphics.newCanvas()
|
||||
self.glowMap2 = love.graphics.newCanvas()
|
||||
self.refractionMap = love.graphics.newCanvas()
|
||||
self.refractionMap2 = love.graphics.newCanvas()
|
||||
self.reflectionMap = love.graphics.newCanvas()
|
||||
self.reflectionMap2 = love.graphics.newCanvas()
|
||||
|
||||
self.ambient = {0, 0, 0}
|
||||
self.normalInvert = false
|
||||
self.glowBlur = 1.0
|
||||
self.glowTimer = 0.0
|
||||
self.glowDown = false
|
||||
self.refractionStrength = 8.0
|
||||
|
||||
self.pixelShadow = love.graphics.newCanvas()
|
||||
self.pixelShadow2 = love.graphics.newCanvas()
|
||||
|
||||
self.blurv = love.graphics.newShader("shader/blurv.glsl")
|
||||
self.blurh = love.graphics.newShader("shader/blurh.glsl")
|
||||
self.blurv:send("screen", {love.window.getWidth(), love.window.getHeight()})
|
||||
self.blurh:send("screen", {love.window.getWidth(), love.window.getHeight()})
|
||||
|
||||
self.shader = love.graphics.newShader("shader/poly_shadow.glsl")
|
||||
self.glowShader = love.graphics.newShader("shader/glow.glsl")
|
||||
self.normalShader = love.graphics.newShader("shader/normal.glsl")
|
||||
self.normalInvertShader = love.graphics.newShader("shader/normal_invert.glsl")
|
||||
self.materialShader = love.graphics.newShader("shader/material.glsl")
|
||||
self.refractionShader = love.graphics.newShader("shader/refraction.glsl")
|
||||
self.refractionShader:send("screen", {love.window.getWidth(), love.window.getHeight()})
|
||||
self.reflectionShader = love.graphics.newShader("shader/reflection.glsl")
|
||||
self.reflectionShader:send("screen", {love.window.getWidth(), love.window.getHeight()})
|
||||
|
||||
self.reflectionStrength = 16.0
|
||||
self.reflectionVisibility = 1.0
|
||||
self.blur = 2.0
|
||||
self.optionShadows = true
|
||||
self.optionPixelShadows = true
|
||||
self.optionGlow = true
|
||||
self.optionRefraction = true
|
||||
self.optionReflection = true
|
||||
self.isShadows = false
|
||||
self.isLight = false
|
||||
self.isPixelShadows = false
|
||||
self.isGlow = false
|
||||
self.isRefraction = false
|
||||
self.isReflection = false
|
||||
|
||||
self:refreshScreenSize()
|
||||
end
|
||||
|
||||
function light_world:updateShadows()
|
||||
if not self.optionShadows or not (self.isShadows or self.isLight) then
|
||||
if not self.isShadows and not self.isLight then
|
||||
return
|
||||
end
|
||||
|
||||
self.last_buffer = love.graphics.getCanvas()
|
||||
love.graphics.setShader(self.shader)
|
||||
|
||||
for i = 1, #self.lights do
|
||||
self.lights[i]:updateShadow()
|
||||
end
|
||||
|
||||
-- update shadow
|
||||
love.graphics.setShader()
|
||||
love.graphics.setCanvas(self.shadow)
|
||||
love.graphics.setStencil()
|
||||
love.graphics.setColor(unpack(self.ambient))
|
||||
@ -132,7 +99,7 @@ function light_world:updateShadows()
|
||||
end
|
||||
|
||||
function light_world:updatePixelShadows()
|
||||
if not self.optionPixelShadows or not self.isPixelShadows then
|
||||
if not self.isPixelShadows then
|
||||
return
|
||||
end
|
||||
|
||||
@ -173,7 +140,7 @@ function light_world:updatePixelShadows()
|
||||
end
|
||||
|
||||
function light_world:updateGlow()
|
||||
if not self.optionGlow or not self.isGlow then
|
||||
if not self.isGlow then
|
||||
return
|
||||
end
|
||||
|
||||
@ -201,7 +168,7 @@ function light_world:updateGlow()
|
||||
end
|
||||
|
||||
function light_world:updateRefraction()
|
||||
if not self.optionRefraction or not self.isRefraction then
|
||||
if not self.isRefraction then
|
||||
return
|
||||
end
|
||||
|
||||
@ -217,7 +184,7 @@ function light_world:updateRefraction()
|
||||
end
|
||||
|
||||
function light_world:updateRelfection()
|
||||
if not self.optionReflection or not self.isReflection then
|
||||
if not self.isReflection then
|
||||
return
|
||||
end
|
||||
|
||||
@ -234,6 +201,8 @@ end
|
||||
function light_world:refreshScreenSize()
|
||||
self.shadow = love.graphics.newCanvas()
|
||||
self.shadow2 = love.graphics.newCanvas()
|
||||
self.pixelShadow = love.graphics.newCanvas()
|
||||
self.pixelShadow2 = love.graphics.newCanvas()
|
||||
self.shine = love.graphics.newCanvas()
|
||||
self.shine2 = love.graphics.newCanvas()
|
||||
self.normalMap = love.graphics.newCanvas()
|
||||
@ -243,13 +212,11 @@ function light_world:refreshScreenSize()
|
||||
self.refractionMap2 = love.graphics.newCanvas()
|
||||
self.reflectionMap = love.graphics.newCanvas()
|
||||
self.reflectionMap2 = love.graphics.newCanvas()
|
||||
self.pixelShadow = love.graphics.newCanvas()
|
||||
self.pixelShadow2 = love.graphics.newCanvas()
|
||||
end
|
||||
|
||||
-- draw shadow
|
||||
function light_world:drawShadow()
|
||||
if not self.optionShadows or not (self.isShadows or self.isLight) then
|
||||
if not self.isShadows and not self.isLight then
|
||||
return
|
||||
end
|
||||
|
||||
@ -281,7 +248,7 @@ end
|
||||
|
||||
-- draw shine
|
||||
function light_world:drawShine()
|
||||
if not self.optionShadows or not self.isShadows then
|
||||
if not self.isShadows then
|
||||
return
|
||||
end
|
||||
love.graphics.setColor(255, 255, 255)
|
||||
@ -311,7 +278,7 @@ end
|
||||
|
||||
-- draw pixel shadow
|
||||
function light_world:drawPixelShadow()
|
||||
if not self.optionPixelShadows or not self.isPixelShadows then
|
||||
if not self.isPixelShadows then
|
||||
return
|
||||
end
|
||||
self:updatePixelShadows()
|
||||
@ -324,16 +291,14 @@ end
|
||||
|
||||
-- draw material
|
||||
function light_world:drawMaterial()
|
||||
love.graphics.setShader(self.materialShader)
|
||||
for i = 1, #self.body do
|
||||
self.body[i]:drawMaterial()
|
||||
end
|
||||
love.graphics.setShader()
|
||||
end
|
||||
|
||||
-- draw glow
|
||||
function light_world:drawGlow()
|
||||
if not self.optionGlow or not self.isGlow then
|
||||
if not self.isGlow then
|
||||
return
|
||||
end
|
||||
|
||||
@ -364,7 +329,7 @@ function light_world:drawGlow()
|
||||
end
|
||||
-- draw refraction
|
||||
function light_world:drawRefraction()
|
||||
if not self.optionRefraction or not self.isRefraction then
|
||||
if not self.isRefraction then
|
||||
return
|
||||
end
|
||||
|
||||
@ -386,7 +351,7 @@ end
|
||||
|
||||
-- draw reflection
|
||||
function light_world:drawReflection()
|
||||
if not self.optionReflection or not self.isReflection then
|
||||
if not self.isReflection then
|
||||
return
|
||||
end
|
||||
|
||||
|
@ -21,19 +21,21 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
]]
|
||||
local _PACKAGE = (...):match("^(.+)[%./][^%./]+") or ""
|
||||
local class = require(_PACKAGE..'/class')
|
||||
|
||||
LOVE_POSTSHADER_BUFFER_RENDER = love.graphics.newCanvas()
|
||||
LOVE_POSTSHADER_BUFFER_BACK = love.graphics.newCanvas()
|
||||
LOVE_POSTSHADER_LAST_BUFFER = nil
|
||||
|
||||
LOVE_POSTSHADER_BLURV = love.graphics.newShader("shader/blurv.glsl")
|
||||
LOVE_POSTSHADER_BLURH = love.graphics.newShader("shader/blurh.glsl")
|
||||
LOVE_POSTSHADER_CONTRAST = love.graphics.newShader("shader/contrast.glsl")
|
||||
LOVE_POSTSHADER_CHROMATIC_ABERRATION = love.graphics.newShader("shader/chromatic_aberration.glsl")
|
||||
LOVE_POSTSHADER_FOUR_COLOR = love.graphics.newShader("shader/four_colors.glsl")
|
||||
LOVE_POSTSHADER_MONOCHROM = love.graphics.newShader("shader/monochrom.glsl")
|
||||
LOVE_POSTSHADER_SCANLINES = love.graphics.newShader("shader/scanlines.glsl")
|
||||
LOVE_POSTSHADER_TILT_SHIFT = love.graphics.newShader("shader/tilt_shift.glsl")
|
||||
LOVE_POSTSHADER_BLURV = love.graphics.newShader(_PACKAGE.."/shaders/blurv.glsl")
|
||||
LOVE_POSTSHADER_BLURH = love.graphics.newShader(_PACKAGE.."/shaders/blurh.glsl")
|
||||
LOVE_POSTSHADER_CONTRAST = love.graphics.newShader(_PACKAGE.."/shaders/contrast.glsl")
|
||||
LOVE_POSTSHADER_CHROMATIC_ABERRATION = love.graphics.newShader(_PACKAGE.."/shaders/chromatic_aberration.glsl")
|
||||
LOVE_POSTSHADER_FOUR_COLOR = love.graphics.newShader(_PACKAGE.."/shaders/four_colors.glsl")
|
||||
LOVE_POSTSHADER_MONOCHROM = love.graphics.newShader(_PACKAGE.."/shaders/monochrom.glsl")
|
||||
LOVE_POSTSHADER_SCANLINES = love.graphics.newShader(_PACKAGE.."/shaders/scanlines.glsl")
|
||||
LOVE_POSTSHADER_TILT_SHIFT = love.graphics.newShader(_PACKAGE.."/shaders/tilt_shift.glsl")
|
||||
|
||||
love.postshader = {}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user