2
0

DynamicVignetting.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System.Linq;
  2. using Controller.Bicycle;
  3. using SicknessReduction.Visual.Rendering;
  4. using UnityEditor.Recorder.Input;
  5. using UnityEngine;
  6. using UnityEngine.Rendering.Universal;
  7. namespace SicknessReduction.Visual.Vignetting
  8. {
  9. public class DynamicVignetting : DynamicReductionSource
  10. {
  11. //TODO: cite https://www.researchgate.net/publication/326760789_Assessing_vignetting_as_a_means_to_reduce_VR_sickness_during_amplified_head_rotations
  12. //TODO: there is a patent for this https://patents.google.com/patent/US9645395B2/en
  13. //TODO: check Fernandes & Feiner
  14. [Header("Unity Objects")] public ForwardRendererData forwardRenderer;
  15. public Transform hmd;
  16. [Range(0, 1)] public float distInnerOuterRadius = .05f;
  17. //TODO: figure out what angular velocity means in my context
  18. private MaterialBlitFeature blitFeature;
  19. private Material blitFeatureMaterial;
  20. private bool blitFeatureAvailable;
  21. protected override void Start()
  22. {
  23. blitFeature = (MaterialBlitFeature) forwardRenderer.rendererFeatures.FirstOrDefault(f =>
  24. f is MaterialBlitFeature && f.name.Equals("VignetteBlitFilter"));
  25. blitFeatureAvailable = blitFeature != null;
  26. if (!blitFeatureAvailable)
  27. {
  28. Debug.LogError("VignetteBlitFilter not available!");
  29. return;
  30. };
  31. // ReSharper disable once PossibleNullReferenceException
  32. blitFeature.SetActive(true);
  33. blitFeatureMaterial = blitFeature.settings.MaterialToBlit;
  34. base.Start();
  35. }
  36. protected override void Update()
  37. {
  38. if (!blitFeatureAvailable) return;
  39. base.Update();
  40. if (currentValue > 0)
  41. {
  42. blitFeature.SetActive(true);
  43. UpdateMaterial();
  44. }
  45. else
  46. {
  47. blitFeature.SetActive(false);
  48. }
  49. }
  50. private void UpdateMaterial()
  51. {
  52. var r = Mathf.Clamp(1 - currentValue, 0, 1);
  53. blitFeatureMaterial.SetFloat("_OFOV", r);
  54. blitFeatureMaterial.SetFloat("_IFOV", Mathf.Clamp(r - distInnerOuterRadius, 0, 1));
  55. }
  56. private void OnDestroy()
  57. {
  58. if (!blitFeatureAvailable) return;
  59. blitFeature.SetActive(false);
  60. }
  61. }
  62. }