Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

[Solved]Possible to detect if lightmap/lightprobe is baked inside cg shader?

Discussion in 'Shaders' started by colin299, Jun 8, 2016.

  1. colin299

    colin299

    Joined:
    Sep 2, 2013
    Posts:
    176
    I use these 2 function to sample light map & light probe color in cg shader.

    Code (CSharp):
    1. //static object light map color, will return black if lightmap not baked
    2. DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, unityLightMapUV));
    3.  
    4. //dynamic object light probe color, will return black if lightprobes not baked
    5. ShadeSH9(worldNormal);
    when light map & light probes were baked, it works perfectly.
    but the following 2 cases will result in black color
    1. light probes not baked(not exist) -> dynamic objects will be black
    2. light map & light probes not baked -> everything will be black

    Question is:
    Is there a way to detect if light map and light probe is baked(not rely on C#)?

    I tried multi compile the shader to support these cases
    Code (CSharp):
    1. #pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
    2. #pragma multi_compile LIGHTPROBE_OFF LIGHTPROBE_ON
    but I have to rely C# to turn on/off these keywords.
    Code (CSharp):
    1. LightmapSettings.lightmaps
    2. LightmapSettings.lightProbes
    I want the shader to detect whether light map & light probes is baked, by the shader itself.
    Does Unity provide any keyword like these?
    Code (CSharp):
    1. UNITY_LIGHTMAP_ON
    2. UNITY_LIGHTMAP_OFF
    3. UNITY_LIGHTPROBE_ON
    4. UNITY_LIGHTPROBE_OFF
    thank you!
     
    zwcloud likes this.
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    The almost completely undocumented #pragma multi_compile_fwd**** lines include a bunch of keywords for lightmaps and I think 5.4 added some addition ones for light probes.

    There's no simple list of the keywords they include, but you can go through the cginc files of the built-in shader source files, or use the frame debugger to see which keywords are in use on materials in the scene. Note there isn't do much a list of what the multi compile command expand to add you can search the files for all of the #ifdef / #ifndef / #if defined() lines.

    http://docs.unity3d.com/Manual/SL-MultipleProgramVariants.html
     
    colin299 likes this.
  3. colin299

    colin299

    Joined:
    Sep 2, 2013
    Posts:
    176
    Thankyou.
    I checked from FrameDebugger and find that standard shader use the keyword "LIGHTMAP_ON".
    I checked from some cginc files, some of them also using #ifdef LIGHTMAP_ON.

    LIGHTMAP_ON is a keyword controlled by Unity,
    so we just need to multi compile a shader for it.
    Code (CSharp):
    1. //just pick 1 and it will work
    2. #pragma multi_compile __ LIGHTMAP_ON
    3. #pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
    4. #pragma multi_compile_fwdbase
    5.  
    6. #if LIGHTMAP_ON
    7.      //do the light map sampling
    8. #endif
     
    chrismarch likes this.
  4. lvan_chen

    lvan_chen

    Joined:
    Jun 20, 2017
    Posts:
    2
    Hi , I have the same issue with you , but I only need to sample light probe, So I tried it according to the above method,
    and when light probes not baked(not exist) ,it works fine ,but when light probes is baked ,ShadeSH9() failed,here is my code ,could you help me see why? I am very grateful !

    #pragma multi_compile __ LIGHTMAP_ON
    // #pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
    #pragma multi_compile_fwdbase

    #ifdef LIGHTMAP_ON
    fixed3 SHLighting : COLOR1;
    // half2 uvLM : TEXCOORD6;
    #endif

    #ifdef LIGHTMAP_ON
    o.SHLighting= ShadeSH9(float4(o.worldNormal,1)) ;
    // o.uvLM = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
    #endif

    #ifdef LIGHTMAP_ON
    albedo *= i.SHLighting;
    // fixed3 lm = DecodeLightmap (UNITY_SAMPLE_TEX2D(unity_Lightmap, i.uvLM.xy));
    // albedo*=lm;
    #endif
     
  5. lvan_chen

    lvan_chen

    Joined:
    Jun 20, 2017
    Posts:
    2
    could you help me ?
     
  6. unity_NpcxBF-EduiEdA

    unity_NpcxBF-EduiEdA

    Joined:
    Apr 9, 2019
    Posts:
    10
    For lightprobe is it?

    #pragma multi_compile LIGHTPROBE_OFF LIGHTPROBE_ON