remo9211

GaussianBlurHighlight

May 23rd, 2024
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. [ExecuteInEditMode]
  6. public class GaussianBlurHighlight : BasePP
  7. {
  8. [Range(0, 50)]
  9. public int blurRadius = 20;
  10. [Range(0.0f, 100.0f)]
  11. public float radius = 10;
  12. [Range(0.0f, 100.0f)]
  13. public float softenEdge = 30;
  14. [Range(0.0f, 1.0f)]
  15. public float shade = 0.5f;
  16. public Transform trackedObject;
  17.  
  18. Vector4 center;
  19. ComputeBuffer weightsBuffer = null;
  20.  
  21. RenderTexture horzOutput = null;
  22. int kernelHorzPassID;
  23.  
  24. protected override void Init()
  25. {
  26. center = new Vector4();
  27. kernelName = "Highlight";
  28. kernelHorzPassID = shader.FindKernel("HorzPass");
  29. base.Init();
  30.  
  31. }
  32.  
  33. float[] SetWeightsArray(int radius, float sigma)
  34. {
  35. int total = radius * 2 + 1;
  36. float[] weights = new float[total];
  37. float sum = 0.0f;
  38.  
  39. for (int n=0; n<radius; n++)
  40. {
  41. float weight = 0.39894f * Mathf.Exp(-0.5f * n * n / (sigma * sigma)) / sigma;
  42. weights[radius + n] = weight;
  43. weights[radius - n] = weight;
  44. if (n != 0)
  45. sum += weight * 2.0f;
  46. else
  47. sum += weight;
  48. }
  49. // normalize kernels
  50. for (int i=0; i<total; i++) weights[i] /= sum;
  51.  
  52. return weights;
  53. }
  54.  
  55. private void UpdateWeightsBuffer()
  56. {
  57. if (weightsBuffer != null)
  58. weightsBuffer.Dispose();
  59.  
  60. float sigma = (float)blurRadius / 1.5f;
  61.  
  62. weightsBuffer = new ComputeBuffer(blurRadius * 2 + 1, sizeof(float));
  63. float[] blurWeights = SetWeightsArray(blurRadius, sigma);
  64. weightsBuffer.SetData(blurWeights);
  65.  
  66. shader.SetBuffer(kernelHorzPassID, "weights", weightsBuffer);
  67. shader.SetBuffer(kernelHandle, "weights", weightsBuffer);
  68. }
  69.  
  70. protected override void CreateTextures()
  71. {
  72. base.CreateTextures();
  73. shader.SetTexture(kernelHorzPassID, "source", renderedSource);
  74.  
  75. CreateTexture(ref horzOutput );
  76.  
  77. shader.SetTexture(kernelHorzPassID, "horzOutput", horzOutput);
  78. shader.SetTexture(kernelHandle, "horzOutput", horzOutput);
  79. }
  80.  
  81. private void OnValidate()
  82. {
  83. if(!init)
  84. Init();
  85.  
  86. SetProperties();
  87.  
  88. UpdateWeightsBuffer();
  89. }
  90.  
  91. protected void SetProperties()
  92. {
  93. float rad = (radius / 100.0f) * texSize.y;
  94. shader.SetFloat("radius", rad);
  95. shader.SetFloat("edgeWidth", rad * softenEdge / 100.0f);
  96. shader.SetInt("blurRadius", blurRadius);
  97. shader.SetFloat("shade", shade);
  98. }
  99.  
  100. protected override void DispatchWithSource(ref RenderTexture source, ref RenderTexture destination)
  101. {
  102. if (!init) return;
  103.  
  104. Graphics.Blit(source, renderedSource);
  105.  
  106. shader.Dispatch(kernelHorzPassID, groupSize.x, groupSize.y, 1);
  107. shader.Dispatch(kernelHandle, groupSize.x, groupSize.y, 1);
  108.  
  109. Graphics.Blit(output, destination);
  110. }
  111.  
  112. protected override void OnRenderImage(RenderTexture source, RenderTexture destination)
  113. {
  114. if (!init || shader == null)
  115. {
  116. Graphics.Blit(source, destination);
  117. }
  118. else
  119. {
  120. if (trackedObject && thisCamera)
  121. {
  122. Vector3 pos = thisCamera.WorldToScreenPoint(trackedObject.position);
  123. center.x = pos.x;
  124. center.y = pos.y;
  125. shader.SetVector("center", center);
  126. }
  127. bool resChange = false;
  128. CheckResolution(out resChange);
  129. if (resChange) SetProperties();
  130. DispatchWithSource(ref source, ref destination);
  131. }
  132. }
  133.  
  134. protected override void OnEnable()
  135. {
  136. base.OnEnable();
  137. UpdateWeightsBuffer();
  138. }
  139.  
  140. protected override void OnDisable()
  141. {
  142. base.OnDisable();
  143. if (weightsBuffer != null) weightsBuffer.Dispose();
  144. }
  145.  
  146. protected override void OnDestroy()
  147. {
  148. base.OnDestroy();
  149. if (weightsBuffer != null) weightsBuffer.Dispose();
  150. }
  151. }
  152.  
Advertisement
Add Comment
Please, Sign In to add comment