Apply mask rendertexture, win and mac shows different result

I have a very simple goal, apply a mask on a rendertexture after that display it on a GUI.

i use the following code, it works perfectly on windows platform, but on mac platform the mask has no effect at all. the entire rendertexture was showing on the GUI.

Shader "Hidden/Mask Off" {
Properties {
	_MainTex ("Base (RGB)", RECT) = "white" {}
	_MaskTex ("Base (RGB)", 2D) = "white" {}
}

SubShader {
	Pass {
        ZTest Always Cull Off ZWrite Off
		Fog { Mode off }
				
CGPROGRAM
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest 
#include "UnityCG.cginc"

uniform samplerRECT _MainTex;
uniform sampler2D _MaskTex;

float4 frag (v2f_img i) : COLOR
{
	float4 rt = texRECT(_MainTex, i.uv);
	float4 mask = tex2D(_MaskTex, i.uv);
	rt.a = 1;
	return min(rt, mask);
}
ENDCG

	}
}

Fallback off

}

_MainTex is the rendertexture and _MaskTex is the mask texture (ARGB), and the technique is very simple,

  1. set the rendertexture alpha to 1 (ignore the rendertexure alpha channel)
  2. get the min value of the rendertexture and mask. on wanted area the mask’s color is white and alpha is 255, on unwanted area the mask’s color is black and alpha is 0, therefore min() could be used to mask out the unwanted area.

Help please.

Anyone know about the shaderlab (or Cg?) version use in mac and win? i suspect the problem is caused by shaderlab not because of the hardware.

The difference comes from the fact that the concept of RECT textures is OpenGL only and there the texture coordinates are in pixels (not in range 0-1).

So you should have something like:

texRECT(_MainTex, i.uv * _RenderTexSize)

where _RenderTexSize is:

  • the size of your texture (e.g. 640x480) on Mac
  • 1x1 on Windows

thanks for the hints. i fixed the problem by

tex2D(_MaskTex, i.uv / _RenderTexSize);

seem like texRect is expecting coordinate in pixel but tex2D is still expecting it in float.

thanks for your help