Realtime reflections via scripting

have a trigger on reflection probe to do realtime reflection when player is ontriggerstay. (multiple mirrors in rooms with moving parts doors etc, want real time reflections but to save resources only when in the room in front of mirror. I debugged each step. update method is being called but the reflection is just showing the on awake update. Tried over riding via scripting with configure reflection probe methods, used every frame and viascripting, still can’t get it to update in real time. any suggestions for desired outcome besides just leaving it full time real time which would be pretty heavy on 6 different mirrors I gather? starting to think baked reflection is good enough unless anyone has any ideas ? :wink:

public class RealtimeReflectionProbeUpdater : MonoBehaviour
{
    public GameObject triggerObject; // Assign the TriggerObject GameObject in the Inspector
    private ReflectionProbe reflectionProbe;
    private bool isTriggerObjectInside = false;

    private void Awake()
    {
        reflectionProbe = GetComponent<ReflectionProbe>();
        ConfigureReflectionProbe();
        UpdateReflectionProbe();
      
    }

    private void Update()
    {
        if (isTriggerObjectInside)
        {
            UpdateReflectionProbe();
           
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject == triggerObject)
        {
            isTriggerObjectInside = true;
           
            UpdateReflectionProbe();
        }
        else
        {
            Debug.Log("OnTriggerEnter: " + other.gameObject.name + " entered trigger, but it is not the triggerObject");
        }
    }

    private void OnTriggerStay(Collider other)
    {
        if (other.gameObject == triggerObject)
        {
          
            UpdateReflectionProbe();
        }
        else
        {
            Debug.Log("OnTriggerStay: " + other.gameObject.name + " is staying in trigger, but it is not the triggerObject");
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject == triggerObject)
        {
            isTriggerObjectInside = false;
           
        }
        else
        {
            Debug.Log("OnTriggerExit: " + other.gameObject.name + " exited trigger, but it is not the triggerObject");
        }
    }

    private void UpdateReflectionProbe()
    {
        reflectionProbe.RenderProbe();
     
    }

    private void ConfigureReflectionProbe()
    {
        reflectionProbe.mode = ReflectionProbeMode.Realtime;
        reflectionProbe.refreshMode = ReflectionProbeRefreshMode.EveryFrame;
        reflectionProbe.timeSlicingMode = ReflectionProbeTimeSlicingMode.NoTimeSlicing;
      
    }
}

I would “configure” the reflection probe in the Inspector, no need to write code for that and perhaps some of these settings don’t get applied when changed at runtime.

Try experimenting with a non-scripted reflection probe first that gets updated at runtime. You can test it in playmode and move the camera or probe with the transform handles to see if it updates.

Updating the probe every frame may be prohibitively expensive. Try to focus on using time slicing because you may need to have this anyway as a performance setting.

1 Like

my understanding if it’s realtime and anything other than on awake, it’s running the whole time and very expensive no matter what. At most would only do one like that for a rearview mirror where it’s totally critical. My thoughts were to have a trigger where it updates only when in front of it. so it would actually reflect realtime but only when totally needed. I got it to work with script above but ran into another issue, putting rigidbody on starter asset 1st person breaks the function of it… so… I guess I’d have to do it with some custom physics collider detection?

if it helps anyone. I got this working. I’m sure my code is clunky as hell, (combo of me and chatgpt) box trigger on probe for character controller collisions. two probe config methods. one for on awake and when exiting trigger to shut it down. Realtime when in front of mirrors. Hope it helps someone.

using UnityEngine;
using UnityEngine.Rendering;

[RequireComponent(typeof(ReflectionProbe))]
public class RealtimeReflectionProbeUpdater : MonoBehaviour
{
    public GameObject player; // Assign the Player GameObject with the CharacterController in the Inspector
    private ReflectionProbe reflectionProbe;
    private bool isPlayerColliding = false;

    private void Awake()
    {
        reflectionProbe = GetComponent<ReflectionProbe>();
        ConfigureInitialReflectionProbe();
        Debug.Log("Reflection Probe configured");

        UpdateReflectionProbe();
        Debug.Log("Initial reflection probe update");
    }

    private void Update()
    {
        if (isPlayerColliding)
        {
            UpdateReflectionProbe();
            Debug.Log("Reflection probe updated during collision");
        }
    }

    private void OnControllerColliderHit(ControllerColliderHit hit)
    {
        if (hit.gameObject == player)
        {
            isPlayerColliding = true;
            UpdateReflectionProbe();
            Debug.Log("OnControllerColliderHit: Player collided");
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject == player)
        {
            isPlayerColliding = true;
            ConfigureReflectionProbe();
            UpdateReflectionProbe();
            Debug.Log("OnTriggerEnter: Player entered trigger");
        }
    }

    private void OnTriggerStay(Collider other)
    {
        if (other.gameObject == player)
        {
            //ConfigureReflectionProbe();
            UpdateReflectionProbe();
            Debug.Log("OnTriggerStay: Player staying in trigger");
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if (other.gameObject == player)
        {
            isPlayerColliding = false;
            ConfigureInitialReflectionProbe();
            UpdateReflectionProbe();
            Debug.Log("OnTriggerExit: Player exited trigger");
        }
    }

    private void UpdateReflectionProbe()
    {
        reflectionProbe.RenderProbe();
        Debug.Log("Reflection probe rendered");
    }

    private void ConfigureReflectionProbe()
    {
        reflectionProbe.mode = ReflectionProbeMode.Realtime;
        reflectionProbe.refreshMode = ReflectionProbeRefreshMode.EveryFrame;
        reflectionProbe.timeSlicingMode = ReflectionProbeTimeSlicingMode.NoTimeSlicing;
        reflectionProbe.renderDynamicObjects = true;
    }
    private void ConfigureInitialReflectionProbe()
    {
        reflectionProbe.mode = ReflectionProbeMode.Realtime;
        reflectionProbe.refreshMode = ReflectionProbeRefreshMode.OnAwake;
        reflectionProbe.timeSlicingMode = ReflectionProbeTimeSlicingMode.AllFacesAtOnce;
        reflectionProbe.renderDynamicObjects = true;
    }
}