Shader "Unlit/ShaderClip"
{
Properties
{
//顺时针以图片中心点为原点
//_MainTex ("Texture", 2D) = "white" {}
[PerRendererData] _MainTex("Sprite Texture", 2D) = "white" {}
_Point1("Point1",Vector) = (0,0.5,0,0)
_Point2("Point2",Vector) = (0.5,0.25,0,0)
_Point3("Point3",Vector) = (0.5,-0.25,0,0)
_Point4("Point4",Vector) = (0,-0.5,0,0)
_Point5("Point5",Vector) = (-0.5,-0.25,0,0)
_Point6("Point6",Vector) = (-0.5,0.25,0,0)
}
SubShader
{
Tags
{
"Queue" = "Transparent"
"RenderType" = "Transparent"
}
//AlphaTest Greater 1
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
float4 _Point1;
float4 _Point2;
float4 _Point3;
float4 _Point4;
float4 _Point5;
float4 _Point6;
float GetValue(float4 point1, float4 point2, float2 point3)
{
float v = 0;
if (point1.x == point2.x)
{
v = point3.x - point1.x;
}
else if (point1.y == point2.y)
{
v = point3.y - point1.y;
}
else
{
float k = (point1.y - point2.y) / (point1.x - point2.x);
v = point3.x - point1.x + (point1.y - point3.y) / k;
}
return v;
}
bool CheckIfPointInArea(float2 pos)
{
bool isIn = false;
if (GetValue(_Point1, _Point2, pos) <= 0
&& GetValue(_Point2, _Point3, pos) <= 0
&& GetValue(_Point3, _Point4, pos) <= 0
&& GetValue(_Point4, _Point5, pos) >= 0
&& GetValue(_Point5, _Point6, pos) >= 0
&& GetValue(_Point6, _Point1, pos) >= 0
)
{
isIn = true;
}
return isIn;
}
fixed4 frag (v2f i):COLOR
{
float2 temp = float2(i.uv.x - 0.5, i.uv.y-0.5);
if (!CheckIfPointInArea(temp))
{
return float4(0, 0, 0, 0);
}
else
{
fixed4 col = tex2D(_MainTex, i.uv);
return col;
}
}
ENDCG
}
}
Fallback "Diffuse"
}