Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Custom Voronoi shader
- //I've divided the image into blocks where each block contains a point
- //every point's offset from the block's corner is stored in PointsSampler(a noisy texture)
- float2 BlockSize : register(C0);
- float2 BlockOffset : register(C1);
- float2 pSize : register(C2);
- sampler2D ImageSampler : register(S0);
- sampler2D PointsSampler : register(S1);
- float4 main(float2 uv : TEXCOORD) : COLOR
- {
- //get coresponding point from noisy texture
- //convert uv from [0,1] to actual pixels
- float bx = floor((uv.x * pSize.x - BlockOffset.x) / BlockSize.x);
- float by = floor((uv.y * pSize.y - BlockOffset.y) / BlockSize.y);
- float cx = pSize.x * uv.x;
- float cy = pSize.y * uv.y;
- float2 centerOfBrick;
- float2 bSize = float2(floor(pSize.x / BlockSize.x), floor(pSize.x / BlockSize.x));
- //Here I find the closest point and how far away it is(cell1, dist1)
- //and the second closest point(cell2, point2)
- float dist1 = 10 * BlockSize.x, dist2, dist;
- float4 coords;
- float dx, dy;
- float2 cell1 = float2(bx,by), cell2;
- for(int y = -1; y < 2; y++)
- for(int x = -1; x < 2; x++)
- {
- if(bx + x >= 0 && by + y >= 0 && bx + x < bSize.x && by + y < bSize.y)
- {
- coords = tex2D(PointsSampler, float2((bx + x)/bSize.x,(by + y)/bSize.y));
- dx = coords.r * 255 + (bx + x) * BlockSize.x + BlockOffset.x - cx;
- dy = coords.g * 255 + (by + y) * BlockSize.y + BlockOffset.y - cy;
- dist = sqrt(dx*dx + dy*dy);
- if(dist < dist1)
- {
- dist2 = dist1;
- cell2 = cell1;
- dist1 = dist;
- cell1 = float2(bx + x, by + y);
- }
- }
- }
- // You may ignore this following part
- //pixelated color
- centerOfBrick.x = (0.5f + cell1.x) * BlockSize.x + BlockOffset.x;
- centerOfBrick.x /= pSize.x;
- centerOfBrick.y = (0.5f + cell1.y) * BlockSize.y + BlockOffset.y;
- centerOfBrick.y /= pSize.y;
- float4 imgcolor = tex2D(ImageSampler, centerOfBrick);
- imgcolor.r = 0;
- imgcolor.g = 0;
- imgcolor.b = 0;
- imgcolor.r = 1 - (dist1) / 5;
- //this line somehow clears the red value for some pixels
- imgcolor.g = 1 - min(1,max(0,dist2)); //clip dist2 to [0,1]
- imgcolor.a = 1;
- return imgcolor;//rgba
- }
- technique Tech
- {
- pass Pss
- {
- pixelshader = compile ps_3_0 main();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement