/// ---------------------------------------------
/// Senses Pack for Behavior Designer Pro
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.BehaviorDesigner.AddOns.SensesPack.Runtime.Emitters
{
using System.Collections.Generic;
using UnityEngine;
///
/// Manages all luminance emitters in the scene and provides methods to calculate total luminance at any point.
///
public class LuminanceManager : MonoBehaviour
{
private static LuminanceManager s_Instance;
public static LuminanceManager Instance {
get {
if (s_Instance == null) {
s_Instance = new GameObject("LuminanceManager").AddComponent();
}
return s_Instance;
}
}
[Tooltip("Should the ambient light be used in the luminance calculation?")]
[SerializeField] protected bool m_UseAmbientLight = true;
///
/// List of all active luminance emitters in the scene.
///
private List m_LuminanceEmitters = new List();
///
/// Called when the object is enabled. Sets up the singleton instance.
///
private void OnEnable()
{
s_Instance = this;
}
///
/// Registers a new luminance emitter with the manager.
///
/// The emitter to register.
public void Register(LuminanceEmitter emitter)
{
if (emitter == null) {
return;
}
m_LuminanceEmitters.Add(emitter);
}
///
/// Calculates the total luminance at a target's position from all registered emitters.
///
/// The GameObject to calculate luminance for.
/// The total luminance value at the target's position.
public float GetLuminance(GameObject target)
{
var luminance = m_UseAmbientLight ? RenderSettings.ambientIntensity : 0;
for (int i = 0; i < m_LuminanceEmitters.Count; ++i) {
luminance += m_LuminanceEmitters[i].GetLuminance(target);
}
return luminance;
}
///
/// Unregisters a luminance emitter from the manager.
///
/// The emitter to unregister.
public static void Unregister(LuminanceEmitter emitter)
{
if (s_Instance == null) {
return;
}
Instance.UnregisterInternal(emitter);
}
///
/// Unregisters a luminance emitter from the manager.
///
/// The emitter to unregister.
private void UnregisterInternal(LuminanceEmitter emitter)
{
m_LuminanceEmitters.Remove(emitter);
}
///
/// Called when the object is disabled. Cleans up the singleton instance.
///
private void OnDisable()
{
s_Instance = null;
}
///
/// Reset the static variables for domain reloading.
///
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
private static void DomainReset()
{
s_Instance = null;
}
}
}