forked from patriciogonzalezvivo/thebookofshaders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tissue.frag
86 lines (73 loc) · 2.27 KB
/
tissue.frag
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Author: @patriciogv - 2015
// Title: Tissue
#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);
}
#define ANIMATE
vec3 voronoi( in vec2 x, float rnd ) {
vec2 n = floor(x);
vec2 f = fract(x);
// 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 )*rnd;
#ifdef ANIMATE
o = 0.5 + 0.5*sin( u_time + 6.2831*o );
#endif
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)*rnd;
#ifdef ANIMATE
o = 0.5 + 0.5*sin( u_time + 6.2831*o );
#endif
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 = (st-.5)*.75+.5;
if (u_resolution.y > u_resolution.x ) {
st.y *= u_resolution.y/u_resolution.x;
st.y -= (u_resolution.y*.5-u_resolution.x*.5)/u_resolution.x;
} else {
st.x *= u_resolution.x/u_resolution.y;
st.x -= (u_resolution.x*.5-u_resolution.y*.5)/u_resolution.y;
}
vec3 color = vec3(0.0);
float d = dot(st-.5,st-.5);
vec3 c = voronoi( 20.*st, pow(d,.4) );
// borders
color = mix( vec3(1.0), color, smoothstep( 0.01, 0.02, c.x ) );
// feature points
float dd = length( c.yz );
color += vec3(1.)*(1.0-smoothstep( 0.0, 0.1, dd));
gl_FragColor = vec4(color,1.0);
}