Advertisement
Guest User

Custom Voronoi Shader

a guest
Dec 31st, 2014
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. //Custom Voronoi shader
  2. //I've divided the image into blocks where each block contains a point
  3. //every point's offset from the block's corner is stored in PointsSampler(a noisy texture)
  4. float2 BlockSize : register(C0);
  5. float2 BlockOffset : register(C1);
  6. float2 pSize : register(C2);
  7. sampler2D ImageSampler : register(S0);
  8. sampler2D PointsSampler : register(S1);
  9.  
  10. float4 main(float2 uv : TEXCOORD) : COLOR
  11. {
  12. //get coresponding point from noisy texture
  13. //convert uv from [0,1] to actual pixels
  14. float bx = floor((uv.x * pSize.x - BlockOffset.x) / BlockSize.x);
  15. float by = floor((uv.y * pSize.y - BlockOffset.y) / BlockSize.y);
  16. float cx = pSize.x * uv.x;
  17. float cy = pSize.y * uv.y;
  18.  
  19. float2 centerOfBrick;
  20.  
  21. float2 bSize = float2(floor(pSize.x / BlockSize.x), floor(pSize.x / BlockSize.x));
  22.  
  23. //Here I find the closest point and how far away it is(cell1, dist1)
  24. //and the second closest point(cell2, point2)
  25. float dist1 = 10 * BlockSize.x, dist2, dist;
  26. float4 coords;
  27. float dx, dy;
  28. float2 cell1 = float2(bx,by), cell2;
  29.  
  30. for(int y = -1; y < 2; y++)
  31. for(int x = -1; x < 2; x++)
  32. {
  33. if(bx + x >= 0 && by + y >= 0 && bx + x < bSize.x && by + y < bSize.y)
  34. {
  35. coords = tex2D(PointsSampler, float2((bx + x)/bSize.x,(by + y)/bSize.y));
  36. dx = coords.r * 255 + (bx + x) * BlockSize.x + BlockOffset.x - cx;
  37. dy = coords.g * 255 + (by + y) * BlockSize.y + BlockOffset.y - cy;
  38. dist = sqrt(dx*dx + dy*dy);
  39.  
  40. if(dist < dist1)
  41. {
  42. dist2 = dist1;
  43. cell2 = cell1;
  44.  
  45. dist1 = dist;
  46. cell1 = float2(bx + x, by + y);
  47. }
  48. }
  49. }
  50.  
  51. // You may ignore this following part
  52. //pixelated color
  53. centerOfBrick.x = (0.5f + cell1.x) * BlockSize.x + BlockOffset.x;
  54. centerOfBrick.x /= pSize.x;
  55. centerOfBrick.y = (0.5f + cell1.y) * BlockSize.y + BlockOffset.y;
  56. centerOfBrick.y /= pSize.y;
  57. float4 imgcolor = tex2D(ImageSampler, centerOfBrick);
  58.  
  59. imgcolor.r = 0;
  60. imgcolor.g = 0;
  61. imgcolor.b = 0;
  62.  
  63. imgcolor.r = 1 - (dist1) / 5;
  64.  
  65. //this line somehow clears the red value for some pixels
  66. imgcolor.g = 1 - min(1,max(0,dist2)); //clip dist2 to [0,1]
  67.  
  68. imgcolor.a = 1;
  69.  
  70.  
  71.  
  72. return imgcolor;//rgba
  73. }
  74.  
  75.  
  76. technique Tech
  77. {
  78. pass Pss
  79. {
  80. pixelshader = compile ps_3_0 main();
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement