Problem building to WebGL with URP, Unlit Shader Graph and custom function

I’m trying to build a WebGL distribution of my game, but I am running into issues with shader compile. I have an Unlit Shader Graph, with a custom function node. The shader works fine on Windows/Linux and even in Editor when WebGL platform is selected, but I get a strange error and in the final WebGL build I get the purple material treatment.

The custom function’s purpose is to enable shadows for an Unlit Shader Graph.

I’m using Unity 2022.3.25f1

Here are the errors:

Shader error in 'Sub Graphs/Master': '_AdditionalLightsPosition': implicit array missing initial value at Gorillas/Library/PackageCache/com.unity.render-pipelines.universal@14.0.11/ShaderLibrary/Input.hlsl(151) (on d3d11)

Compiling Subshader: 0, Pass: <Unnamed Pass 0>, Vertex program with <no keywords>
Platform defines: SHADER_API_DESKTOP UNITY_ENABLE_DETAIL_NORMALMAP UNITY_ENABLE_REFLECTION_BUFFERS UNITY_LIGHTMAP_FULL_HDR UNITY_LIGHT_PROBE_PROXY_VOLUME UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BLENDING UNITY_SPECCUBE_BOX_PROJECTION UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS
Disabled keywords: SHADER_API_GLES30 UNITY_ASTC_NORMALMAP_ENCODING UNITY_COLORSPACE_GAMMA UNITY_FRAMEBUFFER_FETCH_AVAILABLE UNITY_HALF_PRECISION_FRAGMENT_SHADER_REGISTERS UNITY_HARDWARE_TIER1 UNITY_HARDWARE_TIER2 UNITY_HARDWARE_TIER3 UNITY_LIGHTMAP_DLDR_ENCODING UNITY_LIGHTMAP_RGBM_ENCODING UNITY_METAL_SHADOWS_USE_POINT_FILTERING UNITY_NO_DXT5nm UNITY_NO_FULL_STANDARD_SHADER UNITY_NO_SCREENSPACE_SHADOWS UNITY_PBS_USE_BRDF2 UNITY_PBS_USE_BRDF3 UNITY_PRETRANSFORM_TO_DISPLAY_ORIENTATION UNITY_UNIFIED_SHADER_PRECISION_MODEL UNITY_VIRTUAL_TEXTURING

Shader error in 'Sub Graphs/Master': undeclared identifier 'MAX_VISIBLE_LIGHT_COUNT_DESKTOP' at Gorillas/Library/PackageCache/com.unity.render-pipelines.universal@14.0.11/ShaderLibrary/Input.hlsl(151) (on d3d11)

Compiling Subshader: 0, Pass: <Unnamed Pass 0>, Vertex program with <no keywords>
Platform defines: SHADER_API_DESKTOP UNITY_ENABLE_DETAIL_NORMALMAP UNITY_ENABLE_REFLECTION_BUFFERS UNITY_LIGHTMAP_FULL_HDR UNITY_LIGHT_PROBE_PROXY_VOLUME UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BLENDING UNITY_SPECCUBE_BOX_PROJECTION UNITY_USE_DITHER_MASK_FOR_ALPHABLENDED_SHADOWS
Disabled keywords: SHADER_API_GLES30 UNITY_ASTC_NORMALMAP_ENCODING UNITY_COLORSPACE_GAMMA UNITY_FRAMEBUFFER_FETCH_AVAILABLE UNITY_HALF_PRECISION_FRAGMENT_SHADER_REGISTERS UNITY_HARDWARE_TIER1 UNITY_HARDWARE_TIER2 UNITY_HARDWARE_TIER3 UNITY_LIGHTMAP_DLDR_ENCODING UNITY_LIGHTMAP_RGBM_ENCODING UNITY_METAL_SHADOWS_USE_POINT_FILTERING UNITY_NO_DXT5nm UNITY_NO_FULL_STANDARD_SHADER UNITY_NO_SCREENSPACE_SHADOWS UNITY_PBS_USE_BRDF2 UNITY_PBS_USE_BRDF3 UNITY_PRETRANSFORM_TO_DISPLAY_ORIENTATION UNITY_UNIFIED_SHADER_PRECISION_MODEL UNITY_VIRTUAL_TEXTURING

Here’s the full custom function hlsl script:

#ifndef CUSTOM_LIGHTING_INCLUDED
#define CUSTOM_LIGHTING_INCLUDED

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

struct CustomLightingData
{
// position and orientation
float3 positionWS;
float4 shadowCoord;

// Surface attributes
float3 albedo;

float disableFog;
float fogFactor;
};

#ifndef SHADERGRAPH_PREVIEW
float3 CustomLightHandling(CustomLightingData d, Light light)
{
float3 shadowAtten;
#if SHADOWS_SCREEN
shadowAtten = SampleScreenSpaceShadowmap(shadowCoord);
#else
ShadowSamplingData shadowSamplingData = GetMainLightShadowSamplingData();
half shadowStrength = GetMainLightShadowStrength();
shadowAtten = SampleShadowmap(d.shadowCoord, TEXTURE2D_ARGS(_MainLightShadowmapTexture,
sampler_MainLightShadowmapTexture),
shadowSamplingData, shadowStrength, false);
#endif

float3 color = d.albedo * shadowAtten;
return color;
}
#endif

float3 CalculateCustomLighting(CustomLightingData d)
{
#ifdef SHADERGRAPH_PREVIEW
float3 color = 0;
#else
// Get the main light. Located in URP/ShaderLibrary/Lighting.hlsl
Light mainLight = GetMainLight(d.shadowCoord, d.positionWS, 1);

float3 color = 0;
// Shade the main light
color += CustomLightHandling(d, mainLight);
if (d.disableFog <= 0.0)
{
color = MixFog(color, d.fogFactor);
}
#endif
return color;
}

void CalculateCustomLighting_float(float3 Position, float3 Albedo, float DisableFog, out float3 Color)
{
CustomLightingData d;
d.albedo = Albedo;
d.positionWS = Position;

#ifdef SHADERGRAPH_PREVIEW
// In preview, there's no shadows or bakedGI
d.shadowCoord = 0;
d.fogFactor = 0;
d.disableFog = 1.0;
#else
// Calculate the main light shadow coord
// There are two types depending on if cascades are enabled
float4 positionCS = TransformWorldToHClip(Position);
// This returns 0 if fog is turned off
// It is not the same as the fog node in the shader graph
if (DisableFog > 0.0)
{
d.fogFactor = 0.0;
}
else
{
d.fogFactor = ComputeFogFactor(positionCS.z);
}
d.disableFog = DisableFog;
#if SHADOWS_SCREEN
d.shadowCoord = ComputeScreenPos(positionCS);
#else
d.shadowCoord = TransformWorldToShadowCoord(Position);
#endif
#endif

Color = CalculateCustomLighting(d);
}

#endif
1 Like