add DebugCascadeShadow

This commit is contained in:
StarBeats 2025-07-14 16:40:10 +08:00
parent 0b1956a095
commit 052294ae82
5 changed files with 154 additions and 11 deletions

View File

@ -0,0 +1,105 @@
namespace UnityEngine.Rendering.Universal
{
public class DebugCascadeShadow : MonoBehaviour
{
public Camera targetCamera;
public bool showCullingSpheres = true;
public bool showCameraFrustums = true;
public Color[] cascadeColors = new Color[]
{
new Color(1, 0, 0, 0.5f), // 红
new Color(0, 1, 0, 0.5f), // 绿
new Color(0, 0.5f, 1, 0.5f), // 蓝
new Color(1, 1, 0, 0.5f) // 黄
};
public static Vector3 cascadeSplits;
public static Vector4[] cullingSpheres= new Vector4[4];
void OnDrawGizmos()
{
if (targetCamera == null) return;
// 可视化Culling Spheres
if (showCullingSpheres)
{
for (int i = 0; i < cullingSpheres.Length; i++)
{
if (cullingSpheres[i].w <= 0) continue;
Gizmos.color = cascadeColors[i % cascadeColors.Length];
Vector3 sphereCenter = cullingSpheres[i];
float sphereRadius = cullingSpheres[i].w;
Gizmos.DrawWireSphere(sphereCenter, sphereRadius);
}
}
// 可视化相机视锥体级联
if (showCameraFrustums)
{
float lastSplit = targetCamera.nearClipPlane;
for (int i = 0; i < 3; i++)
{
if (cascadeSplits[i] <= 0) break;
Gizmos.color = cascadeColors[i % cascadeColors.Length];
DrawCascadeFrustum(targetCamera, lastSplit, cascadeSplits[i]);
lastSplit = cascadeSplits[i];
}
}
}
private void DrawCascadeFrustum(Camera cam, float startDist, float endDist)
{
// 计算视锥体角点
Vector3[] nearCorners = GetFrustumCorners(cam, startDist);
Vector3[] farCorners = GetFrustumCorners(cam, endDist);
// 绘制视锥体线框
Gizmos.DrawLine(nearCorners[0], farCorners[0]);
Gizmos.DrawLine(nearCorners[1], farCorners[1]);
Gizmos.DrawLine(nearCorners[2], farCorners[2]);
Gizmos.DrawLine(nearCorners[3], farCorners[3]);
// 绘制近/远平面
DrawFrustumPlane(nearCorners);
DrawFrustumPlane(farCorners);
}
private Vector3[] GetFrustumCorners(Camera cam, float distance)
{
Vector3[] corners = new Vector3[4];
Transform camTransform = cam.transform;
float halfFOV = cam.fieldOfView * 0.5f * Mathf.Deg2Rad;
float aspect = cam.aspect;
float height = distance * Mathf.Tan(halfFOV);
float width = height * aspect;
// 计算相对于相机的角点
corners[0] = camTransform.forward * distance + camTransform.up * height - camTransform.right * width; // 左下
corners[1] = camTransform.forward * distance + camTransform.up * height + camTransform.right * width; // 右下
corners[2] = camTransform.forward * distance - camTransform.up * height + camTransform.right * width; // 右上
corners[3] = camTransform.forward * distance - camTransform.up * height - camTransform.right * width; // 左上
// 转换到世界坐标
for (int i = 0; i < 4; i++)
{
corners[i] = camTransform.position + corners[i];
}
return corners;
}
private void DrawFrustumPlane(Vector3[] corners)
{
Gizmos.DrawLine(corners[0], corners[1]);
Gizmos.DrawLine(corners[1], corners[2]);
Gizmos.DrawLine(corners[2], corners[3]);
Gizmos.DrawLine(corners[3], corners[0]);
}
}
}

View File

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

View File

@ -139,7 +139,13 @@ namespace UnityEngine.Rendering.Universal.Internal
if (!success) if (!success)
return SetupForEmptyRendering(ref renderingData); return SetupForEmptyRendering(ref renderingData);
} }
#if DEBUG
if(renderingData.cameraData.cameraType == CameraType.Game)
{
DebugCascadeShadow.cascadeSplits = renderingData.shadowData.mainLightShadowCascadesSplit * renderingData.cameraData.maxShadowDistance;
Array.Copy(m_CascadeSplitDistances, DebugCascadeShadow.cullingSpheres, 4);
}
#endif
m_MaxShadowDistanceSq = renderingData.cameraData.maxShadowDistance * renderingData.cameraData.maxShadowDistance; m_MaxShadowDistanceSq = renderingData.cameraData.maxShadowDistance * renderingData.cameraData.maxShadowDistance;
m_CascadeBorder = renderingData.shadowData.mainLightShadowCascadeBorder; m_CascadeBorder = renderingData.shadowData.mainLightShadowCascadeBorder;
m_CreateEmptyShadowmap = false; m_CreateEmptyShadowmap = false;

View File

@ -1,16 +1,16 @@
{ {
"dependencies": { "dependencies": {
"com.unity.burst": "1.8.18", "com.unity.burst": "1.8.19",
"com.unity.cinemachine": "2.10.3", "com.unity.cinemachine": "2.10.3",
"com.unity.collab-proxy": "2.6.0", "com.unity.collab-proxy": "2.7.1",
"com.unity.ide.rider": "3.0.34", "com.unity.ide.rider": "3.0.34",
"com.unity.ide.visualstudio": "2.0.22", "com.unity.ide.visualstudio": "2.0.22",
"com.unity.ide.vscode": "1.2.5", "com.unity.ide.vscode": "1.2.5",
"com.unity.inputsystem": "1.11.2", "com.unity.inputsystem": "1.11.2",
"com.unity.learn.iet-framework": "3.1.3", "com.unity.learn.iet-framework": "3.1.3",
"com.unity.memoryprofiler": "1.1.1", "com.unity.memoryprofiler": "1.1.4",
"com.unity.recorder": "4.0.3", "com.unity.recorder": "4.0.3",
"com.unity.render-pipelines.universal": "14.0.11", "com.unity.render-pipelines.universal": "14.0.12",
"com.unity.splines": "2.7.2", "com.unity.splines": "2.7.2",
"com.unity.test-framework": "1.1.33", "com.unity.test-framework": "1.1.33",
"com.unity.textmeshpro": "3.0.7", "com.unity.textmeshpro": "3.0.7",

View File

@ -1,7 +1,7 @@
{ {
"dependencies": { "dependencies": {
"com.unity.burst": { "com.unity.burst": {
"version": "1.8.18", "version": "1.8.19",
"depth": 0, "depth": 0,
"source": "registry", "source": "registry",
"dependencies": { "dependencies": {
@ -20,12 +20,22 @@
"url": "https://packages.unity.cn" "url": "https://packages.unity.cn"
}, },
"com.unity.collab-proxy": { "com.unity.collab-proxy": {
"version": "2.6.0", "version": "2.7.1",
"depth": 0, "depth": 0,
"source": "registry", "source": "registry",
"dependencies": {}, "dependencies": {},
"url": "https://packages.unity.cn" "url": "https://packages.unity.cn"
}, },
"com.unity.collections": {
"version": "1.2.4",
"depth": 1,
"source": "registry",
"dependencies": {
"com.unity.burst": "1.6.6",
"com.unity.test-framework": "1.1.31"
},
"url": "https://packages.unity.cn"
},
"com.unity.editorcoroutines": { "com.unity.editorcoroutines": {
"version": "1.0.0", "version": "1.0.0",
"depth": 1, "depth": 1,
@ -92,14 +102,25 @@
"url": "https://packages.unity.cn" "url": "https://packages.unity.cn"
}, },
"com.unity.memoryprofiler": { "com.unity.memoryprofiler": {
"version": "1.1.1", "version": "1.1.4",
"depth": 0, "depth": 0,
"source": "registry", "source": "registry",
"dependencies": { "dependencies": {
"com.unity.burst": "1.8.0",
"com.unity.collections": "1.2.3",
"com.unity.mathematics": "1.2.1",
"com.unity.profiling.core": "1.0.0",
"com.unity.editorcoroutines": "1.0.0" "com.unity.editorcoroutines": "1.0.0"
}, },
"url": "https://packages.unity.cn" "url": "https://packages.unity.cn"
}, },
"com.unity.profiling.core": {
"version": "1.0.2",
"depth": 1,
"source": "registry",
"dependencies": {},
"url": "https://packages.unity.cn"
},
"com.unity.recorder": { "com.unity.recorder": {
"version": "4.0.3", "version": "4.0.3",
"depth": 0, "depth": 0,
@ -110,7 +131,7 @@
"url": "https://packages.unity.cn" "url": "https://packages.unity.cn"
}, },
"com.unity.render-pipelines.core": { "com.unity.render-pipelines.core": {
"version": "14.0.11", "version": "14.0.12",
"depth": 1, "depth": 1,
"source": "builtin", "source": "builtin",
"dependencies": { "dependencies": {
@ -156,11 +177,11 @@
"url": "https://packages.unity.cn" "url": "https://packages.unity.cn"
}, },
"com.unity.shadergraph": { "com.unity.shadergraph": {
"version": "14.0.11", "version": "14.0.12",
"depth": 1, "depth": 1,
"source": "builtin", "source": "builtin",
"dependencies": { "dependencies": {
"com.unity.render-pipelines.core": "14.0.11", "com.unity.render-pipelines.core": "14.0.12",
"com.unity.searcher": "4.9.2" "com.unity.searcher": "4.9.2"
} }
}, },