Skip to content

Instantly share code, notes, and snippets.

@phosphoer
Created March 28, 2018 18:44
Show Gist options
  • Save phosphoer/5ef30cf0f045f2f300b3319f8705963b to your computer and use it in GitHub Desktop.
Save phosphoer/5ef30cf0f045f2f300b3319f8705963b to your computer and use it in GitHub Desktop.
Unity PostFX Profile Blending
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
using System.Collections;
public abstract class PostFXBlend : MonoBehaviour
{
public float FadeInTime = 1.0f;
public float FadeOutTime = 1.0f;
public float BlendWeightDebug;
public float BlendWeight
{
get { return _volume.weight; }
set { _volume.weight = value; BlendWeightDebug = value; }
}
private PostProcessVolume _volume = null;
private PostProcessEffectSettings[] _effect = null;
protected virtual PostProcessEffectSettings[] CreateEffect()
{
return null;
}
private void OnEnable()
{
ResetEffect();
}
private void OnDisable()
{
if (_volume != null)
{
RuntimeUtilities.DestroyVolume(_volume, false);
_volume = null;
}
}
public void ResetEffect()
{
_effect = CreateEffect();
if (_effect != null)
{
if (_volume != null)
{
RuntimeUtilities.DestroyVolume(_volume, false);
}
_volume = PostProcessManager.instance.QuickVolume(gameObject.layer, 100f, _effect);
_volume.weight = 0;
_volume.isGlobal = true;
}
}
public Coroutine FadeIn()
{
return StartCoroutine(FadeInRoutine());
}
public Coroutine FadeOut()
{
return StartCoroutine(FadeOutRoutine());
}
private IEnumerator FadeInRoutine()
{
while (_volume == null)
{
yield return null;
}
for (float time = 0; time < FadeInTime; time += Time.deltaTime)
{
_volume.weight = time / FadeInTime;
yield return null;
}
_volume.weight = 1.0f;
}
private IEnumerator FadeOutRoutine()
{
for (float time = 0; time < FadeOutTime; time += Time.deltaTime)
{
_volume.weight = Mathf.Lerp(1.0f, 0.0f, time / FadeOutTime);
yield return null;
}
_volume.weight = 0.0f;
Destroy(gameObject);
}
}
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
public class PostFXBlendAsset : PostFXBlend
{
public PostProcessEffectSettings[] EffectSettings;
protected override PostProcessEffectSettings[] CreateEffect()
{
return EffectSettings;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment