include/terraingen/noise/lattice.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief Points split into a lattice cell and a fraction, interpolation helpers, and the frozen | ||
| 5 | /// lattice hash keys. | ||
| 6 | #pragma once | ||
| 7 | |||
| 8 | #include "terraingen/noise/hash.h" | ||
| 9 | |||
| 10 | namespace terraingen { | ||
| 11 | |||
| 12 | /// A 2D point as its integer lattice cell and its fraction within the cell. | ||
| 13 | struct LatticePoint2 { | ||
| 14 | int32_t ix = 0; ///< Cell x. | ||
| 15 | int32_t iy = 0; ///< Cell y. | ||
| 16 | float fx = 0.0f; ///< Fraction in x, in [0, 1). | ||
| 17 | float fy = 0.0f; ///< Fraction in y, in [0, 1). | ||
| 18 | }; | ||
| 19 | |||
| 20 | /// A 3D point as its integer lattice cell and its fraction within the cell. | ||
| 21 | struct LatticePoint3 { | ||
| 22 | int32_t ix = 0; ///< Cell x. | ||
| 23 | int32_t iy = 0; ///< Cell y. | ||
| 24 | int32_t iz = 0; ///< Cell z. | ||
| 25 | float fx = 0.0f; ///< Fraction in x, in [0, 1). | ||
| 26 | float fy = 0.0f; ///< Fraction in y, in [0, 1). | ||
| 27 | float fz = 0.0f; ///< Fraction in z, in [0, 1). | ||
| 28 | }; | ||
| 29 | |||
| 30 | /// The lattice cell and fraction of (`x`, `y`) in lattice units, split at floor(x) and floor(y). | ||
| 31 | 140104912 | inline LatticePoint2 SplitLattice2(float x, float y) { | |
| 32 | 140104912 | const float cellX = floor(x); | |
| 33 | 140104912 | const float cellY = floor(y); | |
| 34 | 140104912 | const LatticePoint2 point = {int32_t(cellX), int32_t(cellY), x - cellX, y - cellY}; | |
| 35 | 140104912 | return point; | |
| 36 | } | ||
| 37 | |||
| 38 | /// The lattice cell and fraction of (`x`, `y`, `z`) in lattice units. | ||
| 39 | 28381513 | inline LatticePoint3 SplitLattice3(float x, float y, float z) { | |
| 40 | 28381513 | const float cellX = floor(x); | |
| 41 | 28381513 | const float cellY = floor(y); | |
| 42 | 28381513 | const float cellZ = floor(z); | |
| 43 | 28381513 | const LatticePoint3 point = {int32_t(cellX), int32_t(cellY), int32_t(cellZ), | |
| 44 | 28381513 | x - cellX, y - cellY, z - cellZ}; | |
| 45 | 28381513 | return point; | |
| 46 | } | ||
| 47 | |||
| 48 | #ifndef __SLANG__ | ||
| 49 | /// `SplitLattice2` in double precision: the fraction is found before narrowing, so it | ||
| 50 | /// stays exact far from the origin. | ||
| 51 | 66576846 | inline LatticePoint2 SplitLatticePrecise2(double x, double y) { | |
| 52 | 66576846 | const double cellX = floor(x); | |
| 53 | 66576846 | const double cellY = floor(y); | |
| 54 | 66576846 | return {int32_t(static_cast<std::int64_t>(cellX)), int32_t(static_cast<std::int64_t>(cellY)), | |
| 55 | 66576846 | float(x - cellX), float(y - cellY)}; | |
| 56 | } | ||
| 57 | |||
| 58 | /// `SplitLattice3` in double precision, as `SplitLatticePrecise2`. | ||
| 59 | 7807329 | inline LatticePoint3 SplitLatticePrecise3(double x, double y, double z) { | |
| 60 | 7807329 | const double cellX = floor(x); | |
| 61 | 7807329 | const double cellY = floor(y); | |
| 62 | 7807329 | const double cellZ = floor(z); | |
| 63 | 7807329 | return {int32_t(static_cast<std::int64_t>(cellX)), | |
| 64 | 7807329 | int32_t(static_cast<std::int64_t>(cellY)), | |
| 65 | 7807329 | int32_t(static_cast<std::int64_t>(cellZ)), | |
| 66 | 7807329 | float(x - cellX), | |
| 67 | 7807329 | float(y - cellY), | |
| 68 | 7807329 | float(z - cellZ)}; | |
| 69 | } | ||
| 70 | #endif | ||
| 71 | |||
| 72 | namespace detail { | ||
| 73 | |||
| 74 | /// Perlin's quintic fade 6t^5 - 15t^4 + 10t^3 of a fraction `t` in [0, 1], C^2 across cell | ||
| 75 | /// edges. | ||
| 76 | 239414989 | constexpr float PerlinFade(float t) { | |
| 77 | 187083626 | return t * t * t * (t * (t * 6.0f - 15.0f) + 10.0f); | |
| 78 | } | ||
| 79 | |||
| 80 | /// Linear interpolation, a + t (b - a). | ||
| 81 | 239414989 | constexpr float Lerp(float t, float a, float b) { | |
| 82 | 187083626 | return a + t * (b - a); | |
| 83 | } | ||
| 84 | |||
| 85 | /// The `pcg3d` key of 2D lattice point (`x`, `y`): the seed is the third word. Frozen for | ||
| 86 | /// reproducibility: Perlin, value and Worley noise in 2D and simplex noise hash this key. | ||
| 87 | /// @param x Lattice x, wrapped to 32 bits. | ||
| 88 | /// @param y Lattice y, wrapped to 32 bits. | ||
| 89 | 259864031 | constexpr UVec3 SeededLatticeKey2(uint32_t x, uint32_t y, uint32_t seed) { | |
| 90 | 259864031 | const UVec3 key = {x, y, seed}; | |
| 91 | 219620080 | return key; | |
| 92 | } | ||
| 93 | |||
| 94 | /// The `pcg3d` key of a corner of 3D Perlin noise: the hashed seed is added to z. Frozen | ||
| 95 | /// for reproducibility. | ||
| 96 | /// @param x Lattice x, wrapped to 32 bits. | ||
| 97 | /// @param y Lattice y, wrapped to 32 bits. | ||
| 98 | /// @param z Lattice z, wrapped to 32 bits. | ||
| 99 | 268371394 | constexpr UVec3 PerlinLatticeKey3(uint32_t x, uint32_t y, uint32_t z, uint32_t seed) { | |
| 100 | 268371394 | const UVec3 key = {x, y, z + PcgHash(seed)}; | |
| 101 | 268371394 | return key; | |
| 102 | } | ||
| 103 | |||
| 104 | /// The `pcg3d` key of a corner of 3D value noise, `seedKey` XORed into z; frozen for | ||
| 105 | /// reproducibility. | ||
| 106 | /// @param x,y,z Lattice coordinates, wrapped to 32 bits. | ||
| 107 | /// @param seedKey `PcgHash` of the noise seed. | ||
| 108 | 80968 | constexpr UVec3 ValueLatticeKey3(uint32_t x, uint32_t y, uint32_t z, uint32_t seedKey) { | |
| 109 | 80968 | const UVec3 key = {x, y, z ^ seedKey}; | |
| 110 | 80968 | return key; | |
| 111 | } | ||
| 112 | |||
| 113 | /// The `pcg3d` key of a cell of 3D Worley noise: `seedKey`, the hashed seed, is XORed into x so | ||
| 114 | /// the feature points do not follow the value noise lattice. Frozen for reproducibility. | ||
| 115 | /// @param seedKey `PcgHash` of the noise seed. | ||
| 116 | 701914 | constexpr UVec3 CellularLatticeKey3(uint32_t x, uint32_t y, uint32_t z, uint32_t seedKey) { | |
| 117 | 701914 | const UVec3 key = {x ^ seedKey, y, z}; | |
| 118 | 701914 | return key; | |
| 119 | } | ||
| 120 | |||
| 121 | /// The `pcg3d` key of a fractal octave's seed and lattice offset: (seed, octave, `word`). | ||
| 122 | /// Frozen for reproducibility. | ||
| 123 | /// @param seed The fractal seed. | ||
| 124 | /// @param octave The octave, from 0. | ||
| 125 | /// @param word 0 for the seed and x and y offsets, 1 for the z offset of 3D fractals. | ||
| 126 | 84403692 | constexpr UVec3 OctaveKey(uint32_t seed, uint32_t octave, uint32_t word) { | |
| 127 | 84403692 | const UVec3 key = {seed, octave, word}; | |
| 128 | 84403692 | return key; | |
| 129 | } | ||
| 130 | |||
| 131 | } // namespace detail | ||
| 132 | |||
| 133 | } // namespace terraingen | ||
| 134 |