본문 바로가기
College Study/GLSL

[GLSL] Voronoi

by 2den 2022. 1. 6.
728x90

 

#ifdef GL_ES
precision mediump float;
#endif

uniform vec2 u_resolution;
uniform vec2 u_mouse;

void main(){
	vec2 coord = gl_FragCoord.xy/u_resolution;
    coord.x *= u_resolution.x/u_resolution.y;
    vec2 mouse = u_mouse.xy/u_resolution;
    mouse.x *= u_resolution.x/u_resolution.y;
    
    const int num = 5;
    vec2 cell[num];
    cell[0] = vec2(0.2,0.2);
    cell[1] = vec2(0.3,0.8);
    cell[2] = vec2(0.7,0.1);
    cell[3] = vec2(0.9,0.9);
    cell[4] = mouse;
    
    float min = 1.;
	for (int i=0; i<num ; i++){
        float d = distance(cell[i], coord);
        if (d<min){
            min = d;
        }
    }
    
    
    vec3 col = vec3(min);

    gl_FragColor = vec4(col, 1.0); 
}
 

 

#ifdef GL_ES
precision mediump float;
#endif

uniform vec2 u_resolution;
uniform float u_time;

vec2 random(vec2 st){
    float x = fract(sin(dot(st, vec2(75.8,48.6)))*1e5);
    float y = fract(cos(dot(st, vec2(28.13,39.49)))*1e5);
    
    return vec2(x, y) *2. -1.;
}

float noise(vec2 st){
    vec2 i = floor(st);
    vec2 f = fract(st);
    
    f = f*f*f*(f*(f*6.-15.)+10.);
    
    vec2 v1 = i;
    vec2 v2 = i + vec2(1., 0.);
    vec2 v3 = i + vec2(0., 1.);
    vec2 v4 = i + vec2(1., 1.);
    
    vec2 r1 = random(v1);
    vec2 r2 = random(v2);
    vec2 r3 = random(v3);
    vec2 r4 = random(v4);
    
    float d1 = dot(r1, st-v1);
    float d2 = dot(r2, st-v2);
    float d3 = dot(r3, st-v3);
    float d4 = dot(r4, st-v4);
    
    float bot = mix(d1, d2, f.x);
    float top = mix(d3, d4, f.x);
    float ret = mix(bot, top, f.y);
    
    // float ret = mix(	mix( dot(random(i), st-i),
    //                        	 dot(random(i+vec2(1., 0.)), st-(i+vec2(1., 0.))),
    //                          f.x),
    //                 	mix( dot(random(i+vec2(0., 1.)), st-(i+vec2(0., 1.))),
    //                        	 dot(random(i+vec2(1., 1.)), st-(i+vec2(1., 1.))),
    //                          f.x),
    //                     f.y
    //                 );
    
    return ret;
}

vec2 noiseVec2(vec2 coord){
    float time_speed = 0.3;
    float time_diff = dot(coord.x, coord.y);
    coord += u_time * time_speed + time_diff;
    
    return vec2(noise((coord + vec2(10.550, 71.510))),
               noise((coord + vec2(-710.410, 150.650))) );
}

void main(){
	vec2 coord = gl_FragCoord.xy/u_resolution;
    const float magnification = 4.000;
    coord *= magnification;
    coord.x *= u_resolution.x/u_resolution.y;
    
    vec2 coord_i = floor(coord);
    
    float min = 1.;
    for (float x=-1.; x<2.; x++){
        for (float y=-1.; y<2.; y++){
            vec2 center = coord_i + vec2(x,y) + vec2(0.5);
            vec2 trace = noiseVec2(center);
            
            vec2 cell = center + trace;
            
            float d = distance(coord, cell);
            if (d<min){
                min = d;
            }
        }
    }
    
    
    vec3 col = vec3(min);

    gl_FragColor = vec4(col, 1.0); 
}
 
 
728x90

'College Study > GLSL' 카테고리의 다른 글

[GLSL] Fractal Brownian Motion  (0) 2022.01.06
[GLSL] Gradient Noise  (0) 2022.01.06
[GLSL] Value Noise  (0) 2022.01.06
[GLSL] Float-float Noise  (0) 2022.01.06
[GLSL] Maze and Glitch  (0) 2022.01.06

댓글