Updated version of the Masking shader for NGUI

I did a shader before to mask a texture so that it doesn’t have to be rectangular. Turns out I was using an old version of NGUI, so when I updated (to version 2.65) my previous shader didn’t work anymore. Also Nicki Thomas Hansen made another shader so that you could use the masked texture inside a clipped panel. In doing so he also explained what NGUI was doing and how it was selecting the correct shader. So, based on his AlphaClip, I remade my shader so that it works on the new version of NGUI. Here is the code for it:

Shader "Unlit/Transparent Colored Masked"
{
	Properties
	{
		_MainTex ("Base (RGB), Alpha (A)", 2D) = "white" {}
		_AlphaTex ("MaskTexture", 2D) = "white" {}
	}
 
	SubShader
	{
		LOD 100
 
		Tags
		{
			"Queue" = "Transparent"
			"IgnoreProjector" = "True"
			"RenderType" = "Transparent"
		}
 
		Cull Off
		Lighting Off
		ZWrite Off
		Fog { Mode Off }
		Offset -1, -1
		Blend SrcAlpha OneMinusSrcAlpha
 
		Pass
		{
			CGPROGRAM
				#pragma vertex vertexProgram
				#pragma fragment fragmentProgram
 
				#include "UnityCG.cginc"
 
				struct appdata_t
				{
					float4 vertex : POSITION;
					float2 textureCoordinate : TEXCOORD0;
					fixed4 color : COLOR;
				};
 
				struct vertexToFragment
				{
					float4 vertex : SV_POSITION;
					half2 textureCoordinate : TEXCOORD0;
					fixed4 color : COLOR;
				};
 
				sampler2D _MainTex;
				float4 _MainTex_ST;
				sampler2D _AlphaTex;
 
				vertexToFragment vertexProgram (appdata_t vertexData)
				{
					vertexToFragment output;
					output.vertex = mul(UNITY_MATRIX_MVP, vertexData.vertex);
					output.textureCoordinate = TRANSFORM_TEX(vertexData.textureCoordinate, _MainTex);
					output.color = vertexData.color;
					return output;
				}
 
				fixed4 fragmentProgram (vertexToFragment input) : COLOR
				{
					fixed4 computedColor = tex2D(_MainTex, input.textureCoordinate) * input.color;
					fixed4 alphaGuide = tex2D(_AlphaTex, input.textureCoordinate);
 
					if (alphaGuide.a < computedColor.a) computedColor.a = alphaGuide.a;
 
					return computedColor;
				}
			ENDCG
		}
	}
}

Funny how writing my previous post solved my future problem by someone else writing an answer post.
I will update the previous post so that it points to this post also.

UPDATE: I renamed all the variables to something readable, I thought it might be useful to understand what it is doing.

, , , , ,

  1. No comments yet.
(will not be published)
Subscribe to comments feed
  1. No trackbacks yet.