Skip to content

Instantly share code, notes, and snippets.

@Deadcows
Created August 7, 2024 14:52
Show Gist options
  • Save Deadcows/e9340252ba7838f93fe5b7a3615e0d39 to your computer and use it in GitHub Desktop.
Save Deadcows/e9340252ba7838f93fe5b7a3615e0d39 to your computer and use it in GitHub Desktop.
VolumeMapping component to animate PostProcessing through timeline
using System;
using MyBox;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
namespace Gameplay
{
[Serializable]
public struct DepthOfFieldModule
{
public bool ModuleActive;
public bool Enabled;
public float FocusDistance;
public float FocalLength;
public float Aperture;
public void Apply(DepthOfField dof)
{
if (dof == null || !ModuleActive) return;
dof.active = Enabled;
dof.focusDistance.Override(FocusDistance);
dof.focalLength.Override(FocalLength);
dof.aperture.Override(Aperture);
}
public void Sync(DepthOfField dof)
{
if (dof == null) return;
Enabled = dof.active;
FocusDistance = dof.focusDistance.value;
FocalLength = dof.focalLength.value;
Aperture = dof.aperture.value;
}
}
[Serializable]
public struct ScreenSpaceLensFlareModule
{
public bool ModuleActive;
public bool Enabled;
public float Intensity;
public void Apply(ScreenSpaceLensFlare lensFlare)
{
if (lensFlare == null || !ModuleActive) return;
lensFlare.active = Enabled;
lensFlare.intensity.Override(Intensity);
}
public void Sync(ScreenSpaceLensFlare lensFlare)
{
if (lensFlare == null) return;
Enabled = lensFlare.active;
Intensity = lensFlare.intensity.value;
}
}
[ExecuteInEditMode, TopmostComponent]
public class VolumeMapping : MonoBehaviour
{
[ToggleBoxGroup("ModuleActive")]
public DepthOfFieldModule DepthOfField;
[ToggleBoxGroup("ModuleActive")]
public ScreenSpaceLensFlareModule ScreenSpaceLensFlare;
private DepthOfField DOF
{
get
{
if (_dof != null) return _dof;
Volume?.profile.TryGet(out _dof);
return _dof;
}
}
private DepthOfField _dof;
private ScreenSpaceLensFlare LensFlare
{
get
{
if (_lensFlare != null) return _lensFlare;
Volume?.profile.TryGet(out _lensFlare);
return _lensFlare;
}
}
private ScreenSpaceLensFlare _lensFlare;
private Volume Volume => _volume ? _volume : _volume = GetComponent<Volume>();
private Volume _volume;
[Button(DirtyOnClick = true)]
private void Sync()
{
DepthOfField.Sync(DOF);
ScreenSpaceLensFlare.Sync(LensFlare);
}
private void Update()
{
if (Volume?.weight == 0) return;
DepthOfField.Apply(DOF);
ScreenSpaceLensFlare.Apply(LensFlare);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment