Shader Story

August 1, 2025 · View on GitHub

Common HLSL Functions: Dot

dot(a, b) returns the cosine of the angle between two normalized vectors, scaled by their lengths. It's useful for: lighting calculations, Fresnel effects, Edge detection

float d = dot(normalize(vecA), normalize(vecB)); // returns -1 to 1


Visual demo

This shader demonstrates the dot() function by computing the angle between the surface normal and a user-defined light direction. It shades the surface from dark (opposite) to bright (aligned), producing a soft lighting effect based on vector alignment.

Shader Story: Function - Dot


URP Shader Code

Shader "DecompiledArt/CommonFunctions/Dot/Dot"
{
    Properties
    {
        _LightDir("Light Direction", Vector) = (0, 1, 0, 0)
        _Tint_01("Tint_01", Color) = (0, 0, 0.2, 1)
        _Tint_02("Tint_02", Color) = (1, 1, 1, 1)
    }

    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;
                half3 normalOS : NORMAL;
            };

            struct Varyings
            {
                float4 positionHCS : SV_POSITION;
                half3 normalWS : NORMAL;
            };

            CBUFFER_START(UnityPerMaterial)
            half3 _LightDir;
            half4 _Tint_01;
            half4 _Tint_02;
            CBUFFER_END

            Varyings vert (Attributes IN)
            {
                Varyings OUT;

                OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
                OUT.normalWS = TransformObjectToWorldNormal(IN.normalOS);
                return OUT;
            }

            half4 frag(Varyings IN) : SV_Target
            {
                half3 n = normalize(IN.normalWS);
                half3 l = normalize(_LightDir);

                half d = saturate(dot(n, l));

                half3 col_output = lerp(_Tint_01.rgb, _Tint_02.rgb, d);
                return half4(col_output, 1.0);
            }

            ENDHLSL
        }
    }
}

URP Shader graph

Shader Story: Function - Dot


StepSmoothstepLerp


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