From 09d296149d9ea2e9199dd4508a4729c30c07ff6e Mon Sep 17 00:00:00 2001 From: rezazfn Date: Fri, 7 Feb 2025 16:12:13 +0800 Subject: [PATCH] game(scripts): making a postprocessingmanager to handle all effects + edits --- Game/Assets/Scripts/BrushTeeth.cs | 81 +---------- Game/Assets/Scripts/PostProcessingManager.cs | 132 ++++++++++++++++++ .../Scripts/PostProcessingManager.cs.meta | 11 ++ Game/Assets/Scripts/VignetteBreathing.cs | 34 +++++ Game/Assets/Scripts/VignetteBreathing.cs.meta | 11 ++ 5 files changed, 194 insertions(+), 75 deletions(-) create mode 100644 Game/Assets/Scripts/PostProcessingManager.cs create mode 100644 Game/Assets/Scripts/PostProcessingManager.cs.meta create mode 100644 Game/Assets/Scripts/VignetteBreathing.cs create mode 100644 Game/Assets/Scripts/VignetteBreathing.cs.meta diff --git a/Game/Assets/Scripts/BrushTeeth.cs b/Game/Assets/Scripts/BrushTeeth.cs index a022481..7ba082e 100644 --- a/Game/Assets/Scripts/BrushTeeth.cs +++ b/Game/Assets/Scripts/BrushTeeth.cs @@ -2,7 +2,9 @@ Author: Wai Lam Date: 27/1/25 Description: Bathroom interaction -*/using System.Collections; +*/ + +using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; @@ -12,80 +14,9 @@ using UnityEngine.XR.Interaction.Toolkit.Interactors; public class BrushTeeth : MonoBehaviour { - // public Slider progressBar; // Reference to the Slider (progress bar) - // public float progressTime = 5f; // Time for the progress bar to complete - // private UnityEngine.XR.Interaction.Toolkit.Interactables.XRGrabInteractable grabInteractable; - // private float timer = 0f; - // private bool isGrabbing = false; - // - // void Start() - // { - // grabInteractable = GetComponent(); - // - // if (grabInteractable == null) - // { - // Debug.LogError("XRGrabInteractable component not found on the object!"); - // return; - // } - // - // // Ensure the progress bar is hidden initially - // progressBar.gameObject.SetActive(false); - // - // // Subscribe to grab and release events - // grabInteractable.selectEntered.AddListener(OnGrab); - // grabInteractable.selectExited.AddListener(OnRelease); - // } - // - // void Update() - // { - // if (isGrabbing) - // { - // timer += Time.deltaTime; - // progressBar.value = timer / progressTime; - // - // if (timer >= progressTime) - // { - // CompleteProgress(); - // } - // } - // } - // - // private void OnGrab(SelectEnterEventArgs args) - // { - // // Show and reset the progress bar - // progressBar.gameObject.SetActive(true); - // progressBar.value = 0f; - // timer = 0f; - // isGrabbing = true; - // } - // - // private void OnRelease(SelectExitEventArgs args) - // { - // // Hide the progress bar and stop the timer - // progressBar.gameObject.SetActive(false); - // isGrabbing = false; - // timer = 0f; - // } - // - // private void CompleteProgress() - // { - // // Hide the progress bar and perform any additional actions when complete - // progressBar.gameObject.SetActive(false); - // isGrabbing = false; - // - // Debug.Log("Progress completed!"); - // } - // - // private void OnDestroy() - // { - // // Unsubscribe from events to avoid memory leaks - // if (grabInteractable != null) - // { - // grabInteractable.selectEntered.RemoveListener(OnGrab); - // grabInteractable.selectExited.RemoveListener(OnRelease); - // } - // } - public Slider progressBar; // Reference to the Slider (progress bar) + public PostProcessingManager postProcessingManager; + + public Slider progressBar; // Reference to the Slider (progress bar) public float progressTime = 5f; // Time for the progress bar to complete private XRGrabInteractable grabInteractable; private float timer = 0f; diff --git a/Game/Assets/Scripts/PostProcessingManager.cs b/Game/Assets/Scripts/PostProcessingManager.cs new file mode 100644 index 0000000..4189138 --- /dev/null +++ b/Game/Assets/Scripts/PostProcessingManager.cs @@ -0,0 +1,132 @@ +/* +Author: Reza +Date: 7/2/25 +Description: Has all the post processing camera effects that replicate real symptoms like dizziness, fainting, panic, etc +*/ + +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.Rendering; +using UnityEngine.Rendering.Universal; + +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] + public AnimationCurve vignetteIntensity; + + [SerializeField] + public AnimationCurve chromaticAberrationIntensity; + + [SerializeField] + public AnimationCurve motionBlurIntensity; + + [SerializeField] + public AnimationCurve lensDistortionIntensity; + + [SerializeField] + public AnimationCurve colorAdjustmentsIntensity; + + // Checks if effect is active or not + private bool isEffectActive = false; + + // Start is called before the first frame update + 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); + } + + // Update is called once per frame + void Update() + { + + } + + // Function to start effects if there is none currently, and stop current effects to start next ones + public void TriggerEffect(string effectName) + { + // If an effect is already active + if (isEffectActive) + { + // Stop the effect + StopEffect(effectName); + } + + // If there's no active effects + else + { + // Start the effect + StartEffect(effectName); + } + } + + // Function to start the effect + private void StartEffect(string effectName) + { + // The effect is active + isEffectActive = true; + + // Calls coroutine to apply the effect + StartCoroutine(ApplyEffect(effectName)); + } + + // Function to stop the effect + private void StopEffect(string effectName) + { + // The effect is not active + isEffectActive = false; + + // Reset effects to default + vignette.intensity.Override(0f); + chromaticAberration.intensity.Override(0f); + motionBlur.intensity.Override(0f); + lensDistortion.intensity.Override(0f); + colorAdjustments.postExposure.Override(0f); + } + + // Applies effects over time based on the type + private IEnumerator ApplyEffect(string effectName) + { + float time = 0f; + float timeToMax = 1f; + + while (time < timeToMax) + { + time += Time.deltaTime; + time = Mathf.Clamp01(time); + + // Values for headache effect + if (effectName == "Headache") + { + vignette.intensity.Override(vignetteIntensity.Evaluate(time)); + chromaticAberration.intensity.Override(chromaticAberrationIntensity.Evaluate(time)); + } + + // Values for dizziness effect + else if (effectName == "Dizziness") + { + motionBlur.intensity.Override(motionBlurIntensity.Evaluate(time)); + lensDistortion.intensity.Override(lensDistortionIntensity.Evaluate(time)); + } + + yield return null; + } + } +} diff --git a/Game/Assets/Scripts/PostProcessingManager.cs.meta b/Game/Assets/Scripts/PostProcessingManager.cs.meta new file mode 100644 index 0000000..0534b81 --- /dev/null +++ b/Game/Assets/Scripts/PostProcessingManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 46e96ab748a3a844f9137261de774bfe +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Game/Assets/Scripts/VignetteBreathing.cs b/Game/Assets/Scripts/VignetteBreathing.cs new file mode 100644 index 0000000..d315fba --- /dev/null +++ b/Game/Assets/Scripts/VignetteBreathing.cs @@ -0,0 +1,34 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.Rendering; +using UnityEngine.Rendering.Universal; + +public class VignetteBreathing : MonoBehaviour +{ + [SerializeField] + public Volume postProcessingVolume; + + private Vignette vignette; + + [SerializeField] + public AnimationCurve intensityCurve; // Assign in Inspector + public float cycleDuration = 3f; // Time for one full cycle + + void Start() + { + if (postProcessingVolume.profile.TryGet(out vignette)) + { + Debug.Log("Vignette found!"); + } + } + + void Update() + { + if (vignette != null) + { + float t = (Time.time % cycleDuration) / cycleDuration; // Loop 0-1 over time + vignette.intensity.Override(intensityCurve.Evaluate(t)); + } + } +} \ No newline at end of file diff --git a/Game/Assets/Scripts/VignetteBreathing.cs.meta b/Game/Assets/Scripts/VignetteBreathing.cs.meta new file mode 100644 index 0000000..b2a3bb8 --- /dev/null +++ b/Game/Assets/Scripts/VignetteBreathing.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4e25f510b8cc01f4e8a29b3995d1bde9 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: