111111111111

Signed-off-by: TRAfoer <lhf190@outlook.com>
This commit is contained in:
2026-01-18 13:11:38 +08:00
parent a31269c632
commit de4e399d78
76 changed files with 3199823 additions and 10619 deletions

View File

@@ -1,57 +1,36 @@
Shader "Custom/Grid"
Shader "Custom/SimpleGrid"
{
Properties
{
_LineColor("Line Color", Color) = (1,1,1,1)
_BackgroundColor("Background Color", Color) = (0,0,0,0)
_GridScale("Grid Scale", Float) = 1
_LineWidth("Line Width", Float) = 0.05
_Fade("Fade", Float) = 0.1
_Plane("Plane (0: XZ, 1: XY, 2: YZ)", Float) = 0
_DisappearStartDistance("Disappear Start Distance", Float) = 50
_DisappearEndDistance("Disappear End Distance", Float) = 100
_MainColor ("Sub Grid Color", Color) = (0.5, 0.5, 0.5, 0.2)
_AxisColor ("Major Grid Color", Color) = (0.8, 0.8, 0.8, 0.5)
_GridSpacing ("Grid Spacing", Float) = 1.0
_LineWidth ("Line Width", Range(0.001, 0.1)) = 0.02
_FadeDist ("Fade Distance", Float) = 100.0
}
SubShader
{
Tags { "RenderType"="Transparent" "Queue"="Transparent" }
LOD 100
Tags { "RenderType"="Transparent" "Queue"="Transparent-1" } // -1 保证在普通物体后面
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
Cull Off
Pass
{
Name "GridPass"
Cull Off
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
// 内置变量:摄像机在世界空间中的位置
//float3 _WorldSpaceCameraPos;
struct Attributes
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct Varyings
{
float4 position : SV_POSITION;
float3 worldPos : TEXCOORD0;
};
struct Attributes { float4 vertex : POSITION; };
struct Varyings { float4 position : SV_POSITION; float3 worldPos : TEXCOORD0; };
CBUFFER_START(UnityPerMaterial)
float4 _LineColor;
float4 _BackgroundColor;
float _GridScale;
float _LineWidth;
float _Fade;
float _Plane;
float _DisappearStartDistance;
float _DisappearEndDistance;
float4 _MainColor;
float4 _AxisColor;
float _GridSpacing;
float _LineWidth;
float _FadeDist;
CBUFFER_END
Varyings vert(Attributes IN)
@@ -62,52 +41,50 @@ Shader "Custom/Grid"
return OUT;
}
// 根据 _Plane 选择二维坐标
float2 GetGridCoordinates(float3 pos)
// 优化的网格计算
float gridAlpha(float2 worldPos, float spacing, float width)
{
if (_Plane < 0.5) // XZ 平面
return pos.xz;
else if (_Plane < 1.5) // XY 平面
return pos.xy;
else // YZ 平面
return pos.yz;
}
// 根据 _Plane 获取摄像机在网格平面上的位置
float2 GetCameraPlanePosition()
{
if (_Plane < 0.5) // XZ 平面:摄像机投影到 XZ
return _WorldSpaceCameraPos.xz;
else if (_Plane < 1.5) // XY 平面:摄像机投影到 XY
return _WorldSpaceCameraPos.xy;
else // YZ 平面:摄像机投影到 YZ
return _WorldSpaceCameraPos.yz;
float2 coord = worldPos / spacing;
// fwidth 用于保持线条在屏幕上的像素宽度一致
float2 grid = abs(frac(coord - 0.5) - 0.5) / fwidth(coord);
float lineVal = min(grid.x, grid.y);
// 1.0 - smoothstep 提供抗锯齿
return 1.0 - smoothstep(width, width + 1.0, lineVal);
}
half4 frag(Varyings IN) : SV_Target
{
// 计算二维网格坐标并乘以 _GridScale
float2 gridCoord = GetGridCoordinates(IN.worldPos) * _GridScale;
// 用 frac 与 fwidth 实现平滑网格线(抗锯齿)
float2 grid = abs(frac(gridCoord - 0.5) - 0.5) / fwidth(gridCoord);
float lineIntensity = 1.0 - smoothstep(0.0, _LineWidth, min(grid.x, grid.y));
// 距离裁剪:太远直接 discard节省后续计算
float dist = distance(IN.worldPos, _WorldSpaceCameraPos);
if (dist > _FadeDist) discard;
// 计算摄像机在平面上的位置与当前片元的二维距离
float2 camPos2D = GetCameraPlanePosition();
float2 fragPos2D = GetGridCoordinates(IN.worldPos);
float dist = distance(fragPos2D, camPos2D);
float alphaFade = 1.0 - (dist / _FadeDist);
// 二次衰减让边缘更柔和
alphaFade *= alphaFade;
float2 pos = IN.worldPos.xz;
// 计算两层网格
float baseGrid = gridAlpha(pos, _GridSpacing, _LineWidth);
float largeGrid = gridAlpha(pos, _GridSpacing * 10.0, _LineWidth); // 10倍格
// 混合
float4 finalColor = _MainColor;
finalColor.a *= baseGrid;
// 叠加高亮网格
// 使用 max 避免 alpha 混合错误
finalColor = lerp(finalColor, _AxisColor, largeGrid);
finalColor.a = max(finalColor.a, _AxisColor.a * largeGrid);
// 超过 100 米开始淡出,到 200 米完全透明
float distanceFade = saturate((_DisappearEndDistance - dist) / _DisappearStartDistance);
finalColor.a *= alphaFade;
// 最终 alpha 是网格线强度乘以距离淡出因子
float finalAlpha = lineIntensity * distanceFade;
// 背景颜色始终透明,所以直接输出网格线颜色与 alpha
return float4(_LineColor.rgb, finalAlpha);
// 极低 Alpha 剔除
if (finalColor.a < 0.01) discard;
return finalColor;
}
ENDHLSL
}
}
FallBack "Diffuse"
}
}