#version 450 #extension GL_EXT_buffer_reference : require #include "hex_common.glsl" #define PI 3.1415926535897932384626433832795 const float w = 0.5; const float x = 0.75; const float z = sqrt(3.0)/2.0; const vec4 vertices[] = { vec4(0, 0, 0, 1), vec4(w*cos(0*PI/3), 0, w*sin(0*PI/3), 1), vec4(w*cos(1*PI/3), 0, w*sin(1*PI/3), 1), vec4(w*cos(2*PI/3), 0, w*sin(2*PI/3), 1), vec4(w*cos(3*PI/3), 0, w*sin(3*PI/3), 1), vec4(w*cos(4*PI/3), 0, w*sin(4*PI/3), 1), vec4(w*cos(5*PI/3), 0, w*sin(5*PI/3), 1), }; const uint indices[] = { 0, 2, 1, 0, 3, 2, 0, 4, 3, 0, 5, 4, 0, 6, 5, 0, 1, 6, }; const vec4 starts[] = { vec4( 0, 0, z, 0), vec4(-x, 0, z/2, 0), vec4(-x, 0, -z/2, 0), vec4( 0, 0, -z, 0), vec4( x, 0, -z/2, 0), vec4( x, 0, z/2, 0), }; const vec4 direction[] = { vec4(-x, 0, -z/2, 0), vec4( 0, 0, -z, 0), vec4( x, 0, -z/2, 0), vec4( x, 0, z/2, 0), vec4( 0, 0, z, 0), vec4(-x, 0, z/2, 0), }; layout(location=0) out vec4 color; vec4 int2color(uint color_int) { float r = float((color_int >> 24) & 0xFF)/255.0; float g = float((color_int >> 16) & 0xFF)/255.0; float b = float((color_int >> 8) & 0xFF)/255.0; float a = float((color_int >> 0) & 0xFF)/255.0; return vec4(r, g, b, a); } const uint region_size = 6; const uint region_hex_count = 3*region_size*(region_size-1)+1; void main() { uint region_index = uint(floor(gl_InstanceIndex/region_hex_count)); uint hex_index = gl_InstanceIndex - (region_index*region_hex_count); Region region = pc.context.regions[region_index]; float q = region.q; float r = region.r; float z_off = 0; if(r != 0) { z_off = r/2; } float x_off = 0; if((q+r) != 0) { x_off = (q+r)/2; } vec4 region_offset = vec4(x*(x_off + 2*(r+(-q-r)/2)*(region_size-0.5)), 0, z*(z_off + 2*(-q-r)*(region_size-1.75)), 0); uint radius = 0; uint ring = 0; uint side = 0; uint hex = 0; if(hex_index != 0) { radius = uint(floor(0.5 + sqrt(12*hex_index-3)/6)); ring = hex_index - (3*radius*radius - 3*radius + 1); side = uint(floor(ring/(radius))); hex = ring - (radius*side); } vec4 position = vertices[indices[gl_VertexIndex]] + (starts[side]*radius) + (direction[side]*hex) + region_offset; if(gl_VertexIndex % 3 == 0) { position.y = (region.hexes[hex_index].heights[0] + region.hexes[hex_index].heights[1] + region.hexes[hex_index].heights[2] + region.hexes[hex_index].heights[3] + region.hexes[hex_index].heights[4] + region.hexes[hex_index].heights[5])/6; color = (int2color(region.hexes[hex_index].colors[0]) + int2color(region.hexes[hex_index].colors[1]) + int2color(region.hexes[hex_index].colors[2]) + int2color(region.hexes[hex_index].colors[3]) + int2color(region.hexes[hex_index].colors[4]) + int2color(region.hexes[hex_index].colors[5]))/6; } else { position.y = region.hexes[hex_index].heights[indices[gl_VertexIndex]-1]; color = int2color(region.hexes[hex_index].colors[indices[gl_VertexIndex]-1]); } gl_Position = pc.context.proj * pc.context.view * position; }