Shader Story
August 1, 2025 · View on GitHub
Common HLSL Functions: Abs
abs(x)returns the absolute value of a number, effectively flipping negative values to positive.
Useful for mirrored gradients, distance calculations, symmetry effects, and contrast shaping.
float abs(float x);
float2 abs(float2 x);
float3 abs(float3 x);
// etc.
Visual demo
This shader demonstrates the use of abs() by mirroring the UV space around the center, producing a diamond-like symmetry. A toggle allows you to compare the behavior with and without the absolute value.
URP Shader Code
Shader "DecompiledArt/CommonFunctions/Abs/Abs"
{
Properties
{
_UVTile("UVTile", Range(0.5, 10.0)) = 1.0
[Toggle(USE_ABS)] _UseAbs("useAbs", Int) = 0
}
SubShader
{
Tags { "RenderType"="Opaque" "RenderPipeline" = "UniversalPipeline" }
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma shader_feature USE_ABS
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
half2 uvs: TEXCOORD0;
};
struct Varyings
{
float4 positionHCS : SV_POSITION;
half2 uvs: TEXCOORD0;
};
CBUFFER_START(UnityPerMaterial)
half _UVTile;
CBUFFER_END
Varyings vert (Attributes IN)
{
Varyings OUT;
OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
OUT.uvs = IN.uvs;
return OUT;
}
half4 frag(Varyings i) : SV_Target
{
#ifdef USE_ABS
half2 abs_output = abs(frac(i.uvs * _UVTile) - 0.5);
#else
half2 abs_output = frac(i.uvs * _UVTile) - 0.5;
#endif
half col_output = max(abs_output.x, abs_output.y);
return half4(col_output, col_output, col_output, 1.0);
}
ENDHLSL
}
}
}
URP Shader graph
🔗 Related Functions
Step • Smoothstep • Remap
❤️ 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.