Shader Story
August 1, 2025 · View on GitHub
Common HLSL Functions: Distance
distance(a, b)returns the Euclidean distance between two pointsaandb. It’s useful for radial falloffs, circular masks, ripples, and proximity-based effects.
float d = distance(float3(0.0, 0.0, 0.0), float3(1.0, 1.0, 1.0));
Visual demo
This shader visualizes how distance() calculates the separation between world-space positions and a given center point. The result is a smooth radial gradient, often used in glow masks, area effects, and procedural transitions.
URP Shader Code
Shader "DecompiledArt/CommonFunctions/Distance/Distance"
{
Properties
{
_Pos_X("Pos_X", Range(-10.0, 10.0)) = 0.0
_Pos_Y("Pos_Y", Range(-10.0, 10.0)) = 0.0
_Pos_Z("Pos_Z", Range(-10.0, 10.0)) = 0.0
_Contrast("Contrast", Range(0.01, 8.0)) = 1.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;
};
struct Varyings
{
float4 positionHCS : SV_POSITION;
float3 positionWS : TEXCOORD0;
};
CBUFFER_START(UnityPerMaterial)
half _Pos_X;
half _Pos_Y;
half _Pos_Z;
half _Contrast;
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
{
half3 pos = half3(_Pos_X, _Pos_Y, _Pos_Z);
half col_output = pow((1 - saturate(distance(pos, IN.positionWS.xyz))), _Contrast);
return half4(col_output.xxx, 1.0);
}
ENDHLSL
}
}
}
URP Shader graph
🔗 Related Functions
Length • Normalize • Smoothstep • Dot
❤️ 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.