Code archives/3D Graphics - Effects/fade in/out

This code has been declared by its author to be Public Domain code.

Download source code

fade in/out by Ben(t)2007
a simple sprite that can be used to fade in and out
Graphics3D 640,480,32,2

pivot=CreatePivot()
cam=CreateCamera(pivot)
PositionEntity cam,0,5,-10,1

fade=CreateSprite(cam)
ScaleSprite fade,2,2
PositionEntity fade,0,0,1.1
EntityColor fade,0,0,0

cube=CreateCube()
ScaleEntity cube,5,.1,5
EntityColor cube,255,0,0

ball=CreateSphere(32)
MoveEntity ball,1,0,1
EntityColor ball,0,0,255

light=CreateLight()
MoveEntity light,5,8,5

While Not KeyHit(1)
PointEntity cam,cube
TurnEntity pivot,0,1,0

EntityAlpha fade,alpha#
If KeyDown(13) Then alpha#=alpha#+.01
If KeyDown(12) Then alpha#=alpha#-.01
If alpha#<0 Then alpha#=0
If alpha#>1 Then alpha#=1

RenderWorld
Text 0,0,alpha#
Text 0,10,"+ to increase dark and - to decrease"

Flip
Wend
End

Comments

Canardian2007
I'm totally confused how this works.
You create a pivot, assign the camera to the pivot, and then you create a sprite of the camera?
This must be some advanced programming technique which my simple mind cannot understand :)

I think I understood what it's good for though, you can fade a whole scene with one EntityAlpha command in and out?

I would have done an scene fade in/out so, that I display a huge flat wall right in the front of the camera, and then EntityAlpha that flat wall in and out. Or change the fog range until 0. But I guess your method is better?


semar2007
Nice one Ben(t) ! I would use different keys though, because + and - are not always located at the same place on each pc keyboard, and may confuse the users.

Use 200 and 208 (up and down arrow) as keycode, for example:
If KeyDown(200) Then alpha#=alpha#+.01
If KeyDown(208) Then alpha#=alpha#-.01

@Lumooja,
he does not create 'a sprite of the camera', instead he creates a sprite parented to the camera. The command CreateSprite(cam) does exactly this.

In this way, the sprite named 'fade' will remain always attached to the camera, and by changing its alpha, you get the fading effect. Note that the sprite is colored black, so when its alpha = 1, you see a black screen, and when the alpha is 0, you see the 3D scene.

It's like a (black) fading glass screen in front of the camera, if you get the point.

Sergio.


Ked2007
Good job.


Ben(t)2007
to respond to lumooja that is exactly what the sprite is for
the sprite is a black square colored 0,0,0 and it is in front of the camera
the fog Idea also works but you have to wait for it to get to zero for a complete fade out this fades everything out in less time.


GfK2007
I would enable Flatshading and Fullbright, otherwise it won't be pure black if a light source is affecting it.


big10p2007
Shouldn't be necessary, Gfk: i think sprites are fullbright by default. Fullbright entities are always fully lit, regardless of light sources, therefore setting flatshading is unnecessary, too.


Ben(t)2007
yeah sprites are always fullbright


blade0072007
yeah! its like im passing out!


Ben(t)2007
hey that's not a bad Idea! I could color it red and when my character dies have it fade in!
just like the james bond games.


Ben(t)2007
actually that sounds rather lame in retrospect, but I might use it for flash grenades


Yo! Wazzup?2007
Wow thanks this is one of the only things in the code archives I actually understand! (I even learned a bit from it!)

And also... the fact that it is an exaple already and not just some function helps too :)


Ben(t)2007
I've updated the fade out program so that it has a armor and health setup, whenever you get damaged then the screen flashes red.

it's still tweaky if anyone finds a better way to write the code let me know


Graphics3D 640,480,16,2
health=100
armor=100
SeedRnd MilliSecs()

CreatePlane()

pivot=CreatePivot()
cam=CreateCamera(pivot)
PositionEntity cam,0,5,-10,1

fade=CreateSprite(cam)
ScaleSprite fade,2,2
PositionEntity fade,0,0,1.1
EntityColor fade,255,0,0



cube=CreateCube()
ScaleEntity cube,5,.1,5
EntityColor cube,255,0,0

ball=CreateSphere(32)
MoveEntity ball,1,0,1
EntityColor ball,0,0,255

light=CreateLight()
MoveEntity light,5,8,5

While Not KeyHit(1)

If KeyHit(57) Then damage=Rnd(10)

now#=health+armor

If armor>0 Then
armor=armor-(damage *.75)
health=health-(damage *.25)
Else
health=health-damage
EndIf
If armor<0 Then armor=0
If health=<0 Then health=0
damage=0

later#=health+armor

If now# > later# Then alpha#=.75

If health>0 Then alpha#=alpha#-.01

If health > 0 Then
PointEntity cam,cube
TurnEntity pivot,0,1,0
EndIf
EntityAlpha fade,alpha#



RenderWorld
Text 0,0,health+" health"
Text 0,10,armor+" armor"
If alpha#>0 Then Text 320,240,"OW I'M HURT!!!"
If health=<0 Then
EntityColor fade,0,0,0
PositionEntity cam,0,1,-10,1
RotateEntity cam,0,0,90
alpha#=0
Text 0,20,"dead"
EndIf

Flip
Wend
End


Code Archives Forum