summaryrefslogtreecommitdiffstats
path: root/math.cginc
diff options
context:
space:
mode:
Diffstat (limited to 'math.cginc')
-rwxr-xr-xmath.cginc20
1 files changed, 11 insertions, 9 deletions
diff --git a/math.cginc b/math.cginc
index c2ae26e..0f5f8f5 100755
--- a/math.cginc
+++ b/math.cginc
@@ -13,6 +13,7 @@
#define RCP_SQRT_2 0.707106781f
#define RCP_SQRT_3 0.577350269f
#define TWO_OVER_THREE 0.6666666666666666f
+#define TWO_OVER_SQRT_3 1.15470054f
#define SQRT_3 1.73205081f
#define SQRT_3_OVER_2 0.8660254037844386f
#define EULERS_CONSTANT 2.718281828f
@@ -64,20 +65,20 @@ float3 rotate_vector(float3 v, float4 q)
return v + q.w * t + cross(q.xyz, t);
}
-// Cartesian to cube hexagonal coordinates.
+// Cartesian to packed cube hexagonal coordinates.
+// Stores (q + r, q, r), where the article's cube coordinates satisfy
+// q + r + s = 0 and s = -(q + r).
// Based on this: https://backdrifting.net/post/064_hex_grids
float3 cart_to_hex(float2 cart) {
- float p = cart.x;
- float q = dot(cart, float2(0.5f, SQRT_3_OVER_2));
- float r = dot(cart, float2(0.5f, -SQRT_3_OVER_2));
-
- return float3(p, q, r) * 0.5f;
+ float q = cart.x - cart.y * RCP_SQRT_3;
+ float r = cart.y * (2.0f * RCP_SQRT_3);
+ return float3(q + r, q, r);
}
-float2 hex_to_cart(float3 cart) {
+float2 hex_to_cart(float3 hex_coord) {
return float2(
- cart[0] + (cart[1] + cart[2]) * 0.5f,
- (cart[1] - cart[2]) * SQRT_3_OVER_2);
+ hex_coord[1] + hex_coord[2] * 0.5f,
+ hex_coord[2] * SQRT_3_OVER_2);
}
@@ -166,6 +167,7 @@ float3 domain_warp_procedural(float3 uvw, float strength,
float g = 1;
for (uint ii = 0; ii < octaves; ++ii) {
+ // Need to remap noise onto [-1, 1] when domain warping.
float3 vnoise = value_noise3(uvw + (noise * 2.0 - 1.0) * strength);
noise += vnoise * g;
uvw *= lacunarity;