diff options
| author | yum <yum.food.vr@gmail.com> | 2026-04-13 23:13:16 -0700 |
|---|---|---|
| committer | yum <yum.food.vr@gmail.com> | 2026-04-13 23:13:16 -0700 |
| commit | 07f8f3db17354c3e77aa9cb67338d451de6572d3 (patch) | |
| tree | d526eef93d3091ace08fd12d5a3a0b9737cad0c6 /math.cginc | |
| parent | 2791567370385c9c8d7a5aeaa64c4588346e1779 (diff) | |
Diffstat (limited to 'math.cginc')
| -rwxr-xr-x | math.cginc | 54 |
1 files changed, 46 insertions, 8 deletions
@@ -110,7 +110,6 @@ float3 round_hex(float3 hex_coord) { float3 blendNormalsHill12(float3 n0, float3 n1) { n0.z += 1.0; n1.xy = -n1.xy; - return normalize(n0 * dot(n0, n1) - n1 * n0.z); } @@ -213,19 +212,21 @@ float3 domain_warp_3d_tex(Texture3D noise_tex, SamplerState s, float3 uvw, return noise; } -// Return distance to the nearest voronoi cell edge, clamped to [0, 0.5]. -// 0.5 is on the edge, 0 is far from it. -float voronoi_d_2d(float2 x) { +// Return distance to the nearest voronoi cell edge. Also returns p1 and p2, +// vectors from the x to the nearest 2 lattice points, and p1_id, an identifier +// for p1 which does not within the current cell. +float voronoi_d_2d(float2 x, out float2 p1, out float2 p2, out float2 p1_id) { float2 x_floor = floor(x); float2 x_frac = frac(x); float d1 = 1e6; float d2 = 1e6; - float2 p1 = 0; - float2 p2 = 0; + p1 = 0; + p2 = 0; for (int j = -1; j <= 1; j++) { for (int i = -1; i <= 1; i++) { float2 cell_offset = float2(i, j); + // Radial vector from origin lattice point to current. float2 r = cell_offset + hash22_fast(x_floor + cell_offset) - x_frac; float d = dot(r, r); if (d < d1) { @@ -233,14 +234,51 @@ float voronoi_d_2d(float2 x) { p2 = p1; d1 = d; p1 = r; + p1_id = x_floor + cell_offset; } else if (d < d2) { d2 = d; p2 = r; } } } - float d = (d2 - d1) / (2.0f * max(1e-4, length(p2 - p1))); - return max(0.0f, 0.5f - d); + return (d2 - d1); +} + +// stripe_dir is the direction we want to draw stripes along. It must be +// normalized. +float4 phacelle_noise_2d(float2 x, float2 stripe_dir) { + float2 x_floor = floor(x); + float2 x_frac = frac(x); + + float2 stripe_ortho = float2(-stripe_dir.y, stripe_dir.x) * TAU; + + float2 vec = 0; + float normalization = 0; + for (int j = -1; j <= 2; j++) { + for (int i = -1; i <= 2; i++) { + float2 cell_offset = float2(i, j); + // Radial vector from origin lattice point to current. + float2 r = cell_offset + (hash22_fast(x_floor + cell_offset) - 0.5) * 0.5 - x_frac; + float d2 = dot(r, r); + // 0th cell is centered at 0.5. + float weight = max(0, exp(-2.0 * d2) - 0.01111); + normalization += weight; + float wave_input = dot(r, stripe_ortho); + // TODO what the fuck + // why is r not normalized + float vecs, vecc; + sincos(wave_input, vecs, vecc); + vec += float2(vecc, vecs) * weight; + } + } + vec /= normalization; + float magnitude = dot(vec, vec); + return float4(vec / magnitude, stripe_ortho); +} + +float voronoi_d_2d(float2 x) { + float2 p1, p2, p1_id; + return voronoi_d_2d(x, p1, p2, p1_id); } // Return distance to the nearest voronoi cell edge, clamped to [0, 0.5]. |
