On this page
Sqrt
sqrt(x) returns the square root of a value. rsqrt(x) returns the reciprocal of the square root. These are useful for gradient shaping, distance-based falloffs, attenuation models, and normalizing vectors efficiently.
float result = sqrt(x); // Standard square root
float result = rsqrt(x); // Fast inverse square root (1.0 / sqrt(x))
Visual demo¶
This shader visualizes sqrt(x) or rsqrt(x) using a horizontal UV gradient. You can toggle between the two functions to observe their curve shapes:
- sqrt(x) grows slowly then rises
- rsqrt(x) spikes early then levels off (useful for attenuation/falloff)
Node logic (engine-agnostic)¶
Try it live¶
The same square-root curve, live: toggle sqrt or rsqrt and drag the scale.
Shader "DecompiledArt/CommonFunctions/Sqrt/Sqrt"
{
Properties
{
_Tint01("Tint01", Color) = (1,1,1,1)
_Tint02("Tint02", Color) = (1,1,1,1)
[Toggle(INVERSE_SQRT)] _SqrtInverse("inverseSqrt", Int) = 0
[Toggle(SHOW_GRAPH)] _ShowGraph("showGraph", Int) = 0
_GraphLineWidth("Graph_LineWidth", Range(0.005, 0.025)) = 0.02
}
SubShader
{
Tags { "RenderType"="Opaque" "RenderPipeline" = "UniversalPipeline" }
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#pragma shader_feature_local INVERSE_SQRT
#pragma shader_feature_local SHOW_GRAPH
struct Attributes
{
float4 positionOS : POSITION;
half2 uvs: TEXCOORD0;
};
struct Varyings
{
float4 positionHCS : SV_POSITION;
half2 uvs: TEXCOORD0;
};
CBUFFER_START(UnityPerMaterial)
half3 _Tint01;
half3 _Tint02;
half _GraphLineWidth;
CBUFFER_END
Varyings vert (Attributes IN)
{
Varyings OUT;
OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
OUT.uvs = IN.uvs;
return OUT;
}
half4 frag(Varyings IN) : SV_Target
{
#ifdef INVERSE_SQRT
half sqrt_output = saturate(rsqrt(IN.uvs.x + 0.001));
#else
half sqrt_output = sqrt(IN.uvs.x + 0.001);
#endif
half3 col_output = lerp(_Tint01, _Tint02, sqrt_output);
#ifdef SHOW_GRAPH
half graph_line = saturate(1 - smoothstep(0.0, _GraphLineWidth, abs(IN.uvs.y - sqrt_output)));
col_output += graph_line;
#endif
return half4(col_output, 1.0);
}
ENDHLSL
}
}
}
🔗 Related Functions¶
Remap • MinMax • Exp • Abs • Power
❤️ 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.