# Hexagon pattern

A shader that generates a pattern of hexagons. It’s possible to modify:

* size

* spacing

* color

* transparency

``````shader_type spatial;

uniform float scale = 8.0;
uniform float offset = 0.268;
uniform float power = 1.0;
uniform bool hide_incomplete = false;
uniform vec4 color : hint_color = vec4(0.0, 0.6, 1.0, 1.0);

float is_in_hex(float hex_radius, vec2 local_point) {
const vec2 AXIS[3] = {
vec2(sqrt(3)*0.5, 0.5),
vec2(0.0, 1.0),
vec2(-sqrt(3)*0.5, 0.5)
};
float max_r = 0.0;
for (int i = 0; i < 3; i++) {
float r = dot(local_point, AXIS[i]);
max_r = max(max_r, abs(r));
}
return max_r;
}

float snap_to_center(float local_coord, float hex_radius) {
}

vec2 calculate_local_center(vec2 uv, float r) {
float y_coord_1 = snap_to_center(uv.y, r);
float x_coord_1 = snap_to_center(uv.x, r*sqrt(3));
vec2 point_1 = vec2(x_coord_1, y_coord_1);

float x_coord_2 = snap_to_center(uv.x - r*sqrt(3), r*sqrt(3));
float y_coord_2 = snap_to_center(uv.y - r, r);
vec2 point_2 = vec2(x_coord_2, y_coord_2) + vec2(r*sqrt(3), r);

if (length(uv - point_1) < length(uv - point_2)) {
return point_1;
} else {
return point_2;
}
}

void fragment() {
vec2 uv = (UV - vec2(0.5, 0.5)) * scale;
float r = (radius * sqrt(3) + offset)/2.0;
vec2 local_center = calculate_local_center(uv, r);
vec2 local_coords = uv - local_center;

if (hide_incomplete && (
abs(local_center.x) > scale/2.0 - radius ||
)
) {
ALPHA = 0.0;
} else if (is_in_hex(radius, local_coords) <= 1.0) {
ALBEDO = color.rgb;
} else {
ALPHA = 0.0;
}
}``````
###### Tags
hexagon, pattern
The shader code and all code snippets in this post are under CC0 license and can be used freely without the author's permission. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

## More from miskatonicstudio

### circular pattern

Subscribe
Notify of

1 Comment
Inline Feedbacks
mackatap
1 year ago

How could I modify this so that the grid fades with distance from my character rather than from the camera? I tried to add an offset in a couple places but couldn’t sort it out