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
|
offsetX = 0.0
|
||||||
offsetY = 0.0
|
offsetY = 0.0
|
||||||
|
scale = 1.0
|
||||||
offsetOldX = 0.0
|
offsetOldX = 0.0
|
||||||
offsetOldY = 0.0
|
offsetOldY = 0.0
|
||||||
offsetChanged = false
|
offsetChanged = false
|
||||||
@ -176,14 +177,10 @@ function love.update(dt)
|
|||||||
offsetX = offsetX - dt * 200
|
offsetX = offsetX - dt * 200
|
||||||
end
|
end
|
||||||
|
|
||||||
if offsetX ~= offsetOldX or offsetY ~= offsetOldY then
|
if love.keyboard.isDown("-") then
|
||||||
offsetChanged = true
|
scale = scale - 0.01
|
||||||
for i = 2, lightWorld:getLightCount() do
|
elseif love.keyboard.isDown("=") then
|
||||||
local light = lightWorld:getLight(i)
|
scale = scale + 0.01
|
||||||
light:setPosition(light:getX() + (offsetX - offsetOldX), light:getY() + (offsetY - offsetOldY))
|
|
||||||
end
|
|
||||||
else
|
|
||||||
offsetChanged = false
|
|
||||||
end
|
end
|
||||||
|
|
||||||
for i = 1, lightWorld:getLightCount() do
|
for i = 1, lightWorld:getLightCount() do
|
||||||
@ -195,10 +192,6 @@ function love.update(dt)
|
|||||||
|
|
||||||
for i = 1, phyCnt do
|
for i = 1, phyCnt do
|
||||||
if phyBody[i] and (phyBody[i]:isAwake() or offsetChanged) then
|
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
|
if phyLight[i]:getType() == "polygon" then
|
||||||
phyLight[i]:setPoints(phyBody[i]:getWorldPoints(phyShape[i]:getPoints()))
|
phyLight[i]:setPoints(phyBody[i]:getWorldPoints(phyShape[i]:getPoints()))
|
||||||
elseif phyLight[i]:getType() == "circle" then
|
elseif phyLight[i]:getType() == "circle" then
|
||||||
@ -213,9 +206,6 @@ function love.update(dt)
|
|||||||
--if math.mod(i, 2) == 0 then
|
--if math.mod(i, 2) == 0 then
|
||||||
phyLight[i]:setNormalTileOffset(tileX, tileY)
|
phyLight[i]:setNormalTileOffset(tileX, tileY)
|
||||||
--end
|
--end
|
||||||
if offsetChanged then
|
|
||||||
phyLight[i]:setPosition(phyLight[i]:getX() + (offsetX - offsetOldX), phyLight[i]:getY() + (offsetY - offsetOldY))
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -223,9 +213,6 @@ function love.update(dt)
|
|||||||
physicWorld:update(dt)
|
physicWorld:update(dt)
|
||||||
end
|
end
|
||||||
|
|
||||||
offsetOldX = offsetX
|
|
||||||
offsetOldY = offsetY
|
|
||||||
|
|
||||||
-- draw shader
|
-- draw shader
|
||||||
if colorAberration > 0.0 then
|
if colorAberration > 0.0 then
|
||||||
-- vert / horz blur
|
-- vert / horz blur
|
||||||
@ -246,7 +233,11 @@ end
|
|||||||
|
|
||||||
function love.draw()
|
function love.draw()
|
||||||
-- set shader buffer
|
-- 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))
|
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
|
else
|
||||||
love.graphics.setColor(255, 255, 255)
|
love.graphics.setColor(255, 255, 255)
|
||||||
if textureOn then
|
if textureOn then
|
||||||
love.graphics.draw(imgFloor, quadScreen, offsetX % 32 - 32, offsetY % 24 - 24)
|
love.graphics.draw(imgFloor, quadScreen, 0,0)
|
||||||
else
|
else
|
||||||
love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
|
love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
|
||||||
end
|
end
|
||||||
|
28
lib/body.lua
28
lib/body.lua
@ -399,7 +399,7 @@ function body:shadowStencil()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function body:drawShadow(light, l,t,w,h,s)
|
function body:drawShadow(light)
|
||||||
if self.alpha < 1.0 then
|
if self.alpha < 1.0 then
|
||||||
love.graphics.setBlendMode("multiplicative")
|
love.graphics.setBlendMode("multiplicative")
|
||||||
love.graphics.setColor(self.red, self.green, self.blue)
|
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)
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
function body:drawPixelShadow(l,t,w,h)
|
function body:drawPixelShadow()
|
||||||
if self.type == "image" and self.normalMesh then
|
if self.type == "image" and self.normalMesh then
|
||||||
love.graphics.setColor(255, 255, 255)
|
love.graphics.setColor(255, 255, 255)
|
||||||
love.graphics.draw(self.normalMesh, self.x - self.nx, self.y - self.ny)
|
love.graphics.draw(self.normalMesh, self.x - self.nx, self.y - self.ny)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function body:drawGlow(l,t,w,h)
|
function body:drawGlow()
|
||||||
if self.glowStrength > 0.0 then
|
if self.glowStrength > 0.0 then
|
||||||
love.graphics.setColor(self.glowRed * self.glowStrength, self.glowGreen * self.glowStrength, self.glowBlue * self.glowStrength)
|
love.graphics.setColor(self.glowRed * self.glowStrength, self.glowGreen * self.glowStrength, self.glowBlue * self.glowStrength)
|
||||||
else
|
else
|
||||||
@ -497,14 +497,14 @@ function body:drawGlow(l,t,w,h)
|
|||||||
love.graphics.setShader()
|
love.graphics.setShader()
|
||||||
end
|
end
|
||||||
|
|
||||||
function body:drawRefraction(l,t,w,h)
|
function body:drawRefraction()
|
||||||
if self.refraction and self.normal then
|
if self.refraction and self.normal then
|
||||||
love.graphics.setColor(255, 255, 255)
|
love.graphics.setColor(255, 255, 255)
|
||||||
if self.tileX == 0.0 and self.tileY == 0.0 then
|
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
|
else
|
||||||
self.normalMesh:setVertices(self.normalVert)
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -518,32 +518,32 @@ function body:drawRefraction(l,t,w,h)
|
|||||||
elseif self.type == "polygon" then
|
elseif self.type == "polygon" then
|
||||||
love.graphics.polygon("fill", unpack(self.data))
|
love.graphics.polygon("fill", unpack(self.data))
|
||||||
elseif self.type == "image" and self.img then
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function body:drawReflection(l,t,w,h)
|
function body:drawReflection()
|
||||||
if self.reflection and self.normal then
|
if self.reflection and self.normal then
|
||||||
love.graphics.setColor(255, 0, 0)
|
love.graphics.setColor(255, 0, 0)
|
||||||
self.normalMesh:setVertices(self.normalVert)
|
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
|
||||||
if self.reflective and self.img then
|
if self.reflective and self.img then
|
||||||
love.graphics.setColor(0, 255, 0)
|
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
|
elseif not self.reflection and self.img then
|
||||||
love.graphics.setColor(0, 0, 0)
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
function body:drawMaterial(l,t,w,h)
|
function body:drawMaterial()
|
||||||
if self.material and self.normal then
|
if self.material and self.normal then
|
||||||
love.graphics.setShader(self.materialShader)
|
love.graphics.setShader(self.materialShader)
|
||||||
love.graphics.setColor(255, 255, 255)
|
love.graphics.setColor(255, 255, 255)
|
||||||
self.materialShader:send("material", self.material)
|
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()
|
love.graphics.setShader()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -142,17 +142,16 @@ function light:drawShadow(l,t,w,h,s,bodies, canvas)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- draw shadow
|
||||||
self.shadow:clear()
|
self.shadow:clear()
|
||||||
self.shader:send("lightPosition", {self.x - l, h - (self.y - t), self.z})
|
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("lightRange", self.range)
|
||||||
self.shader:send("lightColor", {self.red / 255.0, self.green / 255.0, self.blue / 255.0})
|
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("lightSmooth", self.smooth)
|
||||||
self.shader:send("lightGlow", {1.0 - self.glowSize, self.glowStrength})
|
self.shader:send("lightGlow", {1.0 - self.glowSize, self.glowStrength})
|
||||||
self.shader:send("lightAngle", math.pi - self.angle / 2.0)
|
self.shader:send("lightAngle", math.pi - self.angle / 2.0)
|
||||||
self.shader:send("lightDirection", self.direction)
|
self.shader:send("lightDirection", self.direction)
|
||||||
|
|
||||||
-- draw shadow
|
|
||||||
util.drawto(self.shadow, l, t, s, function()
|
|
||||||
love.graphics.setShader(self.shader)
|
love.graphics.setShader(self.shader)
|
||||||
love.graphics.setInvertedStencil(stencils.shadow(shadow_geometry, bodies))
|
love.graphics.setInvertedStencil(stencils.shadow(shadow_geometry, bodies))
|
||||||
love.graphics.setBlendMode("additive")
|
love.graphics.setBlendMode("additive")
|
||||||
@ -178,9 +177,9 @@ function light:drawShadow(l,t,w,h,s,bodies, canvas)
|
|||||||
end)
|
end)
|
||||||
|
|
||||||
-- update shine
|
-- update shine
|
||||||
|
self.shine:clear(255, 255, 255)
|
||||||
util.drawto(self.shine, l, t, s, function()
|
util.drawto(self.shine, l, t, s, function()
|
||||||
love.graphics.setShader(self.shader)
|
love.graphics.setShader(self.shader)
|
||||||
self.shine:clear(255, 255, 255)
|
|
||||||
love.graphics.setBlendMode("alpha")
|
love.graphics.setBlendMode("alpha")
|
||||||
love.graphics.setStencil(stencils.colorShadow(bodies))
|
love.graphics.setStencil(stencils.colorShadow(bodies))
|
||||||
love.graphics.rectangle("fill", -l,-t,w,h)
|
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()
|
util.drawto(self.shadow, l, t, s, function()
|
||||||
love.graphics.setColor(unpack(self.ambient))
|
love.graphics.setColor(unpack(self.ambient))
|
||||||
love.graphics.setBlendMode("alpha")
|
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
|
for i = 1, #self.lights do
|
||||||
self.lights[i]:drawShadow(l,t,w,h,s,self.body, self.shadow)
|
self.lights[i]:drawShadow(l,t,w,h,s,self.body, self.shadow)
|
||||||
end
|
end
|
||||||
@ -151,7 +151,7 @@ function light_world:drawShine(l,t,w,h,s)
|
|||||||
util.drawto(self.shine, l, t, s, function()
|
util.drawto(self.shine, l, t, s, function()
|
||||||
love.graphics.setColor(unpack(self.ambient))
|
love.graphics.setColor(unpack(self.ambient))
|
||||||
love.graphics.setBlendMode("alpha")
|
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.setColor(255, 255, 255)
|
||||||
love.graphics.setBlendMode("additive")
|
love.graphics.setBlendMode("additive")
|
||||||
for i = 1, #self.lights do
|
for i = 1, #self.lights do
|
||||||
|
Loading…
Reference in New Issue
Block a user