Search Unity

Shader Graph - Light Direction

Discussion in 'Shaders' started by Sark_Klimento, Jan 19, 2019.

  1. Sark_Klimento

    Sark_Klimento

    Joined:
    Jan 17, 2019
    Posts:
    9
    So i want to somehow get the _WorldSpaceLightPos0 value (basic in the default shaders) or Main Light Direction at least. Any ideas how can i get it ?

    I tried to use the custom shader graph node that gets the _WorldSpaceLightPos0 by using shader code, but it seems not working because the _WorldSpaceLightPos0 variable is not found.
     
  2. Ubawesome

    Ubawesome

    Joined:
    Jul 18, 2015
    Posts:
    6
    Found this on github. Custom node that's been working splendidly for Unity 2018.3.
    Would credit the original author, but I can't seem to find the post.

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEditor.ShaderGraph;
    4. using System.Reflection;
    5.  
    6. // IMPORTANT:
    7. // - tested with LWRP and Shader Graph 4.6.0-preview ONLY
    8. // - likely to break in SG 5.x and beyond
    9. // - for HDRP, add your own keyword to detect environment
    10. [Title("Input", "Lighting", "Main Light Data")]
    11. public class MainLightDataNode : CodeFunctionNode
    12. {
    13.     // shader code definition
    14.     // handle shader graph editor environment where main light data isn't defined
    15.     private static string shaderText = @"{
    16.        #ifdef LIGHTWEIGHT_LIGHTING_INCLUDED
    17.            Light mainLight = GetMainLight();
    18.            Color = mainLight.color;
    19.            Direction = mainLight.direction;
    20.            Attenuation = mainLight.distanceAttenuation;
    21.        #else
    22.            Color = float3(1.0, 1.0, 1.0);
    23.            Direction = float3(0.0, 1.0, 0.0);
    24.            Attenuation = 1.0;
    25.        #endif
    26.    }";
    27.  
    28.     // disable own preview as no light data in shader graph editor
    29.     public override bool hasPreview
    30.     {
    31.         get
    32.         {
    33.             return false;
    34.         }
    35.     }
    36.  
    37.     // declare node
    38.     public MainLightDataNode()
    39.     {
    40.         name = "Main Light Data";
    41.     }
    42.  
    43.     // reflection to shader function
    44.     protected override MethodInfo GetFunctionToConvert()
    45.     {
    46.         return GetType().GetMethod(
    47.             "GetMainLightData",
    48.             BindingFlags.Static | BindingFlags.NonPublic
    49.         );
    50.     }
    51.  
    52.     // shader function and port definition
    53.     private static string GetMainLightData(
    54.         [Slot(0, Binding.None)] out Vector3 Color,
    55.         [Slot(1, Binding.None)] out Vector3 Direction,
    56.         [Slot(2, Binding.None)] out Vector1 Attenuation
    57.     )
    58.     {
    59.         // define vector3
    60.         Direction = Vector3.zero;
    61.         Color = Vector3.zero;
    62.  
    63.         // actual shader code
    64.         return shaderText;
    65.     }
    66. }
     
  3. Sark_Klimento

    Sark_Klimento

    Joined:
    Jan 17, 2019
    Posts:
    9
    I already seen this, but it seems not work for me - the LIGHTWEIGHT_LIGHTING_INCLUDED is always false for some reason, so i get no info. Also when i call GetMainLight it prints that it cannot find any sequense....
     
  4. Ubawesome

    Ubawesome

    Joined:
    Jul 18, 2015
    Posts:
    6
    Hmm, that's pretty strange. This is a fixed version of the original custom node, so it might be that you were using the old one.
    Unfortunately, if your googling didn't turn up anything, mine probably won't either, sorry.
    Good luck!
     
  5. toothbrush

    toothbrush

    Joined:
    Mar 24, 2017
    Posts:
    2
    You can simply add a property that reference to that build-in parameter, if your pipeline have that variable setup.
    lightposVector.PNG
     
  6. Opaqueplacebo

    Opaqueplacebo

    Joined:
    Oct 15, 2018
    Posts:
    1
    I'm wondering if there is any work around for Shader Graph 5.x?
     
  7. windd123

    windd123

    Joined:
    Jun 7, 2018
    Posts:
    1
    So you can access light direction by using _WorldSpaceLightPos0, what about attenuation, which is needed for the object to receive shadows?
     
    HKesim and Gekigengar like this.
  8. Jarestrepogu

    Jarestrepogu

    Joined:
    Feb 18, 2018
    Posts:
    2
    Amazing!
    It does the trick, thanks man!
     
  9. WendelinReich

    WendelinReich

    Joined:
    Dec 22, 2011
    Posts:
    215
    Hmmm, can't get this trick to work on my system (2019.1 with latest LWRP). Could that be because I don't have that "variable setup", whatever that means?
     
  10. Jarestrepogu

    Jarestrepogu

    Joined:
    Feb 18, 2018
    Posts:
    2
    I did nothing strange, I just created a vector 4 property named "_WorldSpaceLightPos0" and set it to be hidden in the inspector. I used 2019.1 and the latest LWRP as well.
    By the way, I noticed the ShaderGraph window is very slow in the last version of LWRP, has anyone experienced the same?
     
  11. Arafuin

    Arafuin

    Joined:
    Jan 9, 2018
    Posts:
    1
    Any news about this? The vector 4 property named "_WorldSpaceLightPos0" does the trick, but it doesn't receive cast shadows. I dont understand how this shader graph doesn't have the most basic functions
     
  12. pbritton

    pbritton

    Joined:
    Nov 14, 2016
    Posts:
    12
    Is there any update with regards to getting light direction in shader graph for HDRP?
     
  13. pauldrummond

    pauldrummond

    Joined:
    Oct 30, 2014
    Posts:
    64
    Really could do with this for shader graph in HDRP. I hope it appears soon.
     
  14. pbritton

    pbritton

    Joined:
    Nov 14, 2016
    Posts:
    12
    Any update on this feature for HDRP?