Created
February 3, 2022 04:48
-
-
Save ogrew/35b908fd049e3c053f819f164e828364 to your computer and use it in GitHub Desktop.
2D Voronoi Fetch Texture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
uniform vec2 resolution; | |
uniform float time; | |
uniform float width; | |
uniform float scale; | |
uniform float align; | |
out vec4 fragColor; | |
#define EPS 1E-4 | |
// ref1 : https://www.shadertoy.com/view/ldl3W8# | |
// ref2 : https://scrapbox.io/sayachang/%E3%83%9C%E3%83%AD%E3%83%8E%E3%82%A4%EF%BC%81%E3%82%B7%E3%82%A7%E3%83%BC%E3%83%80%E3%83%BC%E3%81%A7%E3%83%9C%E3%83%AD%E3%83%8E%E3%82%A4%E3%82%92%E3%81%82%E3%81%9D%E3%81%BC%E3%81%86%EF%BC%81%EF%BC%81%EF%BD%9E%E7%90%86%E8%AB%96%E3%81%A8%E3%81%8B%E3%81%AF%E3%81%84%E3%81%84%E3%81%8B%E3%82%89%E3%81%A8%E3%81%AB%E3%81%8B%E3%81%8F%E4%BD%BF%E3%81%84%E6%96%B9%E3%82%92%E7%9F%A5%E3%82%8A%E3%81%9F%E3%81%84%EF%BD%9E | |
// ref3 : https://www.iquilezles.org/www/articles/voronoilines/voronoilines.htm | |
vec2 hash22( vec2 p ) | |
{ | |
vec2 v1 = vec2(127.1,311.7); | |
vec2 v2 = vec2(269.5,183.3); | |
float d1 = dot(p,v1); | |
float d2 = dot(p,v2); | |
return fract( sin( vec2(d1,d2) )*43758.5453 ); | |
} | |
vec3 voronoi( vec2 uv ) | |
{ | |
vec2 id = floor(uv); | |
vec2 gv = fract(uv); | |
float t = time * .2; | |
vec2 mg, mr; | |
vec2 mNeighbor, mPoint, mDiff; | |
float mDist; | |
// 1st step : Regular Voronoi | |
mDist = 10.; | |
for( int j=-1; j<=1; j++ ) | |
{ | |
for( int i=-1; i<=1; i++ ) | |
{ | |
vec2 neighbor = vec2( float(i), float(j) ); | |
vec2 point = hash22( id + neighbor ); | |
point = .5*sin( time + 6.2831*point ) + .5; // animation | |
vec2 diff = neighbor + point - gv; | |
float dist = dot(diff, diff); | |
if( dist < mDist ) | |
{ | |
mDist = dist; | |
mDiff = diff; | |
mNeighbor = neighbor; | |
} | |
} | |
} | |
//---------------------------------- | |
// 2nd step: Borders | |
//---------------------------------- | |
mDist = 10.; | |
for( int j=-2; j<=2; j++ ) | |
{ | |
for( int i=-2; i<=2; i++ ) | |
{ | |
vec2 neighbor = mNeighbor + vec2( float(i), float(j) ); | |
vec2 point = hash22( id + neighbor ); | |
point = .5*sin( time + 6.2831*point ) + .5; // animation | |
vec2 diff = neighbor + point - gv; | |
float dist = dot(mDiff-diff, mDiff-diff); | |
if( dist > EPS ) | |
{ | |
float dist = dot(.5*(diff + mDiff), normalize(diff - mDiff)); | |
mDist = min(mDist, dist); | |
} | |
} | |
} | |
return vec3( mDist, mDiff ); | |
} | |
void main() | |
{ | |
vec2 p = gl_FragCoord.xy/resolution.xx; | |
vec2 uv = gl_FragCoord.xy / resolution.xy; | |
vec2 st = p * scale; | |
vec3 c = voronoi( st ); | |
vec2 uvT = uv; | |
uvT.x += c.y/scale; | |
uvT.y += c.z/scale * resolution.x/resolution.y; | |
vec3 col = texture(sTD2DInputs[0], uvT).rgb; | |
// borders | |
vec3 bCol = vec3(0.); | |
col = mix( bCol, col, smoothstep( .02, .02+width, c.x ) ); | |
vec4 o = vec4(col, 1.0); | |
fragColor = TDOutputSwizzle(o); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment