From Unify Community Wiki
Revision as of 19:19, 30 January 2013 by Max70 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 Render:

using UnityEngine;
[AddComponentMenu("Image Effects/Night Vision v1")]
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

Invalid language.

You need to specify a language like this: <source lang="html4strict">...</source>

Supported languages for syntax highlighting:

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, asm, asp, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, oobas, oracle11, oracle8, oxygene, oz, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, python, q, qbasic, rails, rebol, reg, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, sql, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, uscript, vala, vb, vbnet, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic

// 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