Search Unity

Question Static objects + baked mixed directional light + shader graph = black

Discussion in 'Universal Render Pipeline' started by SgerbwdGwyn, Jan 2, 2021.

  1. SgerbwdGwyn

    SgerbwdGwyn

    Joined:
    Dec 2, 2013
    Posts:
    25
    I'm using the shader graph to do a cross-hatching effect and in short I'm doing my own lighting calculations.
    It all works with realtime lights and mixed directional lights, but I want to bake this lighting for my static environment, which uses this cross hatching shader.
    upload_2021-1-2_21-58-22.png

    When the lighting baking is set to "baked indirect", the scene works! Shadows aren't baked, however, which obviously means redrawing them every frame. I want to bake the static object's shadows as is standard for a big scene, so I'm looking the the subtractive option in the lighting settings as it seems to do exactly that - bake static objects's lighting while allowing realtime or mixed lights to influence them + light other dynamic objects.

    With a shader using a PBR Graph master output and a typical setup, it all works as expected. With my shader, the main light is completely black when backed using Subtractive lighting, but only to static objects:
    upload_2021-1-2_21-58-37.png
    upload_2021-1-2_21-58-52.png

    After some testing, I've found that the issue is that on static objects, my code for grabbing the main light's properties is returning 0 for most things:

    Code (CSharp):
    1. void MainLight_half(float3 WorldPos, out half3 Direction, out half3 Color, out half DistanceAtten, out half ShadowAtten)
    2. {
    3. #if SHADERGRAPH_PREVIEW
    4.     Direction = half3(0.5, 0.5, 0);
    5.     Color = 1;
    6.     DistanceAtten = 1;
    7.     ShadowAtten = 1;
    8. #else
    9. #if SHADOWS_SCREEN
    10.     half4 clipPos = TransformWorldToHClip(WorldPos);
    11.     half4 shadowCoord = ComputeScreenPos(clipPos);
    12. #else
    13.     half4 shadowCoord = TransformWorldToShadowCoord(WorldPos);
    14. #endif
    15.  
    16.     Light mainLight = GetMainLight(shadowCoord);
    17.     Direction = mainLight.direction;
    18.     Color = mainLight.color;
    19.     DistanceAtten = mainLight.distanceAttenuation;
    20.  
    21. #if !defined(_MAIN_LIGHT_SHADOWS) || defined(_RECEIVE_SHADOWS_OFF)
    22.     ShadowAtten = 1.0h;
    23. #endif
    24. #if SHADOWS_SCREEN
    25.     ShadowAtten = SampleScreenSpaceShadowmap(shadowCoord);
    26. #else
    27.     ShadowSamplingData shadowSamplingData = GetMainLightShadowSamplingData();
    28.     half shadowStrength = GetMainLightShadowStrength();
    29.     ShadowAtten = SampleShadowmap(shadowCoord, TEXTURE2D_ARGS(_MainLightShadowmapTexture,
    30.         sampler_MainLightShadowmapTexture),
    31.         shadowSamplingData, shadowStrength, false);
    32. #endif
    33.  
    34. #endif
    35. }
    Additionally, I am sometimes getting the "invalid subscript 'shadowCoord'" error, which appears to be related

    How should I be accessing lighting information on a static object for a mixed directional light baked with Subtractive Lighting?

    Edit: Updated to 2020.1.0b1, now trying to use the Shadowmask lighting method, and the lighting is generally working, but I'm unable to access the static baked shadows via the code above nor by the GI node in the shader graph.
    upload_2021-1-3_16-22-59.png
     
    Last edited: Jan 3, 2021