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

I use these 2 function to sample light map & light probe color in cg shader.

//static object light map color, will return black if lightmap not baked
DecodeLightmap(UNITY_SAMPLE_TEX2D(unity_Lightmap, unityLightMapUV));

//dynamic object light probe color, will return black if lightprobes not baked
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

#pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
#pragma multi_compile LIGHTPROBE_OFF LIGHTPROBE_ON

but I have to rely C# to turn on/off these keywords.

LightmapSettings.lightmaps
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?

UNITY_LIGHTMAP_ON
UNITY_LIGHTMAP_OFF
UNITY_LIGHTPROBE_ON
UNITY_LIGHTPROBE_OFF

thank you!

1 Like

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.

1 Like

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.

//just pick 1 and it will work
#pragma multi_compile __ LIGHTMAP_ON
#pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON
#pragma multi_compile_fwdbase

#if LIGHTMAP_ON
     //do the light map sampling
#endif
1 Like

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

could you help me ?

For lightprobe is it?

#pragma multi_compile LIGHTPROBE_OFF LIGHTPROBE_ON