My game gives the user the option of taking a photo of him- or herself to use as the player sprite. This pulls 500x500 pixels out of the middle of the photo and the sprite displays as 75x75 pixels. I wanted to make the edges of the sprite a bit transparent so that it blends into the background. I couldn’t find an example, so I developed the highly unsophisticated code below. It works OK but I have two questions: 1. How could I have done this more efficiently? 2. Is there a way to do it as a circle? Thanks.
(I use 100 as the FadeWidth.)
function FadePhoto( RawPhoto, FadeWidth ) -- Fades edges of photo
local RawWidth, RawHeight = spriteSize ( RawPhoto )
for WidthIndex = 1, RawWidth do -- Bottom edge
for HeightIndex = 1, FadeWidth do
r, g, b, a = RawPhoto:get( WidthIndex, HeightIndex)
RawPhoto:set( WidthIndex, HeightIndex, r, g, b, HeightIndex * 2.5 )
end
end
for WidthIndex = 1, RawWidth do -- Top edge
for HeightIndex = RawHeight, RawHeight - FadeWidth, -1 do
r, g, b, a = RawPhoto:get( WidthIndex, HeightIndex)
RawPhoto:set( WidthIndex, HeightIndex, r, g, b, ( RawHeight - HeightIndex ) * 2.5 )
end
end
for WidthIndex = 1, FadeWidth do -- Left edge
for HeightIndex = 1, RawHeight do
r, g, b, a = RawPhoto:get( WidthIndex, HeightIndex)
RawPhoto:set( WidthIndex, HeightIndex, r, g, b, WidthIndex * 2.5 )
end
end
for WidthIndex = RawWidth, RawWidth - FadeWidth, -1 do -- Right edge
for HeightIndex = 1, RawHeight - FadeWidth do
r, g, b, a = RawPhoto:get( WidthIndex, HeightIndex)
RawPhoto:set( WidthIndex, HeightIndex, r, g, b, ( RawWidth - WidthIndex ) * 2.5 )
end
end
for WidthIndex = 1, FadeWidth do -- Lower left corner
for HeightIndex = 1, FadeWidth do
r, g, b, a = RawPhoto:get( WidthIndex, HeightIndex)
RawPhoto:set( WidthIndex, HeightIndex, r, g, b, WidthIndex * HeightIndex * (1/FadeWidth) * 2.5 )
end
end
for WidthIndex = 1, FadeWidth do -- Upper left corner
for HeightIndex = RawHeight, RawHeight - FadeWidth, -1 do
r, g, b, a = RawPhoto:get( WidthIndex, HeightIndex)
RawPhoto:set( WidthIndex, HeightIndex, r, g, b, WidthIndex * (RawHeight - HeightIndex) * (1/FadeWidth) * 2.5 )
end
end
for WidthIndex = RawWidth, RawWidth - FadeWidth, -1 do -- Upper Right corner
for HeightIndex = RawHeight, RawHeight - FadeWidth, -1 do
r, g, b, a = RawPhoto:get( WidthIndex, HeightIndex)
RawPhoto:set( WidthIndex, HeightIndex, r, g, b, (RawWidth - WidthIndex) * (RawHeight - HeightIndex) * (1/FadeWidth) * 2.5 )
end
end
for WidthIndex = RawWidth, RawWidth - FadeWidth, -1 do -- Bottom Right corner
for HeightIndex = 1, FadeWidth do
r, g, b, a = RawPhoto:get( WidthIndex, HeightIndex)
RawPhoto:set( WidthIndex, HeightIndex, r, g, b, (RawWidth - WidthIndex) * HeightIndex * (1/FadeWidth) * 2.5 )
end
end
return( RawPhoto )
end