优化调试过程,避免刷屏。

更好的调试效果。
This commit is contained in:
2026-06-09 19:33:37 +08:00
parent 95677e9536
commit bed44a6d82
2 changed files with 180 additions and 153 deletions
@@ -40,6 +40,7 @@ namespace XericLibrary.PostProcess
{ {
return GeoLayerPickerRenderPassFeature.Instance.GetMaterialByID(matID); return GeoLayerPickerRenderPassFeature.Instance.GetMaterialByID(matID);
} }
return null; return null;
} }
@@ -84,6 +85,7 @@ namespace XericLibrary.PostProcess
{ {
return GeoLayerPickerRenderPassFeature.Instance.GetMaterialByID(matID); return GeoLayerPickerRenderPassFeature.Instance.GetMaterialByID(matID);
} }
return null; return null;
} }
@@ -146,6 +148,7 @@ namespace XericLibrary.PostProcess
{ {
camera = Camera.main; camera = Camera.main;
} }
if (camera == null) if (camera == null)
return Vector2.zero; return Vector2.zero;
@@ -158,6 +161,7 @@ namespace XericLibrary.PostProcess
{ {
camera = Camera.main; camera = Camera.main;
} }
if (camera == null) if (camera == null)
return Vector2.zero; return Vector2.zero;
@@ -212,6 +216,7 @@ namespace XericLibrary.PostProcess
{ {
return material; return material;
} }
return null; return null;
} }
@@ -221,6 +226,7 @@ namespace XericLibrary.PostProcess
{ {
return id; return id;
} }
return -1; return -1;
} }
@@ -356,10 +362,12 @@ namespace XericLibrary.PostProcess
if (enableDebugLog) if (enableDebugLog)
{ {
Debug.Log($"[GeoLayerPicker] 材质孤岛统计完成,共 {m_MaterialIslands.Count} 个孤岛"); using (MacroMath.GetStringBuilder(out var sb))
foreach (var island in m_MaterialIslands)
{ {
Debug.Log($" MatID {island.matID} ({island.GetMaterialName()}): {island.totalPixelCount} 像素, 中心: {island.centerWorldPos}"); foreach (var island in m_MaterialIslands)
sb.Append(
$" MatID {island.matID} ({island.GetMaterialName()}): {island.totalPixelCount} 像素, 中心: {island.centerWorldPos}");
Debug.Log($"[GeoLayerPicker] 材质孤岛统计完成,共 {m_MaterialIslands.Count} 个孤岛\n{sb.ToString()}");
} }
} }
} }
@@ -423,6 +431,7 @@ namespace XericLibrary.PostProcess
{ {
break; break;
} }
col++; col++;
} }
@@ -438,7 +447,8 @@ namespace XericLibrary.PostProcess
return segments; return segments;
} }
private List<MaterialIsland> BuildIslandsForMaterial(int targetMatID, List<List<MaterialSegment>> rowSegments, int width, int height) private List<MaterialIsland> BuildIslandsForMaterial(int targetMatID, List<List<MaterialSegment>> rowSegments,
int width, int height)
{ {
List<MaterialIsland> islands = new List<MaterialIsland>(); List<MaterialIsland> islands = new List<MaterialIsland>();
@@ -493,6 +503,7 @@ namespace XericLibrary.PostProcess
mergedIsland.segments.AddRange(islandToMerge.segments); mergedIsland.segments.AddRange(islandToMerge.segments);
islands.Remove(islandToMerge); islands.Remove(islandToMerge);
} }
mergedIsland.segments.Add(currentSegment); mergedIsland.segments.Add(currentSegment);
islands.Add(mergedIsland); islands.Add(mergedIsland);
} }
@@ -540,6 +551,7 @@ namespace XericLibrary.PostProcess
{ {
rowPixelCounts[seg.rowIndex] = 0; rowPixelCounts[seg.rowIndex] = 0;
} }
rowPixelCounts[seg.rowIndex] += seg.length; rowPixelCounts[seg.rowIndex] += seg.length;
} }
@@ -642,10 +654,15 @@ namespace XericLibrary.PostProcess
float ndcYMin = passSampleRect.y * 2f - 1f; float ndcYMin = passSampleRect.y * 2f - 1f;
float ndcYMax = (passSampleRect.y + passSampleRect.height) * 2f - 1f; float ndcYMax = (passSampleRect.y + passSampleRect.height) * 2f - 1f;
// 将 NDC 子区域 [ndcMin, ndcMax] 映射到 [-1, 1]
// 投影矩阵变换: ndc = -m00 * viewX/viewZ - m02
// 修改后: ndc_new = scale * ndc_original - offset
// ndcMin → -1: scale * ndcMin - offset = -1 → offset = scale * ndcMin + 1
// ndcMax → +1: scale * ndcMax - offset = +1 → scale = 2/(ndcMax - ndcMin)
float scaleX = 2f / (ndcXMax - ndcXMin); float scaleX = 2f / (ndcXMax - ndcXMin);
float scaleY = 2f / (ndcYMax - ndcYMin); float scaleY = 2f / (ndcYMax - ndcYMin);
float offsetX = -1f - ndcXMin * scaleX; float offsetX = 1f + ndcXMin * scaleX;
float offsetY = -1f - ndcYMin * scaleY; float offsetY = 1f + ndcYMin * scaleY;
projMatrix.m00 *= scaleX; projMatrix.m00 *= scaleX;
projMatrix.m11 *= scaleY; projMatrix.m11 *= scaleY;
@@ -696,40 +713,47 @@ namespace XericLibrary.PostProcess
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{ {
if (!s_HasLogged && GeoLayerPickerRenderPassFeature.Instance != null && GeoLayerPickerRenderPassFeature.Instance.enableDebugLog) if (!s_HasLogged && GeoLayerPickerRenderPassFeature.Instance != null &&
GeoLayerPickerRenderPassFeature.Instance.enableDebugLog)
{ {
Debug.Log($"[GeoLayerPicker] DebugOverlayRenderPass.Execute called"); Debug.Log($"[GeoLayerPicker] DebugOverlayRenderPass.Execute called" +
Debug.Log($" overlayMaterial: {(overlayMaterial != null ? "OK" : "null")}"); $" overlayMaterial: {(overlayMaterial != null ? "OK" : "null")}" +
Debug.Log($" sourceRTHandle: {(sourceRTHandle != null ? "OK" : "null")}"); $" sourceRTHandle: {(sourceRTHandle != null ? "OK" : "null")}" +
Debug.Log($" sampleRect: {sampleRect}"); $" sampleRect: {sampleRect}");
s_HasLogged = true; s_HasLogged = true;
} }
if (overlayMaterial == null) if (overlayMaterial == null)
{ {
if (GeoLayerPickerRenderPassFeature.Instance != null && GeoLayerPickerRenderPassFeature.Instance.enableDebugLog) if (GeoLayerPickerRenderPassFeature.Instance != null &&
GeoLayerPickerRenderPassFeature.Instance.enableDebugLog)
{ {
Debug.LogWarning("[GeoLayerPicker] DebugOverlayRenderPass: overlayMaterial is null"); Debug.LogWarning("[GeoLayerPicker] DebugOverlayRenderPass: overlayMaterial is null");
} }
return; return;
} }
if (sourceRTHandle == null) if (sourceRTHandle == null)
{ {
if (GeoLayerPickerRenderPassFeature.Instance != null && GeoLayerPickerRenderPassFeature.Instance.enableDebugLog) if (GeoLayerPickerRenderPassFeature.Instance != null &&
GeoLayerPickerRenderPassFeature.Instance.enableDebugLog)
{ {
Debug.LogWarning("[GeoLayerPicker] DebugOverlayRenderPass: sourceRTHandle is null"); Debug.LogWarning("[GeoLayerPicker] DebugOverlayRenderPass: sourceRTHandle is null");
} }
return; return;
} }
RTHandle currentColorTarget = renderingData.cameraData.renderer.cameraColorTargetHandle; RTHandle currentColorTarget = renderingData.cameraData.renderer.cameraColorTargetHandle;
if (currentColorTarget == null) if (currentColorTarget == null)
{ {
if (GeoLayerPickerRenderPassFeature.Instance != null && GeoLayerPickerRenderPassFeature.Instance.enableDebugLog) if (GeoLayerPickerRenderPassFeature.Instance != null &&
GeoLayerPickerRenderPassFeature.Instance.enableDebugLog)
{ {
Debug.LogWarning("[GeoLayerPicker] DebugOverlayRenderPass: currentColorTarget is null"); Debug.LogWarning("[GeoLayerPicker] DebugOverlayRenderPass: currentColorTarget is null");
} }
return; return;
} }
@@ -747,7 +771,8 @@ namespace XericLibrary.PostProcess
)); ));
CoreUtils.SetRenderTarget(debugCmd, currentColorTarget); CoreUtils.SetRenderTarget(debugCmd, currentColorTarget);
debugCmd.SetViewport(new Rect(0, 0, renderingData.cameraData.camera.pixelWidth, renderingData.cameraData.camera.pixelHeight)); debugCmd.SetViewport(new Rect(0, 0, renderingData.cameraData.camera.pixelWidth,
renderingData.cameraData.camera.pixelHeight));
debugCmd.DrawProcedural( debugCmd.DrawProcedural(
Matrix4x4.identity, Matrix4x4.identity,
@@ -827,7 +852,8 @@ namespace XericLibrary.PostProcess
} }
else else
{ {
Debug.LogError("[GeoLayerPicker] 找不到调试 Shader: Hidden/XericLibrary/GeoLayerPicker/ScreenOverrideRect"); Debug.LogError(
"[GeoLayerPicker] 找不到调试 Shader: Hidden/XericLibrary/GeoLayerPicker/ScreenOverrideRect");
} }
} }
} }
@@ -988,10 +1014,11 @@ namespace XericLibrary.PostProcess
if (enableDebugLog) if (enableDebugLog)
{ {
Debug.Log($"[GeoLayerPicker] 材质映射重建完成,共 {m_MaterialToID.Count} 个材质"); using (MacroMath.GetStringBuilder(out var sb))
foreach (var kvp in m_MaterialToID)
{ {
Debug.Log($" ID {kvp.Value}: {kvp.Key.name}"); foreach (var kvp in m_MaterialToID)
sb.Append($" ID {kvp.Value}: {kvp.Key.name}");
Debug.Log($"[GeoLayerPicker] 材质映射重建完成,共 {m_MaterialToID.Count} 个材质 \n{sb.ToString()}");
} }
} }
} }
@@ -75,8 +75,8 @@ Shader "Hidden/XericLibrary/GeoLayerPicker/ScreenOverrideRect"
} }
// 将世界坐标映射为可见颜色 // 将世界坐标映射为可见颜色
float3 debugColor = abs(sin(col.rgb * float3(0.03, 0.1, 0.05))) * 0.5; float3 debugColor = sin(col.rgb * float3(0.03, 0.1, 0.05)) * 0.5 + 0.5;
float3 debugColor2 = abs(cos(col.rgb * float3(0.05, 0.09, 0.07))) * 0.5 + 0.5; float3 debugColor2 = cos(col.rgb * float3(0.05, 0.09, 0.07)) * 0.5 + 0.5;
float matIDColor = frac(col.a * 0.07); float matIDColor = frac(col.a * 0.07);
debugColor = lerp(debugColor, debugColor2, matIDColor); debugColor = lerp(debugColor, debugColor2, matIDColor);
return float4(debugColor, 0.9); return float4(debugColor, 0.9);