GridVids
Sign in with YouTube

URP Lighting Model - Custom Lighting Models - Episode 20

Ben Cloward Jun 19, 2025 15:00
Create an account to hide videos, mark them watched, or save them to Don't Miss.
Don't Miss
Create an account to hide videos, mark them watched, or save them to Don't Miss.
Mark watched
Create an account to hide videos, mark them watched, or save them to Don't Miss.
Hide
Open in popup Open on YouTube
Share video

Share video

Copy the GridVids link or iframe code.

Video link
Iframe code
Iframe content

In this video, I show how to re-create the URP lighting model using Shader Graph. Last week we created just the specular component of the lighting, and this week we fill in the rest so you have the complete URP lighting model. Here's the code for the Additional Lights custom function node: --------------------------------------- Diffuse = MainDiffuse; Specular = MainSpecular; Color = MainColor * (MainDiffuse + MainSpecular); #ifndef SHADERGRAPH_PREVIEW uint pixelLightCount = GetAdditionalLightsCount(); half Roughness = pow(1 - Smoothness, 2); half Roughness2 = Roughness * Roughness; half Roughness2Minus1 = Roughness2 - 1; half normalizationTerm = (Roughness * half(4.0) + half(2.0)); #if USE_CLUSTER_LIGHT_LOOP // for Foward+ LIGHT_LOOP_BEGIN macro uses inputData.normalizedScreenSpaceUV and inputData.positionWS InputData inputData = (InputData)0; inputData.normalizedScreenSpaceUV = ScreenPosition; inputData.positionWS = WorldPosition; #endif LIGHT_LOOP_BEGIN(pixelLightCount) // Convert the pixel light index to the light data index #if !USE_CLUSTER_LIGHT_LOOP lightIndex = GetPerObjectLightIndex(lightIndex); #endif // Call the URP additional light algorithm. This will not calculate shadows, since we don't pass a shadow mask value Light light = GetAdditionalPerObjectLight(lightIndex, WorldPosition); // Manually set the shadow attenuation by calculating realtime shadows light.shadowAttenuation = AdditionalLightRealtimeShadow(lightIndex, WorldPosition, light.direction); #if defined(_LIGHT_COOKIES) float3 cookieColor = SampleAdditionalLightCookie(lightIndex, WorldPosition); light.color *= cookieColor; #endif float NdotL = saturate(dot(WorldNormal, light.direction)); float atten = light.distanceAttenuation * light.shadowAttenuation; float thisDiffuse = NdotL * atten; //DirectBRDFSpecular float3 lightDirectionWSFloat3 = float3(light.direction); float3 halfDir = SafeNormalize(lightDirectionWSFloat3 + float3(WorldView)); float NoH = saturate(dot(float3(WorldNormal), halfDir)); half LoH = half(saturate(dot(lightDirectionWSFloat3, halfDir))); float d = NoH * NoH * Roughness2Minus1 + 1.00001f; half LoH2 = LoH * LoH; half spec = Roughness2 / ((d * d) * max(0.1h, LoH2) * normalizationTerm); #if REAL_IS_HALF spec = spec - HALF_MIN; spec = clamp(spec, 0.0, 1000.0); #endif float3 thisSpecular = spec * Reflectance * NdotL * atten; Diffuse += thisDiffuse; Specular += thisSpecular; Color += light.color * (thisDiffuse + thisSpecular); LIGHT_LOOP_END float total = Diffuse + dot(Specular, float3(0.333, 0.333, 0.333)); Color = total &&&&= 0 ? MainColor : Color / total; #endif ---------------------------------------------------- Be sure to replace the &&&& with a less than symbol. YouTube doesn't allow that symbol in descriptions. Here's the set of Keywords that need to go into the Lit subgraph: _CLUSTER_LIGHT_LOOP _LIGHT_COOKIES _REFLECTION_PROBE_BLENDING _REFLECTION_PROBE_BOX_PROJECTION _REFLECTION_PROBE_ATLAS _SHADOWS_SOFT _ADDITIONAL_LIGHT_SHADOWS _MAIN_LIGHT_SHADOWS_CASCADE Here's last week's video on URP Specular: https://www.youtube.com/watch?v=2ABQj7qDT4E Here's the playlist for the whole series: https://www.youtube.com/playlist?list=PL78XDi0TS4lGnGa7L2X4o3UV-XYZEKNwj Shader Book Recommendations https://www.bencloward.com/resources_books.shtml ------------------------------ Theme Music Peace in the Circuitry - Glitch Hop http://teknoaxe.com/Link_Code_3.php?q=1526 Background Music Speo - The Little Things https://www.youtube.com/watch?v=kvCYuyyLgC0 #UnrealEngine #shadergraph #Unity
Education
#Unity #shader #material #material editor #game development #real-time #tutorial #training #graphics #3d #GPU #tech art #computer graphics #fundamentals #shader graph #URP #Universal Render Pipeline #lighting #reflectance #ambient #diffuse #specular