SyntaxBomb - Indie Coders

Languages & Coding => Blitz Code Archives => 3D Graphics - Effects => Topic started by: BlitzBot on June 29, 2017, 00:28:40

Title: [bb] fade in/out by Ben(t) [ 1+ years ago ]
Post by: BlitzBot on June 29, 2017, 00:28:40
Title : fade in/out
Author : Ben(t)
Posted : 1+ years ago

Description : a simple sprite that can be used to fade in and out

Code :
Code (blitzbasic) Select
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 :


Canardian(Posted 1+ years ago)

 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?


semar(Posted 1+ years ago)

 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#+.01If 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.


Ked(Posted 1+ years ago)

 Good job.


Ben(t)(Posted 1+ years ago)

 to respond to lumooja that is exactly what the sprite is forthe sprite is a black square colored 0,0,0 and it is in front of the camerathe 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.


GfK(Posted 1+ years ago)

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


big10p(Posted 1+ years ago)

 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)(Posted 1+ years ago)

 yeah sprites are always fullbright


blade007(Posted 1+ years ago)

 yeah! its like im passing out!


Ben(t)(Posted 1+ years ago)

 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)(Posted 1+ years ago)

 actually that sounds rather lame in retrospect, but I might use it for flash grenades


Yo! Wazzup?(Posted 1+ years ago)

 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)(Posted 1+ years ago)

 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 knowGraphics3D 640,480,16,2health=100armor=100SeedRnd MilliSecs()CreatePlane()pivot=CreatePivot()cam=CreateCamera(pivot)PositionEntity cam,0,5,-10,1fade=CreateSprite(cam)ScaleSprite fade,2,2PositionEntity fade,0,0,1.1EntityColor fade,255,0,0cube=CreateCube()ScaleEntity cube,5,.1,5EntityColor cube,255,0,0ball=CreateSphere(32)MoveEntity ball,1,0,1EntityColor ball,0,0,255light=CreateLight()MoveEntity light,5,8,5While Not KeyHit(1)If KeyHit(57) Then damage=Rnd(10)now#=health+armorIf armor>0 Thenarmor=armor-(damage *.75)health=health-(damage *.25)Elsehealth=health-damageEndIfIf armor<0 Then armor=0If health=<0 Then health=0damage=0later#=health+armorIf now# > later# Then alpha#=.75If health>0 Then alpha#=alpha#-.01If health > 0 ThenPointEntity cam,cubeTurnEntity pivot,0,1,0EndIfEntityAlpha fade,alpha#RenderWorldText 0,0,health+" health"Text 0,10,armor+" armor"If alpha#>0 Then Text 320,240,"OW I'M HURT!!!"If health=<0 ThenEntityColor fade,0,0,0PositionEntity cam,0,1,-10,1RotateEntity cam,0,0,90alpha#=0Text 0,20,"dead"EndIfFlipWendEnd