• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by callebo_FK · Sep 14, 2018 at 08:13 AM · shadershadersreflectionprobesreflective

Blend Cubemaps in reflection probe

I am trying to make a reflection probe render once every second, and blend between the old render and the new one. See how it looks like in Zelda BOTW: https://twitter.com/brainchildlight/status/937439281890467840

.

I am currently only able to render once every second, but not blend between the old and new cubemap. It instantly swaps to the new one. I found the ReflectionProbe.BlendCubemap function but have had no success with it. Most attempts I make crash Unity, and since there are a grand total of zero examples of it being used online I have a hard time even figuring out if it is the way to go.

I am hoping not to have to rely on blending the cubemaps inside the shader since I want this to work on all objects with all kinds of materials, not just certain ones which are blend-approved...

Here's some of the code I've tried, which in my mind should probably work. All it does is render a new reflection every second with no blending. But as I said there isn't really that much information available about this stuff so I have a hard time to figure out what I am doing wrong.

 public class PlayerReflectionProbe : MonoBehaviour
     {
         private float timer;
         public float updateTime = 1f;
         private ReflectionProbe thisProbe;
         public Texture oldCubemap;
         public Texture newCubemap;
         private RenderTexture finalCubemap;
         public float blend = 0.5f;
         public RenderTexture render;
 
         void Awake()
         {
             // Set up reflection probe component.
             if (thisProbe == null)
             {
                 thisProbe = GetComponent<ReflectionProbe>();
             }
             // Set up final texture diplayed by render probe.
             render = new RenderTexture(128, 128, 0);
             render.dimension = UnityEngine.Rendering.TextureDimension.Cube;
             render.useMipMap = true;
             thisProbe.customBakedTexture = render;
         }
 
         // Update is called once per frame
         void Update()
         {
             // Blend between old and new.
             ReflectionProbe.BlendCubemap(oldCubemap, newCubemap, blend, render);
 
             // Assign final render to probe ???
             thisProbe.customBakedTexture = render;
 
             // Update blend
             blend -= Time.deltaTime;
             if (blend <= 0f)
                 blend = 1f;
 
             // Render new once every second, and save old render.
             timer += Time.deltaTime;
             if (timer >= updateTime)
             {
                 oldCubemap = thisProbe.texture;
 
                 thisProbe.RenderProbe();
                 newCubemap = thisProbe.texture;
 
                 timer = 0f;
             }
         }
     }



Any help at all is greatly appreciated

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by TFED · Apr 27, 2019 at 05:17 PM

