Shader "Unlit/NewUnlitShader" { Properties { _MainTex ("Texture", 2DArray) = "white" {} _TexNo ("TexNo", 2D) = "white" {} _MaskNo ("MaskNo", 2D) = "black" {} _MaskTex ("Mask", 2DArray) = "black" {} _BorderColor ("BorderColor", Color) = (1,1,1,1) _BorderWidth ("BorderWidth", Range(0,0.1)) = 0.05 } 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; float2 uv2: TEXCOORD2; }; struct v2f { float2 uv : TEXCOORD0; float2 uv2 : TEXCOORD2; // UNITY_FOG_COORDS(1) float4 vertex : SV_POSITION; }; UNITY_DECLARE_TEX2DARRAY(_MainTex); sampler2D _TexNo; sampler2D _MaskNo; UNITY_DECLARE_TEX2DARRAY(_MaskTex); float4 _MainTex_ST; float4 _TexNo_TexelSize; float4 _BorderColor; float _BorderWidth; v2f vert(appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); // o.uv = TRANSFORM_TEX(v.uv, _MainTex); // o.uv2 = TRANSFORM_TEX(v.uv2, _MainTex); o.uv = v.uv; o.uv2 = v.uv2; return o; } float4 Blend(float4 color, float2 uv, float mask, int layer) { float4 col2 = UNITY_SAMPLE_TEX2DARRAY(_MainTex, float3(uv , layer)); int idx = round(mask * 16); float4 maskTexCol = UNITY_SAMPLE_TEX2DARRAY(_MaskTex, float3(uv , idx)); return lerp(color, col2, maskTexCol); } int GetTexNo(float f) { float idx = (f * 64); return round(idx); } fixed4 showGridLine(fixed4 col, float2 uv, float2 uv1) { if (uv.x < _BorderWidth || uv.y < _BorderWidth || uv.x > 1 - _BorderWidth || uv.y > 1 - _BorderWidth) { col = _BorderColor; } return col; } fixed4 frag(v2f i) : SV_Target { // 整个大uv往下偏移 float2 uv = i.uv + float2(0, _TexNo_TexelSize.y); // 贴图编号 float4 texNo = tex2D(_TexNo, uv); int layer1 = GetTexNo(texNo.r); int layer2 = GetTexNo(texNo.g); int layer3 = GetTexNo(texNo.b); int layer4 = GetTexNo(texNo.a); // 取出遮罩值 float4 maskNo = tex2D(_MaskNo, uv); float4 col = UNITY_SAMPLE_TEX2DARRAY(_MainTex, float3(i.uv2 , layer1)); if (layer2 > 0) { col = Blend(col, i.uv2, maskNo.g, layer2); } if (layer3 > 0) { col = Blend(col, i.uv2, maskNo.b, layer3); } if (layer4 > 0) { col = Blend(col, i.uv2, maskNo.a, layer4); } // col = showGridLine(col, i.uv2, i.uv); return col; } ENDCG } } }