Shader Story

August 1, 2025 · View on GitHub

Common HLSL Functions: Negate

Negating a value flips its sign.
Use -x or multiply by -1.0 to invert a value or direction.
This is useful for mirrored UVs, reversed gradients, animation flipping, and directional math.

float negated = -x;
// or
float negated = x * -1.0;


Visual demo

This shader demonstrates how negation can flip UV-space symmetries. When toggled, the output inverts horizontally and vertically around the center.

Shader Story: Function - Negate


URP Shader Code

Shader "DecompiledArt/CommonFunctions/Negate/Negate"
{
    Properties
    {
        _UVTile("UVTile", Range(0.5, 10.0)) = 1.0
        [Toggle(USE_NEGATE)] _UseNegate("useNegate", Int) = 0
    }

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

        Pass
        {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #pragma shader_feature USE_NEGATE

            #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
            {
                #ifdef USE_NEGATE
                    half2 result = mul((frac(i.uvs * _UVTile) - 0.5), -1.0);
                #else
                    half2 result = frac(i.uvs * _UVTile) - 0.5;
                #endif

                half col_output = max(result.x, result.y);
                return half4(col_output, col_output, col_output, 1.0);
            }

            ENDHLSL
        }
    }
}

URP Shader graph

Shader Story: Function - Negate


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.