From 170ec83fb1c74768b2df8879db790244bed4afeb Mon Sep 17 00:00:00 2001 From: Jarrod Doyle Date: Sat, 22 Feb 2025 20:28:28 +0000 Subject: [PATCH] Early out on light vis max range --- KeepersCompound.Lightmapper/LightMapper.cs | 5 +++-- .../PotentiallyVisibleSet.cs | 14 +++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/KeepersCompound.Lightmapper/LightMapper.cs b/KeepersCompound.Lightmapper/LightMapper.cs index 7cc4e22..946085b 100644 --- a/KeepersCompound.Lightmapper/LightMapper.cs +++ b/KeepersCompound.Lightmapper/LightMapper.cs @@ -536,10 +536,10 @@ public class LightMapper var visibleSet = settings.FastPvs switch { true => pvs.ComputeVisibilityFast(lightCellMap[i]), - false => pvs.ComputeVisibilityExact(_lights[i].Position, lightCellMap[i]) + false => pvs.ComputeVisibilityExact(_lights[i].Position, lightCellMap[i], _lights[i].Radius) }; - // Log.Information("Light {i} sees {c} cells", i, visibleSet.Length); + // Log.Information("Light {i} sees {c} cells", i, visibleSet.Count); visibleCellMap[i] = visibleSet; }); @@ -599,6 +599,7 @@ public class LightMapper cell.LightIndices[0]++; } + Log.Information("Cell {Id} sees ({Count}) lights.", i, cell.LightIndices[0]); if (cell.LightIndexCount > 97) { Log.Warning("Cell {Id} sees too many lights ({Count})", i, cell.LightIndices[0]); diff --git a/KeepersCompound.Lightmapper/PotentiallyVisibleSet.cs b/KeepersCompound.Lightmapper/PotentiallyVisibleSet.cs index 755c6d8..e436886 100644 --- a/KeepersCompound.Lightmapper/PotentiallyVisibleSet.cs +++ b/KeepersCompound.Lightmapper/PotentiallyVisibleSet.cs @@ -170,8 +170,7 @@ public class PotentiallyVisibleSet return visibleCells; } - // TODO: Max distance :) - public HashSet ComputeVisibilityExact(Vector3 pos, int cellIdx) + public HashSet ComputeVisibilityExact(Vector3 pos, int cellIdx, float maxRange) { if (cellIdx >= _graph.Length) { @@ -185,7 +184,7 @@ public class PotentiallyVisibleSet foreach (var edgeIdx in _graph[cellIdx].EdgeIndices) { var edge = _edges[edgeIdx]; - ComputeVisibilityExactRecursive(pos, visibleCells, visited, edge.Destination, edge.Poly); + ComputeVisibilityExactRecursive(pos, maxRange, visibleCells, visited, edge.Destination, edge.Poly); } return visibleCells; @@ -193,6 +192,7 @@ public class PotentiallyVisibleSet private void ComputeVisibilityExactRecursive( Vector3 lightPos, + float maxRange, HashSet visibleCells, Stack visited, int currentCellIdx, @@ -222,8 +222,12 @@ public class PotentiallyVisibleSet foreach (var targetEdgeIdx in _graph[currentCellIdx].EdgeIndices) { + // This only checks is there is a point on the plane in range. + // Could probably use poly center + radius to get an even better early out. var targetEdge = _edges[targetEdgeIdx]; - if (visited.Contains(targetEdge.Destination) || passPoly.IsCoplanar(targetEdge.Poly)) + if (visited.Contains(targetEdge.Destination) || + passPoly.IsCoplanar(targetEdge.Poly) || + Math.Abs(MathUtils.DistanceFromNormalizedPlane(targetEdge.Poly.Plane, lightPos)) > maxRange) { continue; } @@ -239,7 +243,7 @@ public class PotentiallyVisibleSet continue; } - ComputeVisibilityExactRecursive(lightPos, visibleCells, visited, targetEdge.Destination, poly); + ComputeVisibilityExactRecursive(lightPos, maxRange, visibleCells, visited, targetEdge.Destination, poly); } visited.Pop();