Code archives/3D Graphics - Effects/fade in/out
This code has been declared by its author to be Public Domain code.
Download source code
|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
| 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?
| 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
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.
| Good job. |
| 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.
| I would enable Flatshading and Fullbright, otherwise it won't be pure black if a light source is affecting it. |
| 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. |
| yeah sprites are always fullbright |
| yeah! its like im passing out! |
| 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.
| actually that sounds rather lame in retrospect, but I might use it for flash grenades |
| 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 :)
| 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
While Not KeyHit(1)
If KeyHit(57) Then damage=Rnd(10)
If armor>0 Then
If armor<0 Then armor=0
If health=<0 Then health=0
If now# > later# Then alpha#=.75
If health>0 Then alpha#=alpha#-.01
If health > 0 Then
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
Code Archives Forum
|Blitz Social Club|