Shader "Unlit/ShapeShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
float box0(float2 uv,float left,float right,float top,float bottom)
{
if (uv.x>left&&uv.x<right&&uv.y>bottom&&uv.y<top)
{
return 1;
}
return 0;
}
//距离上下左右边界的距离
float box1(float2 uv, float left, float right, float top, float bottom)
{
float x1 = step(left,uv.x);
float x2 = step(right, 1-uv.x);
float y1 = step(top, 1-uv.y);
float y2 = step(bottom,uv.y);
float value = x1 * x2 * y1 * y2;
return value;
}
float box2(float2 uv, float left, float right, float top, float bottom)
{
//左下边界
float2 bl = step(float2(left,bottom),uv);
float value = bl.x * bl.y;
//右上边界
float2 tr = step(float2(right,top),1-uv);
value *= tr.x * tr.y;
return value;
}
//绝对值对称法
float box3(float2 uv, float left, float top)
{
float2 b1 = 1 - step(float2(left,top),abs(uv));
float value = b1.x * b1.y;
return value;
}
//矩形框
float box4(float2 uv,float left,float top, float width)
{
float value = box3(uv,left,top);
value -= box3(uv,left-width,top-width);
return value;
}
//圆形
float circle(float2 uv,float radius)
{
float blur = 0.002;
float pct = sqrt(uv.x*uv.x+uv.y*uv.y);
return 1 - smoothstep(radius,radius+blur,pct);
}
float circle2(float2 uv,float radius,float width)
{
float value = circle(uv,radius);
value -= circle(uv,radius-width);
return value;
}
fixed4 frag(v2f i) : SV_Target
{
sample the texture
//fixed4 col = tex2D(_MainTex, i.uv);
apply fog
//UNITY_APPLY_FOG(i.fogCoord, col);
i.uv -= float2(0.5,0.5);//移动中心点
float4 col = float4(1,1,0,1);
float4 box_color = float4(1,0,0,1);
float value = circle2(i.uv,0.2,0.01);//box4(i.uv,0.3,0.3,0.02);//box2(i.uv,0.2,0.2,0.2,0.2);
col = lerp(col,box_color, value);
return col;
}
ENDCG
}
}
}
shader学习记录——矩形圆形
于 2022-12-07 11:10:26 首次发布