mirror of
https://github.com/tanema/light_world.lua.git
synced 2024-12-24 20:24:19 +00:00
small refactors because I am stuck
This commit is contained in:
parent
73e203bd76
commit
c167edccc6
40
lib/body.lua
40
lib/body.lua
@ -538,10 +538,42 @@ function body:drawShadow(light, l,t,w,h)
|
||||
local shadowRotation = math.atan2((self.x) - light.x, (self.y + self.oy) - light.y)
|
||||
|
||||
self.shadowVert = {
|
||||
{math.sin(shadowRotation) * self.imgHeight * length, (length * math.cos(shadowRotation) + 1.0) * self.imgHeight + (math.cos(shadowRotation) + 1.0) * self.shadowY, 0, 0, self.red, self.green, self.blue, self.alpha * self.fadeStrength * 255},
|
||||
{self.imgWidth + math.sin(shadowRotation) * self.imgHeight * length, (length * math.cos(shadowRotation) + 1.0) * self.imgHeight + (math.cos(shadowRotation) + 1.0) * self.shadowY, 1, 0, self.red, self.green, self.blue, self.alpha * self.fadeStrength * 255},
|
||||
{self.imgWidth, self.imgHeight + (math.cos(shadowRotation) + 1.0) * self.shadowY, 1, 1, self.red, self.green, self.blue, self.alpha * 255},
|
||||
{0, self.imgHeight + (math.cos(shadowRotation) + 1.0) * self.shadowY, 0, 1, self.red, self.green, self.blue, self.alpha * 255}
|
||||
{
|
||||
math.sin(shadowRotation) * self.imgHeight * length,
|
||||
(length * math.cos(shadowRotation) + 1.0) * self.imgHeight + (math.cos(shadowRotation) + 1.0) * self.shadowY,
|
||||
0, 0,
|
||||
self.red,
|
||||
self.green,
|
||||
self.blue,
|
||||
self.alpha * self.fadeStrength * 255
|
||||
},
|
||||
{
|
||||
self.imgWidth + math.sin(shadowRotation) * self.imgHeight * length,
|
||||
(length * math.cos(shadowRotation) + 1.0) * self.imgHeight + (math.cos(shadowRotation) + 1.0) * self.shadowY,
|
||||
1, 0,
|
||||
self.red,
|
||||
self.green,
|
||||
self.blue,
|
||||
self.alpha * self.fadeStrength * 255
|
||||
},
|
||||
{
|
||||
self.imgWidth,
|
||||
self.imgHeight + (math.cos(shadowRotation) + 1.0) * self.shadowY,
|
||||
1, 1,
|
||||
self.red,
|
||||
self.green,
|
||||
self.blue,
|
||||
self.alpha * 255
|
||||
},
|
||||
{
|
||||
0,
|
||||
self.imgHeight + (math.cos(shadowRotation) + 1.0) * self.shadowY,
|
||||
0, 1,
|
||||
self.red,
|
||||
self.green,
|
||||
self.blue,
|
||||
self.alpha * 255
|
||||
}
|
||||
}
|
||||
|
||||
self.shadowMesh:setVertices(self.shadowVert)
|
||||
|
@ -122,10 +122,14 @@ function light:setGlowStrength(strength)
|
||||
self.glowStrength = strength
|
||||
end
|
||||
|
||||
function light:inRange(l,t,w,h)
|
||||
return self.x + self.range > l and self.x - self.range < (l+w) and self.y + self.range > t and self.y - self.range < (t+h)
|
||||
end
|
||||
|
||||
function light:updateShadow(l,t,w,h, bodies)
|
||||
love.graphics.setShader(self.shader)
|
||||
if self.x + self.range > l and self.x - self.range < (l+w) and self.y + self.range > t and self.y - self.range < (t+h) then
|
||||
local lightposrange = {self.x, h - self.y, self.range}
|
||||
if self:inRange(l,t,w,h) then
|
||||
|
||||
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})
|
||||
@ -133,9 +137,8 @@ function light:updateShadow(l,t,w,h, bodies)
|
||||
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)
|
||||
|
||||
self.shadow:clear()
|
||||
love.graphics.setCanvas(self.shadow)
|
||||
love.graphics.clear()
|
||||
|
||||
-- calculate shadows
|
||||
local shadow_geometry = self:calculateShadows(bodies)
|
||||
@ -172,9 +175,6 @@ function light:updateShadow(l,t,w,h, bodies)
|
||||
love.graphics.setStencil(stencils.poly(bodies))
|
||||
love.graphics.rectangle("fill", l,t,w,h)
|
||||
love.graphics.setStencil()
|
||||
self.visible = true
|
||||
else
|
||||
self.visible = false
|
||||
end
|
||||
love.graphics.setShader()
|
||||
end
|
||||
|
@ -237,6 +237,7 @@ function light_world:draw(l,t,w,h,s)
|
||||
|
||||
local last_buffer = love.graphics.getCanvas()
|
||||
love.graphics.setCanvas(self.render_buffer)
|
||||
|
||||
self.drawBackground(l,t,w,h,s)
|
||||
self:drawShadow(l,t,w,h,scale)
|
||||
self.drawForground(l,t,w,h,s)
|
||||
@ -245,12 +246,13 @@ function light_world:draw(l,t,w,h,s)
|
||||
self:drawGlow(l,t,w,h,scale)
|
||||
self:drawRefraction(l,t,w,h,scale)
|
||||
self:drawReflection(l,t,w,h,scale)
|
||||
|
||||
love.graphics.setBackgroundColor(0, 0, 0)
|
||||
love.graphics.setBlendMode("alpha")
|
||||
love.graphics.setCanvas(last_buffer)
|
||||
love.graphics.setShader()
|
||||
love.graphics.setColor(255, 255, 255)
|
||||
love.graphics.draw(self.render_buffer)
|
||||
love.graphics.draw(self.render_buffer, l, t)
|
||||
end
|
||||
|
||||
-- draw shadow
|
||||
|
Loading…
Reference in New Issue
Block a user