- Home /
How to read cubemap from realtime reflection probe
I'm using an ocean rendering system which accepts an input cubemap for reflections. My scenes are generated dynamically. I have a reflection probe in my scene which is set to "Realtime > Via Scripting > Individual Faces".
Even after calling _probe.RenderProbe(), both _probe.bakedTexture and _probe.customBakedTexture are null and the static ReflectionProbe.defaultTexture variable returns a "UnityBlackCube" Cubemap. How do I access the internal cubemap texture generated by this render probe?
Relevant docs: https://docs.unity3d.com/ScriptReference/ReflectionProbe.html
Answer by Remy_Unity · Feb 13, 2018 at 09:13 AM
And what about _probe.texture ?
https://docs.unity3d.com/ScriptReference/ReflectionProbe-texture.html
Hey thanks! Looks like _probe.texture should indeed contain the dynamically rendered cubemap texture, but this still isn't working for me. Attempting to assign _probe.texture to a UnityEngine.Cubemap variable results in a "cannot convert source type" error, while attempting to cast the .texture to (Cubemap) results in a similar failure. I have successfully used statically baked EXR cubemaps generated by reflection probes for cubemap reflections, but can't find the way to accomplish this at runtime. Should I give up and use Camera.RenderToCubemap, or is there a way to use the texture generated by the probe I am already maintaining in my scene?
Ok, I tried replicating what you are doing by rendering a probe via script, with individual faces, and set back this probe texture to an other one.
Take a look at this doc page : https://docs.unity3d.com/ScriptReference/Rendering.ReflectionProbeTimeSlicing$$anonymous$$ode.IndividualFaces.html
It mentions that updating the cubemap will take 14 frames. What you need to do in your case is to call _probe.RenderProbe() 14 times before trying to get _probe.texture. This will allow all faces of the probe + mips to get correctly rendered and available in _probe.texture.
Best thing to do is to use the render ID returned by _probe.RenderProbe() to check if it is finished rendering with _probe.IsRinishedRendering(int renderID), or set your probe to render in one frame.
Here's a the script I made for example:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
[RequireComponent(typeof(ReflectionProbe))]
public class ReflectionCopy : $$anonymous$$onoBehaviour
{
ReflectionProbe reflectionProbe;
[SerializeField] ReflectionProbe target;
int renderID;
// Use this for initialization
void Start ()
{
if (target == null)
{
enabled = false;
return;
}
reflectionProbe = GetComponent<ReflectionProbe>();
reflectionProbe.refresh$$anonymous$$ode = ReflectionProbeRefresh$$anonymous$$ode.ViaScripting;
reflectionProbe.timeSlicing$$anonymous$$ode = ReflectionProbeTimeSlicing$$anonymous$$ode.IndividualFaces;
Destroy( reflectionProbe.texture );
target.mode = ReflectionProbe$$anonymous$$ode.Custom;
renderID = reflectionProbe.RenderProbe();
}
// Update is called once per frame
void Update ()
{
if (reflectionProbe.IsFinishedRendering(renderID))
{
target.customBakedTexture = reflectionProbe.texture;
// Render Again
renderID = reflectionProbe.RenderProbe();
}
}
}
Thanks Remy! I appreciate you testing this. The real challenge is in assigning the final probe texture to a cubemap. I was chatting with a few other devs yesterday, and our consensus was that the only apparent way to accomplish this would be manually, via a series of SetPixels calls. If this is indeed the case, I am probably better off using a Camera.RenderToCubemap approach even given the double performance hit and apparent decrease in quality: https://forum.unity.com/threads/quality-of-mipmaps-by-rendertocubemap-are-not-consistent-with-that-of-default-reflection-probe.436271/
Your answer
Follow this Question
Related Questions
Writing to RWTexture3D using a pixel shader without using Graphics.Blit 0 Answers
Irregular shape window to a different background world? 0 Answers
Render object ID to texture 2 Answers
Low resolution RenderTexture is jitter 0 Answers
RenderTexture captured from camera with post effects only shows the emission pass. 1 Answer