I am not a great coder, so there’s probably ways to do this in, like, two extra lines of code, but this is how I did it, and it works, at least.
Anyway it’s just a dumb little addition but it lets you control the amount that the shadow fades out the farther away it gets from the source.
-- Shadowcast
--by West
-- Use this function to perform your initial setup
function setup()
--source image - ussume bottom of the image aligns with the ground
srcimg=readImage(asset.builtin.Platformer_Art.Guy_Look_Right)
w=srcimg.width
h=srcimg.height
shadowMesh=mesh()
shadowMesh.texture=makeShadow(readImage(asset.builtin.Platformer_Art.Guy_Look_Right), 170)
shadowMesh.texCoords = {vec2(0,1),vec2(0,0),vec2(1,1),vec2(1,0),vec2(1,1),vec2(0,0)}
parameter.number("xoff",-5,5,0)
parameter.number("yoff",-1,4,0)
parameter.number("alpha", 0, 255, 170, function(value)
local newShadow = makeShadow(readImage(asset.builtin.Platformer_Art.Guy_Look_Right), value)
shadowMesh=mesh()
shadowMesh.texture = newShadow
shadowMesh.texCoords = {vec2(0,1),vec2(0,0),vec2(1,1),vec2(1,0),vec2(1,1),vec2(0,0)}
print("h")
end)
end
function makeShadow(srcimg, fade)
local w, h, shadow
w=srcimg.width
h=srcimg.height
--create a flipped black and white shadow image
shadow=image(w,h)
setContext(shadow)
local alpha = fade
for j=1,h do
for i=1,w do
local r,g,b,a=srcimg:get(i,j)
local useAlpha = alpha - (j * 1)
if useAlpha < 0 then useAlpha = 0 end
if a~=0 then
shadow:set(i,h-j,color(0,useAlpha))
end
end
end
return shadow
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(94, 94, 203)
suny=HEIGHT-(HEIGHT/8*yoff)
sunx=WIDTH/2-xoff*WIDTH/8
fill(255,255,0)
ellipse(sunx,suny,100)
noStroke()
fill(95, 129, 32)
rect(0,0,WIDTH,HEIGHT/2+h/2)
local tr = vec2(w/2, h/2)
local tl = vec2(-w/2,h/2)
local br = vec2(w/2+w*xoff,-h/2-h*yoff)
local bl = vec2(-w/2+w*xoff,-h/2-h*yoff)
shadowMesh.vertices={tl,bl,tr,br,tr,bl}
pushMatrix()
translate(WIDTH/2, HEIGHT/2)
sprite(srcimg,0,h)
shadowMesh:draw()
popMatrix()
end