Advertisement
remo9211

GaussianBlurHighlight compute shader

May 23rd, 2024
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. // Each #kernel tells which function to compile; you can have many kernels
  2. #pragma kernel HorzPass
  3. #pragma kernel Highlight
  4.  
  5. shared Texture2D<float4> source;
  6. shared RWTexture2D<float4> horzOutput;
  7. RWTexture2D<float4> outputrt;
  8. float radius;
  9. float edgeWidth;
  10. float shade;
  11. float4 center;
  12. int blurRadius;
  13. shared StructuredBuffer<float> weights;
  14.  
  15. float inCircle( float2 pt, float2 center, float radius, float edgeWidth ){
  16. float len = length(pt - center);
  17. return 1.0 - smoothstep(radius-edgeWidth, radius, len);
  18. }
  19.  
  20. [numthreads(8, 8, 1)]
  21. void HorzPass(uint3 id : SV_DispatchThreadID)
  22. {
  23. int left = max(0, (int)id.x-blurRadius);
  24. int count = min(blurRadius, (int)id.x) + min(blurRadius, source.Length.x - (int)id.x);
  25. float4 color = 0;
  26.  
  27. uint2 index = uint2((uint)left, id.y);
  28.  
  29. [unroll(100)]
  30. for(int x=0; x<count; x++){
  31. color += source[index] * weights[x];
  32. index.x++;
  33. }
  34.  
  35. horzOutput[id.xy] = color;
  36.  
  37. }
  38.  
  39. [numthreads(8, 8, 1)]
  40. void Highlight(uint3 id : SV_DispatchThreadID)
  41. {
  42. //Vert blur
  43. int top = max(0, (int)id.y-blurRadius);
  44. int count = min(blurRadius, (int)id.y) + min(blurRadius, source.Length.y - (int)id.y);
  45. float4 blurColor = 0;
  46.  
  47. uint2 index = uint2(id.x, (uint)top);
  48.  
  49. [unroll(100)]
  50. for(int y=0; y<count; y++){
  51. blurColor += horzOutput[index] * weights[y];
  52. index.y++;
  53. }
  54.  
  55. float4 srcColor = source[id.xy];
  56. float4 shadedBlurColor = blurColor * shade;
  57. float highlight = inCircle( (float2)id.xy, center.xy, radius, edgeWidth);
  58. float4 color = lerp( shadedBlurColor, srcColor, highlight );
  59.  
  60. outputrt[id.xy] = color;
  61.  
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement