Skip to content
On this page

Single Object Mesh Pivot

Simple offset motion is cheap and readable, but it often feels like the mesh is translating through space. Real objects don’t do that: a leaf rotates from its stem, a branch from its base, a door around its hinge. The motion is believable because it is anchored to a meaningful point. For a single object whose mesh is authored around that point, the anchor is the mesh origin, and the math is one rotation.

Same angle, three completely different results: a plain offset floats the mesh, a rotation around the object origin reads as spinning on itself, a rotation around a local pivot reads as the part actually hinging.

float3 rotated = RotateAroundAxis(positionOS, axisOS, angle);  // origin is the pivot

The core pattern

For any pivot, the shape of the math is always the same three steps: move into pivot-local space, rotate, move back. When the pivot is the object origin, the subtract/add steps disappear; the position is already pivot-local:

float3 RotateAroundAxis(float3 v, float3 axis, half angle)
{
    axis = normalize(axis);
    half s, c;
    sincos(angle, s, c);

    // Rodrigues' rotation formula: 1 sincos, 1 cross, 1 dot.
    // Cheaper and less error-prone than materializing a 3x3 per vertex.
    return v * c
         + cross(axis, v) * s
         + axis * dot(axis, v) * (1.0 - c);
}

One production detail: position, pivot, and axis must agree on space. Object-space position with a world-space pivot is a silent, confusing failure.

This is the deliberate alternative to explicit matrices from Matrices: direct math when it says the same thing, and cheaper per vertex on top of that.

When the mesh is not authored around the pivot, or when you need many pivots in one mesh, the subtract/add form and baked pivot data take over: Single Object Pivot Data and Multiple Objects Pivot Bake.


Visual demo

Three anchors, one angle. The offset slides the whole mesh in place; the rotation reads as the part hinging where it should. The pivot debug marker makes the anchor visible.

Shader Story: Vertex Animation - Single Object Mesh Pivot


Offset vs pivot rotation as one live switch, with a debug marker at the pivot so the anchor can be seen.

Shader code
Shader "DecompiledArt/VertexAnimation/PivotMotion/SingleObjectMeshPivot/SingleObjectMeshPivot"
{
    Properties
    {
        _BaseColor("Base Color", Color) = (0.45, 0.8, 0.35, 1.0)

        [Header(Demo)]
        [Enum(Offset,0,RotateAroundPivot,1)]
        _Mode("Motion Mode", Float) = 0

        [Header(Animation)]
        _Speed("Speed", Float) = 1.5

        [Header(Offset Motion)]
        _OffsetDirectionOS("Offset Direction OS", Vector) = (1, 0, 0, 0)
        _OffsetAmplitude("Offset Amplitude", Float) = 0.25

        [Header(Pivot Rotation)]
        _AxisOS("Rotation Axis OS", Vector) = (0, 0, 1, 0)
        _AngleAmplitude("Angle Amplitude (Radians)", Float) = 0.5

        [Header(Debug Pivot)]
        [Toggle] _ShowPivotDebug("Show Pivot Debug", Float) = 0
        _PivotDebugRadius("Pivot Debug Radius", Float) = 0.15
        _PivotDebugColor("Pivot Debug Color", Color) = (1.0, 0.75, 0.05, 1.0)
        _PivotDebugIntensity("Pivot Debug Intensity", Float) = 1.5
    }

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

        Pass
        {
            Name "ForwardUnlit"
            Tags { "LightMode" = "UniversalForward" }

            Cull Back
            ZWrite On
            ZTest LEqual

            HLSLPROGRAM

            #pragma vertex Vert
            #pragma fragment Frag

            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

            #define TWO_PI 6.2831853h
            #define INV_TWO_PI 0.15915494h

            CBUFFER_START(UnityPerMaterial)

                half4 _BaseColor;

                half _Mode;
                half _Speed;

                float4 _OffsetDirectionOS;
                half _OffsetAmplitude;

                float4 _AxisOS;
                half _AngleAmplitude;

                half _ShowPivotDebug;
                half _PivotDebugRadius;
                half4 _PivotDebugColor;
                half _PivotDebugIntensity;

            CBUFFER_END

            struct Attributes
            {
                float3 positionOS : POSITION;
            };

            struct Varyings
            {
                float4 positionCS : SV_POSITION;
                float3 originalOS : TEXCOORD0;
            };

            // Rodrigues' rotation: rotate v around axis by angle.
            // One sincos, one cross, one dot: the production form of the
            // rotation step in the pivot pattern.
            float3 RotateAroundAxis(float3 v, float3 axis, half angle)
            {
                axis = normalize(axis);

                half s, c;
                sincos(angle, s, c);

                return v * c
                     + cross(axis, v) * s
                     + axis * dot(axis, v) * (1.0 - c);
            }

            Varyings Vert(Attributes IN)
            {
                Varyings OUT;

                float3 positionOS = IN.positionOS;
                OUT.originalOS = positionOS;

                // Keep the sine phase bounded so the demo stays numerically stable.
                half phase = frac(_Time.y * _Speed * INV_TWO_PI) * TWO_PI;
                half wave = sin(phase);

                // Offset motion: the whole mesh slides along one direction.
                // This is what "translating through space" feels like.
                float3 offsetDir = normalize(_OffsetDirectionOS.xyz);
                float3 offsetPos = positionOS + offsetDir * wave * _OffsetAmplitude;

                // Mesh-pivot rotation: the object-space origin is the pivot.
                // Because this mesh is authored around its pivot, the position
                // is already pivot-local, no subtract / add step needed.
                float3 rotatedPos = RotateAroundAxis(
                    positionOS,
                    _AxisOS.xyz,
                    wave * _AngleAmplitude
                );

                positionOS = lerp(offsetPos, rotatedPos, _Mode);

                OUT.positionCS = TransformObjectToHClip(positionOS);
                return OUT;
            }

            half4 Frag(Varyings IN) : SV_Target
            {
                half3 color = _BaseColor.rgb;

                // A small glow at the pivot so the anchor is visible.
                if (_ShowPivotDebug > 0.5h)
                {
                    half dist = length(IN.originalOS);
                    half radius = max(_PivotDebugRadius, 1e-4h);

                    half mask = 1.0h - saturate(dist / radius);
                    mask *= mask;

                    color += _PivotDebugColor.rgb * mask * _PivotDebugIntensity;
                }

                return half4(color, 1.0);
            }

            ENDHLSL
        }
    }

    FallBack Off
}

Single Object Pivot Data • Multiple Objects Pivot Bake • Matrices

Related functions: Cross • Dot • Normalize


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