diff --git a/processing_app/topics/shaders/data/voronoi_distance.glsl b/processing_app/topics/shaders/data/voronoi_distance.glsl new file mode 100644 index 0000000..8da9090 --- /dev/null +++ b/processing_app/topics/shaders/data/voronoi_distance.glsl @@ -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( d0.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); +} diff --git a/processing_app/topics/shaders/voronoi_distance.rb b/processing_app/topics/shaders/voronoi_distance.rb new file mode 100644 index 0000000..564ff18 --- /dev/null +++ b/processing_app/topics/shaders/voronoi_distance.rb @@ -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