DynamicColorBlur.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [ExecuteInEditMode]
  5. public class DynamicColorBlur : MonoBehaviour
  6. {
  7. public Material blurMaterial; //create material from shader and attatch here
  8. public float sigmaMaximum = 15f;
  9. public float angularSpeedThreshold = 40f;
  10. public float angularSpeedModifier = 0.0001f;
  11. [Range(0, 10)]
  12. public int smoothness;
  13. [Range(0,4)]
  14. public float brightnessThreshold;
  15. [Range(0,1)]
  16. public float redThreshold;
  17. [Range(0,1)]
  18. public float greenThreshold;
  19. [Range(0,1)]
  20. public float blueThreshold;
  21. public bool flipThresholds = false;
  22. [Range(.01f,10)]
  23. public float sigma;
  24. float[] kernel;
  25. void Start(){
  26. //initialize to some random matrix
  27. if(blurMaterial == null){
  28. blurMaterial = Resources.Load("GingerVR-master/SicknessReductionTechniques/DynamicColorBlur/ColorBlurMat") as Material;
  29. }
  30. kernel = new float[121];
  31. angularSpeed = new Vector3(0,0,0);
  32. StartCoroutine(TrackAngularSpeed());
  33. }
  34. Vector3 angularSpeed;
  35. IEnumerator TrackAngularSpeed(){
  36. Vector3 lastRotation = new Vector3(0,0,0);
  37. while(true){
  38. yield return null;
  39. Vector3 delta = transform.localEulerAngles - lastRotation;
  40. lastRotation = transform.localEulerAngles;
  41. angularSpeed = delta / Time.deltaTime;
  42. }
  43. }
  44. public bool useEditorValue;
  45. void OnRenderImage(RenderTexture src, RenderTexture dst)
  46. {
  47. if(!useEditorValue){
  48. sigma = Mathf.Lerp(0.01f, sigmaMaximum, (angularSpeed.magnitude - angularSpeedThreshold) * angularSpeedModifier );
  49. }
  50. kernel = new float[121];
  51. //initialize to some
  52. for(int x = 0; x<11; x++){
  53. for(int y = 0; y<11; y++){
  54. kernel[y*11 + x] = GaussianFunction(x-5.0f, y-5.0f,sigma); //update kernel
  55. }
  56. }
  57. //calculate sum for later
  58. float kernelSum = 0;
  59. for(int i = 0; i<kernel.Length; i++){
  60. kernelSum+= kernel[i];
  61. }
  62. for(int i = 0; i<kernel.Length; i++){
  63. kernel[i] *= (1f/kernelSum);
  64. }
  65. //Debug.Log(kernel[4]);
  66. //Debug.Log(kernel[5]);
  67. //communicate kernel through skybox
  68. //float[] kernel = {2f,2fs,2f};
  69. blurMaterial.SetFloatArray("_kernel",kernel);
  70. blurMaterial.SetFloat("_kernelSum", kernelSum);
  71. blurMaterial.GetFloatArray("_kernel");
  72. blurMaterial.SetFloat("_brightnessThreshold", brightnessThreshold);
  73. blurMaterial.SetFloat("_redThreshold", redThreshold);
  74. blurMaterial.SetFloat("_greenThreshold", greenThreshold);
  75. blurMaterial.SetFloat("_blueThreshold", blueThreshold);
  76. if(flipThresholds){
  77. blurMaterial.SetFloat("_darkSaliency", 1);
  78. }else{
  79. blurMaterial.SetFloat("_darkSaliency", 0);
  80. }
  81. RenderTexture renderTexture = RenderTexture.GetTemporary(src.width, src.height);
  82. Graphics.Blit(src, renderTexture); //copies source texture to destination texture
  83. //apply the render texture as many iterations as specified
  84. for (int i = 0; i < smoothness; i++)
  85. {
  86. RenderTexture tempTexture = RenderTexture.GetTemporary(src.width, src.height); //creates a quick temporary texture for calculations
  87. Graphics.Blit(renderTexture, tempTexture, blurMaterial);
  88. RenderTexture.ReleaseTemporary(renderTexture); //releases the temporary texture we got from GetTemporary
  89. renderTexture = tempTexture;
  90. }
  91. Graphics.Blit(renderTexture, dst);
  92. RenderTexture.ReleaseTemporary(renderTexture);
  93. }
  94. float GaussianFunction(float x, float y, float sigma){
  95. float p1 = 1f/ ((2f*Mathf.PI) * Mathf.Pow(sigma,2f));
  96. float eExponent = -(Mathf.Pow(x,2) + Mathf.Pow(y,2)) / (2*Mathf.Pow(sigma,2));
  97. float answer = p1 * Mathf.Exp(eExponent);
  98. return answer;
  99. }
  100. }