Skip to content
On this page

Fmod

fmod(x, y) returns the remainder of x divided by y, preserving the sign of the numerator. It’s especially useful for tiling patterns, time wrapping, and procedural animation.

float fmod(float x, float y);

Visual demo

This shader uses fmod to generate animated, scrolling vertical stripes.

Shader Story: Function - Fmod

Node logic (engine-agnostic)

Shader Story: Function - Fmod


Try it live

The same remainder, live: drag the scale, the speed, the modulus and the width.


Shader code
Shader "DecompiledArt/CommonFunctions/Fmod/Fmod"
{
    Properties
    {
        _Tint_01("Tint_01", Color) = (1,1,1,1)
        _Tint_02("Tint_02", Color) = (0,0,0,1)
        _Line_Width("Line_Width", Range(0.01, 2.0)) = 0.2
        _Speed("Speed", Float) = 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)
            half4 _Tint_01;
            half4 _Tint_02;
            half _Line_Width;
            half _Speed;
            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
            {
                half uv_x = IN.uvs.x + (_Time.y * _Speed); 
                half mask_line = step(_Line_Width, fmod(uv_x, 1.0));

                half3 col_output = lerp(_Tint_01, _Tint_02, mask_line).xyz;

                return half4(col_output, 1.0);
            }

            ENDHLSL
        }
    }
}

Smoothstep • Step • Frac • Lerp


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