Last active
December 18, 2016 02:49
-
-
Save akibmayadav/98fe6c543ffb19b4580473b704405b53 to your computer and use it in GitHub Desktop.
Vornoi Distance Shader
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
<html> | |
<script type="text/javascript" src="https://rawgit.com/patriciogonzalezvivo/glslCanvas/master/build/GlslCanvas.js"></script> | |
<body> | |
<canvas class="glslCanvas" data-fragment= | |
" | |
// HW2: Shader using Random Number Generator | |
// Ambika Yadav | |
// Vornoi Distance Shader | |
// Inspired by https://thebookofshaders.com/edit.php#12/vorono-01.frag | |
#ifdef GL_ES | |
precision mediump float; | |
#endif | |
uniform vec2 u_resolution; | |
uniform vec2 u_mouse; | |
uniform float u_time; | |
vec2 random2( vec2 p ) { | |
return fract(sin(vec2(dot(p,vec2(127.1,311.7)),dot(p,vec2(269.5,183.3))))*43758.5453); | |
} | |
void main() { | |
vec2 st = gl_FragCoord.xy/u_resolution.xy; | |
st.x *= u_resolution.x/u_resolution.y; | |
vec3 color = vec3(.0); | |
st *= 30.; | |
vec2 i_st = floor(st); | |
vec2 f_st = fract(st); | |
float m_dist = 1.; | |
vec2 m_point; | |
for (int j=-1; j<=1; j++ ) { | |
for (int i=-1; i<=1; i++ ) { | |
vec2 neighbor = vec2(float(i),float(j)); | |
vec2 point = random2(i_st + neighbor); | |
point = 0.5 + 0.5*sin(u_time*3.0 + 20.2831*point); | |
vec2 diff = neighbor + point - f_st; | |
float dist = length(diff); | |
if( dist < m_dist ) { | |
m_dist = dist; | |
m_point = point; | |
} | |
} | |
} | |
color = vec3(abs(sin(m_dist*10.0)),0.3,abs(sin(m_dist))); | |
gl_FragColor = vec4(color,1.0); | |
}" | |
width="800" height="600"> </canvas> | |
</body> | |
</html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment