MirrorReflection

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
Line 13: Line 13:
 
* 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==

Revision as of 17:25, 10 January 2007

Author: Aras Pranckevicius

Contents

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.

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

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!

Example

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

ShaderLab - MirrorReflection.shader

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

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

}

} </shaderlab>

JavaScript - MirrorReflectionScript.js

<javascript>@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.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);

} </javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox