Skip to content
On this page

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

Node logic (engine-agnostic)

Shader Story: Function - Frac


Try it live

The same fractional part, live: drag the tiling and the speed.


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
        }
    }
}

Step • Remap • Smoothstep


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

DecompiledArt on Patreon DecompiledArt on Buy Me a Coffee

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