Skip to content

Instantly share code, notes, and snippets.

@ruccho
Created May 31, 2021 14:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruccho/faa17dd3ef35f7d6bca5b3ff0ddaefbf to your computer and use it in GitHub Desktop.
Save ruccho/faa17dd3ef35f7d6bca5b3ff0ddaefbf to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
namespace Ruccho.Utilities
{
public abstract class VolumeProfileExposition
{
public abstract void Update(VolumeProfile profile);
}
[Serializable]
public class VolumeProfileExposition<TComponent, TValue> : VolumeProfileExposition
where TComponent : VolumeComponent where TValue : IEquatable<TValue>
{
[SerializeField] private TValue value = default;
public TValue Value
{
get => value;
set => this.value = value;
}
private TValue tempProfileValue = default;
private TComponent component = default;
private Func<TComponent, VolumeParameter<TValue>> SelectParameter { get; set; }
private TValue GetValue(TComponent settings) => SelectParameter(settings).value;
private void ApplyValue(TComponent settings, TValue value) => SelectParameter(settings).value = value;
private VolumeProfile tempProfile;
public void Initialize(Func<TComponent, VolumeParameter<TValue>> selectParameter)
{
SelectParameter = selectParameter;
}
public override void Update(VolumeProfile profile)
{
value = InternalUpdate(profile);
}
private TValue InternalUpdate(VolumeProfile profile)
{
if ((!component || tempProfile != profile) && profile)
{
tempProfile = profile;
if (profile.TryGet(out component))
{
tempProfileValue = GetValue(component);
}
}
if (!component)
{
Debug.LogWarning($"PostProcessEffectSettings ({typeof(TComponent).Name}) is missing.");
return default;
}
var profileValue = GetValue(component);
if (!profileValue.Equals(tempProfileValue))
{
//primarily use profile value
tempProfileValue = profileValue;
return profileValue;
}
else
{
//use updated value
ApplyValue(component, value);
tempProfileValue = GetValue(component);
return value;
}
}
}
[ExecuteInEditMode]
public abstract class VolumeProfileExpositionBehaviour : MonoBehaviour
{
[SerializeField] private VolumeProfile profile = default;
protected abstract IEnumerable<VolumeProfileExposition> Exposition { get; }
protected virtual void OnEnable()
{
InitializeExpositions();
}
protected virtual void Reset()
{
var volume = GetComponent<Volume>();
if (!volume) return;
profile = volume.sharedProfile;
}
protected virtual void LateUpdate()
{
UpdateExpositions();
}
protected abstract void InitializeExpositions();
protected void UpdateExpositions()
{
foreach (var exposition in Exposition)
{
exposition.Update(profile);
}
}
}
}
using System.Collections.Generic;
using Ruccho.Utilities;
using UnityEngine;
using UnityEngine.Rendering.Universal;
public class VolumeProfileExpositionSample : VolumeProfileExpositionBehaviour
{
[SerializeField] private VolumeProfileExposition<Bloom, float> bloomIntensity = new VolumeProfileExposition<Bloom, float>();
[SerializeField] private VolumeProfileExposition<Bloom, float> bloomThreshold = new VolumeProfileExposition<Bloom, float>();
protected override IEnumerable<VolumeProfileExposition> Exposition
{
get
{
yield return bloomIntensity;
yield return bloomThreshold;
}
}
protected override void InitializeExpositions()
{
bloomIntensity.Initialize(bloom => bloom.intensity);
bloomThreshold.Initialize(bloom => bloom.threshold);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment