Skip to content

Commit 27998f7

Browse files
committed
at last voronoi distance
1 parent 939d117 commit 27998f7

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// After Inigo Quilez adapted for processing by Martin Prout
2+
// The MIT License
3+
// Copyright © 2013 Inigo Quilez
4+
5+
#ifdef GL_ES
6+
precision mediump float;
7+
precision mediump int;
8+
#endif
9+
10+
uniform vec3 iResolution;
11+
uniform float iTime;
12+
uniform sampler2D texture;
13+
14+
void mainImage( out vec4 fragColor, in vec2 fragCoord );
15+
16+
void main() {
17+
mainImage(gl_FragColor,gl_FragCoord.xy);
18+
}
19+
20+
vec2 hash2( vec2 p )
21+
{
22+
// procedural white noise
23+
return fract(sin(vec2(dot(p,vec2(127.1,311.7)),dot(p,vec2(269.5,183.3))))*43758.5453);
24+
}
25+
26+
vec3 voronoi( in vec2 x )
27+
{
28+
vec2 n = floor(x);
29+
vec2 f = fract(x);
30+
31+
//----------------------------------
32+
// first pass: regular voronoi
33+
//----------------------------------
34+
vec2 mg, mr;
35+
36+
float md = 8.0;
37+
for( int j=-1; j<=1; j++ )
38+
for( int i=-1; i<=1; i++ )
39+
{
40+
vec2 g = vec2(float(i),float(j));
41+
vec2 o = 0.5 + 0.5*sin( iTime + 6.2831 * hash2( n + g ));
42+
vec2 r = g + o - f;
43+
float d = dot(r,r);
44+
if( d<md )
45+
{
46+
md = d;
47+
mr = r;
48+
mg = g;
49+
}
50+
}
51+
52+
//----------------------------------
53+
// second pass: distance to borders
54+
//----------------------------------
55+
md = 8.0;
56+
for( int j=-2; j<=2; j++ )
57+
for( int i=-2; i<=2; i++ )
58+
{
59+
vec2 g = mg + vec2(float(i),float(j));
60+
vec2 o = 0.5 + 0.5*sin( iTime + 6.2831 * hash2( n + g ) );
61+
vec2 r = g + o - f;
62+
if( dot(mr-r,mr-r)>0.00001 )
63+
md = min( md, dot( 0.5*(mr+r), normalize(r-mr) ) );
64+
}
65+
return vec3( md, mr );
66+
}
67+
68+
void mainImage( out vec4 fragColor, in vec2 fragCoord )
69+
{
70+
vec2 p = fragCoord/iResolution.xx;
71+
72+
vec3 c = voronoi( 8.0*p );
73+
74+
// isolines
75+
vec3 col = c.x*(0.5 + 0.5*sin(64.0*c.x))*vec3(1.0);
76+
// borders
77+
col = mix( vec3(1.0,0.6,0.0), col, smoothstep( 0.04, 0.07, c.x ) );
78+
// feature points
79+
float dd = length( c.yz );
80+
col = mix( vec3(1.0,0.6,0.1), col, smoothstep( 0.0, 0.12, dd) );
81+
col += vec3(1.0,0.6,0.1)*(1.0-smoothstep( 0.0, 0.04, dd));
82+
83+
fragColor = vec4(col,1.0);
84+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env jruby -v -W2
2+
# frozen_string_literal: true
3+
require 'propane'
4+
5+
##
6+
# Voronoi Distance.
7+
#
8+
# GLSL version of the 1k intro Voronoi from the demoscene
9+
# (http://www.pouet.net/prod.php?which=52761)
10+
# Ported from the webGL version available in ShaderToy:
11+
# http://www.iquilezles.org/apps/shadertoy/
12+
# (Look for Voronoi under the Plane Deformations Presets)
13+
class VoronoiDistance < Propane::App
14+
attr_reader :voronoi
15+
16+
def setup
17+
sketch_title 'Voronoi Distance'
18+
no_stroke
19+
@voronoi = load_shader(data_path('voronoi_distance.glsl'))
20+
voronoi.set('iResolution', width.to_f, height.to_f, 0.0)
21+
end
22+
23+
def draw
24+
voronoi.set('iTime', millis / 1000.0)
25+
shader(voronoi)
26+
# This kind of effects are entirely implemented in the
27+
# fragment shader, they only need a quad covering the
28+
# entire view area so every pixel is pushed through the
29+
# shader.
30+
rect(0, 0, width, height)
31+
end
32+
33+
def settings
34+
size(640, 360, P2D)
35+
end
36+
end
37+
38+
VoronoiDistance.new

0 commit comments

Comments
 (0)