mirror of
https://github.com/tanema/light_world.lua.git
synced 2024-12-24 20:24:19 +00:00
converted the complex example to use the proper way of transformation
This commit is contained in:
parent
b976a90afe
commit
537ff2522c
@ -120,6 +120,7 @@ function love.load()
|
||||
|
||||
offsetX = 0.0
|
||||
offsetY = 0.0
|
||||
scale = 1.0
|
||||
offsetOldX = 0.0
|
||||
offsetOldY = 0.0
|
||||
offsetChanged = false
|
||||
@ -176,14 +177,10 @@ function love.update(dt)
|
||||
offsetX = offsetX - dt * 200
|
||||
end
|
||||
|
||||
if offsetX ~= offsetOldX or offsetY ~= offsetOldY then
|
||||
offsetChanged = true
|
||||
for i = 2, lightWorld:getLightCount() do
|
||||
local light = lightWorld:getLight(i)
|
||||
light:setPosition(light:getX() + (offsetX - offsetOldX), light:getY() + (offsetY - offsetOldY))
|
||||
end
|
||||
else
|
||||
offsetChanged = false
|
||||
if love.keyboard.isDown("-") then
|
||||
scale = scale - 0.01
|
||||
elseif love.keyboard.isDown("=") then
|
||||
scale = scale + 0.01
|
||||
end
|
||||
|
||||
for i = 1, lightWorld:getLightCount() do
|
||||
@ -195,10 +192,6 @@ function love.update(dt)
|
||||
|
||||
for i = 1, phyCnt do
|
||||
if phyBody[i] and (phyBody[i]:isAwake() or offsetChanged) then
|
||||
if offsetChanged then
|
||||
phyBody[i]:setX(phyBody[i]:getX() + (offsetX - offsetOldX))
|
||||
phyBody[i]:setY(phyBody[i]:getY() + (offsetY - offsetOldY))
|
||||
end
|
||||
if phyLight[i]:getType() == "polygon" then
|
||||
phyLight[i]:setPoints(phyBody[i]:getWorldPoints(phyShape[i]:getPoints()))
|
||||
elseif phyLight[i]:getType() == "circle" then
|
||||
@ -213,19 +206,13 @@ function love.update(dt)
|
||||
--if math.mod(i, 2) == 0 then
|
||||
phyLight[i]:setNormalTileOffset(tileX, tileY)
|
||||
--end
|
||||
if offsetChanged then
|
||||
phyLight[i]:setPosition(phyLight[i]:getX() + (offsetX - offsetOldX), phyLight[i]:getY() + (offsetY - offsetOldY))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if physicOn then
|
||||
physicWorld:update(dt)
|
||||
end
|
||||
|
||||
offsetOldX = offsetX
|
||||
offsetOldY = offsetY
|
||||
|
||||
-- draw shader
|
||||
if colorAberration > 0.0 then
|
||||
-- vert / horz blur
|
||||
@ -246,7 +233,11 @@ end
|
||||
|
||||
function love.draw()
|
||||
-- set shader buffer
|
||||
lightWorld:draw()
|
||||
love.graphics.push()
|
||||
love.graphics.translate(offsetX, offsetY)
|
||||
love.graphics.scale(scale)
|
||||
lightWorld:draw(offsetX,offsetY, scale)
|
||||
love.graphics.pop()
|
||||
|
||||
love.graphics.draw(imgLight, mx - 5, (my - 5) - (16.0 + (math.sin(lightDirection) + 1.0) * 64.0))
|
||||
|
||||
@ -349,7 +340,7 @@ function drawBackground(l,t,w,h)
|
||||
else
|
||||
love.graphics.setColor(255, 255, 255)
|
||||
if textureOn then
|
||||
love.graphics.draw(imgFloor, quadScreen, offsetX % 32 - 32, offsetY % 24 - 24)
|
||||
love.graphics.draw(imgFloor, quadScreen, 0,0)
|
||||
else
|
||||
love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
|
||||
end
|
||||
|
28
lib/body.lua
28
lib/body.lua
@ -399,7 +399,7 @@ function body:shadowStencil()
|
||||
end
|
||||
end
|
||||
|
||||
function body:drawShadow(light, l,t,w,h,s)
|
||||
function body:drawShadow(light)
|
||||
if self.alpha < 1.0 then
|
||||
love.graphics.setBlendMode("multiplicative")
|
||||
love.graphics.setColor(self.red, self.green, self.blue)
|
||||
@ -457,18 +457,18 @@ function body:drawShadow(light, l,t,w,h,s)
|
||||
}
|
||||
|
||||
self.shadowMesh:setVertices(self.shadowVert)
|
||||
love.graphics.draw(self.shadowMesh, self.x - self.ox + l, self.y - self.oy + t, 0, s, s)
|
||||
love.graphics.draw(self.shadowMesh, self.x - self.ox, self.y - self.oy, 0, s, s)
|
||||
end
|
||||
end
|
||||
|
||||
function body:drawPixelShadow(l,t,w,h)
|
||||
function body:drawPixelShadow()
|
||||
if self.type == "image" and self.normalMesh then
|
||||
love.graphics.setColor(255, 255, 255)
|
||||
love.graphics.draw(self.normalMesh, self.x - self.nx, self.y - self.ny)
|
||||
end
|
||||
end
|
||||
|
||||
function body:drawGlow(l,t,w,h)
|
||||
function body:drawGlow()
|
||||
if self.glowStrength > 0.0 then
|
||||
love.graphics.setColor(self.glowRed * self.glowStrength, self.glowGreen * self.glowStrength, self.glowBlue * self.glowStrength)
|
||||
else
|
||||
@ -497,14 +497,14 @@ function body:drawGlow(l,t,w,h)
|
||||
love.graphics.setShader()
|
||||
end
|
||||
|
||||
function body:drawRefraction(l,t,w,h)
|
||||
function body:drawRefraction()
|
||||
if self.refraction and self.normal then
|
||||
love.graphics.setColor(255, 255, 255)
|
||||
if self.tileX == 0.0 and self.tileY == 0.0 then
|
||||
love.graphics.draw(normal, self.x - self.nx + l, self.y - self.ny + t)
|
||||
love.graphics.draw(normal, self.x - self.nx, self.y - self.ny)
|
||||
else
|
||||
self.normalMesh:setVertices(self.normalVert)
|
||||
love.graphics.draw(self.normalMesh, self.x - self.nx + l, self.y - self.ny + t)
|
||||
love.graphics.draw(self.normalMesh, self.x - self.nx, self.y - self.ny)
|
||||
end
|
||||
end
|
||||
|
||||
@ -518,32 +518,32 @@ function body:drawRefraction(l,t,w,h)
|
||||
elseif self.type == "polygon" then
|
||||
love.graphics.polygon("fill", unpack(self.data))
|
||||
elseif self.type == "image" and self.img then
|
||||
love.graphics.draw(self.img, self.x - self.ix + l, self.y - self.iy + t)
|
||||
love.graphics.draw(self.img, self.x - self.ix, self.y - self.iy)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function body:drawReflection(l,t,w,h)
|
||||
function body:drawReflection()
|
||||
if self.reflection and self.normal then
|
||||
love.graphics.setColor(255, 0, 0)
|
||||
self.normalMesh:setVertices(self.normalVert)
|
||||
love.graphics.draw(self.normalMesh, self.x - self.nx + l, self.y - self.ny + t)
|
||||
love.graphics.draw(self.normalMesh, self.x - self.nx, self.y - self.ny)
|
||||
end
|
||||
if self.reflective and self.img then
|
||||
love.graphics.setColor(0, 255, 0)
|
||||
love.graphics.draw(self.img, self.x - self.ix + l, self.y - self.iy + t)
|
||||
love.graphics.draw(self.img, self.x - self.ix, self.y - self.iy)
|
||||
elseif not self.reflection and self.img then
|
||||
love.graphics.setColor(0, 0, 0)
|
||||
love.graphics.draw(self.img, self.x - self.ix + l, self.y - self.iy + t)
|
||||
love.graphics.draw(self.img, self.x - self.ix, self.y - self.iy)
|
||||
end
|
||||
end
|
||||
|
||||
function body:drawMaterial(l,t,w,h)
|
||||
function body:drawMaterial()
|
||||
if self.material and self.normal then
|
||||
love.graphics.setShader(self.materialShader)
|
||||
love.graphics.setColor(255, 255, 255)
|
||||
self.materialShader:send("material", self.material)
|
||||
love.graphics.draw(self.normal, self.x - self.nx + l, self.y - self.ny + t)
|
||||
love.graphics.draw(self.normal, self.x - self.nx, self.y - self.ny)
|
||||
love.graphics.setShader()
|
||||
end
|
||||
end
|
||||
|
@ -142,17 +142,16 @@ function light:drawShadow(l,t,w,h,s,bodies, canvas)
|
||||
end
|
||||
end
|
||||
|
||||
self.shadow:clear()
|
||||
self.shader:send("lightPosition", {self.x - l, h - (self.y - t), 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)
|
||||
|
||||
-- draw shadow
|
||||
self.shadow:clear()
|
||||
util.drawto(self.shadow, l, t, s, function()
|
||||
self.shader:send("lightPosition", {self.x + l, h - (self.y + t), 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.setShader(self.shader)
|
||||
love.graphics.setInvertedStencil(stencils.shadow(shadow_geometry, bodies))
|
||||
love.graphics.setBlendMode("additive")
|
||||
@ -178,9 +177,9 @@ function light:drawShadow(l,t,w,h,s,bodies, canvas)
|
||||
end)
|
||||
|
||||
-- update shine
|
||||
self.shine:clear(255, 255, 255)
|
||||
util.drawto(self.shine, l, t, s, function()
|
||||
love.graphics.setShader(self.shader)
|
||||
self.shine:clear(255, 255, 255)
|
||||
love.graphics.setBlendMode("alpha")
|
||||
love.graphics.setStencil(stencils.colorShadow(bodies))
|
||||
love.graphics.rectangle("fill", -l,-t,w,h)
|
||||
|
@ -131,7 +131,7 @@ function light_world:drawShadow(l,t,w,h,s)
|
||||
util.drawto(self.shadow, l, t, s, function()
|
||||
love.graphics.setColor(unpack(self.ambient))
|
||||
love.graphics.setBlendMode("alpha")
|
||||
love.graphics.rectangle("fill", l, t, w, h)
|
||||
love.graphics.rectangle("fill", -l, -t, w, h)
|
||||
for i = 1, #self.lights do
|
||||
self.lights[i]:drawShadow(l,t,w,h,s,self.body, self.shadow)
|
||||
end
|
||||
@ -151,7 +151,7 @@ function light_world:drawShine(l,t,w,h,s)
|
||||
util.drawto(self.shine, l, t, s, function()
|
||||
love.graphics.setColor(unpack(self.ambient))
|
||||
love.graphics.setBlendMode("alpha")
|
||||
love.graphics.rectangle("fill", l, t, w, h)
|
||||
love.graphics.rectangle("fill", -l, -t, w, h)
|
||||
love.graphics.setColor(255, 255, 255)
|
||||
love.graphics.setBlendMode("additive")
|
||||
for i = 1, #self.lights do
|
||||
|
Loading…
Reference in New Issue
Block a user