SurfaceReflection from WIKI and Unity 5 => problem

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:

Shader "FX/Surface Reflection"
{
    Properties
    {
        _MainAlpha("MainAlpha", Range(0, 1)) = 1
        _ReflectionAlpha("ReflectionAlpha", Range(0, 1)) = 1
        _TintColor ("Tint Color (RGB)", Color) = (1,1,1)
        _MainTex ("MainTex (RGBA)", 2D) = ""
        _ReflectionTex ("ReflectionTex", 2D) = "white" { TexGen ObjectLinear }
    }
    //Two texture cards: full thing
    Subshader
    {
        Tags {Queue = Transparent}
        ZWrite Off
        Colormask RGBA
        Color [_TintColor]
        Blend SrcAlpha OneMinusSrcAlpha
       
        Pass {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
           
            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 refl : TEXCOORD1;
                float4 pos : SV_POSITION;
            };
           
            float4 _MainTex_ST;
           
            v2f vert(float4 pos : POSITION, float2 uv : TEXCOORD0)
            {
                v2f o;
                o.pos = mul (UNITY_MATRIX_MVP, pos);
                o.uv = TRANSFORM_TEX(uv, _MainTex);
                o.refl = ComputeScreenPos (o.pos);
                return o;
            }

            sampler2D _MainTex;
            sampler2D _ReflectionTex;
           
            fixed4 frag(v2f i) : SV_Target
            {
                fixed4 tex = tex2D(_MainTex, i.uv);
                fixed4 refl = tex2Dproj(_ReflectionTex, UNITY_PROJ_COORD(i.refl));
                return tex * refl;
            }
            ENDCG
        }

        Pass
        {
            SetTexture[_MainTex] { constantColor(0,0,0, [_MainAlpha]) combine texture * primary, texture * constant}
        }
    }
}

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

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:

Shader "FX/Surface Reflection"
{
    Properties
    {
        _MainAlpha("MainAlpha", Range(0, 1)) = 1
        _TintColor ("Tint Color (RGB)", Color) = (1,1,1)
        _MainTex ("MainTex (RGBA)", 2D) = ""
        _ReflectionTex ("ReflectionTex", 2D) = "white" { }
    }

    Subshader
    {
        Tags {Queue = Transparent}
        ZWrite Off
        Colormask RGBA
        Color [_TintColor]
        Blend SrcAlpha OneMinusSrcAlpha
 
        Pass {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
     
            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 refl : TEXCOORD1;
                float4 pos : SV_POSITION;
            };
     
            float4 _MainTex_ST;
     
            v2f vert(float4 pos : POSITION, float2 uv : TEXCOORD0)
            {
                v2f o;
                o.pos = mul (UNITY_MATRIX_MVP, pos);
                o.uv = TRANSFORM_TEX(uv, _MainTex);
                o.refl = ComputeScreenPos (o.pos);
                return o;
            }
            sampler2D _MainTex;
            sampler2D _ReflectionTex;
            float4 _TintColor;
            float _MainAlpha;
     
            fixed4 frag(v2f i) : SV_Target
            {
                fixed4 tex = tex2D(_MainTex, i.uv);
                fixed4 refl = tex2Dproj(_ReflectionTex, UNITY_PROJ_COORD(i.refl));
                float4 result = (tex + refl) * _TintColor;
                result.a = _MainAlpha;
                return result;
            }
            ENDCG
        }
    }
}

Going to try it now! Thanks!

Extra ! Thanks a lot for your help

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?

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.