Skip to content

Instantly share code, notes, and snippets.

@ciro-unity
Last active October 24, 2020 06:47
Show Gist options
  • Save ciro-unity/a55d73bcda93ca149cf7fd7e407f8812 to your computer and use it in GitHub Desktop.
Save ciro-unity/a55d73bcda93ca149cf7fd7e407f8812 to your computer and use it in GitHub Desktop.
A custom node for Unity's ShaderGraph to capture lighting and use it into the shader. Works as of July 2018, but the APIs might change!
//This API to create new nodes has been deprecated in early 2019.
//This means that if you are on a more recent version of Shader Graph and Universal Render Pipeline, you should use instead a pre-made node, which acts like a container that allows you to inject custom HLSL code into Shader Graph without the need to create a node from scratch.
//Please use the new node, not the C# API described here.
//
//You can find more details about that node in the Docs: https://docs.unity3d.com/Packages/com.unity.shadergraph@6.7/manual/Custom-Function-Node.html
//I also blogged about it here: https://connect.unity.com/p/adding-your-own-hlsl-code-to-shader-graph-the-custom-function-node
//
//If you are still on a very old version of Lightweight Render Pipeline (you shouldn't!!), you can still see the C# code for this custom node by rolling back to a previous version.
@catherineomega
Copy link

I have this: Attenuation = MainLightRealtimeShadow(shadowCoord);
But disconnecting the link from Attenuation doesn't seem to do anything at all, and my objects can't receive shadows. Ideas?

@AcelisWeaven
Copy link

If anyone uses Unity 2019.1+ and can't use this custom node anymore, here's a replacement posted by @toothbrush on the Unity forums. Shader preview won't work, sadly, but it'll work in-game.

@baukeJansen
Copy link

Updated version as custom node for Unity 2019.1 (tested on Shader graph 5.13)

Add a node of type Custom Function and point it to MainLightNode.hlsl.

Or alternatively, add it as a SubGraph.

Copy link

ghost commented Apr 28, 2019

@baukeJansen For some reason I can get this to work properly- the shader graph asset has a warning:

Output value 'ShadowPassVertex' is not completely initialized
Compiling Vertex program with UNITY_PASS_SHADOWCASTER INSTANCING_ON
Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING UNITY_ENABLE_DETAIL_NORMALMAP SHADER_API_DESKTOP UNITY_COLORSPACE_GAMMA UNITY_LIGHT_PROBE_PROXY_VOLUME UNITY_LIGHTMAP_FULL_HDR

And the properties do not show up in the inspector for the graph or material that uses the graph.
Have tried with both subgraph and custom function.

This is my setup:https://imgur.com/a/zNZuBAg

I am very new to shader graph so am finding this a bit of a struggle. Unity 2019.1 and Shader graph 5.13 with LWRP 5.13

Thanks.

@baukeJansen
Copy link

@ewencluley The warning message that you added shouldn't stop the shader from working.

