Instantly share code, notes, and snippets.

Embed
What would you like to do?
タイリングをマスクで区切るシェーダー
Shader "UI/TextureMask"
{
Properties
{
_MainTex("Albedo (RGB)", 2D) = "white" {}
_OpacityTex("Opacity (A)", 2D) = "white" {}
}
SubShader
{
Tags{ "Queue" = "transparent" "RenderType" = "transparent" }
Blend SrcAlpha OneMinusSrcAlpha
Lighting Off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
sampler2D _OpacityTex;
uniform float4 _MainTex_ST;
// Needed by TRANSFORM_TEX
uniform float4 _OpacityTex_ST;
struct v2f
{
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
// "declare" a new set of uvs will be passed from the vertex to the fragment shader
float2 uvOpacityTex : TEXCOORD1;
};
v2f vert(appdata_base v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
// Texture offset - GOOD
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
// "bind" your new uvs with the OpacityTex tile and offset properties you can find in the material
o.uvOpacityTex = TRANSFORM_TEX(v.texcoord, _OpacityTex);
return o;
}
half4 frag(v2f i) : COLOR
{
fixed4 c = tex2D(_MainTex, i.uv);
// Use your OpacityTex uvs to map the texture
half opacity = tex2D(_OpacityTex, i.uvOpacityTex).a;
// sorry but this seems to be garbage
// if (opacity == 0)
// {
// c.a = c.a;
// }
// else
// {
// c.a = 0;
// }
// multiply _MainTex and _OpacityTex alpha
c.a *= opacity;
return c;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment