1
0

DynamicVignetting.cs 2.5 KB

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