Advertisement
BinaryImpactG

Unity Tip - ReflectionProbeUpdater

Nov 16th, 2021
1,099
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.90 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.Rendering;
  4.  
  5. public class ReflectionProbeUpdater : MonoBehaviour {
  6.  
  7.     [SerializeField]
  8.     [Range ( 1, 500 )]
  9.     private int probesPerCycle = 1;             // amount of probes per cycle
  10.     [SerializeField]
  11.     [Range ( 0.01f, 10f )]
  12.     private float updateRateInSeconds = 0.1f;   // in seconds
  13.     [SerializeField]
  14.     [Range ( 0.01f, 1f )]
  15.     private float startOffsetMin = 0f;          // in seconds
  16.     [SerializeField]
  17.     [Range ( 0.01f, 1f )]
  18.     private float startOffsetMax = 1f;          // in seconds
  19.     [SerializeField]
  20.     private ReflectionProbeTimeSlicingMode timeSlicingMode;
  21.     [SerializeField]
  22.     private bool updateAllProbesEachCycle;      // do all probes in one cycle
  23.  
  24.     private int cyclePosition = 0;              // internal position in the probe list
  25.     private List<ReflectionProbeData> reflectionProbes = new List<ReflectionProbeData> ();
  26.  
  27.     // Use this for initialization
  28.     void Start() {
  29.         foreach ( Transform t in transform ) {
  30.             if ( t.TryGetComponent ( out ReflectionProbe r ) ) {
  31.                 reflectionProbes.Add ( new ReflectionProbeData ( r, -1 ) );
  32.                 r.mode = ReflectionProbeMode.Realtime;
  33.                 r.refreshMode = ReflectionProbeRefreshMode.ViaScripting;
  34.                 r.timeSlicingMode = timeSlicingMode;
  35.             }
  36.         }
  37.         startOffsetMax = Mathf.Max ( startOffsetMin, startOffsetMax );
  38.         InvokeRepeating ( nameof ( UpdateCycle ), Random.Range ( startOffsetMin, startOffsetMax ), updateRateInSeconds );
  39.     }
  40.  
  41.     /// <summary>
  42.     /// The main Updater to distribute the workload
  43.     /// </summary>
  44.     private void UpdateCycle() {
  45.         int probesThisCycle;
  46.         if ( updateAllProbesEachCycle ) {
  47.             probesThisCycle = reflectionProbes.Count;
  48.         } else {
  49.             probesThisCycle = Mathf.Min ( probesPerCycle, reflectionProbes.Count );
  50.         }
  51.  
  52.         //cycle Reflection probes
  53.         for ( int i = 0; i < probesThisCycle; i++ ) {
  54.             if ( cyclePosition > reflectionProbes.Count - 1 ) {
  55.                 cyclePosition = 0;
  56.             }
  57.             if ( reflectionProbes[ cyclePosition ].probe.IsFinishedRendering ( reflectionProbes[ cyclePosition ].renderId ) ) {
  58.                 //render the probe and save the renderId
  59.                 reflectionProbes[ cyclePosition ].renderId = reflectionProbes[ cyclePosition ].probe.RenderProbe ();
  60.             }
  61.             cyclePosition++;
  62.         }
  63.     }
  64.  
  65.     /// <summary>
  66.     /// Internal use of a class to hold the reflection probe and render id
  67.     /// </summary>
  68.     private class ReflectionProbeData {
  69.         public ReflectionProbe probe;
  70.         public int renderId;
  71.  
  72.         public ReflectionProbeData( ReflectionProbe nprobe, int nrenderId ) {
  73.             probe = nprobe;
  74.             renderId = nrenderId;
  75.         }
  76.     }
  77. }
  78.  
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement