Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Default mirror shader with bump map.

Discussion in 'Shaders' started by JessyStorm, Nov 26, 2015.

  1. JessyStorm

    JessyStorm

    Joined:
    Nov 26, 2015
    Posts:
    21
    Hey, guys. I am sorry for my bad english, but i have a little problem. I need your help. :(
    http://wiki.unity3d.com/index.php/MirrorReflection4 There is a default fx mirror shader, is this possible to add bump map in this shader? Thx very much.
    Code (CSharp):
    1. Shader "FX/MirrorReflection"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Base (RGB)", 2D) = "white" {}
    6.         [HideInInspector] _ReflectionTex ("", 2D) = "white" {}
    7.     }
    8.     SubShader
    9.     {
    10.         Tags { "RenderType"="Opaque" }
    11.         LOD 100
    12.         Pass {
    13.             CGPROGRAM
    14.             #pragma vertex vert
    15.             #pragma fragment frag
    16.             #include "UnityCG.cginc"
    17.             struct v2f
    18.             {
    19.                 float2 uv : TEXCOORD0;
    20.                 float4 refl : TEXCOORD1;
    21.                 float4 pos : SV_POSITION;
    22.             };
    23.             float4 _MainTex_ST;
    24.             v2f vert(float4 pos : POSITION, float2 uv : TEXCOORD0)
    25.             {
    26.                 v2f o;
    27.                 o.pos = mul (UNITY_MATRIX_MVP, pos);
    28.                 o.uv = TRANSFORM_TEX(uv, _MainTex);
    29.                 o.refl = ComputeScreenPos (o.pos);
    30.                 return o;
    31.             }
    32.             sampler2D _MainTex;
    33.             sampler2D _ReflectionTex;
    34.             fixed4 frag(v2f i) : SV_Target
    35.             {
    36.                 fixed4 tex = tex2D(_MainTex, i.uv);
    37.                 fixed4 refl = tex2Dproj(_ReflectionTex, UNITY_PROJ_COORD(i.refl));
    38.                 return tex * refl;
    39.             }
    40.             ENDCG
    41.         }
    42.     }
     
  2. Vade-Mecum

    Vade-Mecum

    Joined:
    Dec 10, 2012
    Posts:
    17
    Any solution?
     
  3. Namey5

    Namey5

    Joined:
    Jul 5, 2013
    Posts:
    188
    A couple of weeks ago, I rewrote the standard shader to include planar relfections (as a fun test). All you need to do is offset the reflection texels by the normal.

    This is an example of what I did, but it is a surface shader. Give me some time and I'll incorporate it into this shader if you wish.
     
  4. Namey5

    Namey5

    Joined:
    Jul 5, 2013
    Posts:
    188
    Here's the shader:
    Code (ShaderLab):
    1.     Shader "FX/MirrorReflection"
    2.     {
    3.         Properties
    4.         {
    5.             _MainTex ("Base (RGB)", 2D) = "white" {}
    6.             _BumpMap ("Normal Map", 2D) = "bump" {}
    7.             [HideInInspector] _ReflectionTex ("", 2D) = "white" {}
    8.         }
    9.         SubShader
    10.         {
    11.             Tags { "RenderType"="Opaque" }
    12.             LOD 100
    13.             Pass {
    14.                 CGPROGRAM
    15.                 #pragma vertex vert
    16.                 #pragma fragment frag
    17.                 #include "UnityCG.cginc"
    18.                 struct v2f
    19.                 {
    20.                     half3 tspace0 : TEXCOORD1;
    21.                     half3 tspace1 : TEXCOORD2;
    22.                     half3 tspace2 : TEXCOORD3;
    23.                     float2 uv0 : TEXCOORD4;
    24.                     float2 uv1 : TEXCOORD5;
    25.                     float4 refl : TEXCOORD6;
    26.                     float4 pos : SV_POSITION;
    27.                 };
    28.  
    29.                 float4 _MainTex_ST;
    30.                 float4 _BumpMap_ST;
    31.  
    32.                 v2f vert (appdata_tan v)
    33.                 {
    34.                     v2f o;
    35.                     o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    36.  
    37.                     half3 wNormal = UnityObjectToWorldNormal (v.normal);
    38.                     half3 wTangent = UnityObjectToWorldDir (v.tangent.xyz);
    39.                     half tangentSign = v.tangent.w * unity_WorldTransformParams.w;
    40.                     half3 wBitangent = cross (wNormal, wTangent) * tangentSign;
    41.                     o.tspace0 = half3 (wTangent.x, wBitangent.x, wNormal.x);
    42.                     o.tspace1 = half3 (wTangent.y, wBitangent.y, wNormal.y);
    43.                     o.tspace2 = half3 (wTangent.z, wBitangent.z, wNormal.z);
    44.  
    45.                     o.uv0 = TRANSFORM_TEX (v.texcoord, _MainTex);
    46.                     o.uv1 = TRANSFORM_TEX (v.texcoord, _BumpMap);
    47.                     o.refl = ComputeScreenPos (o.pos);
    48.                     return o;
    49.                 }
    50.  
    51.                 sampler2D _MainTex;
    52.                 sampler2D _BumpMap;
    53.                 sampler2D _ReflectionTex;
    54.                 float4 _ReflectionTex_TexelSize;
    55.  
    56.                 fixed4 frag(v2f i) : SV_Target
    57.                 {
    58.                     half3 tnormal = UnpackNormal (tex2D (_BumpMap, i.uv1));
    59.                    
    60.                     half3 worldNormal;
    61.                     worldNormal.x = dot (i.tspace0, tnormal);
    62.                     worldNormal.y = dot (i.tspace1, tnormal);
    63.                     worldNormal.z = dot (i.tspace2, tnormal);
    64.  
    65.                     float2 offset = worldNormal * 100 * _ReflectionTex_TexelSize;
    66.                     i.refl.xy = i.refl.z * offset + i.refl.xy;
    67.  
    68.                     fixed4 tex = tex2D (_MainTex, i.uv0);
    69.                     fixed4 refl = tex2Dproj (_ReflectionTex, UNITY_PROJ_COORD (i.refl));
    70.                     return tex * refl;
    71.                 }
    72.                 ENDCG
    73.             }
    74.         }
    75.         Fallback "Diffuse"
    76.    }
    77.