Here is some code I made a while ago that I forgot about that basically does what you asked, and also has a bunch of features. I forget how exactly you use it, but I tested it and it works fine for me. When set to medium, it updates the cubemap every n seconds, and interpolates to it smoothly. At high, it just updates every frame. At low, I think it just uses the skybox.

 public class ReflectionProbeBlender : MonoBehaviour {
 
     public ReflectionProbe outputProbe;
     public ReflectionProbe samplerProbe;
     public ReflectionProbe previousProbe;
     public LayerMask cullingMask;
     public ReflectionRes lowReflectionResolution;
     public ReflectionRes mediumReflectionResolution;
     public ReflectionRes highReflectionResolution;
     private ReflectionRes currentReflectionRes;
     public float updateReflectionEveryNSeconds = 1f;
     public WaterReflectionQuality reflectionQuality;
     private float updateTimer;
     private RenderTexture blendedTexture;
     public enum ReflectionRes
     {
         _16,
         _32,
         _64,
         _128,
         _256,
         _512,
         _1024,
         _2048
     };
     public enum WaterReflectionQuality
     {
         Low,
         Medium,
         High
     };
     private int resolution;
     private int GetReflectionRes(ReflectionRes res)
     {
         switch (res)
         {
             case ReflectionRes._16:
                 return 16;
             case ReflectionRes._32:
                 return 32;
             case ReflectionRes._64:
                 return 64;
             case ReflectionRes._128:
                 return 128;
             case ReflectionRes._256:
                 return 256;
             case ReflectionRes._512:
                 return 512;
             case ReflectionRes._1024:
                 return 1024;
             case ReflectionRes._2048:
                 return 2048;
             default:
                 return 16;
         }
     }
 
     private void Start()
     {
         
         if (!UnityEngine.QualitySettings.realtimeReflectionProbes)
         {
             reflectionQuality = WaterReflectionQuality.Low;
         }
         samplerProbe.mode = UnityEngine.Rendering.ReflectionProbeMode.Realtime;
         previousProbe.mode = UnityEngine.Rendering.ReflectionProbeMode.Realtime;
         switch (reflectionQuality)
         {
             case WaterReflectionQuality.Low:
                 currentReflectionRes = lowReflectionResolution;   
                 samplerProbe.cullingMask = 0;
                 previousProbe.cullingMask = 0;
                 outputProbe.mode = UnityEngine.Rendering.ReflectionProbeMode.Custom;
                 samplerProbe.mode = UnityEngine.Rendering.ReflectionProbeMode.Realtime;
                 previousProbe.mode = UnityEngine.Rendering.ReflectionProbeMode.Realtime;
                 samplerProbe.refreshMode = UnityEngine.Rendering.ReflectionProbeRefreshMode.OnAwake;
                 previousProbe.refreshMode = UnityEngine.Rendering.ReflectionProbeRefreshMode.OnAwake;
                 outputProbe.customBakedTexture = samplerProbe.texture;
                 break;
             case WaterReflectionQuality.Medium:
                 currentReflectionRes = mediumReflectionResolution;
                 samplerProbe.cullingMask = cullingMask;
                 previousProbe.cullingMask = cullingMask;
                 outputProbe.mode = UnityEngine.Rendering.ReflectionProbeMode.Custom;
                 samplerProbe.mode = UnityEngine.Rendering.ReflectionProbeMode.Realtime;
                 previousProbe.mode = UnityEngine.Rendering.ReflectionProbeMode.Realtime;
                 samplerProbe.refreshMode = UnityEngine.Rendering.ReflectionProbeRefreshMode.ViaScripting;
                 previousProbe.refreshMode = UnityEngine.Rendering.ReflectionProbeRefreshMode.ViaScripting;
                 break;
             case WaterReflectionQuality.High:
                 currentReflectionRes = highReflectionResolution;
                 samplerProbe.enabled = false;
                 previousProbe.enabled = false;
                 outputProbe.mode = UnityEngine.Rendering.ReflectionProbeMode.Realtime;
                 outputProbe.refreshMode = UnityEngine.Rendering.ReflectionProbeRefreshMode.EveryFrame;
                 break;
         }
         resolution = GetReflectionRes(currentReflectionRes);
         blendedTexture = new RenderTexture(resolution, resolution, 0, RenderTextureFormat.ARGBHalf);
         blendedTexture.isCubemap = true;
         blendedTexture.useMipMap = true;
 
         outputProbe.resolution = resolution;
         samplerProbe.resolution = resolution;
         previousProbe.resolution = resolution;
     }
 
     private void LateUpdate()
     {
         if(reflectionQuality == WaterReflectionQuality.Medium)
         {
             updateReflections();
         }
     }
     private void updateReflections()
     {
         updateTimer += Time.deltaTime;
         if (updateTimer >= updateReflectionEveryNSeconds)
         {
             updateTimer -= updateReflectionEveryNSeconds;
             //samplerProbe.RenderProbe();
             ReflectionProbe temp = previousProbe;
             previousProbe = samplerProbe;
             samplerProbe = temp;
             samplerProbe.RenderProbe();
         }
         //Debug.Log
         ReflectionProbe.BlendCubemap(previousProbe.texture, samplerProbe.texture, updateTimer / updateReflectionEveryNSeconds, blendedTexture);
         outputProbe.customBakedTexture = blendedTexture;
     }
 }
 
Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

151 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Reflection probes and legacy shaders? 0 Answers

Self-Illum Fresnel Reflective Bumped Specular issue 0 Answers

Textures different between editor and device 1 Answer

Standard shader looks incorrect on mobile (weird colors) 1 Answer

Disable fresnel in standard shader 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges