This was originally posted here in response to a question:
http://forum.thegamecreators.com/?m=forum_view&t=173197&b=1.
Credit to Van B for the Set Sprite Diffuse idea.
This function allows you to easily use Night Vision in your 3D game. It also includes the static you might see on Night Vision goggles. The function uses sprites.
Example with the function. Use the arrow keys to move and hold SPACE for night vision:
rem ***************************
rem
rem NIGHT VISION
rem
rem By Sixty Squares
rem ***************************
rem Setup
Sync On
Sync Rate 0
Autocam Off
`Generate a noise texture for the static
Noise=1
Size=512 `Change this to change the resolution of the noise (low resolutions make the method I'm using to control the noise easier to notice, which is bad...
Create bitmap 1,Size,Size
for x = 0 to Size-1
for y = 0 to Size-1
c=rnd(255)
ink rgb(c,c,c),0
box x,y,x+1,y+1
next y
next x
Get Image Noise,0,0,Size,Size,1
Delete bitmap 1
rem Make a scene of random cubes to look at.
for o = 1 to 100
make object cube o,10
position object o,-250+rnd(500),0,-250+rnd(500)
set object emissive o,rgb( rnd(255), rnd(255), rnd(255) )
next o
rem Make a night vision camera and set it to an image. You can change the resolution by changing the size of the image.
nvcam=1 `Night vision camera #
nvIMG=2 `Night vision image #
Make Camera nvcam
Set Camera To Image nvcam,nvIMG,screen width(),screen height()
rem Set the noise size to the length of the diagonal of the screen. This way you never see the edge of the texture.
Size=SQRT( Screen Width()^2 + Screen Height()^2 )
rem Setup the timer based movement.
OldTime=Timer()
Do
rem Simple timer based movement
factor#=(Timer()-OldTime)*0.060
OldTime=Timer()
rem Control Camera
Control Camera Using Arrowkeys 0,2*factor#,1.25*factor#
Position Camera nvcam,camera position x(0),camera position y(0),camera position z(0)
Rotate Camera nvcam,camera angle x(0),camera angle y(0),camera angle z(0)
rem Hold space to see the night vision
if Spacekey() then NightVision(nvIMG,Noise,1,Size)
rem Print stuff
set cursor 0,0
ink rgb(255,255,255),0
print "FPS: ",screen fps()
print " "
print "Hold SPACE to see everything in night vision."
Sync
Loop
Function NightVision(CamTex,NoiseTex,SpriteToUse,Size)
remstart
CamTex= Image # of the camera to use
NoiseTex= Image # of the texture to use for noise
SpriteToUse= The Sprite # to use. It gets deleted after each call.
Size= The size of the noise sprite (make sure it's bigger than the screen). If you specify 0 then it will auto-calculate the size.
remend
rem If you do not specify a size then it is auto-calculated.
if Size=0 then Size=SQRT( Screen Width()^2 + Screen Height()^2 )
s=SpriteToUse
rem Stretch the camera texture across the whole screen and make it green.
sprite s,0,0,CamTex
size sprite s,screen width(),screen height()
set sprite diffuse s,0,255,0 `Change this to change the basic color (I chose green for night vision)
set sprite alpha s,255
paste sprite s,0,0
rem Apply the noise texture and stretch it to a size larger than the screen (Specified in size variable)
offset=10 `Increase this for more randomness in the noise. You'll need a bigger "Size" though so the noise texture doesn't go off screen.
sprite s,screen width()/2.0,screen height()/2.0,NoiseTex
size sprite s,Size,Size
set sprite alpha s,25 `Change this to change the noise density.
offset sprite s,Size/2.0-offset/2+rnd(offset),Size/2.0-offset/2+rnd(offset)
set sprite diffuse s,255,255,255 `Change this to change the noise color.
rem Randomly rotate/flip/mirror/offset the nosie so it looks like static
rotate sprite s,rnd(359)
if rnd(1)=0 then flip sprite s
if rnd(1)=0 then mirror sprite s
paste sprite s,screen width()/2.0,screen height()/2.0
delete sprite s
Endfunction
How to use the function in your game:
Before you start:
1) Copy and paste the
NightVision() function into your game. Put it AFTER your loop. Here is the function by itself:
Function NightVision(CamTex,NoiseTex,SpriteToUse,Size)
remstart
CamTex= Image # of the camera to use
NoiseTex= Image # of the texture to use for noise
SpriteToUse= The Sprite # to use. It gets deleted after each call.
Size= The size of the noise sprite (make sure it's bigger than the screen). If you specify 0 then it will auto-calculate the size.
remend
rem If you do not specify a size then it is auto-calculated.
if Size=0 then Size=SQRT( Screen Width()^2 + Screen Height()^2 )
s=SpriteToUse
rem Stretch the camera texture across the whole screen and make it green.
sprite s,0,0,CamTex
size sprite s,screen width(),screen height()
set sprite diffuse s,0,255,0 `Change this to change the basic color (I chose green for night vision)
set sprite alpha s,255
paste sprite s,0,0
rem Apply the noise texture and stretch it to a size larger than the screen (Specified in size variable)
offset=10 `Increase this for more randomness in the noise. You'll need a bigger "Size" though so the noise texture doesn't go off screen.
sprite s,screen width()/2.0,screen height()/2.0,NoiseTex
size sprite s,Size,Size
set sprite alpha s,25 `Change this to change the noise density.
offset sprite s,Size/2.0-offset/2+rnd(offset),Size/2.0-offset/2+rnd(offset)
set sprite diffuse s,255,255,255 `Change this to change the noise color.
rem Randomly rotate/flip/mirror/offset the nosie so it looks like static
rotate sprite s,rnd(359)
if rnd(1)=0 then flip sprite s
if rnd(1)=0 then mirror sprite s
paste sprite s,screen width()/2.0,screen height()/2.0
delete sprite s
Endfunction
Before the loop:
1) Make a static texture (basically it's just random black/white/gray dots [noise]) I use 512x512 resolution in the example and generate the texture in the program. You could just as easily make your own though and use LOAD IMAGE. The higher the resolution the finer the static. Very low resolutions look bad though, so I wouldn't use them...
2) Make another camera using MAKE CAMERA (ex. MAKE CAMERA 1)
3) Set the camera to an image using SET CAMERA TO IMAGE. The width/height of the image is up to you- the smaller the image the more pixelated the night vision looks. The image number is any image number that you are not using in your game.
During the loop:
1) Position and rotate the other camera you made where your main camera (camera 0) is. (This camera decides where you see in night vision from. If you put it where your main camera is, you see the same thing that your main camera sees, but in night vision. You can put it somewhere else to see in night vision from another location. This could be useful for a security camera angle.)
2) Call the
NightVision() function BEFORE YOU PRINT ANY TEXT OR HUD TO THE SCREEN. This is because the night vision will go on top of any text you have already printed. The parameters to specify are:
CamTex- The image number you set your camera to earlier.
NoiseTex- The image number of your static image.
SpriteToUse- Any sprite number you aren't using.
Size- This is the size of the noise texture. It must be larger than the screen. If you aren't sure what to use, just write 0 and the function will calculate a good size automatically.
*If you want to change the colors/transparency or anything, read the comments in the function. They tell you what you can change.
*I use a lot of variables in the example (outside of the function), but you don't need to. You can just as easily use your own numbers instead of variables.
*If you don't want to see everything in night vision, then just don't call the function.
And that's it! Please comment and let me know if you have any questions
Guns, cinematics, stealth, items and more!