Search Unity

Question Is _WorldSpaceLightPos0 defined in URP?

Discussion in 'Universal Render Pipeline' started by trzy, Nov 19, 2022.

  1. trzy

    trzy

    Joined:
    Jul 2, 2016
    Posts:
    128
    I'm having a lot of trouble understanding why Meta's Avatar shaders don't seem to work with the skybox disabled (and this happens only when deployed to the Quest headset, not when running in Unity on my PC). I'm using URP.

    I think I've narrowed down the problem to this bit of code:

    Code (csharp):
    1.  
    2. fixed3 AvatarShaderFragGetLightDir(float3 worldPos) {
    3.     // Directional light only used the lightDir as a normalized direction
    4.   // instead of a light position as well
    5. #ifndef USING_DIRECTIONAL_LIGHT
    6.   return normalize(UnityWorldSpaceLightDir(worldPos));
    7. #else
    8.   return _WorldSpaceLightPos0.xyz;
    9. #endif
    10. }
    11.  
    First of all, I have no idea how to check which preprocessor symbols are defined, either in Unity editor or in the build. Would be nice to know that.

    Secondly, I think the second code path is being taken when compiled for Quest. And when the skybox is disabled and replaced with a solid color (0,0,0,0), this apparently becomes invalid or 0 despite there being a directional light in the scene. All of Unity's included URP shaders continue working just fine!

    I believe that the error therefore is somewhere in the shader but I'm not entirely sure where. It's a massive piece of work and I'm... not entirely sure it's even URP? (Although it actually *does* compile and run.)

    Thank you :)
     
  2. wwWwwwW1

    wwWwwwW1

    Joined:
    Oct 31, 2021
    Posts:
    768
  3. trzy

    trzy

    Joined:
    Jul 2, 2016
    Posts:
    128
    Thanks, this actually looks familiar from other URP shaders I've seen. I'm noticing now though that this shader is using various .cginc includes and is therefore Cg. I thought URP required HLSL. Does this mean that the shader I'm using is in fact not a URP shader? I'm surprised it works at all if that's the case!
     
  4. trzy

    trzy

    Joined:
    Jul 2, 2016
    Posts:
    128
    I don't know if Unity is doing some translation behind the scenes, but these Cg shaders *are* working despite the project using URP. The ultimate problem I'm trying to solve is why, when deployed to the device, _WorldSpaceLightPos0 and _LightColor0 are not properly set anymore (they appear to be effectively 0) when the skybox is removed. It's as if the directional light in the scene is totally ignored. If these values reflected the directional light values, everything would work fine (and this is indeed how it works in the Editor).
     
  5. wwWwwwW1

    wwWwwwW1

    Joined:
    Oct 31, 2021
    Posts:
    768
    I know that you can only use Unlit (does not have lighting calculations) BIRP shaders in URP.
     
  6. wwWwwwW1

    wwWwwwW1

    Joined:
    Oct 31, 2021
    Posts:
    768
    Part of them do work (unlit part) but you cannot include both URP hlsl and BIRP cginc in the same shader. (redefinition of some variables)

    URP uses a "one pass all lights" design for lighting calculation, and it's different from BIRP's "one light per pass".

    I think you can try changing these lighting variables to URP's and include URP's hlsl files (or they won't be defined).