Skip to content

Instantly share code, notes, and snippets.

@gerard-geer
Created September 4, 2015 05:56
Show Gist options
  • Save gerard-geer/4d0be4fbefabe209c9b5 to your computer and use it in GitHub Desktop.
Save gerard-geer/4d0be4fbefabe209c9b5 to your computer and use it in GitHub Desktop.
Random dots in pure GLSL.
/*
The same old random function with a different signature.
*/
vec2 rand(vec2 co){
return vec2(
fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453),
fract(cos(dot(co.yx ,vec2(8.64947,45.097))) * 43758.5453)
)*2.0-1.0;
}
/*
Dot distance field.
*/
float dots(in vec2 uv)
{
// Consider the integer component of the UV coordinate
// to be an ID of a local coordinate space.
vec2 g = floor(uv);
// "What 'local coordinate space'?" you say? Why the one
// implicitly defined by the fractional component of
// the UV coordinate. Here we translate the origin to the
// center.
vec2 f = fract(uv)*2.0-1.0;
// Get a random value based on the "ID" of the coordinate
// system. This value is invariant across the entire region.
vec2 r = rand(g)*.5;
// Return the distance to that point.
return length(f+r);
}
/*
Shadertoy's entrypoint.
*/
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = fragCoord;
fragColor = vec4(smoothstep(.2, .22,dots(uv*.01)));
}
@GameKyuubi
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment