Search Unity

  1. Engage, network and learn at Unite Austin 2017, Oct 3 - 5. Get your ticket today!
    Dismiss Notice
  2. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  3. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  4. Unity 2017.1 is now released.
    Dismiss Notice
  5. Help us improve the editor usability and artist workflows. Join our discussion to provide your feedback.
    Dismiss Notice
  6. Unity 2017.2 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,332
    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:
    411
    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:
    337
    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,332
    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:
    70
    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:
    38
    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.