Barycentric Hexagon for Spatial Nodes
Barycentric Hexagon Polygon Shader
shader_type spatial;
Change the color of each triangle, and the whole shape.
Modify the positions of each vertex as well !
tested with Godot 4.4dev4 – Could work on other versions (-;
Based on award’s canvas_item shader – https://godotshaders.com/shader/barycentric-hexagon/
Shader code
shader_type spatial;
uniform float rotation : hint_range(0.0, 360.0) = 0.0;
uniform float tri0 : hint_range(0.0, 1.0, 1.0) = 1.0;
uniform float tri1 : hint_range(0.0, 1.0, 1.0) = 1.0;
uniform float tri2 : hint_range(0.0, 1.0, 1.0) = 1.0;
uniform float tri3 : hint_range(0.0, 1.0, 1.0) = 1.0;
uniform float tri4 : hint_range(0.0, 1.0, 1.0) = 1.0;
uniform float tri5 : hint_range(0.0, 1.0, 1.0) = 1.0;
uniform vec4 col : source_color = vec4(1);
uniform vec4 col0 : source_color = vec4(1);
uniform vec4 col1 : source_color = vec4(1);
uniform vec4 col2 : source_color = vec4(1);
uniform vec4 col3 : source_color = vec4(1);
uniform vec4 col4 : source_color = vec4(1);
uniform vec4 col5 : source_color = vec4(1);
uniform vec2 zero = vec2(0.0);
uniform vec2 v0 = vec2(0.0);
uniform vec2 v1 = vec2(1.0);
uniform vec2 v2 = vec2(2.0);
uniform vec2 v3 = vec2(3.0);
uniform vec2 v4 = vec2(4.0);
uniform vec2 v5 = vec2(5.0);
// operand
uniform vec2 op = vec2(3.0);
varying vec2 points[7];
float pnt_in_tri(vec2 p, vec2 a, vec2 b, vec2 c) {
float d = ((b.y - c.y) * (a.x - c.x) + (c.x - b.x) * (a.y - c.y));
float u = ((b.y - c.y) * (p.x - c.x) + (c.x - b.x) * (p.y - c.y)) / d;
float v = ((c.y - a.y) * (p.x - c.x) + (a.x - c.x) * (p.y - c.y)) / d;
float w = 1.0 - u - v;
return step(0.,u)*step(u,1.)*step(0.,v)*step(v,1.)*step(0.,w)*step(w,1.);
}
void vertex() {
float rot = rotation * PI / 180.0;
vec2 vec[] = {v0, v1, v2, v3, v4, v5};
for (int i = 0; i < 6; i++) {
points[i] = vec2(cos(rot + vec[i].x * PI / op.x), -sin(rot + vec[i].y * PI / op.y));
}
points[6] = points[0];
}
void fragment() {
vec2 uv = UV * 2.0 - 1.0;
vec4 colors[] = {col0, col1, col2, col3, col4, col5};
float tri[] = {tri0, tri1, tri2, tri3, tri4, tri5};
vec4 hex_albedo = vec4(0);
float hex_alpha = 0.0;
for (int i = 0; i < 6; i++){
float a = pnt_in_tri(uv, zero, points[i], points[i + 1]);
hex_albedo += colors[i] * a;
hex_alpha += tri[i] * a;
}
ALBEDO = hex_albedo.rgb * col.rgb;
ALPHA = hex_albedo.a * hex_alpha * col.a;
}