Search Unity

Simple Reflection Probe Updater

Discussion in 'Scripting' started by RobAnthem, May 28, 2021.

  1. RobAnthem

    RobAnthem

    Joined:
    Dec 3, 2016
    Posts:
    90
    Update reflection probes efficiently to have real-time reflections. Normally it can be very costly but if you minimize the amount of updates it can save a ton of resources. In this example we treat the Update loop like a For loop, to only update one probe per frame.

    Code (CSharp):
    1. public class ReflectionUpdater : MonoBehaviour
    2. {
    3.     public static ReflectionUpdater Instance
    4.     {
    5.         get
    6.         {
    7.             if (m_instance == null)
    8.                 m_instance = FindObjectOfType<ReflectionUpdater>();
    9.             return m_instance;
    10.         }
    11.         private set
    12.         {
    13.             m_instance = value;
    14.         }
    15.     }
    16.     private static ReflectionUpdater m_instance;
    17.     public List<ReflectionProbe> probes = new List<ReflectionProbe>();
    18.     private int index;
    19.     private void Awake()
    20.     {
    21.         Instance = this;
    22.     }
    23.     private void Update()
    24.     {
    25.         probes[index++].RenderProbe();
    26.         if (index >= probes.Count)
    27.             index = 0;
    28.     }
    29.     public void AddProbe(ReflectionProbe probe)
    30.     {
    31.         if (!probes.Contains(probe))
    32.             probes.Add(probe);
    33.     }
    34. }
    As for usage, you could register procedural probes by adding a script to them like


    Code (CSharp):
    1. public class ProbeRegister : MonoBehaviour
    2. {
    3.     void Awake()
    4.     {
    5.         ReflectionUpdater.Instance.AddProbe(GetComponent<ReflectionProbe>());
    6.     }
    7. }
     
    Last edited: May 28, 2021