Skip to content
On this page

Sine & Cosine

sin(x) and cos(x) produce smooth, oscillating values between -1 and 1. These functions are essential for animations, wave patterns, UV warping, procedural motion, and cyclical behaviors.

float result = sin(x);  // Returns sine of input (radians)
float result = cos(x);  // Returns cosine of input (radians)

Visual demo

This shader visualizes either a sine or cosine wave modulated over time. The result is shaped spatially using UV distance, allowing the wave to pulse or animate across the surface.

Shader Story: Function - Sine/Cosine

Node logic (engine-agnostic)

Shader Story: Function - Sqrt


Try it live

The same wave, live: toggle sin or cos and drag the frequency and speed.


Shader code
Shader "DecompiledArt/CommonFunctions/SineCosine/SineCosine"
{
    Properties
    {
        _Frequency("Frequency", Range(0.01, 10.0)) = 1.0
        [Toggle(USE_COSINE)] _UseCosine("useCosine", Int) = 0
    }

    SubShader
    {
        Tags { "RenderType"="Opaque" "RenderPipeline" = "UniversalPipeline" }

        Pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #pragma shader_feature_local USE_COSINE

            #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 _Frequency;
            CBUFFER_END

            Varyings vert (Attributes IN)
            {
                Varyings OUT;

                OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
                OUT.uvs = IN.uvs;
                return OUT;
            }

            half2 remap(half2 In, half2 InMinMax, half2 OutMinMax)
            {
                return OutMinMax.x + (In - InMinMax.x) * (OutMinMax.y - OutMinMax.x) / (InMinMax.y - InMinMax.x);
            }

            half4 frag(Varyings IN) : SV_Target
            {
                half time = _TimeParameters.x * _Frequency;

                #ifdef USE_COSINE
                    half oscillation = cos(time);
                #else
                    half oscillation = sin(time);
                #endif

                half2 shape = remap(IN.uvs, half2(0, 1), half2(-1, 1));
                shape = saturate(1 - distance(half2(0,0), shape));

                oscillation = (oscillation - 1) + shape;

                half4 col_output = half4(oscillation.xxx, 1.0);
                return col_output;
            }

            ENDHLSL
        }
    }
}

Remap • Step • Abs • Power


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