Shader "URP/GridShader" { Properties { _Size("Size",vector) = (1,1,0,0) _Alpha("Alpha",Range(0, 1)) = 1 _UserInput("UserInput",Range(0, 1)) = 0 } SubShader { Tags { "RenderType"="Transparent" "Queue" = "Transparent"} blend srcalpha oneminussrcalpha zwrite off ztest off 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; float3 worldPos : TEXCOORD1; }; struct RenderData { int barrier; float isSelected; float r; float g; float b; }; StructuredBuffer _InputData; uniform float4 _Size; uniform float _Alpha; uniform float _UserInput; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.worldPos = mul(unity_ObjectToWorld, v.vertex); o.uv = v.uv; return o; } #define width _Size.x #define height _Size.y fixed4 frag (v2f i) : SV_Target { //细的分割线 float x0 = i.uv.x * width; float y0 = i.uv.y * height; fixed xLine0 = abs((x0 - floor(x0)) - 0.5) * 2.0; fixed yLine0 = abs((y0 - floor(y0)) - 0.5) * 2.0; fixed edgeLine0 = max(xLine0, yLine0); float w0 = fwidth(edgeLine0 * 0.5) * 2.0; edgeLine0 = smoothstep(-w0, w0, edgeLine0 - 0.925); //细的分割线结束 //粗的分割线 float x1 = i.uv.x * width * 0.05; float y1 = i.uv.y * height * 0.05; fixed xLine1 = abs((x1 - floor(x1)) - 0.5) * 2.0; fixed yLine1 = abs((y1 - floor(y1)) - 0.5) * 2.0; fixed edgeLine1 = max(xLine1, yLine1); float w1 = fwidth(edgeLine1 * 0.5) * 2.0; edgeLine1 = smoothstep(-w1, w1, edgeLine1 - 0.925); //粗的分割线结束 float cameraDis = distance(_WorldSpaceCameraPos.xyz, i.worldPos); float cameraFade = smoothstep(70, 100, cameraDis); fixed3 col = fixed3(0, 0, 0); if(_UserInput < 0.5) { col = fixed3(1,1,1); cameraFade = 1; //只显示粗线 float lineAlpha = max(edgeLine0 * (1 - cameraFade), edgeLine1 * cameraFade); return fixed4(col, _Alpha * lineAlpha * 0.5); } int index = floor(x0) + floor(y0) * width; col = fixed3(_InputData[index].r, _InputData[index].g, _InputData[index].b); col = lerp(col, fixed3(1, 1, 1), edgeLine0 * (1 - cameraFade)); col = lerp(col, fixed3(0, 0, 0), edgeLine1 * cameraFade); col = lerp(col, fixed3(1, 1, 0), _InputData[index].isSelected); return fixed4(col, _Alpha); } ENDCG } } }