Reflecting surrounding objects with Reflection Probes

If you want your scene's environment to be reflected by game objects, featuring reflective materials (such as the ones with high Metallic or Specular levels), then you can achieve such effect using Reflection Probes. They allow for real-time, baked, or even custom reflections through the use of Cubemaps.

Real-time reflections can be expensive in terms of processing; in which case, you should favor baked reflections, unless it's really necessary to display dynamic objects being reflected (mirror-like objects, for instance). Still, there are some ways real-time reflections can be optimized. In this recipe, we will test three different configurations for reflection probes:

  • Real-time reflections (constantly updated)
  • Real-time reflections (updated on-demand) via script
  • Baked reflections (from the Editor)

Getting ready

For this recipe, we have prepared a basic scene, featuring three sets of reflective objects: one is constantly moving, one is static, and one moves whenever it is interacted with. The Probes.unitypackage package that is containing the scene can be found inside the 1362_06_04 folder.

How to do it...

To reflect the surrounding objects using the Reflection probes, follow these steps:

  1. Import Probes.unitypackage to a new project. Then, open the scene named Probes. This is a basic scene featuring three sets of reflective objects.
  2. Play the scene. Observe that one of the systems is dynamic, one is static, and one rotates randomly, whenever a key is pressed.
  3. Stop the scene.
  4. First, let's create a constantly updated real-time reflection probe. From the Create drop-down button of the Hierarchy view, add a Reflection Probe to the scene (Create | Light | Reflection Probe). Name it as RealtimeProbe and make it a child of the System 1 Realtime | MainSphere game object. Then, from the Inspector view, the Transform component, change its Position to X: 0; Y: 0; Z: 0, as shown:
    How to do it...
  5. Now, go to the Reflection Probe component. Set Type as Realtime; Refresh Mode as Every Frame and Time Slicing as No time slicing, shown as follows:
    How to do it...
  6. Play the scene. The reflections will be now be updated in real time. Stop the scene.
  7. Observe that the only object displaying the real-time reflections is System 1 Realtime | MainSphere. The reason for this is the Size of the Reflection Probe. From the Reflection Probe component, change its Size to X: 25; Y: 10; Z: 25. Note that the small red spheres are now affected as well. However, it is important to notice that all objects display the same reflection. Since our reflection probe's origin is placed at the same location as the MainSphere, all reflective objects will display reflections from that point of view.
    How to do it...
  8. If you want to eliminate the reflection from the reflective objects within the reflection probe, such as the small red spheres, select the objects and, from the Mesh Renderer component, set Reflection Probes as Off, as shown in the following screenshot:
    How to do it...
  9. Add a new Reflection Probe to the scene. This time, name it OnDemandProbe and make it a child of the System 2 On Demand | MainSphere game object. Then, from the Inspector view, Transform component, change its Position to X: 0; Y: 0; Z: 0.
  10. Now, go to the Reflection Probe component. Set Type as Realtime, Refresh Mode as Via scripting, and Time Slicing as Individual faces, as shown in the following screenshot:
    How to do it...
  11. Using the Create drop-down menu in the Project view, create a new C# Script named UpdateProbe.
  12. Open your script and replace everything with the following code:
    using UnityEngine;
    using System.Collections;
    
    public class UpdateProbe : MonoBehaviour {
      private ReflectionProbe probe;
    
      void Awake () {
        probe = GetComponent<ReflectionProbe> ();
        probe.RenderProbe();
      }
    
      public void RefreshProbe(){
        probe.RenderProbe();
      }
    }
  13. Save your script and attach it to the OnDemandProbe.
  14. Now, find the script named RandomRotation, which is attached to the System 2 On Demand | Spheres object, and open it in the code editor.
  15. Right before the Update() function, add the following lines:
    private GameObject probe;
    private UpdateProbe up;
    void Awake(){
      probe = GameObject.Find("OnDemandProbe");
      up = probe.GetComponent<UpdateProbe>();
    }
  16. Now, locate the line of code called transform.eulerAngles = newRotation; and, immediately after it, add the following line:
    up.RefreshProbe();
  17. Save the script and test your scene. Observe how the Reflection Probe is updated whenever a key is pressed.
  18. Stop the scene. Add a third Reflection Probe to the scene. Name it as CustomProbe and make it a child of the System 3 On Custom | MainSphere game object. Then, from the Inspector view, the Transform component, change its Position to X: 0; Y: 0; Z: 0.
  19. Go to the Reflection Probe component. Set Type as Custom and click on the Bake button, as shown:
    How to do it...
  20. A Save File dialog window will show up. Save the file as CustomProbe-reflectionHDR.exr.
  21. Observe that the reflection map does not include the reflection of red spheres on it. To change this, you have two options: set the System 3 On Custom | Spheres GameObject (and all its children) as Reflection Probe Static or, from the Reflection Probe component of the CustomProbe GameObject, check the Dynamic Objects option, as shown, and bake the map again (by clicking on the Bake button).
    How to do it...
  22. If you want your reflection Cubemap to be dynamically baked while you edit your scene, you can set the Reflection Probe Type to Baked, open the Lighting window (the Assets | Lighting menu), access the Scene section, and check the Continuous Baking option as shown. Please note that this mode won't include dynamic objects in the reflection, so be sure to set System 3 Custom | Spheres and System 3 Custom | MainSphere as Reflection Probe Static.
    How to do it...

How it works...

The Reflection Probes element act like omnidirectional cameras that render Cubemaps and apply them onto the objects within their constraints. When creating Reflection Probes, it's important to be aware of how the different types work:

  • Real-time Reflection Probes: Cubemaps are updated at runtime. The real-time Reflection Probes have three different Refresh Modes: On Awake (Cubemap is baked once, right before the scene starts); Every frame (Cubemap is constantly updated); Via scripting (Cubemap is updated whenever the RenderProbe function is used).

    Since Cubemaps feature six sides, the Reflection Probes features Time Slicing, so each side can be updated independently. There are three different types of Time Slicing: All Faces at Once (renders all faces at once and calculates mipmaps over 6 frames. Updates the probe in 9 frames); Individual Faces (each face is rendered over a number of frames. It updates the probe in 14 frames. The results can be a bit inaccurate, but it is the least expensive solution in terms of frame-rate impact); No Time Slicing (The Probe is rendered and mipmaps are calculated in one frame. It provides high accuracy, but it also the most expensive in terms of frame-rate).

  • Baked: Cubemaps are baked during editing the screen. Cubemaps can be either manually or automatically updated, depending whether the Continuous Baking option is checked (it can be found at the Scene section of the Lighting window).
  • Custom: The Custom Reflection Probes can be either manually baked from the scene (and even include Dynamic objects), or created from a premade Cubemap.

There's more...

There are a number of additional settings that can be tweaked, such as Importance, Intensity, Box Projection, Resolution, HDR, and so on. For a complete view on each of these settings, we strongly recommend that you read Unity's documentation on the subject, which is available at http://docs.unity3d.com/Manual/class-ReflectionProbe.html.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.19.243