Your setup also looks good although the preview of the MainLightNode is different for me.
If you open up the subgraph, does the custom node give any errors or warnings? (balloon with a # at the top right of the node)

Copy link

ghost commented Apr 29, 2019

@baukeJansen Seriously sorry, apparently I didn't realize there was a seperate Save Asset button in the shader graph editor and that Ctrl+S didnt actually save it. It is working now I have saved it. Thanks for the help anyway, good to have confirmation that I was almost there!

Copy link

ghost commented May 3, 2019

This does not work with,
Unity: 2019.1.1.f1
Shader Graph: 5.13.0
LWRP: 5.13.0

It casts a lot of errors, all stating "is inaccessible due to its protection level"

@baukeJansen
Copy link

baukeJansen commented May 3, 2019

@dog199200 I assume you're referring to the original C# code posted by ciro-unity? Creating custom nodes from c# code in this way is no longer possible. Instead we now have Custom Function nodes which allow for adding hlsl directly.

The update i posted Should work on shader graph 5.13.

Copy link

ghost commented May 3, 2019

@baukeJansen Thank you! I completed overlooked that.

@TerabyteTim
Copy link

Updated version as custom node for Unity 2019.1 (tested on Shader graph 5.13)

Add a node of type Custom Function and point it to MainLightNode.hlsl.

Or alternatively, add it as a SubGraph.

@baukeJansen is there a version of this that works for the HDRP?

@baukeJansen
Copy link

baukeJansen commented May 14, 2019

@TerabyteTim i haven't done anything with HDRP yet, so not from me. If there is a custom node function available in HDRP i would assume that the MainLightNode_float function still works.

It probably needs some adjustments in the imports thought (possibly removing them and just using '#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Lighting.hlsl" might work). If not you'll have to get the missing functions from the unity shaders source code.

Please post it if you manage to get it to work :)

@TerabyteTim
Copy link

After research it seems there's no way to get main light attenuation in the HDRP yet, so looks like we will have to wait for Unity to expose those settings to get any shaders working that rely on light data

@baukeJansen
Copy link

baukeJansen commented May 20, 2019

@earthtojens First off, looking for an answer to your problem I accidently ran into this page which is honestly more hidden that it should be: Custom node documentation (may come in handy).

Secondly, I'm assuming you didn't put 'Light' in the name field of the custom function as this would give a similar error.

Most likely one of the hlsl imports is referencing to the Light_float variable which is not included in your build for some reason. Looking through the imported shader source-code i couldn't find any reference to this variable thought. What happens if you create the variable manually at the start of the file float Light_Float; ?

Also restarting unity / re-saving the shader might be worth trying if you haven't already.

@earthtojens
Copy link

earthtojens commented May 21, 2019

@baukeJansen I am only able to get your updated MainLightNode.hlsl to work with Shader Graph 5.10 using Unity 2019.1.2f1 (Windows)

If I use Shader Graph 5.13 I get these errors when restarting Unity and the PBR Master material is reported as not being compat with the render pipeline. If I revert Shader Graph back to 5.10 and restart Unity, everything works again.

Library\PackageCache\com.unity.render-pipelines.lightweight@5.7.2\Editor\ShaderGraph\LightWeightUnlitSubShader.cs(14,39): error CS0535: 'LightWeightUnlitSubShader' does not implement interface member 'ISubShader.GetPreviewPassIndex()'

Library\PackageCache\com.unity.render-pipelines.lightweight@5.7.2\Editor\ShaderGraph\LightWeightPBRSubShader.cs(15,37): error CS0535: 'LightWeightPBRSubShader' does not implement interface member 'ISubShader.GetPreviewPassIndex()'

ArgumentException: Can not deserialize (UnityEditor.Rendering.LWRP.LightWeightPBRSubShader), type is invalid
UnityEditor.Graphing.SerializationHelper.Deserialize[T] (UnityEditor.Graphing.SerializationHelper+JSONSerializedElement item, System.Collections.Generic.Dictionary`2[TKey,TValue] remapper, System.Object[] constructorArgs) (at Library/PackageCache/com.unity.shadergraph@5.13.0/Editor/Data/Util/SerializationHelper.cs:103)
UnityEditor.Graphing.SerializationHelper.Deserialize[T] (System.Collections.Generic.IEnumerable`1[T] list, System.Collections.Generic.Dictionary`2[TKey,TValue] remapper, System.Object[] constructorArgs) (at Library/PackageCache/com.unity.shadergraph@5.13.0/Editor/Data/Util/SerializationHelper.cs:153)
UnityEditor.EditorApplication:Internal_CallUpdateFunctions()

Funny thing is there are no changes between Shader Graph 5.10 and 5.13 in the Unity Shader Graph Change log that reveal to me what may be the issue

Thoughts?

@baukeJansen
Copy link

baukeJansen commented May 22, 2019

@earthtojens I upgraded to unity 2019.1.3f1 using shader graph 5.13 and the shader seems to be working just fine for me. Honestly I can't make much out of the errors you're getting either.

The latest version of LightWeightUnlitSubShader.cs does implement the GetPreviewPassIndex function. Your package seems to refer to version 5.7.2 (although I'm not 100% if that's incorrect).

One thought i had was re-creating the shader while you're on 5.13 and removing the old one from your project. Other then that I'm pretty lost as well. You could try the shader graph section on the unity forums if these errors persist.

@Hellzbellz123
Copy link

Hellzbellz123 commented Jun 11, 2019

EDIT:: NO

(maybe not)
importing both lwrp and hdrp made the errors go away BUT im not sure how the shader will handle it

@interpol-kun
Copy link

I can't understand. I recreated the whole graph (and custom nodes), used the latest custom node from @baukeJansen, and nothing works. The problem came from the very start from the normal blend node.
image

No errors, no warnings, most of the previews are broken (except fresnel, of course). I've tested it with both 5.13 and 5.16 LWRP+SG, no difference.
image

@LawrieR
Copy link

LawrieR commented Mar 1, 2020

@earthtojens First off, looking for an answer to your problem I accidently ran into this page which is honestly more hidden that it should be: Custom node documentation (may come in handy).
Secondly, I'm assuming you didn't put 'Light' in the name field of the custom function as this would give a similar error.

Just make this a little clearer, if you call your function "Main Light" you'll get this error, where as "MainLight" is perfectly fine. I guess space in the custom function name is not allowed.

@ciro-unity
Copy link
Author

Hey everyone, as I reported in the gist itself, the API described here is really obsolete now. Please refer to this blog post for the explanation: https://connect.unity.com/p/adding-your-own-hlsl-code-to-shader-graph-the-custom-function-node

As such, I hid the code. You shouldn't use it if you're in a recent version of Universal Render Pipeline. Please use the Custom Function Node instead: https://docs.unity3d.com/Packages/com.unity.shadergraph@6.7/manual/Custom-Function-Node.html

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