Search Unity

SurfaceReflection from WIKI and Unity 5 => problem

Discussion in 'Shaders' started by Axel-F, Apr 14, 2015.

  1. Axel-F

    Axel-F

    Joined:
    Mar 20, 2009
    Posts:
    224
    Hi,

    I was using the surface reflection example from the Unity Wiki: http://wiki.unity3d.com/index.php/SurfaceReflection

    Everything was fine with Unity 4. Today I upgraded my project to Unity 5 and the shader does not seems to work anymore. The reflection is totally distorted and the main texture is not visible at all any more.
    Well, I noticed that the MirrorReflection example from the Wiki, which SurfaceReflction is based on, got updated for Unity 5 and the first pass was replaced by CGCODE. If I use the CGCODE pass from that example the reflection is working fine again, but I'm failing to combine the reflection with my main texture from the SurfaceReflection example.Here is what I changed the SurfaceReflection example to:

    Code (JavaScript):
    1. Shader "FX/Surface Reflection"
    2. {
    3.     Properties
    4.     {
    5.         _MainAlpha("MainAlpha", Range(0, 1)) = 1
    6.         _ReflectionAlpha("ReflectionAlpha", Range(0, 1)) = 1
    7.         _TintColor ("Tint Color (RGB)", Color) = (1,1,1)
    8.         _MainTex ("MainTex (RGBA)", 2D) = ""
    9.         _ReflectionTex ("ReflectionTex", 2D) = "white" { TexGen ObjectLinear }
    10.     }
    11.     //Two texture cards: full thing
    12.     Subshader
    13.     {
    14.         Tags {Queue = Transparent}
    15.         ZWrite Off
    16.         Colormask RGBA
    17.         Color [_TintColor]
    18.         Blend SrcAlpha OneMinusSrcAlpha
    19.        
    20.         Pass {
    21.             CGPROGRAM
    22.             #pragma vertex vert
    23.             #pragma fragment frag
    24.             #include "UnityCG.cginc"
    25.            
    26.             struct v2f
    27.             {
    28.                 float2 uv : TEXCOORD0;
    29.                 float4 refl : TEXCOORD1;
    30.                 float4 pos : SV_POSITION;
    31.             };
    32.            
    33.             float4 _MainTex_ST;
    34.            
    35.             v2f vert(float4 pos : POSITION, float2 uv : TEXCOORD0)
    36.             {
    37.                 v2f o;
    38.                 o.pos = mul (UNITY_MATRIX_MVP, pos);
    39.                 o.uv = TRANSFORM_TEX(uv, _MainTex);
    40.                 o.refl = ComputeScreenPos (o.pos);
    41.                 return o;
    42.             }
    43.  
    44.             sampler2D _MainTex;
    45.             sampler2D _ReflectionTex;
    46.            
    47.             fixed4 frag(v2f i) : SV_Target
    48.             {
    49.                 fixed4 tex = tex2D(_MainTex, i.uv);
    50.                 fixed4 refl = tex2Dproj(_ReflectionTex, UNITY_PROJ_COORD(i.refl));
    51.                 return tex * refl;
    52.             }
    53.             ENDCG
    54.         }
    55.  
    56.         Pass
    57.         {
    58.             SetTexture[_MainTex] { constantColor(0,0,0, [_MainAlpha]) combine texture * primary, texture * constant}
    59.         }
    60.     }
    61. }
    Does anyone have an idea how to combine these two passes in a correct way soo the same effect as in the SurfaceReflection example is achieved? Thanks a lot in advance for any help!

    -Axel
     
  2. Axel-F

    Axel-F

    Joined:
    Mar 20, 2009
    Posts:
    224
    Well, answer to myself - here is how it works. Hopefully someone finds it useful. I hope I have no typo here...my own shader is slightly different:

    Code (JavaScript):
    1. Shader "FX/Surface Reflection"
    2. {
    3.     Properties
    4.     {
    5.         _MainAlpha("MainAlpha", Range(0, 1)) = 1
    6.         _TintColor ("Tint Color (RGB)", Color) = (1,1,1)
    7.         _MainTex ("MainTex (RGBA)", 2D) = ""
    8.         _ReflectionTex ("ReflectionTex", 2D) = "white" { }
    9.     }
    10.  
    11.     Subshader
    12.     {
    13.         Tags {Queue = Transparent}
    14.         ZWrite Off
    15.         Colormask RGBA
    16.         Color [_TintColor]
    17.         Blend SrcAlpha OneMinusSrcAlpha
    18.  
    19.         Pass {
    20.             CGPROGRAM
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.             #include "UnityCG.cginc"
    24.      
    25.             struct v2f
    26.             {
    27.                 float2 uv : TEXCOORD0;
    28.                 float4 refl : TEXCOORD1;
    29.                 float4 pos : SV_POSITION;
    30.             };
    31.      
    32.             float4 _MainTex_ST;
    33.      
    34.             v2f vert(float4 pos : POSITION, float2 uv : TEXCOORD0)
    35.             {
    36.                 v2f o;
    37.                 o.pos = mul (UNITY_MATRIX_MVP, pos);
    38.                 o.uv = TRANSFORM_TEX(uv, _MainTex);
    39.                 o.refl = ComputeScreenPos (o.pos);
    40.                 return o;
    41.             }
    42.             sampler2D _MainTex;
    43.             sampler2D _ReflectionTex;
    44.             float4 _TintColor;
    45.             float _MainAlpha;
    46.      
    47.             fixed4 frag(v2f i) : SV_Target
    48.             {
    49.                 fixed4 tex = tex2D(_MainTex, i.uv);
    50.                 fixed4 refl = tex2Dproj(_ReflectionTex, UNITY_PROJ_COORD(i.refl));
    51.                 float4 result = (tex + refl) * _TintColor;
    52.                 result.a = _MainAlpha;
    53.                 return result;
    54.             }
    55.             ENDCG
    56.         }
    57.     }
    58. }
     
    Last edited: Apr 14, 2015
  3. ArchVizPRO

    ArchVizPRO

    Joined:
    Apr 27, 2014
    Posts:
    457
    Going to try it now! Thanks!
     
  4. Loup

    Loup

    Joined:
    Apr 4, 2013
    Posts:
    11
    Extra ! Thanks a lot for your help
     
  5. cerrec

    cerrec

    Joined:
    Jan 19, 2017
    Posts:
    41
    This is great! But it would be even greater if you could apply a normal map to it. Then you could make distorted fun-house mirrors. Does anybody know how to do this?
     
  6. NikkoFuentes19

    NikkoFuentes19

    Joined:
    Nov 12, 2019
    Posts:
    2
    This doesn't work for me I just created a material, attach the shader to it, but when I apply it to a plane its just a white plane where I can just modify the main alpha. Do I need to do something else? please help me :(. I have a plane that cast reflections but its not invisible yet.