Skip to content

Commit

Permalink
at last voronoi distance
Browse files Browse the repository at this point in the history
  • Loading branch information
monkstone committed Jun 12, 2020
1 parent 939d117 commit 27998f7
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
84 changes: 84 additions & 0 deletions processing_app/topics/shaders/data/voronoi_distance.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// After Inigo Quilez adapted for processing by Martin Prout
// The MIT License
// Copyright © 2013 Inigo Quilez

#ifdef GL_ES
precision mediump float;
precision mediump int;
#endif

uniform vec3 iResolution;
uniform float iTime;
uniform sampler2D texture;

void mainImage( out vec4 fragColor, in vec2 fragCoord );

void main() {
mainImage(gl_FragColor,gl_FragCoord.xy);
}

vec2 hash2( vec2 p )
{
// procedural white noise
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);

//----------------------------------
// 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 = 0.5 + 0.5*sin( iTime + 6.2831 * hash2( n + g ));
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 = 0.5 + 0.5*sin( iTime + 6.2831 * hash2( n + g ) );
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 mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 p = fragCoord/iResolution.xx;

vec3 c = voronoi( 8.0*p );

// isolines
vec3 col = c.x*(0.5 + 0.5*sin(64.0*c.x))*vec3(1.0);
// borders
col = mix( vec3(1.0,0.6,0.0), col, smoothstep( 0.04, 0.07, c.x ) );
// feature points
float dd = length( c.yz );
col = mix( vec3(1.0,0.6,0.1), col, smoothstep( 0.0, 0.12, dd) );
col += vec3(1.0,0.6,0.1)*(1.0-smoothstep( 0.0, 0.04, dd));

fragColor = vec4(col,1.0);
}
38 changes: 38 additions & 0 deletions processing_app/topics/shaders/voronoi_distance.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env jruby -v -W2
# frozen_string_literal: true
require 'propane'

##
# Voronoi Distance.
#
# GLSL version of the 1k intro Voronoi from the demoscene
# (http://www.pouet.net/prod.php?which=52761)
# Ported from the webGL version available in ShaderToy:
# http://www.iquilezles.org/apps/shadertoy/
# (Look for Voronoi under the Plane Deformations Presets)
class VoronoiDistance < Propane::App
attr_reader :voronoi

def setup
sketch_title 'Voronoi Distance'
no_stroke
@voronoi = load_shader(data_path('voronoi_distance.glsl'))
voronoi.set('iResolution', width.to_f, height.to_f, 0.0)
end

def draw
voronoi.set('iTime', millis / 1000.0)
shader(voronoi)
# This kind of effects are entirely implemented in the
# fragment shader, they only need a quad covering the
# entire view area so every pixel is pushed through the
# shader.
rect(0, 0, width, height)
end

def settings
size(640, 360, P2D)
end
end

VoronoiDistance.new

0 comments on commit 27998f7

Please sign in to comment.