照例先放效果图
Shader代码:
Shader "Hidden/RectangluarCoordinateSystemGrid"
{
Properties
{
_XAxisColor("XAxisColor",Color) = (1,1,1,1)
_YAxisColor("YAxisColor",Color) = (1,1,1,1)
_GridColor("GridColor",Color) = (1,1,1,1)
_AxisLineWidth("AxisLineWidth",float) = 10
_GirdlLineWidth("GirdlLineWidth",float) = 10
_GridCount("GridCount",float) = 10
}
SubShader
{
// No culling or depth
Cull Back
Blend SrcAlpha OneMinusSrcAlpha
Tags{"Queue" ="Transparent"}
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 = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
sampler2D _MainTex;
fixed4 _XAxisColor;
fixed4 _YAxisColor;
fixed4 _GridColor;
fixed _AxisLineWidth;
fixed _GirdlLineWidth;
fixed _GridCount;
fixed4 frag(v2f i) : SV_Target
{
//坐标调整到(-1,1)
fixed2 uv = i.uv * 2 - 1;
//计算网格线
fixed x1 = 1 - smoothstep(0,_GirdlLineWidth / _ScreenParams.x * 0.5, frac(uv.x * _GridCount));
fixed x2 = smoothstep(1 - _GirdlLineWidth / _ScreenParams.x * 0.5, 1,frac(uv.x * _GridCount));
fixed y1 = 1 - smoothstep(0, _GirdlLineWidth / _ScreenParams.x * 0.5, frac(uv.y * _GridCount));
fixed y2 = smoothstep(1 - _GirdlLineWidth / _ScreenParams.x * 0.5, 1, frac(uv.y * _GridCount));
fixed grid = x1 + x2 + y1 + y2;
grid = clamp(0, 1, grid);
//计算XY坐标线
fixed XAxis1 = 1 - smoothstep(0.5, _AxisLineWidth / _ScreenParams.x * 0.5 + 0.5, i.uv.x);
fixed XAxis2 = smoothstep(0.5 - _AxisLineWidth / _ScreenParams.x * 0.5, 0.5, i.uv.x);
fixed YAxis1 = 1 - smoothstep(0.5, _AxisLineWidth / _ScreenParams.x * 0.5 + 0.5, i.uv.y);
fixed YAxis2 = smoothstep(0.5 - _AxisLineWidth / _ScreenParams.x * 0.5, 0.5, i.uv.y);
fixed xAxis = XAxis1 * XAxis2;
fixed YAxis = YAxis1 * YAxis2;
//颜色计算
fixed4 col = (XAxis1 * XAxis2) * _XAxisColor +(YAxis1 * YAxis2) * _YAxisColor;
col += grid * _GridColor;
return col;
}
ENDCG
}
}
}