Custom Function Node HLSL Files in URP.

I followed the tutorial in this blog: Custom lighting in Shader Graph: Expanding your graphs in 2019
It worked fine with URP so far but when I tried to customize the code and use Light struct as input parameter for an internal function, that’s when things went haywire.

That’s the file in the Project:

That’s my function prototype:
void Lambert_half(Light light, out half3 Diffuse)

That’s the error:

Shader error in 'hidden/preview/CustomFunction_A10CE6A': unrecognized identifier 'Light' at Assets/Includes/DiffuseSpecular.hlsl(4) (on d3d11)

I tried to improvise and included some files which have the Light struct defined like:

#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

But it only got me more errors, mainly: redefinition of '_Time'

Any idea if this will work, I don’t even want to expose that function, just want to call it internally to avoid passing the light data through subgraphs for each functions.

Also would like to know how to get vertex data from those functions procedurally if possible.

Thanks.

1 Like

This is what ended up with, URP v11, plain and simple.

#ifndef CUSTOM_LIGHTING_INCLUDED
#define CUSTOM_LIGHTING_INCLUDED
/* IN(5): SpecColor(4), Smoothness(1), WPos(3), WNormal(3), WView(3) */
/* OUT(2): Diffuse(3), Specular(3) NdotL(1) for toon ramp: point,clamp) */
void CalculateLights_half(half4 SpecColor, half Smoothness, half3 WPos, half3 WNormal, half3 WView,
                          out half3 Diffuse, out half3 Specular, out half NdotL)
{          
    Diffuse = 0;
    Specular = 0;
    NdotL = 0;
    #ifndef SHADERGRAPH_PREVIEW
    Smoothness = exp2(10 * Smoothness + 1);  // WNormal = normalize(WNormal);    WView = SafeNormalize(WView);        
                                                        
    Light light = GetMainLight(); // Main Pixel Light
    half3 attenCol = light.color * light.distanceAttenuation * light.shadowAttenuation;
    Diffuse = LightingLambert(attenCol, light.direction, WNormal);     /* LAMBERT */        
    Specular = LightingSpecular(attenCol, light.direction, WNormal, WView, SpecColor, Smoothness); /*Blinn-Phong*/    
    NdotL = (saturate(dot(light.direction, WNormal)) + 1) / 2; // NdotL [-1..1] normalized [0..1] for toon ramp
    #endif
}

#endif

2 Likes