Skip to content

Commit

Permalink
Attempt of a voronoi from few to lots of cells
Browse files Browse the repository at this point in the history
  • Loading branch information
vhiribarren committed Dec 16, 2023
1 parent fb59585 commit 35398fc
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/app/sketches/voronoi-stretched/fragment.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#pragma glslify: random = require(../../../glsl-modules/random)

varying vec2 v_uv;

const float INFINITY = 1.0 / 0.0;
const float FREQUENCE = 31.0;
const float LINE_PRECISION = 0.5;


ivec2 uv_to_grid(vec2 uv) {
//return ivec2(uv/2.0);
//return ivec2(uv.x, log2(uv.y + 1.0));
return ivec2(uv.x, log2(uv.y + 1.0));
}

vec2 gen_cell_at_grid(int x, int y) {
//return 2.0*(vec2(x, y) + 0.5);
//return vec2(float(x), exp2(float(y))-1.0)*(vec2(1.0, 1.0));
//return vec2(float(x)+0.5, exp2(float(y-1))-1.0) + vec2(0.0, 0.5) * vec2(float(x), exp2(float(y-1)));
return vec2(float(x), exp2(float(y-1))-1.0) + vec2(random(vec2(y, x)), random(vec2(x, y))) * vec2(1.0, exp2(float(y-1)));
}

void main() {
vec2 canvas_size = gl_FragCoord.xy / v_uv;
float canvas_ratio = canvas_size.x / canvas_size.y;
vec2 uv = v_uv;
//uv.y = 1.0 - uv.y;
uv *= vec2(canvas_ratio, 1.0); // Cancel screen deformation

//TODO faire aussi du voronoi avec des octaves, des grosses cellules replies de petites cellules avec un coeff de ligne qui baisse

uv *= FREQUENCE; // zoom out

ivec2 grid = uv_to_grid(uv);
float min_dist = INFINITY;
float snd_min_dist = INFINITY;
vec2 min_vcell;
int scan_max = int(exp2(float(grid.y))); // 1;
for (int i = -scan_max; i <= scan_max; i++) {
for (int j = -scan_max; j <= scan_max; j++) {
vec2 current_vcell = gen_cell_at_grid(grid.x + i, grid.y + j);
float vcell_dist = distance(uv, current_vcell);
if (vcell_dist < min_dist) {
snd_min_dist = min_dist;
min_dist = vcell_dist;
min_vcell = current_vcell;
}
}
}

// Cell dots
float dot_col = 1.0-step(0.1, min_dist);
// Web
float web_col;
if (abs(min_dist - snd_min_dist) < LINE_PRECISION) {
web_col = 1.0;
}
// Worley field
float worley_col = 0.4*min_dist;
// Grid
float grid_col = step(0.98, fract(uv.y)) + step(0.98, fract(uv.x));

// Final color
vec3 fragCol;
//fragCol.r = grid_col;
fragCol = max(fragCol, vec3(dot_col, dot_col, 0.0));
//fragCol = max(fragCol, vec3(worley_col));
fragCol.b = max(fragCol.b, (web_col));
gl_FragColor = vec4(fragCol, 1.0);
}
12 changes: 12 additions & 0 deletions src/app/sketches/voronoi-stretched/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"use client";

import fragmentShader from "./fragment.glsl";
import BasicFragmentShader from "@/components/shaders/BasicFragmentShader";

export default function Page() {
return (
<BasicFragmentShader
title="Voronoi Stretched"
fragmentShader={fragmentShader} />
);
}

0 comments on commit 35398fc

Please sign in to comment.