Skip to content

Instantly share code, notes, and snippets.

@caputomarcos
Created July 10, 2022 04:35
Show Gist options
  • Save caputomarcos/6196543b2d5aeb2139b491871450700a to your computer and use it in GitHub Desktop.
Save caputomarcos/6196543b2d5aeb2139b491871450700a to your computer and use it in GitHub Desktop.
GLSL: Hex Grid

GLSL: Hex Grid

This is a hex UV function from Gary Warne (et. al.). This is something of a study in hexagonal gridding.

A Pen by Liam Egan on CodePen.

License.

<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/88/three.min.js"></script>
<script id="vertexShader" type="x-shader/x-vertex">
void main() {
gl_Position = vec4( position, 1.0 );
}
</script>
<script id="fragmentShader" type="x-shader/x-fragment">
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
uniform sampler2D u_noise;
#define PI 3.141592653589793
// Helper vector. If you're doing anything that involves regular triangles or hexagons, the
// 30-60-90 triangle will be involved in some way, which has sides of 1, sqrt(3) and 2.
const vec2 s = vec2(1, 1.7320508);
vec2 hash2(vec2 p)
{
vec2 o = texture2D( u_noise, (p+0.5)/256.0, -100.0 ).xy;
return o;
}
// hex(uv)
// getHex(uv)
// -------------
// These very very helpful functions are by Gary Warne. I hope to understand it one day, but for now it's a black box :)
//
// -------------
float hex(in vec2 p){
p = abs(p);
return max(dot(p, s*.5), p.x); // Hexagon.
}
vec4 getHex(vec2 p){
vec4 hC = floor(vec4(p, p - vec2(.5, 1))/s.xyxy) + .5;
vec4 h = vec4(p - hC.xy*s, p - (hC.zw + .5)*s);
return dot(h.xy, h.xy)<dot(h.zw, h.zw) ? vec4(h.xy, hC.xy) : vec4(h.zw, hC.zw + vec2(.5, 1));
}
const float divisor = 8.;
vec3 getColour(float col_id) {
col_id = mod(col_id, divisor);
vec3 col = vec3(.16, .15, .18);
if(col_id == 0.) {
col = vec3(.78, .78, .29);
} else if(col_id == 1.) {
col = vec3(.29, .62, .64);
} else if(col_id == 2.) {
col = vec3(.18, .46, .4);
} else if(col_id == 3.) {
col = vec3(.18, .22, .24);
}
return col;
}
// vec3 easeOutQuad(in float current, in vec3 begin_value, in vec3 change_value, in float duration) {
// return -change_value *(current/=duration)*(current-2.) + begin_value;
// }
vec3 easeOutQuad(float t, vec3 b, vec3 c, float d) {
t /= d;
return -c *(t/d)*(t-2.) + b;
}
vec3 easeOutSine(float t, vec3 b, vec3 c, float d) {
return c * sin(t/d * (PI/2.)) + b;
}
vec3 hexColour(vec2 id) {
float ani_length = 2.;
float pause_length = 1.;
vec2 h = hash2(id);
float time_offset = h.y;
float t = (u_time / ani_length + time_offset);
// t = u_time / ani_length;
float time_id_offset = floor(t);
float time_id_offset_next = floor(t + 1.);
float col_id = floor(h.x * divisor + time_id_offset);
float col_id_next = floor(h.x * divisor + time_id_offset_next);
vec3 colour = getColour(col_id);
vec3 next_colour = getColour(col_id_next);
// t = mod(t, 1.);
// return easeOutSine(t, colour, next_colour, 1.);
float intermix = mod(t, 1.);
return mix(colour, next_colour, intermix);
}
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * u_resolution.xy) / min(u_resolution.y, u_resolution.x);
uv *= 5.;
uv.x += u_time;
vec4 hex_uv = getHex(uv);
float iso = hex(hex_uv.xy);
vec2 id = hex_uv.zw;
vec3 colour = hexColour(id);
colour -= smoothstep(.49, .5, iso);
gl_FragColor = vec4(colour,1.0);
}
</script>
<div id="container" touch-action="none"></div>
/*
Most of the stuff in here is just bootstrapping. Essentially it's just
setting ThreeJS up so that it renders a flat surface upon which to draw
the shader. The only thing to see here really is the uniforms sent to
the shader. Apart from that all of the magic happens in the HTML view
under the fragment shader.
*/
let container;
let camera, scene, renderer;
let uniforms;
let loader=new THREE.TextureLoader();
let texture;
loader.setCrossOrigin("anonymous");
loader.load(
'https://s3-us-west-2.amazonaws.com/s.cdpn.io/982762/noise.png',
function do_something_with_texture(tex) {
texture = tex;
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.minFilter = THREE.LinearFilter;
init();
animate();
}
);
function init() {
container = document.getElementById( 'container' );
camera = new THREE.Camera();
camera.position.z = 1;
scene = new THREE.Scene();
var geometry = new THREE.PlaneBufferGeometry( 2, 2 );
uniforms = {
u_time: { type: "f", value: 1.0 },
u_resolution: { type: "v2", value: new THREE.Vector2() },
u_noise: { type: "t", value: texture },
u_mouse: { type: "v2", value: new THREE.Vector2() }
};
var material = new THREE.ShaderMaterial( {
uniforms: uniforms,
vertexShader: document.getElementById( 'vertexShader' ).textContent,
fragmentShader: document.getElementById( 'fragmentShader' ).textContent
} );
material.extensions.derivatives = true;
var mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
container.appendChild( renderer.domElement );
onWindowResize();
window.addEventListener( 'resize', onWindowResize, false );
document.addEventListener('pointermove', (e)=> {
let ratio = window.innerHeight / window.innerWidth;
uniforms.u_mouse.value.x = (e.pageX - window.innerWidth / 2) / window.innerWidth / ratio;
uniforms.u_mouse.value.y = (e.pageY - window.innerHeight / 2) / window.innerHeight * -1;
e.preventDefault();
});
}
function onWindowResize( event ) {
renderer.setSize( window.innerWidth, window.innerHeight );
uniforms.u_resolution.value.x = renderer.domElement.width;
uniforms.u_resolution.value.y = renderer.domElement.height;
}
function animate() {
requestAnimationFrame( animate );
render();
}
function render() {
uniforms.u_time.value += 0.01;
renderer.render( scene, camera );
}
body {
margin: 0;
padding: 0;
}
#container {
position: fixed;
touch-action: none;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment