summaryrefslogtreecommitdiffstats
path: root/math.cginc
diff options
context:
space:
mode:
Diffstat (limited to 'math.cginc')
-rw-r--r--math.cginc15
1 files changed, 15 insertions, 0 deletions
diff --git a/math.cginc b/math.cginc
index 58a9c08..f188a91 100644
--- a/math.cginc
+++ b/math.cginc
@@ -74,5 +74,20 @@ float2 hex_to_cart(float3 cart) {
(cart[1] - cart[2]) * SQRT_3_OVER_2);
}
+
+float3 round_hex(float3 hex_coord) {
+ float3 rounded = round(hex_coord);
+ float3 diff = abs(rounded - hex_coord);
+ float error = rounded.x - rounded.y - rounded.z;
+
+ float3 max_mask = float3(
+ diff.x > diff.y && diff.x > diff.z,
+ diff.y > diff.x && diff.y > diff.z,
+ diff.z > diff.x && diff.z > diff.y);
+
+ rounded += error * float3(-1, 1, 1) * max_mask;
+ return rounded;
+}
+
#endif // __MATH_INC