DTM Trail
This commit is contained in:
113
Assets/Shaders/Grid.shader
Normal file
113
Assets/Shaders/Grid.shader
Normal file
@@ -0,0 +1,113 @@
|
||||
Shader "Custom/Grid"
|
||||
{
|
||||
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
|
||||
}
|
||||
SubShader
|
||||
{
|
||||
Tags { "RenderType"="Transparent" "Queue"="Transparent" }
|
||||
LOD 100
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
CBUFFER_START(UnityPerMaterial)
|
||||
float4 _LineColor;
|
||||
float4 _BackgroundColor;
|
||||
float _GridScale;
|
||||
float _LineWidth;
|
||||
float _Fade;
|
||||
float _Plane;
|
||||
float _DisappearStartDistance;
|
||||
float _DisappearEndDistance;
|
||||
CBUFFER_END
|
||||
|
||||
Varyings vert(Attributes IN)
|
||||
{
|
||||
Varyings OUT;
|
||||
OUT.position = TransformObjectToHClip(IN.vertex);
|
||||
OUT.worldPos = TransformObjectToWorld(IN.vertex).xyz;
|
||||
return OUT;
|
||||
}
|
||||
|
||||
// 根据 _Plane 选择二维坐标
|
||||
float2 GetGridCoordinates(float3 pos)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
// 计算摄像机在平面上的位置与当前片元的二维距离
|
||||
float2 camPos2D = GetCameraPlanePosition();
|
||||
float2 fragPos2D = GetGridCoordinates(IN.worldPos);
|
||||
float dist = distance(fragPos2D, camPos2D);
|
||||
|
||||
// 超过 100 米开始淡出,到 200 米完全透明
|
||||
float distanceFade = saturate((_DisappearEndDistance - dist) / _DisappearStartDistance);
|
||||
|
||||
// 最终 alpha 是网格线强度乘以距离淡出因子
|
||||
float finalAlpha = lineIntensity * distanceFade;
|
||||
|
||||
// 背景颜色始终透明,所以直接输出网格线颜色与 alpha
|
||||
return float4(_LineColor.rgb, finalAlpha);
|
||||
}
|
||||
ENDHLSL
|
||||
}
|
||||
}
|
||||
FallBack "Diffuse"
|
||||
}
|
||||
Reference in New Issue
Block a user