Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Code Snippets / [DBP] Easy Night Vision Function (No external media required)

Author
Message
Sixty Squares
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 28th Jul 2010 03:10
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:


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:



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!
HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 28th Jul 2010 13:28 Edited at: 28th Jul 2010 13:28
Sixty Squares, thought your version Brilliant, here mine based on your code but using object plain locked to screen instead of sprites, only thing I could not get to do was making the noise image to rotate.





Dark Physics makes any hot drink go cold.
Blobby 101
17
Years of Service
User Offline
Joined: 17th Jun 2006
Location: England, UK
Posted: 2nd Aug 2010 14:08 Edited at: 2nd Aug 2010 14:28
Just before I actually implement this, does it actually increase visibility? or does it just make things green and static? xD

EDIT: OK, it doesn't - any ideas on how I could achieve this?

Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 2nd Aug 2010 15:09
Yeah, all you really need to do is extend the 'green' cameras draw distance.

SET CAMERA RANGE nvcam,10,10000

Or whatever values work best for the size of the environment.

Health, Ammo, and bacon and eggs!
Blobby 101
17
Years of Service
User Offline
Joined: 17th Jun 2006
Location: England, UK
Posted: 2nd Aug 2010 15:41
haha, thanks Van, but not quite what I meant - I meant like Real Night vision goggles make it easier to see in the dark they don't just make things green, how could I actually make it do that?

Van B
Moderator
21
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 2nd Aug 2010 15:46
Ahhh, well it depends on how the level is - I mean, maybe the normal camera has fog and a low ambient light setting - with the night vision I'd use a high ambient light, and disable fog.

Health, Ammo, and bacon and eggs!
Blobby 101
17
Years of Service
User Offline
Joined: 17th Jun 2006
Location: England, UK
Posted: 2nd Aug 2010 16:17 Edited at: 2nd Aug 2010 16:30
hmmm.. OK - i'll try messing around with ambient light xD cheers.

EDIT: no, that's not gonna work - you see, I have a light mapped level that I made in 3DWS and it (deliberately) has areas of shadow (it's a stealth game). You can't raise ambient light higher than 100 (it's default) and I'd rather not have to make things darker without goggles on as that probably wouldn't do much good

Sixty Squares
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 2nd Aug 2010 18:28 Edited at: 2nd Aug 2010 20:50
Hi, I was away so sorry for the delayed response.

@HowDo: That's a cool way to do it . I like the fog effect. The plain doesn't cover the whole screen for me though

@blobby101: Hmmm... That's a tricky one. This could be possible with a shader, but even then I'm not sure how one would do it...

An easy option could be to export 2 copies of your world. The first copy would be lightmapped with your shadows and everything, and the second copy wouldn't be lightmapped at all, no shadows whatsoever. In your game you would load both copies at once, in the same spot. You would want to display the lightmapped version when night vision is NOT turned on, and you would want to display the non-lightmapped version when night vision IS turned on. Here are 2 ways to do this:
1) Just hide the objects accordingly in your loop, i.e.:


2) Use SET OBJECT MASK to make sure that the lightmapped version only renders to the screen camera, and the non-lightmapped version only renders to the night vision camera. The SET OBJECT MASK command uses binary for the mask value, but since binary is base 2 there is an easy way to use it. Just raise 2 to the power of the camera you want the object to render to, and the object will only render to that camera. For example, to make the lightmapped world only render to the normal screen camera, and the non-lightmapped world render to the night vision camera you would just do this:


In both situations, you would only do collision for one of the two objects.

Now, I'm not sure what this would do to your polygon count or FPS, but if it lags you could always use SYNC MASK to only render the night vision camera when needed, like this:


Then it might only lag with night vision on, if it lagged at all. Anyway let me know how it works out


Guns, cinematics, stealth, items and more!
Blobby 101
17
Years of Service
User Offline
Joined: 17th Jun 2006
Location: England, UK
Posted: 2nd Aug 2010 19:51 Edited at: 2nd Aug 2010 19:53
ah, yeah - couldn't get the render mask stuff to work properly but I just hid them when not needed and it works like a charm - also - instead of just deleting the lightmaps, I increased the actual image's brightness and I think it worked well, take a look at the pic

(ignore the tall cube - it's just a marker)


Attachments

Login to view attachments
Sixty Squares
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 2nd Aug 2010 20:01
Wow that looks great, I'm glad you've got it working . Which one did you increase the brightness of though- the lightmap or the camera image? If you increased the camera image's brightness then I'm curious as to how you did that


Guns, cinematics, stealth, items and more!
Blobby 101
17
Years of Service
User Offline
Joined: 17th Jun 2006
Location: England, UK
Posted: 2nd Aug 2010 21:13
the lightmap unfortunately, If I could increase the screen image brightness I could use it on non-lightmapped models. I think i may need to use a brighter texture version for characters as well

HowDo
21
Years of Service
User Offline
Joined: 28th Nov 2002
Location: United Kingdom
Posted: 2nd Aug 2010 22:25
Quote: "The plain doesn't cover the whole screen for me though "


nearly got it, but cannot see away to make it move away from the camera and still stay the same.

this get it near.
make object plain 101,1.67,1.2,1

I think I need to make a plain that has the same aspect ratio as the screen it on, not worked out how yet.

Dark Physics makes any hot drink go cold.
Sixty Squares
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 2nd Aug 2010 23:41 Edited at: 2nd Aug 2010 23:44
@blobby: Ah. For the non-lightmapped models you could probably do SET OBJECT AMBIENT obj,0 and/or SET OBJECT LIGHT obj,0 to make them ignore any lighting effects in your game (unless you've already got the ambient light at 100 and really bright lights). If you did this while Night Vision was on and then set the flags back to 1 when Night Vision was off it might work .

@HowDo: I asked a similar question a while back., There might be a good solution here: http://forum.thegamecreators.com/?m=forum_view&t=138071&b=7


Guns, cinematics, stealth, items and more!

Login to post a reply

Server time is: 2024-03-28 16:51:10
Your offset time is: 2024-03-28 16:51:10