Skip to content

Instantly share code, notes, and snippets.

@Donnotron666
Last active December 7, 2017 23:12
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Donnotron666/a2a51b27d489bcaa500d819fea469da0 to your computer and use it in GitHub Desktop.
Save Donnotron666/a2a51b27d489bcaa500d819fea469da0 to your computer and use it in GitHub Desktop.
using Client.Game.Managers;
using Client.Game.Utils;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.PostProcessing;
namespace Client.Game.Cameras
{
public class PostProcessingController
{
private PostProcessingProfile Profile;
private DepthOfFieldModel.Settings DOFSettings;
private BloomModel.Settings BloomSettings;
private ChromaticAberrationModel.Settings ChromSettings;
public PostFXStack CameraDistance = new PostFXStack();
private PostFXStack Last = new PostFXStack();
public HashSet<PostFXStack> StackedFX = new HashSet<PostFXStack>();
private Queue<PostFXStack> OneTimeApplies = new Queue<PostFXStack>();
private Transform TargetTransform;
private const float MIN_APPERATURE = .2f;
private float SettingsAperture;
public PostProcessingController(CameraManager cameraManager)
{
TargetTransform = Camera.main.transform;
var behaviour = TargetTransform.GetComponent<PostProcessingBehaviour>();
Profile = GameObject.Instantiate(behaviour.profile);
behaviour.profile = Profile;
DOFSettings = Profile.depthOfField.settings;
CameraDistance.FocusDistance = DOFSettings.focusDistance;
SettingsAperture = DOFSettings.aperture;
CameraDistance.FocalLength = DOFSettings.focalLength;
BloomSettings = Profile.bloom.settings;
CameraDistance.BloomIntensity = BloomSettings.bloom.intensity;
ChromSettings = Profile.chromaticAberration.settings;
CameraDistance.AberrationIntensity = ChromSettings.intensity;
}
private void UpdateDOFDistance()
{
float pct = (TargetTransform.position.z - CameraManager.CAMERA_DISTANCE + CameraManager.PUNCHED_IN_DISTANCE) / (CameraManager.CAMERA_DISTANCE - CameraManager.PUNCHED_IN_DISTANCE);
CameraDistance.Aperture = Easing.QuadraticEaseInOut(pct) * SettingsAperture;
CameraDistance.FocusDistance = -TargetTransform.position.z;
}
public void Update(float dt)
{
UpdateDOFDistance();
Flush(dt);
}
private void Flush(float dt)
{
var combined = new PostFXStack() + CameraDistance;
foreach( var stacked in StackedFX)
{
combined += stacked;
}
Last = PostFXStack.Lerp(Last, combined, 2f * dt);
while (OneTimeApplies.Count > 0)
{
Last += OneTimeApplies.Dequeue();
}
DOFSettings.focusDistance = Last.FocusDistance;
DOFSettings.aperture = Mathf.Clamp(Last.Aperture, MIN_APPERATURE, SettingsAperture);
DOFSettings.focalLength = Last.FocalLength;
Profile.depthOfField.settings = DOFSettings;
BloomSettings.bloom.intensity = Last.BloomIntensity;
Profile.bloom.settings = BloomSettings;
ChromSettings.intensity = Last.AberrationIntensity;
Profile.chromaticAberration.settings = ChromSettings;
}
public void Add(PostFXStack stacked)
{
StackedFX.Add(stacked);
}
//Adds it to a one-time apply, which doesn't have to be unapplied ever
public void QuickAdd(PostFXStack fx)
{
OneTimeApplies.Enqueue(fx);
}
public void Remove(PostFXStack stacked)
{
StackedFX.Remove(stacked);
}
}
public class PostFXStack {
public float FocusDistance;
public float Aperture;
public float FocalLength;
public float BloomIntensity;
public float AberrationIntensity;
public static PostFXStack operator +(PostFXStack a, PostFXStack b)
{
var ret = new PostFXStack();
ret.FocusDistance = a.FocusDistance + b.FocusDistance;
ret.Aperture = a.Aperture + b.Aperture;
ret.FocalLength = a.FocalLength + b.FocalLength;
ret.BloomIntensity = a.BloomIntensity + b.BloomIntensity;
ret.AberrationIntensity = a.AberrationIntensity + b.AberrationIntensity;
return ret;
}
public static PostFXStack Lerp(PostFXStack a, PostFXStack b, float t)
{
var ret = new PostFXStack();
ret.FocusDistance = Mathf.Lerp(a.FocusDistance, b.FocusDistance, t);
ret.Aperture = Mathf.Lerp(a.Aperture, b.Aperture, t);
ret.FocalLength = Mathf.Lerp(a.FocalLength, b.FocalLength, t);
ret.BloomIntensity = Mathf.Lerp(a.BloomIntensity, b.BloomIntensity, t);
ret.AberrationIntensity = Mathf.Lerp(a.AberrationIntensity, b.AberrationIntensity, t);
return ret;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment