Shader Story
August 1, 2025 · View on GitHub
Common HLSL Functions: Min/Max
min(a, b)andmax(a, b)return the smaller or larger of two values, respectively.
Great for clamping, thresholding, UV bounding, and shader logic control.
float min(float a, float b);
float max(float a, float b);
Visual demo
This shader applies either min() or max() to the horizontal UV axis depending on the toggle.
It demonstrates how values are limited based on shader input.
URP Shader Code
Shader "DecompiledArt/CommonFunctions/MinMax/MinMax"
{
Properties
{
_Min("Min", Range(0.0, 1.0)) = 0.0
_Max("Max", Range(0.0, 1.0)) = 0.0
[Toggle(SHOW_MAX)] _ShowMax("showMax", Int) = 0
}
SubShader
{
Tags { "RenderType"="Opaque" "RenderPipeline" = "UniversalPipeline" }
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma shader_feature SHOW_MAX
#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 _Min;
half _Max;
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 SHOW_MAX
half result = max(i.uvs.x, _Max);
#else
half result = min(i.uvs.x, _Min);
#endif
return half4(result, result, result, 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.