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.
@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