Weird error behaviour with CustomFunction node

I decided to do a CustomFunction node “hello world” in order to get familiar with it, but I’ve run into a weird issue.

I’m trying to start with a function that gets the direction of the main light in the LWRP. I do this by returning GetMainLight(), which is a function in LWRP’s “Lighting.hlsl”. So I created this HLSL file:

void GetMainLightDir_float(out float3 lightDir)
{
    lightDir = GetMainLight().direction;
}

And then I made this graph to test it out:

As you may notice, it gives me an error saying “undeclared identifier “GetMainLight””. I get that same error in the console too. However… the weird thing is that it seems to work perfectly well even if there’s an error:

Now I have many questions:

  • Why does it work and/or why is there an error?
  • Why does it want my function to be named “GetMainLightDir_float” even though I said I wanted it to be named just “GetMainLightDir”?
  • If I modify my .hlsl file with this include (where the GetMainLight() function is) in order to try to solve the error:
#include "Packages/com.unity.render-pipelines.lightweight/ShaderLibrary/Lighting.hlsl"

void GetMainLightDir_float(out float3 lightDir)
{
    lightDir = GetMainLight().direction;
}

…it now gives me a new error: “redefinition of _Time”, which I do not understand

4447279--407524--TIM截图20190419131053.png
this now not an error ♪(^∀^●)ノ

1 Like

Custom Function nodes are currently broken when you use more than one referencing the same HLSL file, it will throw the “redefinition of” errors. There’s a PR open that apparently fixes this: https://github.com/Unity-Technologies/ScriptableRenderPipeline/pull/3349

I haven’t tried this yet, but it may work to put your Custom Function node into a Sub Graph, then use that in your main graph instead.

Edit: Doesn’t work. Looks like every Custom Function needs to use a separate HLSL file until this is fixed, or use the string type and paste the function.