From Unify Community Wiki
Jump to: navigation, search

Author: Max70.



This is a simple night vision system (green color)

Requires Unity PRO. This script uses texture rendering and Post-processing only available in Unity Pro


  • Luminence: green luminence.
  • Noise Factor: factor of noise for the vision.

Typical setup

C# - GreenNightVision.cs

Script for Camera Renderer:

using UnityEngine;
[AddComponentMenu("Image Effects/Night Vision")]
public class NightVision1 : MonoBehaviour {
	// public data
	public Shader shader;
	public Color luminence;
	public float noiseFactor=0.005f;
	//private data
	private Material mat;
	// start 
	void Start() 
		shader = Shader.Find( "Image Effects/Night Vision" );
		mat = new Material (shader);
		mat.SetVector( "lum", new Vector4( luminence.g, luminence.g, luminence.g, luminence.g) );
		mat.SetFloat("noiseFactor", noiseFactor);
	// Called by camera to apply image effect
	void OnRenderImage (RenderTexture source, RenderTexture destination) 
		mat.SetFloat("time", Mathf.Sin(Time.time * Time.deltaTime));
		Graphics.Blit( source, destination, mat );

ShaderLab - NightVision.shader

// Night Vision Shader Version 1.00 
// Ogre3D convertion
// adapted by Max, Yoda the rebel, 
// July 2012.
Shader "Image Effects/Night Vision" {
Properties {
	_MainTex ("Base (RGB)", RECT) = "white" {}
SubShader {
	Pass {
		ZTest Always Cull Off ZWrite Off
		Fog { Mode off }
#pragma vertex vert_img
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest 
#include "UnityCG.cginc"
// frag shaders data
uniform sampler2D _MainTex;
uniform float4 lum;
uniform float time;
uniform float noiseFactor;
// frag shader
float4 frag (v2f_img i) : COLOR
	float4 pix = tex2D(_MainTex, i.uv);
	//obtain luminence value
	pix = dot(pix,lum);
	// noise simulation
	float2 t = i.uv;		
	float x = t.x *t.y * 123456 * time;
	x = fmod(x,13) * fmod(x,123);	
	float dx = fmod(x,noiseFactor);
	float dy = fmod(x,noiseFactor);
	float4 c = tex2D(_MainTex, t+float2(dx,dy))*0.5;	
    pix += c;
	//add lens circle effect
	//(could be optimised by using texture)
	float dist = distance(i.uv, float2(0.5,0.5));
	pix *= smoothstep(0.5,0.45,dist);
	//add rb to the brightest pixels
	pix.rb = max (pix.r - 0.75, 0)*4;
	// return pix pixel ;)	
	return pix;
Fallback off
Personal tools