I ama writing this custom hlsl shader to calculate the contribution of additional lights on Unity URP.
#ifndef CUSTOM_LIGHT_SHADOWS_INCLUDED
#define CUSTOM_LIGHT_SHADOWS_INCLUDED
#include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/RealtimeLights.hlsl”
#include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl”
#include “Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl”
// Enable additional lights in the vertex or fragment shader stage
#pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
// Enable support for clustered lighting (used in Forward+)
#pragma multi_compile _ _CLUSTER_LIGHT_LOOP
// Enable shadow attenuation for additional lights
#pragma multi_compile_fragment _ _ADDITIONAL_LIGHT_SHADOWS
// -----------------------------------------------------------------------------
// Custom Lighting and Shadow Utility Functions
// -----------------------------------------------------------------------------
// Calculates a basic lighting contribution from a light source,
// factoring in color and shadow attenuation.
float EvaluateLightShadowContribution(Light light)
{
// This returns the red channel of the light’s color * shadow factor.
// In real usage, you’d usually apply all color channels.
return (light.color * light.shadowAttenuation).r;
}
float EvaluateAdditionalLightContribution(float3 normalWS, Light light)
{
float NdotL = dot(normalWS, normalize(light.direction));
NdotL = (NdotL + 1) * 0.5;
return saturate(NdotL) * light.color * light.distanceAttenuation * light.shadowAttenuation;
}
// Computes custom lighting contribution from a specific additional light
// at world-space position positionWS, using the light at lightIndex.
//
// This function outputs a lighting value (as a float), which can be used
// for effects like custom shadow coloring, inverse shadowing, etc.
//
// Parameters:
// - positionWS: World-space position of the shaded point
// - lightIndex: Index of the additional light to query
// - lighting: Output float holding the computed light contribution
void ComputeAdditionalLightContribution_float( float3 positionWS, const int lightIndex, out float lighting)
{
// Sample the additional light at the given index
Light light = GetAdditionalLight(lightIndex, positionWS, half4(1, 1, 1, 1));
// Output a simple inverted lighting value based on shadow attenuation
// (You can modify this to suit your needs — e.g., multiply by color, etc.)
lighting = 1.0 - EvaluateLightShadowContribution(light);
}
void ComputeAdditionalLightContributionFull_float( float3 positionWS, float3 normalWS, const int lightIndex, out float shadowmask, out float lighting)
{
#if SHADERGRAPH_PREVIEW
lighting = 0;
shadowmask = 0;
#else
// Sample the additional light at the given index
Light light = GetAdditionalLight(lightIndex, positionWS, half4(1, 1, 1, 1));
// Output a simple inverted lighting value based on shadow attenuation
// (You can modify this to suit your needs — e.g., multiply by color, etc.)
shadowmask = 1.0 - EvaluateLightShadowContribution(light);
lighting = EvaluateAdditionalLightContribution(normalWS, light);
#endif
}
#endif // CUSTOM_LIGHT_SHADOWS_INCLUDED
however whan i use a custom function in shaderGraph and call ComputeAdditionalLightContributionFull. Although i get both the lighting and shadowmask results without bugs when i try to connect them to any other component in the graph (for example a simple Multiply) i get an error saying
_AdditionalLightsPosition implicit array missing initial value
what can this be? Am im issing something?