Shader Graph - Light Direction

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.

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.

using UnityEngine;
using UnityEditor.ShaderGraph;
using System.Reflection;

// IMPORTANT:
// - tested with LWRP and Shader Graph 4.6.0-preview ONLY
// - likely to break in SG 5.x and beyond
// - for HDRP, add your own keyword to detect environment
[Title("Input", "Lighting", "Main Light Data")]
public class MainLightDataNode : CodeFunctionNode
{
    // shader code definition
    // handle shader graph editor environment where main light data isn't defined
    private static string shaderText = @"{
        #ifdef LIGHTWEIGHT_LIGHTING_INCLUDED
            Light mainLight = GetMainLight();
            Color = mainLight.color;
            Direction = mainLight.direction;
            Attenuation = mainLight.distanceAttenuation;
        #else
            Color = float3(1.0, 1.0, 1.0);
            Direction = float3(0.0, 1.0, 0.0);
            Attenuation = 1.0;
        #endif
    }";

    // disable own preview as no light data in shader graph editor
    public override bool hasPreview
    {
        get
        {
            return false;
        }
    }

    // declare node
    public MainLightDataNode()
    {
        name = "Main Light Data";
    }

    // reflection to shader function
    protected override MethodInfo GetFunctionToConvert()
    {
        return GetType().GetMethod(
            "GetMainLightData",
            BindingFlags.Static | BindingFlags.NonPublic
        );
    }

    // shader function and port definition
    private static string GetMainLightData(
        [Slot(0, Binding.None)] out Vector3 Color,
        [Slot(1, Binding.None)] out Vector3 Direction,
        [Slot(2, Binding.None)] out Vector1 Attenuation
    )
    {
        // define vector3
        Direction = Vector3.zero;
        Color = Vector3.zero;

        // actual shader code
        return shaderText;
    }
}

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…

1 Like

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!

You can simply add a property that reference to that build-in parameter, if your pipeline have that variable setup.
4238926--377248--lightposVector.PNG

8 Likes

I’m wondering if there is any work around for Shader Graph 5.x?

So you can access light direction by using _WorldSpaceLightPos0, what about attenuation, which is needed for the object to receive shadows?

2 Likes

Amazing!
It does the trick, thanks man!

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?

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?

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

1 Like

Is there any update with regards to getting light direction in shader graph for HDRP?

Really could do with this for shader graph in HDRP. I hope it appears soon.

Any update on this feature for HDRP?

2 Likes

More than a month later, no answer to when or how to access light position or direction.

I’d imagine there will be a stable way to access it in HDRP once HDRP is no longer in such an experimental state. But, HDRP is also a drastically more complicated renderer than LWRP and lighting something isn’t quite as simple as just getting a light if you want other elements in the scene to affect the lighting on this object too.

Hi, sorry for bumping the thread. Putting this because other people might need it. You can obtain the HDRP directional light with this HLSL:

GetLight.hlsl

#ifndef GETLIGHT_INCLUDED
#define GETLIGHT_INCLUDED

#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
#include "Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightDefinition.cs.hlsl"

void GetSun_float(out float3 lightDir, out float3 color)
{
#if SHADERGRAPH_PREVIEW
    lightDir = float3(0.707, 0.707, 0);
    color = 1;
#else
    if (_DirectionalLightCount > 0)
    {
        DirectionalLightData light = _DirectionalLightDatas[0];
        lightDir = -light.forward.xyz;
        color = light.color;
    }
    else
    {
        lightDir = float3(1, 0, 0);
        color = 0;
    }
#endif
}

#endif

Save it as GetLight.HLSL, and then you can obtain direction and color in custom function node, reference the file and use GetSun, set 2 vector outputs as in the function out parameters.

5166512--512606--Unity_2019-11-12_14-30-24.png

You can of course wrap the function node in a subgraph so that you can easily add it to other graphs:

5166512--512621--Unity_2019-11-12_14-40-03.png 5166512--512624--Unity_2019-11-12_14-39-38.png

In my case I needed direction and color (which includes intensity), but you can use a bunch of other light parameters, take a look at \Runtime\Lighting\LightDefinition.cs.hlsl.

8 Likes

I tried writing a custom node to get light direction when starting on this. Will investigate again with your code. Thanks.

UPDATE: I tried this but am getting the usual errors about ‘light’ being undeclared. Are you creating unexposed shader properties as well?

You know what, I wanted to move the hlsl to another folder so I can pack it and send it to you, and now I’m getting “undeclared identifier” bug too. I think this is a bug with the shader graph because it works in the subgraph but not in the graph that is using the subgraph. So annoying.

@pauldrummond ok, The files strangely kept rebuilding themselves even if I kept deleting them, and there were multiple copies of “GetLight.hlsl” which I think was the problem. Also the function node doesn’t seem to keep the file reference after you move it, you have to reassign it every time. I restarted Unity (a few times) and now it works again.

See the zip below. Try dropping the subgraph into your shader and hopefully it will work.

Edit: sorry about the 128000 default color in the graph, I’m using real lux values with exposure so the sun is 128000 (in shader preview only), you can change it in the hlsl.

5168234–512783–GetLight.zip (2.5 KB)

2 Likes