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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question How to get the Main Light Direction in Shader Graph [Built-In RP]

Discussion in 'Shader Graph' started by kcastagnini, Jan 8, 2023.

  1. kcastagnini

    kcastagnini

    Joined:
    Dec 14, 2019
    Posts:
    60
    Hello,
    I'd like to start using the Shader Graph (version 12.1.8) with the BUILT-IN render pipeline in Unity 2021 LTS (I am using Unity 2021.3.16f).
    Unfortunately, the graph is missing some fundamental nodes like getting the direction of the main directional light (only present in shader Graph 13+ from Unity 2022+, https://docs.unity3d.com/Packages/com.unity.shadergraph@13.1/manual/Main-Light-Direction-Node.html).

    So I wasted the whole day trying to solve this issue and the best I could find was this CustomLighting.hlsl which it seems like to only work with scriptable render pipelines. (https://github.com/Unity-Technologi...ob/master/Assets/Includes/CustomLighting.hlsl)

    My question is, how can I access the _WorldSpaceLightPos0 built-in variable (containing the main light direction) from a node or a custom function node of my graph?
    And other variables too like attenuation and directional light color?

    Thanks!
     
    kitbashery and Monil like this.
  2. amal_nj

    amal_nj

    Joined:
    Jan 29, 2021
    Posts:
    1
    Did you manage to find a solution for this?
    From my limited knowledge about how lighting works in the built-in renderer pipeline, the concept of having a main light doesn't work here, because unlike URP, the built-in renderer pipeline supports having multiple directional lights, so there isn't really a main light. Meanwhile, the entirety of the URP code is all constructed around this idea of having only one directional light in your scene. So I'm trying to find an alternative to that.
     
  3. apilola

    apilola

    Joined:
    Jul 27, 2018
    Posts:
    17
    Hey kcastagnini, have you tried using _WorldSpaceLightPos0 as a parameter in your shader as a input?
     
  4. jzq740176597

    jzq740176597

    Joined:
    Jul 31, 2015
    Posts:
    17
    any one know this? I tried [Main light Directoin Node] in new-version (13.1) of Shader Graph BUT is ALWAYS Zero for built-in RP (default Forward Render Path).

    Btw, create a variable reference "_WorldSpaceLightPos0" not works too (compiler error : redefinition of that variable) That SO frustrated!
     
  5. Nest_g

    Nest_g

    Joined:
    Jun 18, 2019
    Posts:
    148
    Any news about this? why the function GetMainLight was deprecated without a replace if is important for SSS effects like skin shaders?.
     
  6. jakesee

    jakesee

    Joined:
    Jan 7, 2020
    Posts:
    31
    Hello, is the Main Light Direction node actually working? Any idea why the value is zero? Thanks.

    upload_2023-9-5_0-46-6.png
     
  7. a52

    a52

    Joined:
    Mar 15, 2018
    Posts:
    18
    I'm also having issues with this node (I'm using URP). Using a debug node that displays scalar values as a texture, I found that the main directional light was read as [-0.5, -0.5, 0] in the ShaderGraph editor, and [0, 0, -1] in game. Neither of those are the direction of my light in the scene!
     
  8. SamuelMalmene

    SamuelMalmene

    Joined:
    Aug 17, 2021
    Posts:
    1
    I've been dealing with something similar (Built-in RP) and here's what I managed to figure out, based on the source code for some built-in shaders.

    Most tutorials online for this are for URP it seems, and for URP there's the GetMainLight() method. If you want to get a direction for the sun/main light (assuming you have a custom function with an HLSL), it's:
    _WorldSpaceLightPos0.xyz

    Here's an hlsl sample I've been trying to get to work from a tutorial.

    Code (CSharp):
    1. void MainLight_float(float3 WorldPos,out float3 Direction,  out float3 Color, out float DistanceAtten, out float ShadowAtten)
    2. {
    3. #if SHADERGRAPH_PREVIEW
    4.   Direction = float3(0.5, 0.5, 0);
    5.    Color = 1;
    6.    DistanceAtten = 1;
    7.    ShadowAtten = 1;
    8. #else
    9. #if SHADOWS_SCREEN
    10.    float4 clipPos = TransformWorldToHClip(WorldPos);
    11.    float4 shadowCoord = ComputeScreenPos(clipPos);
    12. #else
    13.    float4 shadowCoord = TransformWorldToShadowCoord(WorldPos);
    14. #endif
    15. //this function doesn't quite work
    16.    Light mainLight = GetMainLight(shadowCoord);
    17.    //Direction = mainLight.position;
    18.    //Color = _MainLightColor.rgb;
    19.    //Direction = mainLight.direction;
    20. //get xyz on lightPos?
    21.    Direction = _WorldSpaceLightPos0;
    22.    //Color = mainLight.color;
    23.    //haven't figured out the color yet
    24.       Color = 1;
    25.    DistanceAtten = unity_ProbesOcclusion.x;
    26.    ShadowAtten = mainLight.shadowAttenuation;
    27. #endif
    28. }
    I'm a complete newcomer regarding shaders, but I hope this is of some use to someone.

    Edit:
    This seems to get the light position, I think further calculations are needed to get the direction from that
     
    Last edited: Oct 13, 2023