Back to shaders

Shader test bench

Crystal Lattice Refraction

stronghold-curated material glsl runnable fragment MIT
Source
runnable fragment

Complete GLSL fragment shader. Stronghold runs it directly when the browser can compile it.

Code

precision mediump float;
uniform vec2 resolution;
uniform float time;
vec2 hash(vec2 p){p=vec2(dot(p,vec2(127.1,311.7)),dot(p,vec2(269.5,183.3)));return fract(sin(p)*43758.5453);} 
void main(){
 vec2 uv=gl_FragCoord.xy/resolution.xy; uv.x*=resolution.x/resolution.y;
 vec2 g=uv*6.0; vec2 id=floor(g); vec2 f=fract(g);
 float d=9.0; vec2 nearest=vec2(0.0);
 for(int y=-1;y<=1;y++)for(int x=-1;x<=1;x++){vec2 o=vec2(float(x),float(y));vec2 r=hash(id+o);float nd=length(o+r-f);if(nd<d){d=nd;nearest=r;}}
 float facet=smoothstep(0.0,0.9,d);
 vec3 col=0.45+0.45*cos(vec3(0.0,2.2,4.1)+facet*6.0+nearest.x*3.0+time*0.15);
 gl_FragColor=vec4(col*(1.0-d*0.55)+0.18,1.0);
}