Skip to content

Commit d8beb65

Browse files
Better handling for excessproximity lights (#10241)
Previously, if the shader's maximum proximity light limit was reached, a warning would be logged and the lights discarded. I've changed this to keep all lights around on the CPU side and only send the first two valid ones to the shader. This helps with race conditions where multiple lights are added and removed in a single frame and eliminates a warning that was spamming to the log and degrading performance. Co-authored-by: Roger Liu <[email protected]>
1 parent 40894a6 commit d8beb65

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

Assets/MRTK/Core/Utilities/StandardShader/ProximityLight.cs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,21 @@
99
namespace Microsoft.MixedReality.Toolkit.Utilities
1010
{
1111
/// <summary>
12-
/// Utility component to animate and visualize a light that can be used with
12+
/// Utility component to animate and visualize a light that can be used with
1313
/// the "MixedRealityToolkit/Standard" shader "_ProximityLight" feature.
1414
/// </summary>
1515
[ExecuteInEditMode]
1616
[HelpURL("https://docs.microsoft.com/windows/mixed-reality/mrtk-unity/features/rendering/proximity-light")]
1717
[AddComponentMenu("Scripts/MRTK/Core/ProximityLight")]
1818
public class ProximityLight : MonoBehaviour
1919
{
20-
// Two proximity lights are supported at this time.
20+
// Two proximity lights are supported at this time. Excess lights will
21+
// be held in a queue and addded to the shader in a first-in, first-out
22+
// manner.
2123
private const int proximityLightCount = 2;
24+
private const int proximityLightQueueSize = 6;
2225
private const int proximityLightDataSize = 6;
23-
private static List<ProximityLight> activeProximityLights = new List<ProximityLight>(proximityLightCount);
26+
private static List<ProximityLight> activeProximityLights = new List<ProximityLight>(proximityLightQueueSize);
2427
private static Vector4[] proximityLightData = new Vector4[proximityLightCount * proximityLightDataSize];
2528
private static int proximityLightDataID;
2629
private static int lastProximityLightUpdate = -1;
@@ -213,11 +216,6 @@ private void OnDrawGizmosSelected()
213216

214217
private static void AddProximityLight(ProximityLight light)
215218
{
216-
if (activeProximityLights.Count >= proximityLightCount)
217-
{
218-
Debug.LogWarningFormat("Max proximity light count ({0}) exceeded.", proximityLightCount);
219-
}
220-
221219
activeProximityLights.Add(light);
222220
}
223221

@@ -243,13 +241,15 @@ private static void UpdateProximityLights(bool forceUpdate = false)
243241
return;
244242
}
245243

246-
for (int i = 0; i < proximityLightCount; ++i)
244+
int lightIndex, queueIndex;
245+
for (lightIndex = queueIndex = 0; queueIndex < activeProximityLights.Count && lightIndex < proximityLightCount; ++queueIndex)
247246
{
248-
ProximityLight light = (i >= activeProximityLights.Count) ? null : activeProximityLights[i];
249-
int dataIndex = i * proximityLightDataSize;
247+
ProximityLight light = activeProximityLights[queueIndex];
250248

251249
if (light)
252250
{
251+
int dataIndex = lightIndex++ * proximityLightDataSize;
252+
253253
proximityLightData[dataIndex] = new Vector4(light.transform.position.x,
254254
light.transform.position.y,
255255
light.transform.position.z,
@@ -267,10 +267,12 @@ private static void UpdateProximityLights(bool forceUpdate = false)
267267
proximityLightData[dataIndex + 4] = light.Settings.MiddleColor;
268268
proximityLightData[dataIndex + 5] = light.Settings.OuterColor;
269269
}
270-
else
271-
{
272-
proximityLightData[dataIndex] = Vector4.zero;
273-
}
270+
}
271+
272+
for (; lightIndex < proximityLightCount; ++lightIndex)
273+
{
274+
int dataIndex = lightIndex * proximityLightDataSize;
275+
proximityLightData[dataIndex] = Vector4.zero;
274276
}
275277

276278
Shader.SetGlobalVectorArray(proximityLightDataID, proximityLightData);

0 commit comments

Comments
 (0)