roleplay/client/shader/hex.vert

135 lines
3.8 KiB
GLSL

2024-10-30 21:24:03 -06:00
#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;
2024-11-02 12:07:11 -06:00
const float z = sqrt(3.0)/2.0;
2024-10-30 21:24:03 -06:00
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),
2024-10-30 21:24:03 -06:00
};
const uint indices[] = {
0, 2, 1,
0, 3, 2,
0, 4, 3,
0, 5, 4,
0, 6, 5,
0, 1, 6,
2024-10-30 21:24:03 -06:00
};
const vec4 starts[] = {
2024-11-02 22:02:32 -06:00
vec4( 0, 0, z, 0), // 0, -1
vec4(-x, 0, z/2, 0), // -1, 0
vec4(-x, 0, -z/2, 0), // -1, +1
vec4( 0, 0, -z, 0), // 0, +1
vec4( x, 0, -z/2, 0), // +1, 0
vec4( x, 0, z/2, 0), // +1, -1
};
const vec2 start_coords[] = {
vec2(0, -1),
vec2(-1, 0),
vec2(-1, 1),
vec2(0, 1),
vec2(1, 0),
vec2(1, -1),
};
const vec4 direction[] = {
2024-11-02 12:07:11 -06:00
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),
};
2024-11-02 22:02:32 -06:00
const vec2 direction_coords[] = {
vec2(-1, 1),
vec2(0, 1),
vec2(1, 0),
vec2(1, -1),
vec2(0, -1),
vec2(-1, 0),
};
2024-11-01 17:34:41 -06:00
layout(location=0) out vec4 color;
vec4 int2color(uint color_int) {
2024-11-02 22:02:32 -06:00
float r = float((color_int >> 24) & 0xFF);
float g = float((color_int >> 16) & 0xFF);
float b = float((color_int >> 8) & 0xFF);
float a = float((color_int >> 0) & 0xFF);
return vec4(r, g, b, a)/255.0;
2024-11-01 17:34:41 -06:00
}
2024-11-02 23:25:21 -06:00
const int region_size = 10;
2024-11-02 22:02:32 -06:00
const float region_width = x*(2*region_size-1);
const float region_height = z*(2*region_size-1);
2024-11-02 23:25:21 -06:00
const int region_hex_count = 3*region_size*(region_size-1)+1;
2024-11-02 12:07:11 -06:00
2024-10-30 21:24:03 -06:00
void main() {
2024-11-02 23:25:21 -06:00
int region_index = gl_InstanceIndex/region_hex_count;
int hex_index = gl_InstanceIndex - (region_index*region_hex_count);
Region region = pc.context.regions[region_index];
2024-11-02 12:07:11 -06:00
2024-11-02 22:02:32 -06:00
vec2 region_qr = vec2(region.q, region.r);
2024-11-02 12:07:11 -06:00
2024-11-02 22:02:32 -06:00
vec4 region_pos = vec4(0, 0, 0, 0);
region_pos.x = (region_qr.x+region_qr.y/2)*region_width - region_qr.y*x/2;
region_pos.z = 0.75*region_qr.y*region_height + 0.25*region_qr.y*z + 0.5*region_qr.x*z;
2024-11-01 17:34:41 -06:00
2024-11-02 23:25:21 -06:00
float radius = 0;
float ring = 0;
int side = 0;
2024-11-02 12:07:11 -06:00
if(hex_index != 0) {
2024-11-02 23:25:21 -06:00
radius = floor(0.5 + sqrt(12*hex_index-3)/6);
2024-11-02 12:07:11 -06:00
ring = hex_index - (3*radius*radius - 3*radius + 1);
2024-11-02 23:25:21 -06:00
side = int(floor(ring/(radius)));
}
vec4 position = vertices[indices[gl_VertexIndex]] + (starts[side]*radius) + (direction[side]*(ring-(radius*side))) + region_pos;
2024-11-01 17:34:41 -06:00
if(gl_VertexIndex % 3 == 0) {
2024-11-02 12:07:11 -06:00
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;
2024-11-01 17:34:41 -06:00
} else {
2024-11-02 12:07:11 -06:00
position.y = region.hexes[hex_index].heights[indices[gl_VertexIndex]-1];
2024-11-01 17:34:41 -06:00
}
2024-11-02 22:02:32 -06:00
color = int2color(region.hexes[hex_index].colors[indices[gl_VertexIndex]]);
/*
2024-11-02 23:25:21 -06:00
vec2 hex_qr = start_coords[side]*radius + direction_coords[side]*(ring-(radius*side));
vec2 world_qr;
world_qr.x = (x*hex_qr.x + (region_qr.x+region_qr.y/2)*region_width - region_qr.y*x/2)/x;
world_qr.y = -(-z*hex_qr.y - z*hex_qr.x/2 + 0.75*region_qr.y*region_height + 0.25*region_qr.y*z + 0.5*region_qr.x*z)/z - (x*hex_qr.x + (region_qr.x+region_qr.y/2)*region_width - region_qr.y*x/2)/(2*x);
2024-11-02 22:02:32 -06:00
vec4 hex_pos = vec4(0, 0, 0, 1);
hex_pos.x = x*hex_qr.x;
hex_pos.z = -z*hex_qr.y - z*hex_qr.x/2;
vec4 world_pos = vec4(0, 0, 0, 1);
world_pos.x = hex_pos.x + region_pos.x;
world_pos.z = hex_pos.z + region_pos.z;
vec2 world_qr = vec2(0, 0);
world_qr.x = world_pos.x/x;
world_qr.y = -world_pos.z/z - world_pos.x/(2*x);
*/
gl_Position = pc.context.proj * pc.context.view * position;
2024-10-30 21:24:03 -06:00
}