Skip to content

Instantly share code, notes, and snippets.

@simonbroggi
Last active February 23, 2026 10:13
Show Gist options
  • Select an option

  • Save simonbroggi/672b979ca37b01db752e0087b26315ab to your computer and use it in GitHub Desktop.

Select an option

Save simonbroggi/672b979ca37b01db752e0087b26315ab to your computer and use it in GitHub Desktop.
Unity Shadergraph custom function node for main light data
#ifndef MAINLIGHT_INCLUDED
#define MAINLIGHT_INCLUDED
void GetMainLightData_float(out half3 direction, out half3 color, out half distanceAttenuation, out half shadowAttenuation)
{
#ifdef SHADERGRAPH_PREVIEW
// In Shader Graph Preview we will assume a default light direction and white color
direction = half3(-0.3, -0.8, 0.6);
color = half3(1, 1, 1);
distanceAttenuation = 1.0;
shadowAttenuation = 1.0;
#else
// Universal Render Pipeline
#if defined(UNIVERSAL_LIGHTING_INCLUDED)
// GetMainLight defined in Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl
Light mainLight = GetMainLight();
direction = -mainLight.direction;
color = mainLight.color;
distanceAttenuation = mainLight.distanceAttenuation;
shadowAttenuation = mainLight.shadowAttenuation;
#elif defined(HD_LIGHTING_INCLUDED)
// ToDo: make it work for HDRP (check define above)
// Packages/com.unity.render-pipelines.high-definition/Runtime/Lighting/LightDefinition.cs.hlsl
// if (_DirectionalLightCount > 0)
// {
// DirectionalLightData light = _DirectionalLightDatas[0];
// lightDir = -light.forward.xyz;
// color = light.color;
// ......
#endif
#endif
}
#endif
@AnthonyD4X

AnthonyD4X commented Apr 16, 2021

Copy link
Copy Markdown

Hello I was woundering if anyone knows of a way to get the shadow attenuation and distance attenuation in HDRP. I have been looking everywhere for answers but cant find one. Here is the code I am using to get light data in hdrp

image

@mauricin

Copy link
Copy Markdown

Is there any way to get the main light intensity also?

@mkrdei

mkrdei commented Mar 10, 2022

Copy link
Copy Markdown

Hi, I'm getting this error. Do you have any idea why?
Shader error in 'hidden/preview/MainLightNodeCustomFunction_034dc8d0349e4af2abb4da742cca9b07': undeclared identifier 'MainLightNode_float' at line 155 (on d3d11)

@vladvsydorenko

Copy link
Copy Markdown

Hi, I'm getting this error. Do you have any idea why? Shader error in 'hidden/preview/MainLightNodeCustomFunction_034dc8d0349e4af2abb4da742cca9b07': undeclared identifier 'MainLightNode_float' at line 155 (on d3d11)

Be sure you set custom function node right.
_half postfix is important and it should match Precision in custom function node settings.

image

@ccnrussell

Copy link
Copy Markdown

Super useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment