Skip to content

Instantly share code, notes, and snippets.

@tshirtman
Last active November 15, 2019 02:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tshirtman/1fc1c8b04b4f32bf13b95fd3170fa8e8 to your computer and use it in GitHub Desktop.
Save tshirtman/1fc1c8b04b4f32bf13b95fd3170fa8e8 to your computer and use it in GitHub Desktop.
animated voronoi of mona lisa
from random import random
from itertools import chain
from kivy.uix.effectwidget import AdvancedEffectBase
from kivy.animation import Animation
from kivy.clock import Clock
from kivy.properties import ListProperty
from kivy.core.window import Window
Window.size = 300, 400
POINTS = 512 * 4
effect_voronoi = '''
uniform float points[%s];
uniform int num_points;
float length2(vec2 xy)
{
return pow(xy.x, 2.0) + pow(xy.y, 2.0);
}
vec4 effect(vec4 vcolor, sampler2D texture, vec2 texcoord, vec2 pixel_coords)
{
vec2 nearest = vec2(0., 0.);
float min_dist = 10.; // much larger than texture (1, 1)
int i;
vec2 point;
float dist;
// very costly, each pixel will go through half of the list of points on average
for (i = 0; i < num_points; i++) {
point = vec2(points[i * 2], points[i * 2 + 1]);
dist = distance(point, texcoord);
if (dist < min_dist) {
min_dist = dist;
nearest = point;
} else {
if (point.x > texcoord.x + min_dist)
break;
}
}
return texture2D(texture, nearest);
}
''' % POINTS
class VoronoiEffect(AdvancedEffectBase):
points = ListProperty()
glsl = effect_voronoi
def on_points(self, *args):
self.uniforms['num_points'] = len(self.points) // 2
self.uniforms['points'] = [float(x) for x in self.points]
if __name__ == '__main__':
import kivy
from kivy.app import App
from kivy.factory import Factory
class Application(App):
random_points = ListProperty()
touches = ListProperty()
def build(self):
self.voronoi = voronoi = VoronoiEffect()
effects = [voronoi]
root = Factory.EffectWidget(effects=effects)
self.random_points = [(random(), random()) for r in range(POINTS // 2)]
root.bind(
on_touch_down=self.create_point,
on_touch_move=self.update_point,
on_touch_up=self.remove_point,
)
root.add_widget(
Factory.AsyncImage(
source='https://upload.wikimedia.org/wikipedia/commons/thumb/e/ec/Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg/300px-Mona_Lisa%2C_by_Leonardo_da_Vinci%2C_from_C2RMF_retouched.jpg'
)
)
Clock.schedule_interval(self.animate_points, 3)
self.bind(random_points=self.update_points)
self.update_points()
return root
def animate_points(self, dt):
Animation(
random_points=[(random(), random()) for r in range(POINTS // 2)],
d=2,
t='out_quad',
).start(self)
def update_points(self, *args):
if not self.root:
return
W, H = self.root.size
points = self.random_points + [(touch.x / W, touch.y / H) for touch in self.touches]
points.sort()
self.voronoi.points = tuple(chain(*points))
def create_point(self, _, touch):
self.touches.append(touch)
self.update_points()
def update_point(self, _, touch):
self.update_points()
def remove_point(self, _, touch):
self.touches.remove(touch)
Application().run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment