On this page
Multiple Objects Pivot Bake
One plant is a few leaves. A canopy is hundreds of leaves in one mesh, and one material property cannot be a hundred pivots. The classic solution is to bake the pivot into the mesh: model each leaf as its own object with the origin at the stem, write each object’s origin into its vertices’ color, merge everything, and let the shader unpack a per-vertex pivot. Every vertex now knows which pivot it belongs to, and the whole canopy can run on one shared material.
In the viewport the packed vertex color looks like a random gradient. It is data.
float3 pivotOS = lerp(_PivotMin, _PivotMax, color.rgb); // gamma-corrected first
float phase = color.a * _PhaseScale; // per-leaf desync
float3 local = positionOS - pivotOS;
local = RotateAroundAxis(local, axisOS, angle);
positionOS = pivotOS + local;
The bake pipeline¶
In a DCC tool (Blender, Maya, or a tooling pass), each leaf gets its object origin written into the vertex color of its own vertices before the merge. After the merge, the packed color is the only record of where each pivot was.
The decode convention, write it down¶
Packed channels are a contract. The one used here, and the one you should document for your own:
- color.rgb: pivot position, 0..1 inside the object bounds, remapped by
_PivotMin/_PivotMax. Apply a gamma correction first if the color was stored in sRGB space, or every pivot shifts. - color.a: a per-leaf phase seed, so the leaves desynchronize (see Phase Offset).
The axis stays flexible, a constant, derived from the normal, or another stream, but pivot, axis, and influence all have to live somewhere, and the source, the packing, and the decode path must all be explicitly defined.
Debug the pivot before you trust the rotation¶
Exactly as with masks. The debug views below are the bake workflow made visible:
- Raw vertex color: what the tool stored, before correction.
- Decoded pivot: every vertex on the same leaf must show an identical solid color; any variation within a leaf is a baking or decoding error.
- Pivot distance: bright at the pivot center, fading toward the tips; an off-center gradient means a packing, axis-conversion, or object-space mismatch.
- Signed pivot delta: 0.5 means the pivot itself; red/green/blue shifts show which side of the pivot the geometry lives on. The fastest way to spot an axis remap mistake.
Visual demo¶
The merged canopy with its packed pivot data: every leaf rotates from its own baked stem, and the packed phase desynchronizes them, so the canopy sways as a field instead of a rigid block.
The full decode + rotate + debug ladder. The vertex-color gamma knob matters in practice: DCC tools often store colors in sRGB space, and skipping the correction shifts every pivot.
Shader "DecompiledArt/VertexAnimation/PivotMotion/MultipleObjectsPivotBake/MultipleObjectsPivotBake"
{
Properties
{
[Header(Surface)]
[MainTexture] _BaseMap("Base Map", 2D) = "white" {}
[MainColor] _BaseColor("Base Color", Color) = (1, 1, 1, 1)
_Cutoff("Alpha Cutoff", Range(0, 1)) = 0.5
[Enum(UnityEngine.Rendering.CullMode)] _Cull("Cull", Float) = 0
[Header(Pivot Data)]
_PivotMin("Pivot Min (Object Space)", Vector) = (-1, -1, -1, 0)
_PivotMax("Pivot Max (Object Space)", Vector) = (1, 1, 1, 0)
_VertexColorGamma("Vertex Color Gamma", Float) = 1.0
[Header(Motion)]
_MotionStrength("Motion Strength", Range(0, 2)) = 1.0
_WobbleSpeed("Wobble Speed", Float) = 1.0
_WobbleAmountDegrees("Wobble Amount (Degrees)", Range(0, 90)) = 25.0
_WindAxisOS("Rotation Axis (Object Space)", Vector) = (1, 0, 0, 0)
_PhaseScale("Vertex Alpha Phase Scale", Float) = 6.2831853
[Header(Debug)]
[Enum(Off,0,Raw_Vertex_Color,1,Decoded_Pivot_OS,2,Pivot_Distance,3,Motion_Delta,4,Alpha_Phase,5,Signed_Pivot_Delta_OS,6)]
_DebugMode("Debug Mode", Float) = 0
_PivotDistanceDebugScale("Pivot Distance Debug Scale", Float) = 8.0
_MotionDeltaDebugScale("Motion Delta Debug Scale", Float) = 10.0
}
SubShader
{
Tags
{
"RenderPipeline" = "UniversalPipeline"
"RenderType" = "TransparentCutout"
"Queue" = "AlphaTest"
}
LOD 200
Cull [_Cull]
Pass
{
Name "ForwardUnlit"
Tags { "LightMode" = "UniversalForward" }
ZWrite On
ZTest LEqual
Cull [_Cull]
HLSLPROGRAM
#pragma vertex Vert
#pragma fragment Frag
#pragma multi_compile_instancing
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
TEXTURE2D(_BaseMap);
SAMPLER(sampler_BaseMap);
CBUFFER_START(UnityPerMaterial)
float4 _BaseMap_ST;
half4 _BaseColor;
half _Cutoff;
float4 _PivotMin;
float4 _PivotMax;
half _VertexColorGamma;
half _MotionStrength;
half _WobbleSpeed;
half _WobbleAmountDegrees;
float4 _WindAxisOS;
half _PhaseScale;
float _DebugMode;
half _PivotDistanceDebugScale;
half _MotionDeltaDebugScale;
CBUFFER_END
struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
float4 color : COLOR;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
// originalOS - pre-animation object-space position
// animatedOS - post-animation object-space position
// pivotOS - the decoded pivot for this vertex
float3 originalOS : TEXCOORD1;
float3 animatedOS : TEXCOORD2;
float3 pivotOS : TEXCOORD3;
float4 color : COLOR;
UNITY_VERTEX_OUTPUT_STEREO
};
// Rotation of v around a normalized axis by angle (radians).
float3 RotateAroundAxis(float3 v, float3 axis, half angle)
{
half s, c;
sincos(angle, s, c);
return v * c + cross(axis, v) * s + axis * dot(axis, v) * (1.0h - c);
}
// Artist-controlled gamma curve on the raw vertex color.
// 1.0 = no change (linear workflow, colors already linear)
// 2.2 = treat sRGB-stored colors as gamma space
half3 ApplyVertexColorGamma(half3 c)
{
return pow(max(c, 1e-4h), (half)_VertexColorGamma);
}
// Reconstruct the per-leaf pivot from vertex color RGB:
// gamma-correct, then remap [0,1] into the object bounds.
float3 DecodePivotOS(float4 vertexColor)
{
half3 corrected = ApplyVertexColorGamma((half3)vertexColor.rgb);
return lerp(_PivotMin.xyz, _PivotMax.xyz, (float3)corrected);
}
Varyings Vert(Attributes input)
{
Varyings output = (Varyings)0;
UNITY_SETUP_INSTANCE_ID(input);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
float3 posOS = input.positionOS.xyz;
float3 pivotOS = DecodePivotOS(input.color);
float3 axisOS = normalize(_WindAxisOS.xyz);
// Per-leaf phase from the alpha channel breaks up
// synchronized motion across the canopy.
half phase = (half)input.color.a * _PhaseScale;
half angle = sin(_Time.y * _WobbleSpeed + phase)
* radians(_WobbleAmountDegrees)
* _MotionStrength;
// Rotate each vertex relative to its decoded pivot, then
// translate back: the general pivot pattern, per vertex.
float3 localOS = posOS - pivotOS;
float3 rotatedOS = RotateAroundAxis(localOS, axisOS, angle);
float3 animatedOS = pivotOS + rotatedOS;
output.positionCS = TransformObjectToHClip(animatedOS);
output.uv = TRANSFORM_TEX(input.uv, _BaseMap);
output.originalOS = posOS;
output.animatedOS = animatedOS;
output.pivotOS = pivotOS;
output.color = input.color;
return output;
}
half4 Frag(Varyings input) : SV_Target
{
half4 baseSample = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, input.uv) * _BaseColor;
// Alpha cutout in all modes so debug views respect the silhouette.
clip(baseSample.a - _Cutoff);
// Debug visualizations:
// 1 · Raw vertex color: exactly what the DCC tool stored.
if (_DebugMode == 1.0)
return half4((half3)input.color.rgb, 1.0h);
// 2 · Decoded pivot remapped to [0,1]. Every vertex on the same
// leaf must show an identical solid color; variation within
// a leaf means a baking or decoding error.
if (_DebugMode == 2.0)
{
half3 pivotNorm = (half3)saturate(
(input.pivotOS - _PivotMin.xyz) /
max(_PivotMax.xyz - _PivotMin.xyz, 1e-5)
);
return half4(pivotNorm, 1.0h);
}
// 3 · Pivot distance: bright at the pivot center, fading toward
// the tips. Off-center gradients mean packing or space issues.
if (_DebugMode == 3.0)
{
float3 pivotDelta = input.originalOS - input.pivotOS;
half pivotMask = 1.0h - saturate(length(pivotDelta) * _PivotDistanceDebugScale);
pivotMask *= pivotMask;
return half4(pivotMask.xxx, 1.0h);
}
// 4 · Motion delta: how far the vertex moved this frame.
// Zero motion = black; tips should glow.
if (_DebugMode == 4.0)
{
half3 delta = (half3)(abs(input.animatedOS - input.originalOS)
* _MotionDeltaDebugScale);
return half4(saturate(delta), 1.0h);
}
// 5 · Alpha / phase seed: each leaf should show a uniform value.
if (_DebugMode == 5.0)
return half4((half3)input.color.aaa, 1.0h);
// 6 · Signed pivot delta: 0.5 is the pivot itself; color shifts
// show which side of the pivot the geometry lives on.
if (_DebugMode == 6.0)
{
float3 pivotDelta = input.originalOS - input.pivotOS;
half3 signedDelta = (half3)(pivotDelta * _PivotDistanceDebugScale * 0.5h + 0.5h);
return half4(saturate(signedDelta), 1.0h);
}
return baseSample;
}
ENDHLSL
}
}
FallBack Off
}
🔗 Related Chapters¶
Single Object Mesh Pivot • Single Object Pivot Data • Authored Masks • Data Sources
Related functions: Lerp • Cross • Power • Length
❤️ 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.
Your support helps keep this library open and free for everyone.