Shader Story

August 1, 2025 · View on GitHub

Common HLSL Functions: Frac

frac(x) returns the fractional (non-integer) part of a value.
It’s often used for tiling UVs, procedural animation, pattern looping, and modular effects.

float frac(float x);
float2 frac(float2 x);
float3 frac(float3 x);
...

Visual demo

This shader uses frac() to wrap the UV coordinates repeatedly based on a tiling multiplier, creating a looping pattern.

Shader Story: Function - Frac


URP Shader Code


Shader "DecompiledArt/CommonFunctions/Frac/Frac"
{
    Properties
    {
        _UVTile("UVTile", Range(0.5, 10.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;
                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
            {
                return half4(frac(i.uvs * _UVTile), 0.0, 1.0);
            }

            ENDHLSL
        }
    }
}

URP Shader graph

Shader Story: Function - Frac


StepRemapSmoothstep


❤️ 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:

DecompiledArt on Patreon

Your support helps keep this library open, growing, and free for everyone.