Posted By: jenGs
Voronoi Cell Position - 06/04/14 14:42
Hi,
I converted this shader from glsl into hlsl.
It gives the distance and a gradient from the input coordinates to the cell center in relation to the voronoi cell to which it belongs to.
Don't ask me how the math works here. I saw this shader had a basic understanding and converted it. It works great.
I have only added the id to the original to color the cells differently.
What I need:
I need the distance of the cell core or the cell middle to the middle of the image. I have some trouble to bring the voronoi point into the 0.0 - 1.0 space.
why?
I want to create an island generator. The cells outside the defined range should be black and the rest white (don't worry randomness can be achieved through a second permutation map). The color should be changed by cell, not by pixel coordinate.
I converted this shader from glsl into hlsl.
It gives the distance and a gradient from the input coordinates to the cell center in relation to the voronoi cell to which it belongs to.
Don't ask me how the math works here. I saw this shader had a basic understanding and converted it. It works great.
I have only added the id to the original to color the cells differently.
Code:
float2 hash2(float2 p) { return tex2D(permSampler2d, (p + 0.5) / 256.0).rg; } float4 voronoi( in float2 x) { float2 n = floor(x); float2 f = frac(x); //---------------------------------- // first pass: regular voronoi //---------------------------------- float2 mg; float2 mr; float id; float md = 8.0; for( int j = -1; j < 2; j++ ) { for( int i = -1; i < 2; i++ ) { float2 g = float2(i, j); float2 o = hash2( n + g ); float2 r = g + o - f; float d = dot(r, r); if( d < md ) { md = d; mr = r; mg = g; id = length(o); } } } //---------------------------------- // second pass: distance to borders //---------------------------------- md = 8.0; for( int j=-2; j<=2; j++ ) { for( int i=-2; i<=2; i++ ) { float2 g = mg + float2(i, j); float2 o = hash2( n + g ); float2 r = g + o - f; if( dot(mr - r, mr - r) > 0.00001 ) md = min( md, dot( 0.5 * ( mr + r ), normalize( r - mr ) ) ); } } return float4( md, mr, id); } float4 postprocessing_noise( float2 Tex : TEXCOORD0 ) : COLOR0 { float2 p; p.x = Tex.x + vecSkill1[0]; // vecSkill - offset_x p.y = Tex.y + vecSkill1[1]; // vecSkill - offset_y float4 c = voronoi( vecSkill1[2] * p ); // vecSkill frequency [...]
What I need:
I need the distance of the cell core or the cell middle to the middle of the image. I have some trouble to bring the voronoi point into the 0.0 - 1.0 space.
why?
I want to create an island generator. The cells outside the defined range should be black and the rest white (don't worry randomness can be achieved through a second permutation map). The color should be changed by cell, not by pixel coordinate.