MirrorReflection

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
 
(12 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 +
[[Category: Unity 1.x shaders]]
 +
[[Category: Unity Pro shaders]]
 +
 
Author: [[User:NeARAZ|Aras Pranckevicius]]
 
Author: [[User:NeARAZ|Aras Pranckevicius]]
  
 
==Description==
 
==Description==
This is shader+script to make perfectly reflective mirrors. Use the Mirror Reflection material on an object, attach the MirrorReflectionScript to it and there you are.
+
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 [[MirrorReflection4|Mirror Reflection for Unity 4/5]]!
  
Works on vertex program capable hardware (Radeon 8500, GeForce3/4Ti, Intel 9xx). Requires Unity Pro.
 
  
 
==Usage==
 
==Usage==
Line 13: Line 19:
 
* Attach the MirrorReflectionScript to the same object.
 
* Attach the MirrorReflectionScript to the same object.
  
Note: 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.
+
Notes:
 +
* 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!
  
 
==Example==
 
==Example==
Line 20: Line 28:
  
 
==ShaderLab - MirrorReflection.shader==
 
==ShaderLab - MirrorReflection.shader==
<shaderlab>Shader "FX/Mirror Reflection" {
+
<syntaxhighlight lang="shaderlab">Shader "FX/Mirror Reflection" {
 
Properties {
 
Properties {
 
     _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
 
     _ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
Line 77: Line 85:
  
 
}
 
}
</shaderlab>
+
</syntaxhighlight>
  
 
==JavaScript - MirrorReflectionScript.js==
 
==JavaScript - MirrorReflectionScript.js==
<javascript>@script RequireComponent (ReflectionRenderTexture)
+
<syntaxhighlight lang="javascript">@script RequireComponent (ReflectionRenderTexture)
  
 
var renderTextureSize = 256;
 
var renderTextureSize = 256;
Line 101: Line 109:
 
     cam.targetTexture = renderTexture;
 
     cam.targetTexture = renderTexture;
 
     cam.clearFlags = mainCam.clearFlags;
 
     cam.clearFlags = mainCam.clearFlags;
 +
    cam.cullingMask = mainCam.cullingMask;
 
     cam.backgroundColor = mainCam.backgroundColor;
 
     cam.backgroundColor = mainCam.backgroundColor;
 
     cam.nearClipPlane = mainCam.nearClipPlane;
 
     cam.nearClipPlane = mainCam.nearClipPlane;
Line 115: Line 124:
 
     Destroy(renderTexture);
 
     Destroy(renderTexture);
 
}
 
}
</javascript>
+
</syntaxhighlight>

Latest revision as of 08:44, 25 February 2015


Author: Aras Pranckevicius

Contents

[edit] Description

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!


[edit] Usage

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.

Notes:

  • 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!

[edit] Example

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

[edit] 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 {
    
CGPROGRAM
// profiles arbfp1
// vertex vert
// fragmentoption ARB_precision_hint_fastest 
// fragmentoption ARB_fog_exp2

#include "UnityCG.cginc"

struct v2f {
    V2F_POS_FOG;
    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;
}
ENDCG

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

}

[edit] 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;
        return;
    }
 
    renderTexture = new RenderTexture( renderTextureSize, renderTextureSize, 16 );
    renderTexture.isPowerOfTwo = true;
 
    gameObject.AddComponent("Camera");
    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() {
    Destroy(renderTexture);
}
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox