Skip to content

Instantly share code, notes, and snippets.

@parzivail
Created January 10, 2017 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzivail/1f8705b5040b3a5d0da8ba0933efb937 to your computer and use it in GitHub Desktop.
Save parzivail/1f8705b5040b3a5d0da8ba0933efb937 to your computer and use it in GitHub Desktop.
Noises
// Author: @patriciogv - 2015
// Title: Metaballs
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_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 = gl_FragCoord.xy/u_resolution.xy;
st.x *= u_resolution.x/u_resolution.y;
vec3 color = vec3(0.);
// Scale
st *= 8.;
// Tile the space
vec2 i_st = floor(st);
vec2 f_st = fract(st);
float m_dist = 5.; // minimun distance
for (int j= -1; j <= 1; j++ ) {
for (int i= -1; i <= 1; i++ ) {
// Neighbor place in the grid
vec2 neighbor = vec2(float(i),float(j));
// Random position from current + neighbor place in the grid
vec2 offset = random2(i_st + neighbor);
// Animate the offset
offset = 0.5 + 0.5*sin(u_time * 1.0 * 3.14159 * offset);
// Position of the cell
vec2 pos = neighbor + offset - f_st;
// Cell distance
float dist = length(pos);
// Metaball it!
m_dist = min(m_dist, m_dist*dist);
}
}
// Draw cells
color += m_dist;//step(0.060, m_dist);
gl_FragColor = vec4(color,1.0);
}
#ifdef GL_ES
precision mediump float;
#endif
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
// Created by inigo quilez - iq/2013
// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
// http://www.iquilezles.org/www/articles/voronoilines/voronoilines.htm
vec2 random2( vec2 p ) {
return fract(sin(vec2(dot(p,vec2(127.1,311.7)),dot(p,vec2(269.5,183.3))))*43758.5453);
}
vec3 voronoi( in vec2 x ) {
vec2 n = floor(x);
vec2 f = fract(x);
float rTime = u_time * 0.5;
// first pass: regular voronoi
vec2 mg, mr;
float md = 8.0;
for (int j= -1; j <= 1; j++) {
for (int i= -1; i <= 1; i++) {
vec2 g = vec2(float(i),float(j));
vec2 o = random2( n + g );
o = 0.5 + 0.5*sin( rTime + 6.2831*o );
vec2 r = g + o - f;
float d = dot(r,r);
if( d<md ) {
md = d;
mr = r;
mg = g;
}
}
}
// second pass: distance to borders
md = 8.0;
for (int j= -2; j <= 2; j++) {
for (int i= -2; i <= 2; i++) {
vec2 g = mg + vec2(float(i),float(j));
vec2 o = random2( n + g );
o = 0.5 + 0.5*sin( rTime + 6.2831*o );
vec2 r = g + o - f;
if ( dot(mr-r,mr-r)>0.00001 ) {
md = min(md, dot( 0.5*(mr+r), normalize(r-mr) ));
}
}
}
return vec3(md, mr);
}
void main() {
vec2 st = gl_FragCoord.xy/u_resolution.xy;
st.x *= u_resolution.x/u_resolution.y;
vec3 color = vec3(0.);
// Scale
st *= 50.;
vec3 c = voronoi(st);
color = vec3(c.x * 2.);
gl_FragColor = vec4(color,1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment