优化回声描边逻辑
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
Shader "Custom/SpriteWithGroundClip"
|
||||
{
|
||||
// 带地面裁剪的Sprite Shader
|
||||
// 带地面裁剪和阴影的Sprite Shader
|
||||
// 当Sprite的世界Y坐标低于地面高度时,会被裁剪掉
|
||||
// 支持投射阴影(可选)
|
||||
Properties
|
||||
{
|
||||
_MainTex ("Texture", 2D) = "white" {}
|
||||
@@ -9,6 +10,12 @@ Shader "Custom/SpriteWithGroundClip"
|
||||
[MaterialToggle] _EnableClip ("启用裁剪", Float) = 1.0
|
||||
_ClipSoftness ("裁剪边缘柔和度", Range(0, 0.5)) = 0.05
|
||||
|
||||
// 阴影设置
|
||||
[MaterialToggle] _CastShadow ("投射阴影", Float) = 1.0
|
||||
_ShadowOpacity ("阴影透明度", Range(0, 1)) = 0.5
|
||||
_ShadowOffset ("阴影偏移 (Y方向)", Float) = -0.1
|
||||
_ShadowColor ("阴影颜色", Color) = (0, 0, 0, 0.5)
|
||||
|
||||
// 颜色
|
||||
_Color ("颜色", Color) = (1, 1, 1, 1)
|
||||
}
|
||||
@@ -110,6 +117,122 @@ Shader "Custom/SpriteWithGroundClip"
|
||||
}
|
||||
ENDHLSL
|
||||
}
|
||||
|
||||
// 阴影Pass
|
||||
Pass
|
||||
{
|
||||
Name "ShadowCaster"
|
||||
Tags { "LightMode" = "ShadowCaster" }
|
||||
|
||||
ZWrite On
|
||||
ZTest LEqual
|
||||
Cull Off
|
||||
|
||||
HLSLPROGRAM
|
||||
#pragma vertex ShadowPassVertex
|
||||
#pragma fragment ShadowPassFragment
|
||||
#pragma multi_compile_instancing
|
||||
|
||||
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
|
||||
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
|
||||
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
|
||||
struct Attributes
|
||||
{
|
||||
float4 positionOS : POSITION;
|
||||
float3 normalOS : NORMAL;
|
||||
float2 uv : TEXCOORD0;
|
||||
UNITY_VERTEX_INPUT_INSTANCE_ID
|
||||
};
|
||||
|
||||
struct Varyings
|
||||
{
|
||||
float4 positionCS : SV_POSITION;
|
||||
float2 uv : TEXCOORD0;
|
||||
float3 worldPos : TEXCOORD1;
|
||||
UNITY_VERTEX_INPUT_INSTANCE_ID
|
||||
};
|
||||
|
||||
CBUFFER_START(UnityPerMaterial)
|
||||
float4 _MainTex_ST;
|
||||
float4 _Color;
|
||||
float _GroundHeight;
|
||||
float _EnableClip;
|
||||
float _ClipSoftness;
|
||||
float _CastShadow;
|
||||
float _ShadowOpacity;
|
||||
float _ShadowOffset;
|
||||
float4 _ShadowColor;
|
||||
CBUFFER_END
|
||||
|
||||
TEXTURE2D(_MainTex);
|
||||
SAMPLER(sampler_MainTex);
|
||||
|
||||
Varyings ShadowPassVertex(Attributes input)
|
||||
{
|
||||
Varyings output;
|
||||
UNITY_SETUP_INSTANCE_ID(input);
|
||||
UNITY_TRANSFER_INSTANCE_ID(input, output);
|
||||
|
||||
// URP阴影Pass需要使用特殊的矩阵变换
|
||||
float3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
|
||||
float4 positionCS = TransformWorldToHClip(positionWS);
|
||||
|
||||
// 应用阴影偏移(向光源方向偏移)
|
||||
#if UNITY_REVERSED_Z
|
||||
positionCS.z -= unity_LightData.z;
|
||||
#else
|
||||
positionCS.z += unity_LightData.z;
|
||||
#endif
|
||||
|
||||
output.positionCS = positionCS;
|
||||
output.uv = TRANSFORM_TEX(input.uv, _MainTex);
|
||||
output.worldPos = positionWS;
|
||||
return output;
|
||||
}
|
||||
|
||||
half4 ShadowPassFragment(Varyings input) : SV_Target
|
||||
{
|
||||
UNITY_SETUP_INSTANCE_ID(input);
|
||||
|
||||
// 如果不投射阴影,直接丢弃
|
||||
if (_CastShadow < 0.5)
|
||||
{
|
||||
discard;
|
||||
}
|
||||
|
||||
// 采样纹理获取Alpha
|
||||
half4 texColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, input.uv);
|
||||
half alpha = texColor.a * _Color.a;
|
||||
|
||||
// 地面裁剪逻辑(与主Pass相同)
|
||||
if (_EnableClip > 0.5)
|
||||
{
|
||||
float worldY = input.worldPos.y;
|
||||
float distanceToGround = worldY - _GroundHeight;
|
||||
|
||||
if (distanceToGround < 0)
|
||||
{
|
||||
float clipAlpha = smoothstep(0, _ClipSoftness, distanceToGround);
|
||||
alpha *= clipAlpha;
|
||||
|
||||
if (alpha < 0.01)
|
||||
{
|
||||
discard;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果Alpha太低,不投射阴影
|
||||
if (alpha < 0.1)
|
||||
{
|
||||
discard;
|
||||
}
|
||||
|
||||
// 输出阴影(URP阴影格式)
|
||||
return half4(0, 0, 0, alpha * _ShadowOpacity);
|
||||
}
|
||||
ENDHLSL
|
||||
}
|
||||
}
|
||||
FallBack "Sprites/Default"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user