From Unify Community Wiki
Jump to: navigation, search

Author: Aras Pranckevicius



This is shader+script to make perfectly reflective mirrors for Unity 1.x. Use the Mirror Reflection material on an object, attach the MirrorReflectionScript to it and there you are.

Works on vertex program capable hardware (Radeon 8500, GeForce3/4Ti, Intel 9xx). Requires Unity 1.x Pro.

Notes: this is for Unity 1.x! For a more modern version, see Mirror Reflection for Unity 4/5!


Prerequisites: This technique requires Unity Pro. The script requires ReflectionRenderTexture script to be somewhere in the project. ReflectionRenderTexture is part of Pro Standard Assets, placed in Pro Standard Assets/Water/Sources.

  • Create a material that uses the shader below (FX/Mirror Reflection)
  • Use this material on a plane-like (i.e. flat) object.
  • Attach the MirrorReflectionScript to the same object.


  • The reflection happens along object's "up" direction (green axis in the scene view). E.g. the builtin plane object is suitable for use as a mirror. If you experience weird reflection, check whether your mirror object is oriented correctly.
  • Reflection calculations use Main Camera (i.e. camera tagged as Main Camera). Be careful not to have multiple cameras tagged as Main in the scene, as you won't know which one is picked up!


A small package including an example scene showing MirrorReflection can be found here:

ShaderLab - MirrorReflection.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

Shader "FX/Mirror Reflection" {
Properties {
    _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
    _ReflectionTex ("Environment Reflection", 2D) = "" {}

// -----------------------------------------------------------
// ARB vertex program

Subshader {
    Pass {
// profiles arbfp1
// vertex vert
// fragmentoption ARB_precision_hint_fastest 
// fragmentoption ARB_fog_exp2

#include "UnityCG.cginc"

struct v2f {
    float4 ref : TEXCOORD0;

v2f vert(appdata_base v)
    v2f o;
    PositionFog( v.vertex, o.pos, o.fog );
    // calculate the reflection vector
    float4x4 mat= float4x4 (
        .5, 0, 0,.5,
         0,.5, 0,.5,
         0, 0,.5,.5,
         0, 0, 0, 1
    o.ref = mul (mat, o.pos);
    return o;

        SetTexture [_ReflectionTex] { constantColor[_ReflectColor] combine texture * constant }

// -----------------------------------------------------------
//  Fallback to non-reflective for older cards or Unity non-Pros

Subshader {
    Pass {
        SetTexture [_MainTex] { constantColor[_ReflectColor] combine constant }


JavaScript - MirrorReflectionScript.js

@script RequireComponent (ReflectionRenderTexture)
var renderTextureSize = 256;
private var renderTexture : RenderTexture;
function Start() {
    if( !RenderTexture.enabled ) {
        print("Render textures are not available. Disabling mirror script");
        enabled = false;
    renderTexture = new RenderTexture( renderTextureSize, renderTextureSize, 16 );
    renderTexture.isPowerOfTwo = true;
    var cam : Camera = camera;
    var mainCam = Camera.main;
    cam.targetTexture = renderTexture;
    cam.clearFlags = mainCam.clearFlags;
    cam.cullingMask = mainCam.cullingMask;
    cam.backgroundColor = mainCam.backgroundColor;
    cam.nearClipPlane = mainCam.nearClipPlane;
    cam.farClipPlane = mainCam.farClipPlane;
    cam.fieldOfView = mainCam.fieldOfView;
    renderer.material.SetTexture("_ReflectionTex", renderTexture);
    var reflScript : ReflectionRenderTexture = GetComponent(ReflectionRenderTexture);
    reflScript.m_ReflectUpperSide = true;
function OnDisable() {
Personal tools