Advertisement
remo9211

Compute Shader Learn_L2_Part2 PassData.compute

May 21st, 2024
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. // Each #kernel tells which function to compile; you can have many kernels
  2. #pragma kernel Circles
  3. #pragma kernel Clear
  4.  
  5. // Create a RenderTexture with enableRandomWrite flag and set it
  6. // with cs.SetTexture
  7. RWTexture2D<float4> Result;
  8.  
  9. float4 clearColor;
  10. float4 circleColor;
  11. int texResolution;
  12. float time;
  13.  
  14. /*Returns pseudo random number in range 0 <= x < 1 */
  15. float random(float value, float seed = 0.546){
  16. float random = (frac(sin(value + seed) * 143758.5453));// + 1.0)/2.0;
  17. return random;
  18. }
  19.  
  20. float2 random2(float value){
  21. return float2(
  22. random(value, 3.9812),
  23. random(value, 7.1536)
  24. );
  25. }
  26.  
  27. void plot1( int x, int y, int2 centre){
  28. Result[uint2(centre.x + x, centre.y + y)] = circleColor;
  29. }
  30.  
  31. void plot8( int x, int y, int2 centre ) {
  32. plot1( x, y, centre ); plot1( y, x, centre );
  33. plot1( x, -y, centre ); plot1( y, -x, centre );
  34. plot1( -x, -y, centre ); plot1( -y, -x, centre );
  35. plot1( -x, y, centre ); plot1( -y, x, centre );
  36. }
  37.  
  38. void drawCircle( int2 centre, int radius){
  39. int x = 0;
  40. int y = radius;
  41. int d = 1 - radius;
  42.  
  43. while (x < y){
  44. if (d < 0){
  45. d += 2 * x + 3;
  46. }else{
  47. d += 2 * (x - y) + 5;
  48. y--;
  49. }
  50.  
  51. plot8(x, y, centre);
  52.  
  53. x++;
  54. }
  55. }
  56.  
  57. [numthreads(32,1,1)]
  58. void Circles (uint3 id : SV_DispatchThreadID)
  59. {
  60. int2 centre = (int2)(random2((float)id.x + time) * (float)texResolution);
  61. int radius = (int)(random((float)id.x) * 30);
  62.  
  63. drawCircle( centre, radius );
  64. }
  65.  
  66. [numthreads(8,8,1)]
  67. void Clear (uint3 id : SV_DispatchThreadID)
  68. {
  69. Result[id.xy] = clearColor;
  70. }
  71.  
  72.  
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement