优化回声描边逻辑

This commit is contained in:
2026-06-28 21:44:33 +08:00
parent 02ac67d338
commit 5c7ffac1bd
12 changed files with 1257 additions and 1328 deletions

View File

@@ -48,7 +48,9 @@ namespace IndianOceanAssets.Engine2_5D
public void Slash() public void Slash()
{ {
// Play particle and sound effects // Play particle and sound effects
if (swordSlashParticle != null)
swordSlashParticle.Play(); swordSlashParticle.Play();
if (swordSlashSFX != null)
swordSlashSFX.Play(); swordSlashSFX.Play();
// Detect and damage enemies within the slash radius // Detect and damage enemies within the slash radius
@@ -57,7 +59,11 @@ namespace IndianOceanAssets.Engine2_5D
{ {
foreach (var E in enemies) foreach (var E in enemies)
{ {
E.GetComponent<HealthSystem>().Damage(damageAmount); HealthSystem health = E.GetComponent<HealthSystem>();
if (health != null)
{
health.Damage(damageAmount);
}
} }
} }
@@ -67,7 +73,11 @@ namespace IndianOceanAssets.Engine2_5D
{ {
foreach (var P in plantation) foreach (var P in plantation)
{ {
P.GetComponent<Plantation>().Cut(); Plantation plant = P.GetComponent<Plantation>();
if (plant != null)
{
plant.Cut();
}
} }
} }
} }

View File

@@ -0,0 +1,93 @@
using UnityEngine;
using UnityEditor;
using IndianOceanAssets.Engine2_5D;
/// <summary>
/// 回声描边诊断工具
/// </summary>
public class EchoDebugger : Editor
{
[MenuItem("Tools/Echo/诊断回声描边状态")]
public static void DebugEchoOutline()
{
// 查找EchoOutlineManager
EchoOutlineManager manager = Object.FindObjectOfType<EchoOutlineManager>();
if (manager == null)
{
EditorUtility.DisplayDialog("错误", "场景中没有找到EchoOutlineManager组件", "确定");
return;
}
// 通过反射获取私有字段
var parentRenderersField = typeof(EchoOutlineManager).GetField("_parentRenderers",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var outlineRenderersField = typeof(EchoOutlineManager).GetField("_outlineRenderers",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var renderersEnabledField = typeof(EchoOutlineManager).GetField("_renderersEnabled",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var parentRenderers = parentRenderersField.GetValue(manager) as System.Collections.Generic.List<SpriteRenderer>;
var outlineRenderers = outlineRenderersField.GetValue(manager) as System.Collections.Generic.List<SpriteRenderer>;
bool renderersEnabled = (bool)renderersEnabledField.GetValue(manager);
string info = $"=== 回声描边诊断 ===\n\n";
info += $"EchoOutlineManager: {manager.name}\n";
info += $"描边材质: {(manager != null ? "" : "")}\n\n";
info += $"父物体数量: {parentRenderers?.Count ?? 0}\n";
info += $"描边子物体数量: {outlineRenderers?.Count ?? 0}\n";
info += $"描边渲染器启用: {renderersEnabled}\n\n";
if (outlineRenderers != null && outlineRenderers.Count > 0)
{
info += $"=== 前5个描边子物体 ===\n\n";
int showCount = Mathf.Min(5, outlineRenderers.Count);
for (int i = 0; i < showCount; i++)
{
var cr = outlineRenderers[i];
var pr = parentRenderers[i];
if (cr == null || pr == null) continue;
info += $"父物体: {pr.name}\n";
info += $" 描边子物体: {cr.name}\n";
info += $" 启用状态: {cr.enabled}\n";
info += $" SortingOrder: {cr.sortingOrder}\n";
info += $" Material: {cr.sharedMaterial?.shader?.name ?? "null"}\n\n";
}
}
else
{
info += "⚠️ 警告: 没有找到描边子物体!\n";
info += "可能原因:\n";
info += "1. EchoOutlineManager.Start()没有执行\n";
info += "2. 场景中没有符合条件的SpriteRenderer\n";
info += "3. outlineMaterial未设置\n";
}
// 检查全局Shader参数
float intensity = Shader.GetGlobalFloat(Shader.PropertyToID("_EchoOutlineIntensity"));
info += $"\n=== 全局Shader参数 ===\n\n";
info += $"_EchoOutlineIntensity: {intensity}\n";
Debug.Log(info);
EditorUtility.DisplayDialog("诊断完成", $"诊断信息已输出到控制台\n\n请查看Unity Console窗口", "确定");
}
[MenuItem("Tools/Echo/手动刷新描边")]
public static void RefreshEchoOutlines()
{
EchoOutlineManager manager = Object.FindObjectOfType<EchoOutlineManager>();
if (manager == null)
{
EditorUtility.DisplayDialog("错误", "场景中没有找到EchoOutlineManager组件", "确定");
return;
}
manager.RefreshList();
EditorUtility.DisplayDialog("完成", "已刷新描边子物体", "确定");
Debug.Log("✓ 已刷新描边子物体");
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: WS5KsnisVC3o2wbtYBTyjcYIBZ6E/8syyO6OKbTpS0KGZp1sJJIPONc=
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,83 @@
using UnityEngine;
using UnityEditor;
using IndianOceanAssets.Engine2_5D;
/// <summary>
/// 遮罩平面调整工具
/// </summary>
public class MaskPlaneAdjuster : Editor
{
[MenuItem("Tools/LightMask/调整遮罩平面设置")]
public static void AdjustMaskPlane()
{
// 查找场景中的LightMaskSystem
LightMaskSystem lms = Object.FindObjectOfType<LightMaskSystem>();
if (lms == null)
{
EditorUtility.DisplayDialog("错误", "场景中没有找到LightMaskSystem组件", "确定");
return;
}
// 通过反射获取私有字段
var maskPlaneField = typeof(LightMaskSystem).GetField("maskPlane",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var maskOffsetField = typeof(LightMaskSystem).GetField("maskOffset",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
var planeSizeField = typeof(LightMaskSystem).GetField("planeSize",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
Transform maskPlane = (Transform)maskPlaneField.GetValue(lms);
Vector3 maskOffset = (Vector3)maskOffsetField.GetValue(lms);
float planeSize = (float)planeSizeField.GetValue(lms);
string info = $"当前遮罩平面设置:\n\n";
info += $"遮罩平面物体: {(maskPlane != null ? maskPlane.name : "")}\n";
info += $"遮罩偏移: {maskOffset}\n";
info += $"平面大小: {planeSize}\n\n";
info += "建议设置:\n";
info += "1. 遮罩平面Y坐标应该 >= 10在所有Sprite之上\n";
info += "2. 平面大小应该 >= 100覆盖整个可视区域\n";
info += "3. 遮罩平面的SortingOrder应该 = 9999最大";
EditorUtility.DisplayDialog("遮罩平面信息", info, "确定");
Debug.Log(info);
}
[MenuItem("Tools/LightMask/设置遮罩平面SortingOrder")]
public static void SetMaskPlaneSortingOrder()
{
LightMaskSystem lms = Object.FindObjectOfType<LightMaskSystem>();
if (lms == null)
{
EditorUtility.DisplayDialog("错误", "场景中没有找到LightMaskSystem组件", "确定");
return;
}
var maskPlaneField = typeof(LightMaskSystem).GetField("maskPlane",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
Transform maskPlane = (Transform)maskPlaneField.GetValue(lms);
if (maskPlane == null)
{
EditorUtility.DisplayDialog("错误", "LightMaskSystem中没有设置遮罩平面", "确定");
return;
}
// 设置SortingOrder
SpriteRenderer sr = maskPlane.GetComponent<SpriteRenderer>();
if (sr == null)
{
EditorUtility.DisplayDialog("错误", $"遮罩平面 {maskPlane.name} 没有SpriteRenderer组件", "确定");
return;
}
sr.sortingOrder = 9999;
EditorUtility.SetDirty(maskPlane.gameObject);
EditorUtility.DisplayDialog("完成", $"已设置 {maskPlane.name} 的SortingOrder为9999", "确定");
Debug.Log($"✓ 已设置 {maskPlane.name} 的SortingOrder为9999");
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: Wn8X4yqrW3xTulVGfMlI6RQgwjQkOIFlM4QT5XpGWHNvdp0589QGBHg=
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -69,6 +69,24 @@ namespace IndianOceanAssets.Engine2_5D
{ {
_mainCamera = Camera.main; _mainCamera = Camera.main;
UpdateMaterialProperties(); 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() private void Update()

View File

@@ -97,6 +97,14 @@ Shader "IndianOcean/DarknessOverlay"
// 从黑暗底色插值到白色 // 从黑暗底色插值到白色
float3 maskColor = lerp(_DarknessColor.rgb, float3(1.0, 1.0, 1.0), lightAmount); 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); return half4(maskColor, 1.0);
} }
ENDHLSL ENDHLSL

View File

@@ -16,7 +16,7 @@ Material:
m_LightmapFlags: 4 m_LightmapFlags: 4
m_EnableInstancingVariants: 0 m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0 m_DoubleSidedGI: 0
m_CustomRenderQueue: -1 m_CustomRenderQueue: 3100
stringTagMap: {} stringTagMap: {}
disabledShaderPasses: [] disabledShaderPasses: []
m_LockedProperties: m_LockedProperties:
@@ -31,7 +31,7 @@ Material:
- _EchoRadius: 39.41617 - _EchoRadius: 39.41617
- _EchoWidth: 2.5 - _EchoWidth: 2.5
- _LightSoftness: 0.5 - _LightSoftness: 0.5
- _MinBrightness: 1 - _MinBrightness: 0
m_Colors: m_Colors:
- _DarknessColor: {r: 0.01, g: 0.01, b: 0.02, a: 1} - _DarknessColor: {r: 0.01, g: 0.01, b: 0.02, a: 1}
- _EchoCenter: {r: -2.4399998, g: -1.5000057, b: 0, a: 0} - _EchoCenter: {r: -2.4399998, g: -1.5000057, b: 0, a: 0}

View File

@@ -1,7 +1,8 @@
Shader "Custom/SpriteWithGroundClip" Shader "Custom/SpriteWithGroundClip"
{ {
// 带地面裁剪的Sprite Shader // 带地面裁剪和阴影的Sprite Shader
// 当Sprite的世界Y坐标低于地面高度时会被裁剪掉 // 当Sprite的世界Y坐标低于地面高度时会被裁剪掉
// 支持投射阴影(可选)
Properties Properties
{ {
_MainTex ("Texture", 2D) = "white" {} _MainTex ("Texture", 2D) = "white" {}
@@ -9,6 +10,12 @@ Shader "Custom/SpriteWithGroundClip"
[MaterialToggle] _EnableClip ("启用裁剪", Float) = 1.0 [MaterialToggle] _EnableClip ("启用裁剪", Float) = 1.0
_ClipSoftness ("裁剪边缘柔和度", Range(0, 0.5)) = 0.05 _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) _Color ("颜色", Color) = (1, 1, 1, 1)
} }
@@ -110,6 +117,122 @@ Shader "Custom/SpriteWithGroundClip"
} }
ENDHLSL 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" FallBack "Sprites/Default"
} }

View File

@@ -13,6 +13,7 @@ Material:
m_ModifiedSerializedProperties: 0 m_ModifiedSerializedProperties: 0
m_ValidKeywords: [] m_ValidKeywords: []
m_InvalidKeywords: m_InvalidKeywords:
- _CASTSHADOW_ON
- _ENABLECLIP_ON - _ENABLECLIP_ON
m_LightmapFlags: 4 m_LightmapFlags: 4
m_EnableInstancingVariants: 0 m_EnableInstancingVariants: 0
@@ -30,9 +31,13 @@ Material:
m_Offset: {x: 0, y: 0} m_Offset: {x: 0, y: 0}
m_Ints: [] m_Ints: []
m_Floats: m_Floats:
- _CastShadow: 1
- _ClipSoftness: 0.05 - _ClipSoftness: 0.05
- _EnableClip: 1 - _EnableClip: 1
- _GroundHeight: 0 - _GroundHeight: 0
- _ShadowOffset: -0.1
- _ShadowOpacity: 0.5
m_Colors: m_Colors:
- _Color: {r: 1, g: 1, b: 1, a: 1} - _Color: {r: 1, g: 1, b: 1, a: 1}
- _ShadowColor: {r: 0, g: 0, b: 0, a: 0.5}
m_BuildTextureStacks: [] m_BuildTextureStacks: []

File diff suppressed because it is too large Load Diff

View File

@@ -110,7 +110,9 @@ namespace IndianOceanAssets.Engine2_5D
} }
cr.sortingLayerID = parent.sortingLayerID; cr.sortingLayerID = parent.sortingLayerID;
cr.sortingOrder = parent.sortingOrder + 1; // 描边需要在黑暗遮罩SortingOrder=9999之后渲染
// 所以设置一个很大的值,确保在黑暗遮罩之上
cr.sortingOrder = 10000 + parent.sortingOrder;
cr.enabled = false; // 默认关闭,回声激活时再开 cr.enabled = false; // 默认关闭,回声激活时再开
_parentRenderers.Add(parent); _parentRenderers.Add(parent);
@@ -142,7 +144,9 @@ namespace IndianOceanAssets.Engine2_5D
if (cr.flipX != pr.flipX) cr.flipX = pr.flipX; if (cr.flipX != pr.flipX) cr.flipX = pr.flipX;
if (cr.flipY != pr.flipY) cr.flipY = pr.flipY; if (cr.flipY != pr.flipY) cr.flipY = pr.flipY;
if (cr.sortingLayerID != pr.sortingLayerID) cr.sortingLayerID = pr.sortingLayerID; if (cr.sortingLayerID != pr.sortingLayerID) cr.sortingLayerID = pr.sortingLayerID;
if (cr.sortingOrder != pr.sortingOrder + 1) cr.sortingOrder = pr.sortingOrder + 1; // 描边需要在黑暗遮罩SortingOrder=9999之后渲染
if (cr.sortingOrder != 10000 + pr.sortingOrder)
cr.sortingOrder = 10000 + pr.sortingOrder;
} }
} }