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âŚ
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!
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
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.
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.
You can of course wrap the function node in a subgraph so that you can easily add it to other graphs:
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.
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.