Jump to content

fade in/fade out


Ameshi
 Share

Recommended Posts

Funnily enough I just coded a function for this today...

 

I put the function below in the player script and then use the following to call to run and check it in post render...

 

if self:FadeScreen(0,100) == true then

-- screen has now faded to black

end

 

 

if self:FadeScreen(1,100) == true then

-- screen has now faded from black

end

 

It seems to work pretty good but if anyone spots anything dodge in the code let me know. Still learning lua as I go.

 

-- fades screen to black
-- fade 0 fade to black
-- fade 1 fade from black
-- speed eg. 10=fast 100 slow
function Script:FadeScreen(fade,speed)
local screenWidth = App.context:GetWidth()
local screenHeight = App.context:GetHeight()
if fade>0 then
-- fade from black
if self.fadeValue == nil then self.fadeValue=speed end

local alpha = self.fadeValue/speed
if alpha<=0 then alpha=0 end
App.context:SetBlendMode(Blend.Alpha)
App.context:SetColor(Vec4(0,0,0,alpha))
App.context:DrawRect(0,0,screenWidth,screenHeight)

if self.fadeValue <= 0 then
self.fadeValue = nil
return true
end

self.fadeValue = self.fadeValue-1

else
-- fade to black
if self.fadeValue == nil then self.fadeValue=1 end

local alpha = self.fadeValue/speed
if alpha>1 then alpha=1 end
App.context:SetBlendMode(Blend.Alpha)
App.context:SetColor(Vec4(0,0,0,alpha))
App.context:DrawRect(0,0,screenWidth,screenHeight)

if self.fadeValue >= speed then
self.fadeValue = nil
return true
end

self.fadeValue = self.fadeValue+1
end

return false

end

  • Upvote 1

Check out my games: One More Day / Halloween Pumpkin Run

Link to comment
Share on other sites

The only thing, that probably doesn't affect you since you are using a Lua project, would be to get the context via Context:GetCurrent() instead of App.context as C++ projects don't have the App object so the script won't work in a C++ project. By having Context:GetCurrent() to get the context it'll work on both projects. That's obviously detailed but it's something that we all found out early on with scripts in the Workshop after the C++ projects were released in LE (Lua only was the first project type released in LE so everyone just used the App object without understanding the issues with it in C++ projects that would come later on).

  • Upvote 1
Link to comment
Share on other sites

This one is on my todo list and I was going to have a go using camera post effects.

 

The simplest idea was to have a straight fade in/out to a specific colour, black or white being the two most obvious.

 

If that worked well I was then going to look at building a small library of shaders for transition effects, fades, page turns, melts, pixellates, all those kind of things.

content over form, game play over all.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...