Shader Story
August 1, 2025 · View on GitHub
Derivatives Sample: Slope Detect
This sample demonstrates how to use screen-space derivatives (ddx, ddy) to measure surface slope by analyzing how much the world-space Y position changes across pixels. This might be useful for terrain-aware blending, snow accumulating or moss growing on gentle slopes. You can mask or blend between two materials or tints based on slope intensity.
float dy_dx = ddx(worldPos.y);
float dy_dy = ddy(worldPos.y);
float slope = length(float2(dy_dx, dy_dy));
Visual demo
This shader visualizes the slope of each surface fragment.
URP Shader Code
Shader "DecompiledArt/Derivatives/SlopeDetect/SlopeDetect"
{
Properties
{
_Tint_Flat("Tint Flat", Color) = (1,1,1,1)
_Tint_Steep("Tint Steep", Color) = (0.2,0.8,1,1)
_Slope_Threshold("Slope Threshold", Range(0.0, 0.005)) = 0.004
_Slope_Blend("Slope Blend", Range(0.0, 0.005)) = 0.002
}
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;
half3 normalOS : NORMAL;
};
struct Varyings
{
float4 positionHCS : SV_POSITION;
float3 positionWS : TEXCOORD0;
};
CBUFFER_START(UnityPerMaterial)
half _Slope_Threshold;
half _Slope_Blend;
half4 _Tint_Flat;
half4 _Tint_Steep;
CBUFFER_END
Varyings vert (Attributes IN)
{
Varyings OUT;
OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
OUT.positionWS = TransformObjectToWorld(IN.positionOS.xyz);
return OUT;
}
half4 frag(Varyings IN) : SV_Target
{
half dy_dx = ddx(IN.positionWS.y);
half dy_dy = ddy(IN.positionWS.y);
half slopeMagnitude = length(half2(dy_dx, dy_dy));
half slopeMask = smoothstep(_Slope_Threshold - _Slope_Blend, _Slope_Threshold + _Slope_Blend, slopeMagnitude);
half3 col_output = lerp(_Tint_Flat.rgb, _Tint_Steep.rgb, slopeMask);
return half4(col_output, 1.0);
}
ENDHLSL
}
}
}
URP Shader graph
🔗 Related Functions
Length • Lerp • Smoothstep
❤️ Support Shader Story
If this article helped you, consider supporting the project on Patreon - you'll get access to the related source files, reference cheat-sheets, and other exclusive resources:
Your support helps keep this library open, growing, and free for everyone.