1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
| Shader "Custom/URP/SkillRangeIndicator" { Properties { _MainColor ("Fill Color", Color) = (0.2, 0.8, 1.0, 0.3) _EdgeColor ("Edge Glow Color", Color) = (0.4, 1.0, 1.0, 1.0) _SectorColor ("Sector Color", Color) = (1.0, 0.5, 0.1, 0.8) _InnerRadius ("Inner Radius", Range(0, 0.5)) = 0.1 _OuterRadius ("Outer Radius", Range(0, 0.5)) = 0.45 _EdgeWidth ("Edge Glow Width", Range(0.001, 0.1)) = 0.02 _SectorAngle ("Sector Angle (deg)", Range(0, 360)) = 60 _FillPercent ("Fill Percent", Range(0, 1)) = 1.0 _RotateSpeed ("Rotation Speed", Float) = 0.5 _GridTex ("Grid/Pattern Tex", 2D) = "white" {} _GridScrollSpeed("Grid Scroll Speed", Float) = 0.1 } SubShader { Tags { "RenderType" = "Transparent" "RenderPipeline" = "UniversalPipeline" "Queue" = "Transparent" } Blend SrcAlpha OneMinusSrcAlpha ZWrite Off Cull Off // 双面渲染(地面贴片两面都可见)
Pass { Tags { "LightMode" = "UniversalForward" } HLSLPROGRAM #pragma vertex vert #pragma fragment frag #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct Attributes { float4 positionOS : POSITION; float2 uv : TEXCOORD0; };
struct Varyings { float4 positionHCS : SV_POSITION; float2 uv : TEXCOORD0; };
TEXTURE2D(_GridTex); SAMPLER(sampler_GridTex);
CBUFFER_START(UnityPerMaterial) float4 _MainColor; float4 _EdgeColor; float4 _SectorColor; float _InnerRadius; float _OuterRadius; float _EdgeWidth; float _SectorAngle; float _FillPercent; float _RotateSpeed; float _GridScrollSpeed; CBUFFER_END
static const float PI = 3.14159265; static const float TAU = 6.28318530;
// UV 旋转辅助 float2 RotateUV(float2 uv, float2 pivot, float angle) { float s = sin(angle), c = cos(angle); uv -= pivot; uv = float2(uv.x * c - uv.y * s, uv.x * s + uv.y * c); return uv + pivot; }
Varyings vert(Attributes IN) { Varyings OUT; OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz); OUT.uv = IN.uv; return OUT; }
half4 frag(Varyings IN) : SV_Target { float2 uv = IN.uv; float2 p = uv - 0.5; // 以中心为原点 float r = length(p); float t = _Time.y;
// ===== 圆环基础形状 ===== float inCircle = step(_InnerRadius, r) * step(r, _OuterRadius); // 边缘发光(外圈和内圈各一条光边) float outerEdge = 1.0 - smoothstep(_OuterRadius - _EdgeWidth, _OuterRadius, r); float innerEdge = smoothstep(_InnerRadius, _InnerRadius + _EdgeWidth, r); float edgeMask = (1.0 - innerEdge) + (1.0 - outerEdge * step(r, _OuterRadius)); edgeMask = saturate(edgeMask * inCircle);
// ===== 扇形遮罩 ===== float sectorAngleRad = _SectorAngle * PI / 180.0; float angle = atan2(p.x, p.y); // Y 轴向上为 0 度 angle = frac(angle / TAU + 0.5); // 归一化 [0,1] float sectorNorm = sectorAngleRad / TAU; float inSector = step(angle, _FillPercent * sectorNorm) * inCircle;
// ===== 网格纹理(旋转滚动)===== float2 gridUV = RotateUV(uv, float2(0.5, 0.5), t * _RotateSpeed); // 向外滚动效果 float2 polarUV = float2(atan2(p.y, p.x) / TAU + 0.5, r); polarUV.y += t * _GridScrollSpeed; // 极坐标中的径向滚动 half4 grid = SAMPLE_TEXTURE2D(_GridTex, sampler_GridTex, polarUV);
// ===== 合成输出 ===== // 底层:半透明圆环填充 half4 col = _MainColor; col.rgb += grid.rgb * 0.2; // 叠加网格纹理增加细节
// 扇形指向区域:使用扇形颜色 col = lerp(col, _SectorColor, inSector * (1.0 - edgeMask));
// 发光边缘叠加 col.rgb = lerp(col.rgb, _EdgeColor.rgb, edgeMask); col.a = saturate(col.a + edgeMask * _EdgeColor.a);
// 遮罩:只在圆环范围内显示 col.a *= inCircle;
// 呼吸动画:边缘亮度随时间脉动 float breathe = 0.8 + 0.2 * sin(t * 3.0); col.rgb *= breathe;
return col; } ENDHLSL } } }
|