优化回声描边逻辑
This commit is contained in:
@@ -69,6 +69,24 @@ namespace IndianOceanAssets.Engine2_5D
|
||||
{
|
||||
_mainCamera = Camera.main;
|
||||
UpdateMaterialProperties();
|
||||
|
||||
// 确保遮罩平面在所有Sprite之后渲染
|
||||
if (maskPlane != null)
|
||||
{
|
||||
Renderer renderer = maskPlane.GetComponent<Renderer>();
|
||||
if (renderer != null)
|
||||
{
|
||||
// 设置渲染队列为最后(Overlay之后)
|
||||
renderer.sortingOrder = 9999;
|
||||
|
||||
// 如果使用MeshRenderer,设置material的渲染队列
|
||||
if (renderer is MeshRenderer && darknessMaterial != null)
|
||||
{
|
||||
// 保持在Transparent之后
|
||||
darknessMaterial.renderQueue = 3100; // Transparent+100
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Update()
|
||||
|
||||
@@ -97,6 +97,14 @@ Shader "IndianOcean/DarknessOverlay"
|
||||
|
||||
// 从黑暗底色插值到白色
|
||||
float3 maskColor = lerp(_DarknessColor.rgb, float3(1.0, 1.0, 1.0), lightAmount);
|
||||
|
||||
// 编辑器开关开启时(MinBrightness >= 0.99),输出纯白色
|
||||
// 这样乘法混合不会改变原场景颜色
|
||||
if (_MinBrightness >= 0.99)
|
||||
{
|
||||
return half4(1.0, 1.0, 1.0, 1.0);
|
||||
}
|
||||
|
||||
return half4(maskColor, 1.0);
|
||||
}
|
||||
ENDHLSL
|
||||
|
||||
@@ -16,7 +16,7 @@ Material:
|
||||
m_LightmapFlags: 4
|
||||
m_EnableInstancingVariants: 0
|
||||
m_DoubleSidedGI: 0
|
||||
m_CustomRenderQueue: -1
|
||||
m_CustomRenderQueue: 3100
|
||||
stringTagMap: {}
|
||||
disabledShaderPasses: []
|
||||
m_LockedProperties:
|
||||
@@ -31,7 +31,7 @@ Material:
|
||||
- _EchoRadius: 39.41617
|
||||
- _EchoWidth: 2.5
|
||||
- _LightSoftness: 0.5
|
||||
- _MinBrightness: 1
|
||||
- _MinBrightness: 0
|
||||
m_Colors:
|
||||
- _DarknessColor: {r: 0.01, g: 0.01, b: 0.02, a: 1}
|
||||
- _EchoCenter: {r: -2.4399998, g: -1.5000057, b: 0, a: 0}
|
||||
|
||||
@@ -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