CG equivalent of 'matrix [_ProjMatrix]'

I am trying to create an effect that resembles polished marble, something very similar to the mirror setup found here.
This provides me with the realtime reflection I need, but I’d like to use a fragment shader to apply it to the surface so that I can have greater control over how the textures are blended/distorted. So far I have the following:

Shader "Playmat/Marble"
{
  Properties
  {
      _MainTex ("Base (RGB)", 2D) = "white" {}
      _ReflectionTex ("Reflection", 2D) = "white" { TexGen ObjectLinear }
  }

  SubShader
  {
    Pass
    {
      CGPROGRAM
      #pragma vertex vert
      #pragma fragment frag
      #include "UnityCG.cginc"

      sampler2D _MainTex;
      float4 _MainTex_ST;
      sampler2D _ReflectionTex;

      float4x4 _ProjMatrix;

     struct appdata
      {
        float4 vertex : POSITION;
        float2 texcoord : TEXCOORD0;
      };

      struct v2f
      {
          float4 pos : SV_POSITION;
          float2 uv : TEXCOORD0;
      };

      v2f vert (appdata v)
      {
        v2f o;
        o.pos = mul( UNITY_MATRIX_MVP, v.vertex );
        o.uv = TRANSFORM_TEX( v.texcoord, _MainTex );
        return o;
      }

      half4 frag( v2f i ) : COLOR
      {
        half4 c = tex2D(_MainTex, i.uv);

        //Get Reflection color here
        //half4 r = tex2D(_ReflectionTex, ??);

        //combine with magic

        return c;
      }
     ENDCG
    }
  }
}

Unfortunately I’m at a loss as to how to use the projection matrix to generate the UVs I would need to sample for the reflection texture. Any help would be appriciated.

I think you’re after UNITY_MATRIX_P ?

http://docs.unity3d.com/Documentation/Components/SL-BuiltinStateInPrograms.html

Thanks for the quick reply. I suspect that is the case, yes, but my matrix math is a bit rusty and I’m having difficulty working out the math to calculate the appropriate UVs.

Ah, I see - missed the tiny link in your main post, didn’t see that _ProjMatrix was passed in from a script. So ignore my previous post - that’s not what you’re after :stuck_out_tongue:

As far as I can tell (from here) ShaderLab’s matrix [_Matrix ] is the same as CG’s mul ( _Matrix, uvs );

So you’ll want (I think)…

half4 c = tex2D(_MainTex, i.uv);
half4 r = tex2D(_ReflectionTex, mul(_ProjMatrix, i.uv).xy);
c *= r;
return c;

That was my thinking as well so I tried this:

      half4 frag( v2f i ) : COLOR
      {
        //half4 c = tex2D(_MainTex, i.uv);
        float2 newUV = mul(_ProjMatrix, half4(i.uv, 0, 0)).xy;
        half4 c = tex2D(_ReflectionTex, newUV);

        return c;
      }

but that gives streaks of color all along the texture so somthing isn’t right. That also didn’t intuit quite right to me. I feel like I need to modify that projection matrix by the second camera’s projection matrix, but I am eluded by the maths. Eluded!