Pixel-Composer/shaders/sh_cell_noise/sh_cell_noise.fsh
2022-01-13 11:24:03 +07:00

53 lines
1.2 KiB
GLSL

//
// Simple passthrough fragment shader
//
varying vec2 v_vTexcoord;
varying vec4 v_vColour;
uniform vec2 position;
uniform float scale;
uniform float time;
vec2 random2( vec2 p ) {
return fract(sin(vec2(dot(p, vec2(127.1, 311.7)), dot(p, vec2(269.5, 183.3)))) * 43758.5453);
}
void main() {
vec2 st = v_vTexcoord + position;
vec3 color = vec3(.0);
// Scale
st *= scale;
// Tile the space
vec2 i_st = floor(st);
vec2 f_st = fract(st);
float m_dist = 1.; // minimum distance
for (int y= -1; y <= 1; y++) {
for (int x= -1; x <= 1; x++) {
// Neighbor place in the grid
vec2 neighbor = vec2(float(x),float(y));
// Random position from current + neighbor place in the grid
vec2 point = random2(i_st + neighbor);
point = 0.5 + 0.5 * sin(time + 6.2831 * point);
// Vector between the pixel and the point
vec2 _diff = neighbor + point - f_st;
// Distance to the point
float dist = length(_diff);
// Keep the closer distance
m_dist = min(m_dist, dist);
}
}
// Draw the min distance (distance field)
color += m_dist;
gl_FragColor = vec4(color,1.0);
}