Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Editor/Tools/LightmapTransferTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ static UvtLog.Category[] BuildLogCategoryList()
// ── Scene ──
double sceneSpotLastRaycastTime;
const double sceneSpotThrottleSec = 0.033;
const int sceneSpotTriangleBudget = 12000;

// ════════════════════════════════════════════════════════════
// Lifecycle
Expand Down Expand Up @@ -4710,6 +4711,7 @@ bool TryRaycastPreview(Ray ray, out SceneHit bestHit)
bestHit = default;
bestHit.distance = float.PositiveInfinity;
bool found = false;
int remainingTriangleBudget = sceneSpotTriangleBudget;

foreach (var entry in ctx.ForLod(ctx.PreviewLod))
{
Expand All @@ -4720,10 +4722,28 @@ bool TryRaycastPreview(Ray ray, out SceneHit bestHit)
Bounds wb = TransformBounds(mesh.bounds, l2w);
if (!wb.IntersectRay(ray, out float aabbDist) || aabbDist > bestHit.distance) continue;

// Inspect index metadata before reading mesh arrays: those properties make
// full managed copies and shell extraction is linear in the face count.
// Skip a mesh rather than partially testing it, which could report a false hit.
ulong meshIndexCount = 0;
for (int subMesh = 0; subMesh < mesh.subMeshCount; subMesh++)
{
ulong indexCount = mesh.GetIndexCount(subMesh);
if (indexCount > (ulong)remainingTriangleBudget * 3UL - meshIndexCount)
{
meshIndexCount = ulong.MaxValue;
break;
}
meshIndexCount += indexCount;
}
if (meshIndexCount == ulong.MaxValue) continue;

var v = mesh.vertices;
var tri = canvas.GetTrianglesCached(mesh);
var uv = canvas.RdUvCached(mesh, ctx.PreviewUvChannel);
if (v == null || tri == null || uv == null) continue;
if (tri.Length / 3 > remainingTriangleBudget) continue;
remainingTriangleBudget -= tri.Length / 3;
int[] faceToShell = ctx.UvPreviewShellCache.GetFaceToShell(mesh, ctx.PreviewUvChannel, uv, tri);

for (int f = 0; f + 2 < tri.Length; f += 3)
Expand Down
Loading