MirrorReflection

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Adding mirror reflection)
 
Line 7: Line 7:
  
 
==Usage==
 
==Usage==
Prerequisites: 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.
+
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)
 
* Create a material that uses the shader below (FX/Mirror Reflection)
Line 14: Line 14:
  
 
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.
 
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.
 +
 +
==Example==
 +
A small package including an example scene showing MirrorReflection can be found here:
 +
* [[Media:MirrorReflection.unityPackage.zip|MirrorReflection.unityPackage.zip]]
  
 
==ShaderLab - MirrorReflection.shader==
 
==ShaderLab - MirrorReflection.shader==

Revision as of 15:22, 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.

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.

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