1. We've introduced thread tags, search within a thread and similar thread search. Read more here.
    Dismiss Notice
  2. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    Dismiss Notice
  3. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  4. We've released our first Timeline Experimental Preview, our new tool for creating cutscenes and more! To check it out click here.
    Dismiss Notice
  5. Unity 5.5 is now released.
    Dismiss Notice
  6. Check out all the fixes for 5.5 in patch releases 1 & 2.
    Dismiss Notice
  7. Unity 5.6 beta is now available for download.
    Dismiss Notice

Accessing Reflection Probe cubemaps

Discussion in 'Shaders' started by chingwa, Mar 4, 2015.

  1. chingwa

    chingwa

    Joined:
    Dec 4, 2009
    Posts:
    2,092
    Has anyone here had success in accessing Unity 5 reflection probe textures for use in custom shaders? I had started implementing my own custom reflection probe system, however when it comes to blending between probes, and the box projection calculation I sort of derped out. Would love to be able to just access from the Unity probes...

    ...or is the blending and box projection handled on the receiving shader?
     
  2. Void24

    Void24

    Joined:
    Oct 15, 2013
    Posts:
    50
    Either way I would be happy to hear thoughts on accessing this information, even if it is applied as a post process.
     
    chingwa likes this.
  3. kebrus

    kebrus

    Joined:
    Oct 10, 2011
    Posts:
    388
    Create a shader from the editor and see the generated code, all functions are inside the built-in shaders and you can backtrack.

    I'm trying myself and it's pretty damn difficult because the code goes back and forth into functions from different files. I've managed to render something from the dynamic lightmaps that reacts to the skybox but it's all garbage data because i'm still missing some vital information that is calculated before that i'm still backtracking. I'm now considering stripping down the generated code instead of creating one from scratch and introduce the necessary code like I am doing now.

    Look into UnityGlobalIllumination function inside the file UnityGlobalIllumination.cginc

    I know it doesn't help much but I wanted to at least inform you that it's possible, it's just a lot of heavy lifting from convoluted code that some else wrote :p
     
  4. Jean-Moreno

    Jean-Moreno

    Joined:
    Jul 23, 2012
    Posts:
    311
    I think you can find the relevant data in the following files.
    In UnityShaderVariables.cginc:
    Code (shader):
    1. float4 unity_SpecCube0_BoxMax;
    2. float4 unity_SpecCube0_BoxMin;
    3. float4 unity_SpecCube0_ProbePosition;
    4. half4  unity_SpecCube0_HDR;
    5.  
    6. float4 unity_SpecCube1_BoxMax;
    7. float4 unity_SpecCube1_BoxMin;
    8. float4 unity_SpecCube1_ProbePosition;
    9. half4  unity_SpecCube1_HDR;
    I believe this is the raw reflection probe data passed by Unity to shaders.

    and then in UnityGlobalIllumination.cginc, look at the snippet starting at line 157 (in Unity5.0.0f4), which is contained in the following block:
    Code (shader):
    1. if (reflections)
    2. {
    3. ...
    4. }
    This is where the magic happens!

    Note: you can download the built-in shaders here.
     
    chrismarch likes this.
  5. chingwa

    chingwa

    Joined:
    Dec 4, 2009
    Posts:
    2,092
    Thanks for this Jean, I'm gonna have to start digging but it's good to have a starting place :)
     
  6. customphase

    customphase

    Joined:
    Aug 19, 2012
    Posts:
    54
    So, can anyone post the actual shader code for this? I tried doing this, but with absolutely no positive result - i only get pure blackness.
     
    Ben-BearFish likes this.
  7. Barkley

    Barkley

    Joined:
    Dec 1, 2012
    Posts:
    37
    I think you can access the cubemap using unity_SpecCube0. The following line worked for me:

    Code (CSharp):
    1. fixed3 reflectionCol = reflColor * UNITY_SAMPLE_TEXCUBE(unity_SpecCube0, reflectDir).rgb;
    2.  
     
    Kokokaka likes this.