载入中...
搜索中...
未找到
HexNoise.cpp
浏览该文件的文档.
1#include "hexmap/HexNoise.h"
2
3#include <cmath>
4
5namespace eve::hexmap {
6namespace {
7
9[[nodiscard]] std::uint32_t hash3(std::uint32_t x, std::uint32_t y, std::uint32_t z) noexcept {
10 std::uint32_t h = x * 0x9E3779B1u;
11 h ^= y * 0x85EBCA77u;
12 h = (h ^ (h >> 15)) * 0xC2B2AE3Du;
13 h ^= z * 0x27D4EB2Fu;
14 h = (h ^ (h >> 13)) * 0x165667B1u;
15 return h ^ (h >> 16);
16}
17
18[[nodiscard]] float toUnit(std::uint32_t value) noexcept {
19 // 24 significant bits -> [0, 1)
20 return static_cast<float>(value >> 8) * (1.f / 16777216.f);
21}
22
23[[nodiscard]] float smooth(float t) noexcept { return t * t * (3.f - 2.f * t); }
24
25} // namespace
26
27float HexNoise::lattice(int ix, int iz, int channel) const noexcept {
28 return toUnit(hash3(static_cast<std::uint32_t>(ix), static_cast<std::uint32_t>(iz),
29 seed_ + static_cast<std::uint32_t>(channel) * 0x9E3779B9u));
30}
31
32HexVec4 HexNoise::sample(float worldX, float worldZ) const noexcept {
33 const float fx = worldX * (1.f / kCellScale);
34 const float fz = worldZ * (1.f / kCellScale);
35 const float bx = std::floor(fx);
36 const float bz = std::floor(fz);
37 const int ix = static_cast<int>(bx);
38 const int iz = static_cast<int>(bz);
39 const float tx = smooth(fx - bx);
40 const float tz = smooth(fz - bz);
41
42 HexVec4 result;
43 float* channels[4] = {&result.x, &result.y, &result.z, &result.w};
44 for (int channel = 0; channel < 4; ++channel) {
45 const float c00 = lattice(ix, iz, channel);
46 const float c10 = lattice(ix + 1, iz, channel);
47 const float c01 = lattice(ix, iz + 1, channel);
48 const float c11 = lattice(ix + 1, iz + 1, channel);
49 const float top = c00 + (c10 - c00) * tx;
50 const float bottom = c01 + (c11 - c01) * tx;
51 *channels[channel] = top + (bottom - top) * tz;
52 }
53 return result;
54}
55
57 const HexVec4 n = sample(position.x, position.z);
58 position.x += (n.x * 2.f - 1.f) * HexMetrics::kCellPerturbStrength;
59 position.z += (n.z * 2.f - 1.f) * HexMetrics::kCellPerturbStrength;
60 return position;
61}
62
63float HexNoise::elevationPerturb(float worldX, float worldZ) const noexcept {
64 const HexVec4 n = sample(worldX, worldZ);
65 return (n.y * 2.f - 1.f) * HexMetrics::kElevationPerturbStrength;
66}
67
68} // namespace eve::hexmap
double value
SQInteger top
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
float z
Definition AnimClip.cpp:738
std::map< std::string, std::vector< Key >, std::less<> > channels
int bz
Definition CaveMesh.cpp:114
int bx
Definition CaveMesh.cpp:114
glm::vec3 n
Definition Grass.cpp:63
Deterministic asset-free noise for hex perturbation and generation.
int h
std::array< float, 3 > position
float t
float bottom
static constexpr float kElevationPerturbStrength
Strength of the vertical elevation perturbation.
Definition HexMetrics.h:140
static constexpr float kCellPerturbStrength
Strength of the XZ position perturbation.
Definition HexMetrics.h:138
HexVec3 perturb(HexVec3 position) const noexcept
Perturbs a position in XZ by ±kCellPerturbStrength.
Definition HexNoise.cpp:56
float elevationPerturb(float worldX, float worldZ) const noexcept
Vertical perturbation applied to a cell's elevation, ±kElevationPerturbStrength.
Definition HexNoise.cpp:63
HexVec4 sample(float worldX, float worldZ) const noexcept
Samples the four noise channels at a world XZ position.
Definition HexNoise.cpp:32
Minimal 3-component float vector used by the hex mesh builders.
Definition HexMetrics.h:18
Four-component float vector, matching the reference four-channel noise sample.
Definition HexMetrics.h:43