Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@simonbroggi

This comment has been minimized.

Copy link
Owner Author

@simonbroggi simonbroggi commented Jul 6, 2020

Add the file to your Assets folder, then create a custom function node in Shader Graph like such:
Screenshot 2020-07-06 at 22 45 54

@baglayan

This comment has been minimized.

Copy link

@baglayan baglayan commented Oct 18, 2020

Hello, did you make any progress on getting this to work with HDRP?

@simonbroggi

This comment has been minimized.

Copy link
Owner Author

@simonbroggi simonbroggi commented Nov 2, 2020

Hello, did you make any progress on getting this to work with HDRP?

No, I haven't looked at it since. But it shouldn't be to hard, I think the comments get point in the right direction..
The problem might be that there is no main light in HDRP. You might need to just take the brightest directional light, or define your main light somewhere.

@stylerhall

This comment has been minimized.

Copy link

@stylerhall stylerhall commented Apr 6, 2021

Hello, did you make any progress on getting this to work with HDRP?

You can hack around the HDRP issue by creating a component to sample your Sun Light. Once you have your component just sample the properties you want and set global variables. Then, in your shaders create the global properties that the component can write to.

Look up the Shader.SetGlobal* methods for setting those global property types.

I have this setup in my project and it works like a charm.

@AnthonyD4X

This comment has been minimized.

Copy link

@AnthonyD4X AnthonyD4X commented Apr 16, 2021

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

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