Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Rendering;
- public class ReflectionProbeUpdater : MonoBehaviour {
- [SerializeField]
- [Range ( 1, 500 )]
- private int probesPerCycle = 1; // amount of probes per cycle
- [SerializeField]
- [Range ( 0.01f, 10f )]
- private float updateRateInSeconds = 0.1f; // in seconds
- [SerializeField]
- [Range ( 0.01f, 1f )]
- private float startOffsetMin = 0f; // in seconds
- [SerializeField]
- [Range ( 0.01f, 1f )]
- private float startOffsetMax = 1f; // in seconds
- [SerializeField]
- private ReflectionProbeTimeSlicingMode timeSlicingMode;
- [SerializeField]
- private bool updateAllProbesEachCycle; // do all probes in one cycle
- private int cyclePosition = 0; // internal position in the probe list
- private List<ReflectionProbeData> reflectionProbes = new List<ReflectionProbeData> ();
- // Use this for initialization
- void Start() {
- foreach ( Transform t in transform ) {
- if ( t.TryGetComponent ( out ReflectionProbe r ) ) {
- reflectionProbes.Add ( new ReflectionProbeData ( r, -1 ) );
- r.mode = ReflectionProbeMode.Realtime;
- r.refreshMode = ReflectionProbeRefreshMode.ViaScripting;
- r.timeSlicingMode = timeSlicingMode;
- }
- }
- startOffsetMax = Mathf.Max ( startOffsetMin, startOffsetMax );
- InvokeRepeating ( nameof ( UpdateCycle ), Random.Range ( startOffsetMin, startOffsetMax ), updateRateInSeconds );
- }
- /// <summary>
- /// The main Updater to distribute the workload
- /// </summary>
- private void UpdateCycle() {
- int probesThisCycle;
- if ( updateAllProbesEachCycle ) {
- probesThisCycle = reflectionProbes.Count;
- } else {
- probesThisCycle = Mathf.Min ( probesPerCycle, reflectionProbes.Count );
- }
- //cycle Reflection probes
- for ( int i = 0; i < probesThisCycle; i++ ) {
- if ( cyclePosition > reflectionProbes.Count - 1 ) {
- cyclePosition = 0;
- }
- if ( reflectionProbes[ cyclePosition ].probe.IsFinishedRendering ( reflectionProbes[ cyclePosition ].renderId ) ) {
- //render the probe and save the renderId
- reflectionProbes[ cyclePosition ].renderId = reflectionProbes[ cyclePosition ].probe.RenderProbe ();
- }
- cyclePosition++;
- }
- }
- /// <summary>
- /// Internal use of a class to hold the reflection probe and render id
- /// </summary>
- private class ReflectionProbeData {
- public ReflectionProbe probe;
- public int renderId;
- public ReflectionProbeData( ReflectionProbe nprobe, int nrenderId ) {
- probe = nprobe;
- renderId = nrenderId;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement