mirror of
https://github.com/tanema/light_world.lua.git
synced 2024-12-24 20:24:19 +00:00
added image shadows, took out useless code
This commit is contained in:
parent
410388d423
commit
0a2da63f24
@ -24,9 +24,8 @@ function love.load()
|
|||||||
end)
|
end)
|
||||||
circle_image = love.graphics.newImage(circle_canvas:getImageData())
|
circle_image = love.graphics.newImage(circle_canvas:getImageData())
|
||||||
|
|
||||||
local t = lightWorld:newImage(circle_image, 150, 150)
|
local t = lightWorld:newCircle(150, 150, radius)
|
||||||
t:setNormalMap(normal_map.generateFlat(circle_image, "top"))
|
t:setNormalMap(normal_map.generateFlat(circle_image, "top"))
|
||||||
t:setShadowType('circle', 50)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.mousepressed(x, y, c)
|
function love.mousepressed(x, y, c)
|
||||||
|
120
lib/body.lua
120
lib/body.lua
@ -373,80 +373,6 @@ function body:setShadowType(type, ...)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function body:stencil()
|
|
||||||
if self.shadowType == "circle" then
|
|
||||||
love.graphics.circle("fill", self.x - self.ox, self.y - self.oy, self.radius)
|
|
||||||
elseif self.shadowType == "rectangle" then
|
|
||||||
love.graphics.rectangle("fill", self.x - self.ox, self.y - self.oy, self.width, self.height)
|
|
||||||
elseif self.shadowType == "polygon" then
|
|
||||||
love.graphics.polygon("fill", unpack(self.data))
|
|
||||||
elseif self.shadowType == "image" then
|
|
||||||
--love.graphics.rectangle("fill", self.x - self.ox, self.y - self.oy, self.width, self.height)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function body:drawShadow(light)
|
|
||||||
if self.alpha < 1.0 then
|
|
||||||
love.graphics.setBlendMode("multiplicative")
|
|
||||||
love.graphics.setColor(self.red, self.green, self.blue)
|
|
||||||
if self.shadowType == "circle" then
|
|
||||||
love.graphics.circle("fill", self.x - self.ox, self.y - self.oy, self.radius)
|
|
||||||
elseif self.shadowType == "rectangle" then
|
|
||||||
love.graphics.rectangle("fill", self.x - self.ox, self.y - self.oy, self.width, self.height)
|
|
||||||
elseif self.shadowType == "polygon" then
|
|
||||||
love.graphics.polygon("fill", unpack(self.data))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if self.shadowType == "image" and self.img then
|
|
||||||
love.graphics.setBlendMode("alpha")
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
self.shadowMesh:setVertices(self.shadowVert)
|
|
||||||
love.graphics.draw(self.shadowMesh, self.x - self.ox, self.y - self.oy, 0, s, s)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function body:drawNormal()
|
function body:drawNormal()
|
||||||
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)
|
||||||
@ -539,6 +465,52 @@ function body:drawCalculatedShadow(light)
|
|||||||
return self:calculatePolyShadow(light)
|
return self:calculatePolyShadow(light)
|
||||||
elseif self.shadowType == "circle" then
|
elseif self.shadowType == "circle" then
|
||||||
return self:calculateCircleShadow(light)
|
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)
|
||||||
|
self.shadowMesh:setVertices(self.shadowVert)
|
||||||
|
love.graphics.draw(self.shadowMesh, self.x - self.ox, self.y - self.oy, 0, s, s)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
local _PACKAGE = (...):match("^(.+)[%./][^%./]+") or ""
|
local _PACKAGE = (...):match("^(.+)[%./][^%./]+") or ""
|
||||||
local class = require(_PACKAGE.."/class")
|
local class = require(_PACKAGE.."/class")
|
||||||
local stencils = require(_PACKAGE..'/stencils')
|
|
||||||
local util = require(_PACKAGE..'/util')
|
local util = require(_PACKAGE..'/util')
|
||||||
|
|
||||||
local light = class()
|
local light = class()
|
||||||
|
@ -1,41 +0,0 @@
|
|||||||
#define PI 3.1415926535897932384626433832795
|
|
||||||
|
|
||||||
extern vec3 lightPosition;
|
|
||||||
extern vec3 lightColor;
|
|
||||||
extern float lightRange;
|
|
||||||
extern float lightSmooth;
|
|
||||||
extern vec2 lightGlow;
|
|
||||||
extern float lightDirection;
|
|
||||||
extern float lightAngle;
|
|
||||||
|
|
||||||
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords){
|
|
||||||
vec4 pixel = Texel(texture, texture_coords);
|
|
||||||
vec3 lightToPixel = vec3(pixel_coords.x, pixel_coords.y, 0.0) - lightPosition;
|
|
||||||
float distance = length(lightToPixel);
|
|
||||||
float att = 1 - distance / lightRange;
|
|
||||||
|
|
||||||
if(lightAngle > 0.0) {
|
|
||||||
float angle2 = atan(lightPosition.x - pixel_coords.x, pixel_coords.y - lightPosition.y) + PI;
|
|
||||||
if(lightDirection - lightAngle > 0 && lightDirection + lightAngle < PI * 2) {
|
|
||||||
if(angle2 < mod(lightDirection + lightAngle, PI * 2) && angle2 > mod(lightDirection - lightAngle, PI * 2)) {
|
|
||||||
return vec4(0.0, 0.0, 0.0, 1.0);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if(angle2 < mod(lightDirection + lightAngle, PI * 2) || angle2 > mod(lightDirection - lightAngle, PI * 2)) {
|
|
||||||
return vec4(0.0, 0.0, 0.0, 1.0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (distance <= lightRange) {
|
|
||||||
if (lightGlow.x < 1.0 && lightGlow.y > 0.0) {
|
|
||||||
pixel.rgb = clamp(lightColor * pow(att, lightSmooth) + pow(smoothstep(lightGlow.x, 1.0, att), lightSmooth) * lightGlow.y, 0.0, 1.0);
|
|
||||||
} else {
|
|
||||||
pixel.rgb = lightColor * pow(att, lightSmooth);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return vec4(0.0, 0.0, 0.0, 1.0);
|
|
||||||
}
|
|
||||||
|
|
||||||
return pixel;
|
|
||||||
}
|
|
@ -1,36 +0,0 @@
|
|||||||
local stencils = {}
|
|
||||||
|
|
||||||
function stencils.shadow(geometry, bodies)
|
|
||||||
return function()
|
|
||||||
--cast shadows
|
|
||||||
for i = 1,#geometry do
|
|
||||||
if geometry[i].alpha == 1.0 then
|
|
||||||
love.graphics.polygon("fill", unpack(geometry[i]))
|
|
||||||
if geometry[i].circle then
|
|
||||||
love.graphics.arc("fill", unpack(geometry[i].circle))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
-- underneath shadows
|
|
||||||
for i = 1, #bodies do
|
|
||||||
if not bodies[i].castsNoShadow then
|
|
||||||
bodies[i]:stencil()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function stencils.shine(bodies)
|
|
||||||
return function()
|
|
||||||
for i = 1, #bodies do
|
|
||||||
if bodies[i].shine and
|
|
||||||
(bodies[i].glowStrength == 0.0 or
|
|
||||||
(bodies[i].type == "image" and not bodies[i].normal))
|
|
||||||
then
|
|
||||||
bodies[i]:stencil()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return stencils
|
|
Loading…
Reference in New Issue
Block a user