# 2. Custom Light Intensity Track with a Mixer Part of [[Timeline Advanced]]. Previous: [[1. Playables Mental Model and Clip Lifecycle]]. Next: [[3. Subtitle and Dialogue Track]]. > [!warning] Code not compiled in Unity > Type-checked against Unity 6000.5.8f1 assemblies and Timeline 1.8.13 (see the hub). Not compiled or run in the Editor. You *can* animate `Light.intensity` with an Animation track. A custom track earns its place when you want **designer-facing clips** ("flicker 2 s, then swell"), **blending that falls back to the scene's own lighting**, and **behaviour an AnimationClip cannot express** (noise, curves over normalized clip time). The same shape works for any component value: fog density, a post-processing volume weight, a material property, a rumble strength for [[Haptics and Controller Rumble]]. ## 2.1 The clip data The behaviour is the per-clip data. Its fields are **keyable**: in the Timeline window's Curves view you can animate `intensity` or `color` *inside* a clip, on top of the curve field. Mark a field `[NotKeyable]` to stop that. ```csharp // Assets/TimelineAdvanced/Runtime/LightIntensityBehaviour.cs using System; using UnityEngine; using UnityEngine.Playables; namespace TimelineAdvanced { // Per-clip data. Fields on the template are keyable: Timeline can animate them // with clip curves (Curves view) unless you mark them [NotKeyable]. [Serializable] public class LightIntensityBehaviour : PlayableBehaviour { public Color color = Color.white; [Min(0f)] public float intensity = 1f; // Shape over the clip's normalized local time (0..1). Multiplies intensity. public AnimationCurve intensityOverClip = AnimationCurve.Constant(0f, 1f, 1f); // 0 = steady, 1 = full Perlin flicker. [Range(0f, 1f)] public float flicker; [Min(0f)] public float flickerSpeed = 12f; } } ``` ## 2.2 The clip asset `ClipCaps.Blending` is the important flag: it gives Ease In/Out and overlap blends, which Timeline turns into the mixer's input weights. `Extrapolation` lets a clip hold its value into a following gap; `SpeedMultiplier` changes how fast the curve and flicker run. ```csharp // Assets/TimelineAdvanced/Runtime/LightIntensityClip.cs using System; using UnityEngine; using UnityEngine.Playables; using UnityEngine.Timeline; namespace TimelineAdvanced { [Serializable] public class LightIntensityClip : PlayableAsset, ITimelineClipAsset { public LightIntensityBehaviour template = new LightIntensityBehaviour(); // Blending: ease-in/out and overlaps produce input weights for the mixer. // Extrapolation: Hold/Loop before and after the clip (clip inspector). // SpeedMultiplier: the clip's local time can run faster or slower. public ClipCaps clipCaps => ClipCaps.Blending | ClipCaps.Extrapolation | ClipCaps.SpeedMultiplier; public override double duration => 1.5; public override Playable CreatePlayable(PlayableGraph graph, GameObject owner) { // Create(graph, template) clones the template, so each clip instance // gets its own copy of the serialized values. return ScriptPlayable<LightIntensityBehaviour>.Create(graph, template); } } } ``` ## 2.3 The mixer The mixer is where a custom track becomes good or bad. Three rules: 1. **Capture defaults from the binding**, not from code constants, the first time you see it (and again if the binding changes at runtime). 2. **Blend toward the defaults by the missing weight.** A clip easing in from 0 to 1 then fades from the scene's lighting, not from black. 3. **Restore in `OnPlayableDestroy`** unless the designer asked to keep the final value. ```csharp // Assets/TimelineAdvanced/Runtime/LightIntensityMixerBehaviour.cs using UnityEngine; using UnityEngine.Playables; namespace TimelineAdvanced { // One mixer per track instance. Timeline connects every clip playable as an input // and sets the input weights from clip timing, ease-in/out and blends. public class LightIntensityMixerBehaviour : PlayableBehaviour { // Set by LightIntensityTrack.CreateTrackMixer. public bool restoreOnStop = true; Light m_Bound; float m_DefaultIntensity; Color m_DefaultColor; public override void ProcessFrame(Playable playable, FrameData info, object playerData) { var light = playerData as Light; if (light != m_Bound) { // Binding changed (first frame, or rebinding at runtime): restore the old // light and capture the new light's scene values as the "no clip" defaults. Restore(); m_Bound = light; if (m_Bound != null) { m_DefaultIntensity = m_Bound.intensity; m_DefaultColor = m_Bound.color; } } if (m_Bound == null) return; int inputCount = playable.GetInputCount(); float totalWeight = 0f; float blendedIntensity = 0f; Color blendedColor = Color.clear; for (int i = 0; i < inputCount; i++) { float weight = playable.GetInputWeight(i); if (weight <= 0f) continue; var input = (ScriptPlayable<LightIntensityBehaviour>)playable.GetInput(i); LightIntensityBehaviour clip = input.GetBehaviour(); double localTime = input.GetTime(); double clipDuration = input.GetDuration(); float t01 = clipDuration > 0.0 ? Mathf.Clamp01((float)(localTime / clipDuration)) : 0f; float clipIntensity = clip.intensity * clip.intensityOverClip.Evaluate(t01); if (clip.flicker > 0f) { float noise = Mathf.PerlinNoise((float)localTime * clip.flickerSpeed, i * 13.37f); clipIntensity *= 1f - clip.flicker * noise; } blendedIntensity += clipIntensity * weight; blendedColor += clip.color * weight; totalWeight += weight; } // Where clips do not reach full weight (gaps, ease-in/out), blend toward the // light's own scene values instead of toward black. float remainder = 1f - Mathf.Clamp01(totalWeight); m_Bound.intensity = blendedIntensity + m_DefaultIntensity * remainder; m_Bound.color = blendedColor + m_DefaultColor * remainder; } public override void OnPlayableDestroy(Playable playable) { // The graph is destroyed on Stop() or when a None-wrap timeline ends. if (restoreOnStop) Restore(); m_Bound = null; } void Restore() { if (m_Bound == null) return; m_Bound.intensity = m_DefaultIntensity; m_Bound.color = m_DefaultColor; } } } ``` Notes on the details: - `input.GetTime()` is the clip's **local** time, already adjusted for Clip In and Speed Multiplier. Dividing by `GetDuration()` gives a 0..1 position for the curve. It is clamped because extrapolation can push local time outside the clip. - `i * 13.37f` gives each clip its own noise row, so two overlapping flicker clips do not flicker in sync. - Overlapping clips have weights that sum to about 1, so `remainder` is about 0 inside a blend and 1 in a gap. ## 2.4 The track ```csharp // Assets/TimelineAdvanced/Runtime/LightIntensityTrack.cs using System.ComponentModel; using UnityEngine; using UnityEngine.Playables; using UnityEngine.Timeline; namespace TimelineAdvanced { [TrackColor(1f, 0.82f, 0.25f)] [TrackClipType(typeof(LightIntensityClip))] [TrackBindingType(typeof(Light))] [DisplayName("Timeline Advanced/Light Intensity Track")] public class LightIntensityTrack : TrackAsset { [Tooltip("Restore the light's scene intensity and color when the graph is destroyed (Stop, or end of a None-wrap timeline). Turn off to keep the final value, like an Activation track's Leave As Is.")] public bool restoreOnStop = true; public override Playable CreateTrackMixer(PlayableGraph graph, GameObject go, int inputCount) { var mixer = ScriptPlayable<LightIntensityMixerBehaviour>.Create(graph, inputCount); mixer.GetBehaviour().restoreOnStop = restoreOnStop; return mixer; } // Edit-mode preview: tell Timeline which serialized properties this track drives, // so scrubbing in the editor does not dirty the scene, and the values are restored // when preview ends. Names are the serialized field names on Light. public override void GatherProperties(PlayableDirector director, IPropertyCollector driver) { var light = director.GetGenericBinding(this) as Light; if (light != null) { driver.AddFromName<Light>(light.gameObject, "m_Intensity"); driver.AddFromName<Light>(light.gameObject, "m_Color"); } base.GatherProperties(director, driver); } protected override void OnCreateClip(TimelineClip clip) { clip.displayName = "Light"; } } } ``` **Why `GatherProperties`.** When you scrub in Edit Mode, the mixer writes to a real scene `Light`. Without `GatherProperties`, those writes are real edits: the scene is dirtied, and the values stay after you close the Timeline window. `GatherProperties` registers the serialized properties (`m_Intensity`, `m_Color`) with Timeline's preview system, which records them before preview and restores them afterwards. The names are the serialized field names, the same names the Timeline sample `TextTrack` uses for `TMP_Text` (`m_text`, `m_fontSize`, ...). If you add another property, find its serialized name with the Inspector in **Debug** mode or in the `.unity` file. **Why `restoreOnStop`.** A cutscene that ends with the lights *on* is common. With `restoreOnStop` on (the default), stopping the director puts the light back to its scene value; with it off, the last mixed value stays, like an Activation track's *Leave As Is*. Edit-mode preview restores either way, through `GatherProperties`. ## 2.5 Use it 1. Select a timeline, **+ > Timeline Advanced > Light Intensity Track**, and bind a Point or Spot light (drag the GameObject or the Light component). 2. Right-click the track > **Add Light Intensity Clip**. Set intensity 4, a warm color, and a curve that rises 0 → 1. 3. Add a second clip, `flicker = 0.8`, overlapping the first by 0.5 s. The overlap is the blend. 4. On the first clip set **Ease In** to 0.5 s: the light fades up from its scene value. 5. Scrub in Edit Mode, then close the Timeline window: the light is back to its scene value and the scene is not dirty. 6. Play, then Stop the director in Play Mode, with `restoreOnStop` on and then off. ## 2.6 Exercise Add a `range` field to the behaviour, blend it in the mixer and register `m_Range` in `GatherProperties`. Then make a second track type for a `Volume` weight (URP post-processing) using the same three rules. ## Sources - `TrackAsset.CreateTrackMixer`, `GatherProperties`, `OnCreateClip` (Timeline 1.8): https://docs.unity3d.com/Packages/[email protected]/api/UnityEngine.Timeline.TrackAsset.html - `ClipCaps` (Timeline 1.8): https://docs.unity3d.com/Packages/[email protected]/api/UnityEngine.Timeline.ClipCaps.html - Timeline Customization samples (the Text track sample's mixer and `GatherProperties` pattern): https://docs.unity3d.com/Packages/[email protected]/manual/samp-custom-samples.html - `PlayableBehaviour.ProcessFrame` (6000.5): https://docs.unity3d.com/6000.5/Documentation/ScriptReference/Playables.PlayableBehaviour.ProcessFrame.html - Related: [[Lighting and Baking]] for which lights are realtime and therefore animatable at all.