Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Extract dominant light direction from lightprobes?

Discussion in 'Shaders' started by Bas-Smit, Dec 18, 2014.

  1. Bas-Smit

    Bas-Smit

    Joined:
    Dec 23, 2012
    Posts:
    274
    Im trying to find a way to do specular highlights using lightprobes. This article describes a way to extract the dominant light direction using this equation:



    I am struggling a bit to implement it using the variables exposed by Unity:

    // SH lighting environment
    float4 unity_SHAr;
    float4 unity_SHAg;
    float4 unity_SHAb;
    float4 unity_SHBr;
    float4 unity_SHBg;
    float4 unity_SHBb;
    float4 unity_SHC;

    I am not entirely sure how for instance Le3r corresponds to the variables above. Am I correct to assume part of the third band is packed in the w values of bands one and two?

    To complicate things further, I have been looking at the example code provided in the article linked above, but the implementation there does not seem to correspond to the equation from the same article:

    vec3 lightDirR = normalize(vec3(-sh1_r.x, -sh0_r.y, sh0_r.z));
    vec3 lightDirG = normalize(vec3(-sh1_g.x, -sh0_g.y, sh0_g.z));
    vec3 lightDirB = normalize(vec3(-sh1_b.x, -sh0_b.y, sh0_b.z));
    vec3 lightDir = normalize( 0.3*lightDirR + 0.59*lightDirG + 0.11*lightDirB );

    It seems that the third band is not used at all, and naively translating this code to CG using the variables exposed by Unity does not yield correct results:

    float3 lightDirR = normalize(float3(-unity_SHBr.x, -unity_SHAr.y, unity_SHAr.z));
    float3 lightDirG = normalize(float3(-unity_SHBg.x, -unity_SHAg.y, unity_SHAg.z));
    float3 lightDirB = normalize(float3(-unity_SHBb.x, -unity_SHAb.y, unity_SHAb.z));
    float3 lightDir = normalize(0.3*lightDirR + 0.59*lightDirG + 0.11*lightDirB);

    I have been seeing good results with the following, but my understanding of sh is not deep enough to be confident this is a robust approach:

    normalize((0.3*unity_SHAr + 0.59*unity_SHAg + 0.11*unity_SHAb).rgb)


    Thanks, Bas