Harden Normals
In this sample screen-space derivatives (
ddx,ddy) and a cross product are used to calculate a new surface normal. By blending between the geometry normal and the derived normal, we can “harden” the appearance of shading. Might be useful for toon lighting, stylized rendering, or normal flattening.
float3 normal_hardened = normalize(cross(ddy(positionWS), ddx(positionWS)));
Visual demo¶
This shader demonstrates how modifying normals with screen-space derivatives sharpens lighting transitions across surfaces. Especially noticeable on curved or smooth geometry.
Node logic (engine-agnostic)¶
Shader "DecompiledArt/Derivatives/HardenNormals/HardenNormals"
{
Properties
{
_NormalsFactor("NormalsFactor", Range(0.0, 1.0)) = 0.0
}
SubShader
{
Tags { "RenderType"="Opaque" "RenderPipeline" = "UniversalPipeline" }
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
float3 normalOS : NORMAL;
};
struct Varyings
{
float4 positionHCS : SV_POSITION;
float3 positionWS: TEXCOORD0;
float3 normalWS : NORMAL;
};
CBUFFER_START(UnityPerMaterial)
half _NormalsFactor;
CBUFFER_END
Varyings vert (Attributes IN)
{
Varyings OUT;
OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
OUT.positionWS = TransformObjectToWorld(IN.positionOS.xyz);
OUT.normalWS = TransformObjectToWorldNormal(IN.normalOS);
return OUT;
}
half4 frag(Varyings IN) : SV_Target
{
half3 out_cross = normalize(cross(ddy(IN.positionWS), ddx(IN.positionWS)));
half surface_factor = lerp(IN.normalWS, out_cross, _NormalsFactor).x;
half4 col_output = half4(surface_factor.xxx, 1.0);
return col_output;
}
ENDHLSL
}
}
}
🔗 Related Functions¶
Cross • Normalize • Lerp • Step
❤️ Support Shader Story¶
If this article helped you, consider supporting the project on Patreon: you’ll get access to the related source files and reference cheat-sheets.
Your support helps keep this library open and free for everyone.