Search Unity

RealTime Mirror (Reflection) using RndertToCubeMap (Real Mirror)

Discussion in 'Community Learning & Teaching' started by DragonForce, Aug 4, 2013.

  1. DragonForce

    DragonForce

    Joined:
    May 27, 2013
    Posts:
    14
    hi every body :D
    i will teach you how to create real mirror using RenderToCubeMp( It is work wih Unity Pro)

    here the sorce Package : http://www.mediafire.com/download/setk5df3d7ll555/Reflection_Mirror_RenderToCubeMap.rar

    Note ::: The Mirror woking in editor Mode But !! it is not real Reflection you have to look at mirror using Main camera

    let's Start

    we have 2 types of mirror
    A. Plane Mirror(like water,Sea or Plate Miror)
    B. Mesh Mirror(like Sphee , Car , Cube .......)


    First Plane Mirrro in this case reflection camera must take the same positin of main camera (X,Z) and negative Position of maine Camera (Y)

    here the steps

    1. Create Plane (GameObect >> Create Other >> Plane)
    2. Create Camera name it "PlaneReflectionCamera" (GameObjct >> Create Other >> Cmera)
    3. Make sure camera t working as main Camera (Select Camera - - in the inspector tab uncheck Camea)
    4. Create material (Assets >> Create >> Material) and drop it into Plane
    5. Creater Shader (Assets >> Create >> Shader) and drop it into mateial
    6. Create c# Script (Assets >> Create >> c# Scrip) and drop it into Plane
    then Enjoy With real Mirror :grin:

    Shaer Code

    Code (csharp):
    1.  
    2. Shader "Custom/NewShader" {
    3. Properties {
    4.       _MainTex ("Texture", 2D) = "white" {}
    5.       _Cube ("Cubemap", CUBE) = "" {}
    6.     }
    7.     SubShader {
    8.       Tags { "RenderType" = "Opaque" }
    9.       CGPROGRAM
    10.       #pragma surface surf Lambert
    11.       struct Input {
    12.           float2 uv_MainTex;
    13.           float3 worldRefl;
    14.       };
    15.       sampler2D _MainTex;
    16.       samplerCUBE _Cube;
    17.       void surf (Input IN, inout SurfaceOutput o) {
    18.           o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * 0.5;
    19.           o.Emission = texCUBE (_Cube, IN.worldRefl).rgb;
    20.       }
    21.       ENDCG
    22.     }
    23.     Fallback "Diffuse"
    24.   }
    25.  

    c# Code
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [ExecuteInEditMode]  
    6. public class PlaneReflection : MonoBehaviour {
    7.  
    8.     // Use this for initialization
    9.     Camera refcam;
    10.     Camera Minecam;
    11.     Cubemap Reflection;
    12.     void Start () {
    13.     refcam = GameObject.Find("PlaneReflectionCamera").camera;
    14.     Reflection = new Cubemap(512,TextureFormat.ARGB32,false);
    15.     Minecam = Camera.main;
    16.    
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     Vector3 Position;
    21.     void Update () {
    22.         Position = Minecam.transform.position;
    23.         //Position.z *=-1;
    24.         Position.y *=-1f;
    25.         refcam.transform.position = Position;
    26.     refcam.RenderToCubemap(Reflection);
    27.    
    28.    
    29.     renderer.material.SetTexture("_Cube",Reflection);
    30.     }
    31. }
    32.  
    33.  
    Second. Mesh Mirro in this case camera reflection most match Mesh Posittion

    here the steps

    1. Create any mesh (GameObect >> Create Other >> Sphere)
    2. Create Camera name it "MeshReflectionCamera" (GameObjct >> Create Other >> Cmera)
    3. Make sure camera t working as main Camera (Select Camera - - in the inspector tab uncheck Camea)
    4. Create material (Assets >> Create >> Material) and drop it into Mesh
    5. Creater Shader (Assets >> Create >> Shader) and drop it into mateial
    6. Create c# Script (Assets >> Create >> c# Scrip) and drop it into Mesh
    then Enjoy With real Mirror :grin:

    Shader Code
    Code (csharp):
    1.  
    2. Shader "Custom/MeshReflection" {
    3. Properties {
    4.       _MainTex ("Texture", 2D) = "white" {}
    5.       _Cube ("Cubemap", CUBE) = "" {}
    6.      
    7.     }
    8.     SubShader {
    9.       Tags { "RenderType" = "Opaque" }
    10.       CGPROGRAM
    11.       #pragma surface surf Lambert
    12.       struct Input {
    13.           float2 uv_MainTex;
    14.           float3 worldRefl;
    15.       };
    16.       sampler2D _MainTex;
    17.       samplerCUBE _Cube;
    18.       void surf (Input IN, inout SurfaceOutput o) {
    19.           o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * 0.5;
    20.           o.Emission = texCUBE (_Cube, IN.worldRefl).rgb;
    21.           //
    22.       }
    23.       ENDCG
    24.     }
    25.     Fallback "Diffuse"
    26.   }
    27.  
    28.  
    c# Code
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [ExecuteInEditMode]
    6. public class MeshReflection : MonoBehaviour {
    7.     Camera refcam;
    8.     Camera Minecam;
    9.     Cubemap Reflection;
    10.     void Start () {
    11.     refcam = GameObject.Find("MeshReflectionCamera").camera;
    12.     Reflection = new Cubemap(512,TextureFormat.ARGB32,false);
    13.    
    14.     Minecam = Camera.main;
    15.    
    16.     }
    17.    
    18.     // Update is called once per frame
    19.    
    20.     void Update () {
    21.     refcam.transform.position = transform.position;
    22.     refcam.RenderToCubemap(Reflection);
    23.     renderer.material.SetTexture("_Cube",Reflection);
    24.     }
    25. }
    26.  
    27.  
    i hope you enjoy it :grin::grin::grin:
     
    Last edited: Aug 4, 2013
    John-G likes this.
  2. muralinath

    muralinath

    Joined:
    Feb 6, 2014
    Posts:
    3
    how to add blur effect