Advertisement
NickJVaccaro

Unity3D Bloom Animation

Jan 29th, 2019
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.03 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.PostProcessing;
  6.  
  7. public class BloomTrigger : MonoBehaviour
  8. {
  9.     public float Duration;
  10.     public Vector2 IntensityRamp;
  11.     public Vector2 ThresholdRamp;
  12.     public Vector2 RadiusRamp;
  13.  
  14.     private bool _enabled;
  15.     private float _intensity, _threshold, _radius;
  16.     private bool _isRunning = false;
  17.  
  18.     public void Activate()
  19.     {
  20.         StartCoroutine(Activate_Async());
  21.     }
  22.  
  23.     private IEnumerator Activate_Async()
  24.     {
  25.         _isRunning = true;
  26.  
  27.         BloomModel bloom = GameState.Player.PostProcessing.bloom;
  28.         BloomModel.Settings settings = bloom.settings;
  29.  
  30.         // Save these values before modifying them, so we can revert them when we are done.
  31.         _intensity = settings.bloom.intensity;
  32.         _threshold = settings.bloom.threshold;
  33.         _radius = settings.bloom.radius;
  34.         _enabled = bloom.enabled;
  35.  
  36.         bloom.enabled = true;
  37.         float timer = 0;
  38.         while(timer < Duration)
  39.         {
  40.             timer += Time.deltaTime;
  41.             settings.bloom.intensity = Mathf.Lerp(IntensityRamp.x, IntensityRamp.y, timer / Duration);
  42.             settings.bloom.threshold = Mathf.Lerp(ThresholdRamp.x, ThresholdRamp.y, timer / Duration);
  43.             settings.bloom.radius = Mathf.Lerp(RadiusRamp.x, RadiusRamp.y, timer / Duration);
  44.             bloom.settings = settings;
  45.             yield return null;
  46.         }
  47.  
  48.         ResetValues();
  49.  
  50.         _isRunning = false;
  51.     }
  52.  
  53.     private void OnDestroy()
  54.     {
  55.         if (_isRunning)
  56.         {
  57.             ResetValues();
  58.         }
  59.     }
  60.  
  61.     private void ResetValues()
  62.     {
  63.         BloomModel bloom = GameState.Player.PostProcessing.bloom;
  64.         BloomModel.Settings settings = bloom.settings;
  65.  
  66.         bloom.enabled = _enabled;
  67.         settings.bloom.intensity = _intensity;
  68.         settings.bloom.threshold = _threshold;
  69.         settings.bloom.radius = _radius;
  70.  
  71.         bloom.settings = settings;
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement