game(scripts): standardise PostProcessingManager
This commit is contained in:
parent
f226e778f4
commit
9c945c535d
1 changed files with 81 additions and 102 deletions
|
@ -1,11 +1,10 @@
|
|||
/*
|
||||
Author: Reza
|
||||
Date: 7/2/25
|
||||
Description: Has all the post processing camera effects that replicate real symptoms like dizziness, fainting, panic, etc
|
||||
*/
|
||||
* Author: Reza
|
||||
* Date: 7/2/25
|
||||
* Description: Post-processing camera effects emulating various conditions
|
||||
*/
|
||||
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
using UnityEngine.Rendering.Universal;
|
||||
|
@ -17,13 +16,6 @@ public class PostProcessingManager : MonoBehaviour
|
|||
// Defines the Global Volume
|
||||
public Volume volume;
|
||||
|
||||
// All the effect overrides
|
||||
private Vignette vignette;
|
||||
private ChromaticAberration chromaticAberration;
|
||||
private MotionBlur motionBlur;
|
||||
private LensDistortion lensDistortion;
|
||||
private ColorAdjustments colorAdjustments;
|
||||
|
||||
[Header("Effect Intensities")]
|
||||
// Editable override values in inspector
|
||||
[SerializeField]
|
||||
|
@ -37,56 +29,56 @@ public class PostProcessingManager : MonoBehaviour
|
|||
|
||||
[SerializeField] public AnimationCurve colorAdjustmentsIntensity;
|
||||
|
||||
// Checks if effect is active or not
|
||||
private bool isEffectActive = false;
|
||||
|
||||
// Defines Audio References
|
||||
[Header("Audio References")]
|
||||
public AudioSource audioSource;
|
||||
[Header("Audio References")] public AudioSource audioSource;
|
||||
|
||||
public AudioClip heartbeatSound;
|
||||
public AudioClip whisperSound;
|
||||
public AudioClip distortedWhisperSound;
|
||||
private ChromaticAberration _chromaticAberration;
|
||||
private ColorAdjustments _colorAdjustments;
|
||||
|
||||
// Holds the current effect name to manage stopping and starting dynamically
|
||||
private string currentEffectName = "";
|
||||
private string _currentEffectName = "";
|
||||
|
||||
void Awake()
|
||||
// Checks if effect is active or not
|
||||
private bool _isEffectActive;
|
||||
private LensDistortion _lensDistortion;
|
||||
private MotionBlur _motionBlur;
|
||||
|
||||
// All the effect overrides
|
||||
private Vignette _vignette;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Instance == null)
|
||||
{
|
||||
Instance = this;
|
||||
}
|
||||
else
|
||||
{
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
private void Start()
|
||||
{
|
||||
// Get references for effects
|
||||
volume.profile.TryGet(out vignette);
|
||||
volume.profile.TryGet(out chromaticAberration);
|
||||
volume.profile.TryGet(out motionBlur);
|
||||
volume.profile.TryGet(out lensDistortion);
|
||||
volume.profile.TryGet(out colorAdjustments);
|
||||
volume.profile.TryGet(out _vignette);
|
||||
volume.profile.TryGet(out _chromaticAberration);
|
||||
volume.profile.TryGet(out _motionBlur);
|
||||
volume.profile.TryGet(out _lensDistortion);
|
||||
volume.profile.TryGet(out _colorAdjustments);
|
||||
}
|
||||
|
||||
// Checks if an effect is currently active
|
||||
public bool IsEffectActive()
|
||||
{
|
||||
return isEffectActive;
|
||||
return _isEffectActive;
|
||||
}
|
||||
|
||||
// Function to trigger effects dynamically based on the effect name passed
|
||||
public void TriggerEffect(string effectName)
|
||||
{
|
||||
// If an effect is already active, stop the current one
|
||||
if (isEffectActive)
|
||||
{
|
||||
StopEffect(currentEffectName);
|
||||
}
|
||||
if (_isEffectActive) StopEffect(_currentEffectName);
|
||||
|
||||
// Start the new effect
|
||||
StartEffect(effectName);
|
||||
|
@ -95,8 +87,8 @@ public class PostProcessingManager : MonoBehaviour
|
|||
// Start a specific effect
|
||||
public void StartEffect(string effectName)
|
||||
{
|
||||
isEffectActive = true;
|
||||
currentEffectName = effectName;
|
||||
_isEffectActive = true;
|
||||
_currentEffectName = effectName;
|
||||
|
||||
StartCoroutine(ApplyEffect(effectName));
|
||||
}
|
||||
|
@ -104,96 +96,83 @@ public class PostProcessingManager : MonoBehaviour
|
|||
// Stop the active effect
|
||||
public void StopEffect()
|
||||
{
|
||||
if (isEffectActive)
|
||||
{
|
||||
StopEffect(currentEffectName);
|
||||
}
|
||||
if (_isEffectActive) StopEffect(_currentEffectName);
|
||||
}
|
||||
|
||||
// Stop a specific effect
|
||||
private void StopEffect(string effectName)
|
||||
{
|
||||
isEffectActive = false;
|
||||
currentEffectName = "";
|
||||
_isEffectActive = false;
|
||||
_currentEffectName = "";
|
||||
|
||||
// Reset effects to default
|
||||
if (vignette != null) vignette.intensity.Override(0f);
|
||||
if (chromaticAberration != null) chromaticAberration.intensity.Override(0f);
|
||||
if (motionBlur != null) motionBlur.intensity.Override(0f);
|
||||
if (lensDistortion != null) lensDistortion.intensity.Override(0f);
|
||||
if (colorAdjustments != null) colorAdjustments.postExposure.Override(0f);
|
||||
if (_vignette) _vignette.intensity.Override(0f);
|
||||
if (_chromaticAberration) _chromaticAberration.intensity.Override(0f);
|
||||
if (_motionBlur) _motionBlur.intensity.Override(0f);
|
||||
if (_lensDistortion) _lensDistortion.intensity.Override(0f);
|
||||
if (_colorAdjustments) _colorAdjustments.postExposure.Override(0f);
|
||||
|
||||
// Stop the audio
|
||||
if (audioSource != null)
|
||||
{
|
||||
audioSource.Stop();
|
||||
}
|
||||
if (audioSource) audioSource.Stop();
|
||||
}
|
||||
|
||||
// Applies effects over time based on the effect name
|
||||
private IEnumerator ApplyEffect(string effectName)
|
||||
{
|
||||
// Handle audio for the effect
|
||||
if (effectName == "Panic")
|
||||
switch (effectName)
|
||||
{
|
||||
audioSource.clip = heartbeatSound;
|
||||
audioSource.loop = true;
|
||||
audioSource.Play();
|
||||
}
|
||||
else if (effectName == "Headache")
|
||||
{
|
||||
audioSource.clip = whisperSound;
|
||||
audioSource.loop = true;
|
||||
audioSource.Play();
|
||||
}
|
||||
else if (effectName == "Dizziness")
|
||||
{
|
||||
audioSource.clip = distortedWhisperSound;
|
||||
audioSource.loop = true;
|
||||
audioSource.Play();
|
||||
// Handle audio for the effect
|
||||
case "Panic":
|
||||
audioSource.clip = heartbeatSound;
|
||||
audioSource.loop = true;
|
||||
audioSource.Play();
|
||||
break;
|
||||
case "Headache":
|
||||
audioSource.clip = whisperSound;
|
||||
audioSource.loop = true;
|
||||
audioSource.Play();
|
||||
break;
|
||||
case "Dizziness":
|
||||
audioSource.clip = distortedWhisperSound;
|
||||
audioSource.loop = true;
|
||||
audioSource.Play();
|
||||
break;
|
||||
}
|
||||
|
||||
// Apply effects while the effect is active
|
||||
while (isEffectActive)
|
||||
while (_isEffectActive)
|
||||
{
|
||||
// Visual effects for headache
|
||||
if (effectName == "Headache")
|
||||
switch (effectName)
|
||||
{
|
||||
vignette.intensity.Override(vignetteIntensity.Evaluate(Time.time));
|
||||
chromaticAberration.intensity.Override(chromaticAberrationIntensity.Evaluate(Time.time));
|
||||
}
|
||||
|
||||
// Visual effects for dizziness
|
||||
if (effectName == "Dizziness")
|
||||
{
|
||||
motionBlur.intensity.Override(motionBlurIntensity.Evaluate(Time.time));
|
||||
lensDistortion.intensity.Override(lensDistortionIntensity.Evaluate(Time.time));
|
||||
}
|
||||
|
||||
// Visual effects for panic
|
||||
if (effectName == "Panic")
|
||||
{
|
||||
motionBlur.intensity.Override(motionBlurIntensity.Evaluate(Time.time));
|
||||
lensDistortion.intensity.Override(lensDistortionIntensity.Evaluate(Time.time));
|
||||
chromaticAberration.intensity.Override(chromaticAberrationIntensity.Evaluate(Time.time));
|
||||
}
|
||||
|
||||
if (effectName == "Worst")
|
||||
{
|
||||
vignette.intensity.Override(vignetteIntensity.Evaluate(Time.time));
|
||||
motionBlur.intensity.Override(motionBlurIntensity.Evaluate(Time.time));
|
||||
lensDistortion.intensity.Override(lensDistortionIntensity.Evaluate(Time.time));
|
||||
chromaticAberration.intensity.Override(chromaticAberrationIntensity.Evaluate(Time.time));
|
||||
colorAdjustments.postExposure.Override(colorAdjustmentsIntensity.Evaluate(Time.time * 0.8f));
|
||||
// Visual effects for headache
|
||||
case "Headache":
|
||||
_vignette.intensity.Override(vignetteIntensity.Evaluate(Time.time));
|
||||
_chromaticAberration.intensity.Override(chromaticAberrationIntensity.Evaluate(Time.time));
|
||||
break;
|
||||
// Visual effects for dizziness
|
||||
case "Dizziness":
|
||||
_motionBlur.intensity.Override(motionBlurIntensity.Evaluate(Time.time));
|
||||
_lensDistortion.intensity.Override(lensDistortionIntensity.Evaluate(Time.time));
|
||||
break;
|
||||
// Visual effects for panic
|
||||
case "Panic":
|
||||
_motionBlur.intensity.Override(motionBlurIntensity.Evaluate(Time.time));
|
||||
_lensDistortion.intensity.Override(lensDistortionIntensity.Evaluate(Time.time));
|
||||
_chromaticAberration.intensity.Override(chromaticAberrationIntensity.Evaluate(Time.time));
|
||||
break;
|
||||
case "Worst":
|
||||
_vignette.intensity.Override(vignetteIntensity.Evaluate(Time.time));
|
||||
_motionBlur.intensity.Override(motionBlurIntensity.Evaluate(Time.time));
|
||||
_lensDistortion.intensity.Override(lensDistortionIntensity.Evaluate(Time.time));
|
||||
_chromaticAberration.intensity.Override(chromaticAberrationIntensity.Evaluate(Time.time));
|
||||
_colorAdjustments.postExposure.Override(colorAdjustmentsIntensity.Evaluate(Time.time * 0.8f));
|
||||
break;
|
||||
}
|
||||
|
||||
yield return 0;
|
||||
}
|
||||
|
||||
// Stop audio after the effect ends
|
||||
if (audioSource != null && audioSource.isPlaying)
|
||||
{
|
||||
audioSource.Stop();
|
||||
}
|
||||
if (audioSource && audioSource.isPlaying) audioSource.Stop();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue