mirror of
https://github.com/tanema/light_world.lua.git
synced 2024-12-24 20:24:19 +00:00
image shadows look correct again
This commit is contained in:
parent
0a2da63f24
commit
012a5481e9
85
lib/body.lua
85
lib/body.lua
@ -460,63 +460,20 @@ function body:drawMaterial()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function body:drawCalculatedShadow(light)
|
function body:drawShadow(light)
|
||||||
if self.shadowType == "rectangle" or self.shadowType == "polygon" then
|
|
||||||
return self:calculatePolyShadow(light)
|
|
||||||
elseif self.shadowType == "circle" then
|
|
||||||
return self:calculateCircleShadow(light)
|
|
||||||
elseif self.shadowType == "image" and self.img then
|
|
||||||
local length = 1.0
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
love.graphics.setColor(self.red, self.green, self.blue, self.alpha)
|
love.graphics.setColor(self.red, self.green, self.blue, self.alpha)
|
||||||
self.shadowMesh:setVertices(self.shadowVert)
|
if self.shadowType == "rectangle" or self.shadowType == "polygon" then
|
||||||
love.graphics.draw(self.shadowMesh, self.x - self.ox, self.y - self.oy, 0, s, s)
|
self:drawPolyShadow(light)
|
||||||
|
elseif self.shadowType == "circle" then
|
||||||
|
self:drawCircleShadow(light)
|
||||||
|
elseif self.shadowType == "image" and self.img then
|
||||||
|
self:drawImageShadow(light)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--using shadow point calculations from this article
|
--using shadow point calculations from this article
|
||||||
--http://web.cs.wpi.edu/~matt/courses/cs563/talks/shadow/shadow.html
|
--http://web.cs.wpi.edu/~matt/courses/cs563/talks/shadow/shadow.html
|
||||||
function body:calculatePolyShadow(light)
|
function body:drawPolyShadow(light)
|
||||||
if self.castsNoShadow or (self.zheight - light.z) > 0 then
|
if self.castsNoShadow or (self.zheight - light.z) > 0 then
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
@ -565,14 +522,13 @@ function body:calculatePolyShadow(light)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
if #curShadowGeometry >= 6 then
|
if #curShadowGeometry >= 6 then
|
||||||
love.graphics.setColor(self.red, self.green, self.blue, self.alpha)
|
|
||||||
love.graphics.polygon("fill", unpack(curShadowGeometry))
|
love.graphics.polygon("fill", unpack(curShadowGeometry))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--using shadow point calculations from this article
|
--using shadow point calculations from this article
|
||||||
--http://web.cs.wpi.edu/~matt/courses/cs563/talks/shadow/shadow.html
|
--http://web.cs.wpi.edu/~matt/courses/cs563/talks/shadow/shadow.html
|
||||||
function body:calculateCircleShadow(light)
|
function body:drawCircleShadow(light)
|
||||||
if self.castsNoShadow or (self.zheight - light.z) > 0 then
|
if self.castsNoShadow or (self.zheight - light.z) > 0 then
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
@ -608,7 +564,6 @@ function body:calculateCircleShadow(light)
|
|||||||
local distance1 = math.sqrt(math.pow(light.x - self.x, 2) + math.pow(light.y - self.y, 2)) / 2
|
local distance1 = math.sqrt(math.pow(light.x - self.x, 2) + math.pow(light.y - self.y, 2)) / 2
|
||||||
local distance2 = math.sqrt(math.pow(light.x - cx, 2) + math.pow(light.y - cy, 2)) / 2
|
local distance2 = math.sqrt(math.pow(light.x - cx, 2) + math.pow(light.y - cy, 2)) / 2
|
||||||
|
|
||||||
love.graphics.setColor(self.red, self.green, self.blue, self.alpha)
|
|
||||||
love.graphics.polygon("fill", curShadowGeometry)
|
love.graphics.polygon("fill", curShadowGeometry)
|
||||||
|
|
||||||
if distance1 <= self.radius then
|
if distance1 <= self.radius then
|
||||||
@ -622,4 +577,26 @@ function body:calculateCircleShadow(light)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function body:drawImageShadow(light)
|
||||||
|
local height_diff = (light.z - self.zheight)
|
||||||
|
if height_diff <= 0.1 then -- prevent shadows from leaving thier person like peter pan.
|
||||||
|
height_diff = 0.1
|
||||||
|
end
|
||||||
|
|
||||||
|
local length = 1.0 / height_diff
|
||||||
|
local shadowRotation = math.atan2((self.x) - light.x, (self.y + self.oy) - light.y)
|
||||||
|
local shadowStartY = self.imgHeight + (math.cos(shadowRotation) + 1.0) * self.shadowY
|
||||||
|
local shadowX = math.sin(shadowRotation) * self.imgHeight * length
|
||||||
|
local shadowY = (length * math.cos(shadowRotation) + 1.0) * shadowStartY
|
||||||
|
|
||||||
|
self.shadowMesh:setVertices({
|
||||||
|
{shadowX, shadowY, 0, 0, self.red, self.green, self.blue, self.alpha},
|
||||||
|
{shadowX + self.imgWidth, shadowY, 1, 0, self.red, self.green, self.blue, self.alpha},
|
||||||
|
{self.imgWidth, shadowStartY, 1, 1, self.red, self.green, self.blue, self.alpha},
|
||||||
|
{0, shadowStartY, 0, 1, self.red, self.green, self.blue, self.alpha}
|
||||||
|
})
|
||||||
|
|
||||||
|
love.graphics.draw(self.shadowMesh, self.x - self.ox, self.y - self.oy, 0, s, s)
|
||||||
|
end
|
||||||
|
|
||||||
return body
|
return body
|
||||||
|
@ -137,7 +137,7 @@ function light_world:drawNormalShading(l,t,w,h,s)
|
|||||||
self.shadowMap:clear()
|
self.shadowMap:clear()
|
||||||
util.drawto(self.shadowMap, l, t, s, function()
|
util.drawto(self.shadowMap, l, t, s, function()
|
||||||
for k = 1, #self.body do
|
for k = 1, #self.body do
|
||||||
self.body[k]:drawCalculatedShadow(self.lights[i])
|
self.body[k]:drawShadow(self.lights[i])
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
self.lights[i]:drawNormalShading(l,t,w,h,s, self.normalMap, self.shadowMap, self.normal2)
|
self.lights[i]:drawNormalShading(l,t,w,h,s, self.normalMap, self.shadowMap, self.normal2)
|
||||||
|
Loading…
Reference in New Issue
Block a user