GodotComponentTest/scenes/tests/FragmentShaderHexTile.tres

139 lines
3.1 KiB
Plaintext
Raw Permalink Normal View History

2023-05-19 12:29:03 +02:00
[gd_resource type="Shader" format=2]
[resource]
code = "shader_type spatial;
render_mode specular_schlick_ggx, async_visible;
varying vec2 map_coord;
varying vec2 world_coord;
varying vec2 offset_coord;
varying vec3 vertex_coord;
const mat2 _HexAffineInverse = mat2(vec2(1.333333, -0.6666667), vec2(0, -1.154701));
vec3 axial_to_cube(vec2 axial) {
return vec3(axial.x, axial.y, -axial.x - axial.y);
}
ivec3 round_cube_coords(vec3 cube) {
ivec3 rounded = ivec3(round(cube));
vec3 diffs = abs(vec3(rounded) - cube);
if (diffs.x > diffs.y && diffs.x > diffs.z) {
rounded.x = -rounded.y - rounded.z;
} else if (diffs.y > diffs.z) {
rounded.y = -rounded.x - rounded.z;
} else {
rounded.z = -rounded.x - rounded.y;
}
return rounded;
}
ivec2 axial_to_offset(vec2 axial) {
ivec3 cubeCoords = round_cube_coords(axial_to_cube(axial));
int x = cubeCoords.x;
int y = cubeCoords.y;
int off_y = y + (x - (x & 1)) / 2;
2023-05-19 12:29:03 +02:00
return ivec2(x, off_y);
}
void vertex() {
// Input:2
mat4 model_matrix = WORLD_MATRIX;
vec3 origin = vec4(WORLD_MATRIX * vec4(0, 0, 0, 1)).xyz;
vec3 axial_coords = vec3(_HexAffineInverse * origin.xz, 0);
// Output:0
offset_coord = vec2(axial_to_offset(axial_coords.xy));
vertex_coord = vec4(WORLD_MATRIX * vec4(VERTEX, 1.0)).xyz;
world_coord = vec4(WORLD_MATRIX * vec4(VERTEX, 1.0)).xz;
}
vec3 vertex_world_to_color(vec3 vertex_world) {
ivec3 vertex_world_rounded = ivec3(floor(vertex_world));
vec3 result_color = vec3(0);
if (vertex_world_rounded.x % 2 == 0) {
result_color.r = 1.0;
}
if (vertex_world_rounded.z % 2 == 0) {
result_color.b = 1.0;
}
return result_color;
}
vec3 vertex_world_to_hex_center(vec3 vertex_world) {
vec3 axial_coords_f = vec3(_HexAffineInverse * vertex_world.xz, 0);
ivec2 offset_coord_f = axial_to_offset(axial_coords_f.xy);
vec2 mod_result = mod (vec2(offset_coord_f), vec2(2.0));
2023-05-19 12:29:03 +02:00
vec3 result_color = vec3(0);
if (mod_result.x == 0.0) {
2023-05-19 12:29:03 +02:00
result_color.r = 1.0;
}
if (mod_result.y == 0.0) {
2023-05-19 12:29:03 +02:00
result_color.b = 1.0;
}
result_color = vec3(float(offset_coord_f.x), float(offset_coord_f.y), 0.);
result_color.x = result_color.y;
return mod(result_color / 4.0, vec3(1.0));
2023-05-19 12:29:03 +02:00
}
ivec2 cube_to_axial(ivec3 hex_i) {
return ivec2(hex_i.x, hex_i.y);
}
ivec2 axial_round(vec2 hex) {
return cube_to_axial(round_cube_coords(axial_to_cube(hex)));
}
ivec2 axial_to_evenq(ivec2 axial) {
int col = axial.x;
int row = axial.y + (axial.x + (axial.x & 1 )) / 2;
return ivec2 (col, row);
}
vec3 vertex_world_to_hex_center_amid (vec3 vertex_world) {
const mat2 HexAffineInverseAmid = mat2(vec2(2./ 3., -1. / 3.), vec2(0, sqrt(3) / 3.)) * 2.;
vec2 qr = HexAffineInverseAmid * vertex_world.xz;
ivec2 axial_coords_f = axial_round(qr);
ivec2 offset_coord_f = axial_to_evenq(axial_coords_f);
vec3 result_color = vec3(0);
if (offset_coord_f.x % 2 == 0) {
result_color.r = 1.0;
}
if (offset_coord_f.y % 2 == 0) {
result_color.b = 1.0;
}
return result_color;
}
void fragment() {
ALBEDO = vertex_world_to_hex_center(vertex_coord);
// ALBEDO = vertex_world_to_hex_center_amid(vertex_coord);
2023-05-19 12:29:03 +02:00
}
void light() {
// Output:0
}
"