How can I get the lighting information from a skybox?

840 Views Asked by At

I am freshman to the Unity3d shader. I am writing a custome unlit shader now. I know I can use _WorldSpaceLightPos0.xyz and _LightColor0.rgb to get the information of a directional light source.

However, if I have a skybox instead of a light source, how can I get the light information? The radiance(Li) is coming from my skybox now. How can I get the value of them and compute Li*my_custome_brdf?

Thanks

1

There are 1 best solutions below

0
On BEST ANSWER

The skybox does not have a light position since it is omnidirectional, so you can't calculate proper directional lighting from it (only ambient). However, if you just want the skybox color in any direction, there's unity_SpecCube0, unity_SpecCube1, etc. for reflection probes listed by weight. You can use something like this:

inline half3 SurfaceReflection(half3 viewDir, half3 worldNormal, half roughness) {

    half3 worldRefl = reflect(-viewDir, worldNormal);
    half r = roughness * 1.7 - 0.7 * roughness; // Some magic for calculating MIP level
    float4 reflData = UNITY_SAMPLE_TEXCUBE_LOD(
        unity_SpecCube0, worldRefl, r * 6
    );

    return DecodeHDR (reflData, unity_SpecCube0_HDR);
}

If your reflection probe is different from your skybox, just define the skybox as a texCUBE uniform in your shader instead and use that. If you don't want to have to set it for each material using this shader, just declare it in the CGPROGRAM block as a uniform, but not as a ShaderLab property - it will then be considered a shader global. You can then set it through script using Shader.SetGlobalTexture. Check out the documentation for this here:

https://docs.unity3d.com/ScriptReference/Shader.SetGlobalTexture.html