载入中...
搜索中...
未找到
HexTerrain.cpp
浏览该文件的文档.
2
3#include <algorithm>
4#include <array>
5#include <cmath>
6#include <cstdint>
7#include <limits>
8#include <random>
9#include <vector>
10
11namespace eve::procgen {
12namespace {
13
14enum Biome : int {
15 DeepOcean = 0,
16 Ocean,
17 Coast,
19 Hills,
20 Mountain,
21 Forest,
22 Swamp,
24 Ice,
25 Cliff,
26 River,
27};
28
29struct Cell {
30 float elevation = 0.f;
31 float moisture = 0.f;
32 float temperature = 0.f;
33 float river = 0.f;
34 int biome = Ocean;
36 float blend = 0.f;
37 uint8_t riverEdges = 0;
38 bool lake = false;
39};
40
41uint32_t hash(uint32_t x) {
42 x ^= x >> 16;
43 x *= 0x7feb352du;
44 x ^= x >> 15;
45 x *= 0x846ca68bu;
46 return x ^ (x >> 16);
47}
48
49float valueNoise(float x, float y, uint32_t seed) {
50 const int ix = int(std::floor(x));
51 const int iy = int(std::floor(y));
52 const float fx = x - float(ix);
53 const float fy = y - float(iy);
54 const float sx = fx * fx * (3.f - 2.f * fx);
55 const float sy = fy * fy * (3.f - 2.f * fy);
56 auto sample = [seed](int px, int py) {
57 const uint32_t h = hash(uint32_t(px) * 0x9e3779b9u ^ uint32_t(py) * 0x85ebca6bu ^ seed);
58 return float(h & 0xffffu) / 32767.5f - 1.f;
59 };
60 const float a = std::lerp(sample(ix, iy), sample(ix + 1, iy), sx);
61 const float b = std::lerp(sample(ix, iy + 1), sample(ix + 1, iy + 1), sx);
62 return std::lerp(a, b, sy);
63}
64
65float fbm(float x, float y, uint32_t seed) {
66 float value = 0.f, amplitude = 0.55f, frequency = 1.f, norm = 0.f;
67 for (int octave = 0; octave < 5; ++octave) {
68 value += valueNoise(x * frequency, y * frequency, seed + uint32_t(octave) * 1013u) * amplitude;
69 norm += amplitude;
70 amplitude *= 0.5f;
71 frequency *= 2.03f;
72 }
73 return value / norm;
74}
75
76int indexOf(int q, int r, int width, int height) {
77 return q < 0 || r < 0 || q >= width || r >= height ? -1 : q + r * width;
78}
79
80std::array<int, 6> neighbours(int q, int r, int width, int height) {
81 static constexpr int dq[6] = {1, 0, -1, -1, 0, 1};
82 const int parity = q & 1;
83 const int dr[6] = {parity, 1, parity, parity - 1, -1, parity - 1};
84 std::array<int, 6> result{};
85 for (int i = 0; i < 6; ++i) result[i] = indexOf(q + dq[i], r + dr[i], width, height);
86 return result;
87}
88
89void addRiverArm(MeshBuild& out, float cx, float y, float cz, float radius, int edge,
90 float coverage, uint32_t shapeSeed) {
91 const float angle = 1.0471975512f * (float(edge) + 0.5f);
92 const float dx = std::cos(angle), dz = std::sin(angle);
93 const float ex = cx + dx * radius * 0.94f;
94 const float ez = cz + dz * radius * 0.94f;
95 const float bend = (float(hash(shapeSeed ^ uint32_t(edge) * 0x9e3779b9u) & 0xffffu) /
96 65535.f * 2.f - 1.f) * radius * 0.30f;
97 const float mx = cx + dx * radius * 0.48f - dz * bend;
98 const float mz = cz + dz * radius * 0.48f + dx * bend;
99 const float u = float(River) + 0.04f;
100 const float v = float(River) + std::clamp(coverage, 0.f, 0.999f);
101 const uint32_t base = uint32_t(out.getVertexCount());
102 constexpr int curveSegments = 6;
103 for (int segment = 0; segment <= curveSegments; ++segment) {
104 const float t = float(segment) / float(curveSegments);
105 const float omt = 1.f - t;
106 const float x = omt * omt * cx + 2.f * omt * t * mx + t * t * ex;
107 const float z = omt * omt * cz + 2.f * omt * t * mz + t * t * ez;
108 float tx = 2.f * omt * (mx - cx) + 2.f * t * (ex - mx);
109 float tz = 2.f * omt * (mz - cz) + 2.f * t * (ez - mz);
110 const float invLength = 1.f / std::max(0.0001f, std::sqrt(tx * tx + tz * tz));
111 tx *= invLength;
112 tz *= invLength;
113 const float halfWidth = radius * (0.102f + coverage * 0.064f) *
114 (0.82f + 0.18f * std::sin(t * 3.1415926536f));
115 const float px = -tz, pz = tx;
116 out.addVertex(x + px * halfWidth, y, z + pz * halfWidth,
117 0.f, 1.f, 0.f, u, v);
118 out.addVertex(x - px * halfWidth, y, z - pz * halfWidth,
119 0.f, 1.f, 0.f, u, v);
120 }
121 for (int segment = 0; segment < curveSegments; ++segment) {
122 const uint32_t a = base + uint32_t(segment * 2);
123 out.addTriangle(a, a + 1u, a + 3u);
124 out.addTriangle(a, a + 3u, a + 2u);
125 }
126}
127
128void addRiverJunction(MeshBuild& out, float cx, float y, float cz, float radius,
129 float coverage) {
130 constexpr int segments = 12;
131 const float junctionRadius = radius * (0.105f + coverage * 0.045f);
132 const float u = float(River) + 0.04f;
133 const float v = float(River) + std::clamp(coverage, 0.f, 0.999f);
134 const uint32_t center = uint32_t(out.getVertexCount());
135 out.addVertex(cx, y, cz, 0.f, 1.f, 0.f, u, v);
136 for (int k = 0; k < segments; ++k) {
137 const float angle = 6.2831853072f * float(k) / float(segments);
138 out.addVertex(cx + std::cos(angle) * junctionRadius, y,
139 cz + std::sin(angle) * junctionRadius, 0.f, 1.f, 0.f, u, v);
140 }
141 for (int k = 0; k < segments; ++k)
142 out.addTriangle(center, center + 1u + uint32_t(k),
143 center + 1u + uint32_t((k + 1) % segments));
144}
145
146void addShoreBand(MeshBuild& out, float cx, float y, float cz, float radius, int edge) {
147 const float a0 = 1.0471975512f * float(edge);
148 const float a1 = 1.0471975512f * float((edge + 1) % 6);
149 constexpr float innerScale = 0.89f;
150 const float u = float(Coast) + 0.92f;
151 const float v = float(Ocean) + 0.15f;
152 const uint32_t base = uint32_t(out.getVertexCount());
153 out.addVertex(cx + std::cos(a0) * radius, y, cz + std::sin(a0) * radius,
154 0.f, 1.f, 0.f, u, v);
155 out.addVertex(cx + std::cos(a1) * radius, y, cz + std::sin(a1) * radius,
156 0.f, 1.f, 0.f, u, v);
157 out.addVertex(cx + std::cos(a1) * radius * innerScale, y,
158 cz + std::sin(a1) * radius * innerScale, 0.f, 1.f, 0.f, u, v);
159 out.addVertex(cx + std::cos(a0) * radius * innerScale, y,
160 cz + std::sin(a0) * radius * innerScale, 0.f, 1.f, 0.f, u, v);
161 out.addTriangle(base, base + 1u, base + 2u);
162 out.addTriangle(base, base + 2u, base + 3u);
163}
164
165void classify(Cell& c, float sea) {
166 if (c.elevation < sea - 0.22f) c.biome = DeepOcean;
167 else if (c.temperature < 0.16f) c.biome = Ice;
168 else if (c.elevation < sea - 0.045f) c.biome = Ocean;
169 else if (c.elevation < sea + 0.025f) c.biome = Coast;
170 else if (c.elevation > sea + 0.55f) c.biome = Mountain;
171 else if (c.elevation > sea + 0.35f) c.biome = Hills;
172 else if (c.moisture > 0.68f && c.elevation < sea + 0.16f) c.biome = Swamp;
173 else if (c.moisture > 0.64f && c.temperature > 0.55f) c.biome = Rainforest;
174 else if (c.moisture > 0.56f) c.biome = Forest;
175 else c.biome = Grassland;
176 c.secondary = c.biome;
177}
178
179float surfaceHeight(const Cell& c, float sea, float heightScale) {
180 float y = (c.elevation - sea) * heightScale;
181 if (c.lake) return y - 0.035f * heightScale;
182 if (c.biome == DeepOcean || c.biome == Ocean) return -0.08f * heightScale;
183
184 const float land = std::max(0.f, c.elevation - sea);
185 if (c.biome == Hills) y += (0.08f + land * 0.16f) * heightScale;
186 if (c.biome == Mountain) y += (0.22f + land * 0.38f) * heightScale;
187 if (c.biome == Ice && land > 0.28f) y += land * 0.22f * heightScale;
188 if (c.river > 0.f) y -= (0.045f + 0.035f * c.river) * heightScale;
189 return y;
190}
191
192void addCone(MeshBuild& out, float cx, float baseY, float cz, float radius, float height,
193 int biome, int segments = 6, uint32_t shapeSeed = 0) {
194 const uint32_t base = uint32_t(out.getVertexCount());
195 // 0.92 marks procedural decoration vertices for optional wind animation.
196 const float u = float(biome) + 0.92f;
197 const float v = float(biome);
198 for (int k = 0; k < segments; ++k) {
199 const float a = 6.2831853072f * float(k) / float(segments);
200 const float nx = std::cos(a), nz = std::sin(a);
201 const float radial = shapeSeed == 0 ? 1.f :
202 0.78f + float(hash(shapeSeed + uint32_t(k) * 0x85ebca6bu) & 0xffffu) /
203 65535.f * 0.38f;
204 out.addVertex(cx + nx * radius * radial, baseY, cz + nz * radius * radial,
205 nx * 0.82f, radius / std::max(height, 0.01f), nz * 0.82f, u, v);
206 }
207 const float apexX = shapeSeed == 0 ? cx : cx +
208 (float(hash(shapeSeed ^ 0x41c64e6du) & 0xffffu) / 65535.f - 0.5f) * radius * 0.34f;
209 const float apexZ = shapeSeed == 0 ? cz : cz +
210 (float(hash(shapeSeed ^ 0xc2b2ae35u) & 0xffffu) / 65535.f - 0.5f) * radius * 0.34f;
211 if (shapeSeed != 0) {
212 const uint32_t shoulder = uint32_t(out.getVertexCount());
213 for (int k = 0; k < segments; ++k) {
214 const float a = 6.2831853072f * (float(k) + 0.16f) / float(segments);
215 const float nx = std::cos(a), nz = std::sin(a);
216 const float radial = 0.43f +
217 float(hash(shapeSeed ^ 0xa511e9b3u ^ uint32_t(k) * 0x27d4eb2du) & 0xffffu) /
218 65535.f * 0.22f;
219 out.addVertex(std::lerp(cx, apexX, 0.34f) + nx * radius * radial,
220 baseY + height * (0.43f + radial * 0.16f),
221 std::lerp(cz, apexZ, 0.34f) + nz * radius * radial,
222 nx * 0.88f, 0.34f, nz * 0.88f, u, v);
223 }
224 const uint32_t apex = uint32_t(out.getVertexCount());
225 out.addVertex(apexX, baseY + height, apexZ, 0.f, 1.f, 0.f, u, v);
226 for (int k = 0; k < segments; ++k) {
227 const uint32_t next = uint32_t((k + 1) % segments);
228 const uint32_t lower = base + uint32_t(k);
229 const uint32_t lowerNext = base + next;
230 const uint32_t upper = shoulder + uint32_t(k);
231 const uint32_t upperNext = shoulder + next;
232 out.addTriangle(lower, lowerNext, upperNext);
233 out.addTriangle(lower, upperNext, upper);
234 out.addTriangle(upper, upperNext, apex);
235 }
236 return;
237 }
238 const uint32_t apex = uint32_t(out.getVertexCount());
239 out.addVertex(apexX, baseY + height, apexZ, 0.f, 1.f, 0.f, u, v);
240 for (int k = 0; k < segments; ++k)
241 out.addTriangle(base + uint32_t(k), base + uint32_t((k + 1) % segments), apex);
242}
243
244void addTrunk(MeshBuild& out, float cx, float y, float cz, float radius, float height) {
245 constexpr int segments = 5;
246 const uint32_t base = uint32_t(out.getVertexCount());
247 const float u = float(Cliff) + 0.12f;
248 const float v = float(Forest);
249 for (int level = 0; level < 2; ++level) for (int k = 0; k < segments; ++k) {
250 const float a = 6.2831853072f * float(k) / float(segments);
251 const float nx = std::cos(a), nz = std::sin(a);
252 out.addVertex(cx + nx * radius, y + float(level) * height, cz + nz * radius,
253 nx, 0.f, nz, u, v);
254 }
255 for (int k = 0; k < segments; ++k) {
256 const uint32_t next = uint32_t((k + 1) % segments);
257 out.addTriangle(base + uint32_t(k), base + next, base + uint32_t(segments + k));
258 out.addTriangle(base + next, base + uint32_t(segments) + next,
259 base + uint32_t(segments + k));
260 }
261}
262
263void addCrownBlob(MeshBuild& out, float cx, float y, float cz, float radius, float height,
264 int biome, int segments = 7) {
265 const float u = float(biome) + 0.92f;
266 const float v = float(biome);
267 const uint32_t bottom = uint32_t(out.getVertexCount());
268 out.addVertex(cx, y, cz, 0.f, -1.f, 0.f, u, v);
269 const uint32_t ring = uint32_t(out.getVertexCount());
270 for (int k = 0; k < segments; ++k) {
271 const float a = 6.2831853072f * float(k) / float(segments);
272 const float nx = std::cos(a), nz = std::sin(a);
273 out.addVertex(cx + nx * radius, y + height * 0.43f, cz + nz * radius,
274 nx, 0.25f, nz, u, v);
275 }
276 const uint32_t top = uint32_t(out.getVertexCount());
277 out.addVertex(cx, y + height, cz, 0.f, 1.f, 0.f, u, v);
278 for (int k = 0; k < segments; ++k) {
279 const uint32_t a = ring + uint32_t(k);
280 const uint32_t b = ring + uint32_t((k + 1) % segments);
281 out.addTriangle(bottom, b, a);
282 out.addTriangle(a, b, top);
283 }
284}
285
286void addTree(MeshBuild& out, float x, float y, float z, float scale, int biome,
287 bool broadleaf) {
288 addTrunk(out, x, y, z, scale * 0.075f, scale * 0.48f);
289 if (broadleaf) {
290 const float broad = biome == Rainforest ? 0.46f : 0.39f;
291 addCrownBlob(out, x, y + scale * 0.34f, z, scale * broad, scale * 0.78f,
292 biome, biome == Rainforest ? 8 : 7);
293 if (biome == Rainforest)
294 addCrownBlob(out, x + scale * 0.13f, y + scale * 0.55f, z - scale * 0.09f,
295 scale * 0.31f, scale * 0.62f, biome, 7);
296 } else if (biome == Rainforest) {
297 addCone(out, x, y + scale * 0.34f, z, scale * 0.38f, scale * 0.72f, biome, 7);
298 addCone(out, x, y + scale * 0.68f, z, scale * 0.28f, scale * 0.55f, biome, 7);
299 } else {
300 addCone(out, x, y + scale * 0.30f, z, scale * 0.32f, scale * 0.64f, biome, 6);
301 addCone(out, x, y + scale * 0.58f, z, scale * 0.24f, scale * 0.56f, biome, 6);
302 }
303}
304
305void addTop(MeshBuild& out, float cx, float cz, float y, float radius, const Cell& c) {
306 const uint32_t center = uint32_t(out.getVertexCount());
307 const float u = float(c.biome) + std::clamp(c.blend, 0.f, 0.999f);
308 const float v = float(c.secondary) + std::clamp(c.river, 0.f, 0.999f);
309 out.addVertex(cx, y, cz, 0.f, 1.f, 0.f, u, v);
310 for (int k = 0; k < 6; ++k) {
311 const float a = 1.0471975512f * float(k);
312 out.addVertex(cx + std::cos(a) * radius, y, cz + std::sin(a) * radius,
313 0.f, 1.f, 0.f, u, v);
314 }
315 for (int k = 0; k < 6; ++k) out.addTriangle(center, center + 1u + uint32_t((k + 1) % 6), center + 1u + uint32_t(k));
316}
317
318void addCliff(MeshBuild& out, float cx, float cz, float radius, int edge, float top, float bottom,
319 const Cell& c, uint32_t shapeSeed) {
320 const float a0 = 1.0471975512f * float(edge);
321 const float a1 = 1.0471975512f * float((edge + 1) % 6);
322 const float x0 = cx + std::cos(a0) * radius, z0 = cz + std::sin(a0) * radius;
323 const float x1 = cx + std::cos(a1) * radius, z1 = cz + std::sin(a1) * radius;
324 const float nx = std::sin((a0 + a1) * 0.5f), nz = -std::cos((a0 + a1) * 0.5f);
325 const float relief = std::abs(top - bottom);
326 const bool rockFace = relief > radius * 0.42f;
327 const float u = rockFace ? float(Cliff) + std::min(0.999f, relief)
328 : float(c.biome) + std::clamp(c.blend, 0.f, 0.49f);
329 const float v = rockFace ? float(c.biome)
330 : float(c.secondary) + std::clamp(c.river, 0.f, 0.999f);
331 constexpr int wallSegments = 5;
332 std::array<float, wallSegments + 1> topX{}, topY{}, topZ{}, bottomX{}, bottomY{}, bottomZ{};
333 for (int segment = 0; segment <= wallSegments; ++segment) {
334 const float t = float(segment) / float(wallSegments);
335 const float straightX = std::lerp(x0, x1, t);
336 const float straightZ = std::lerp(z0, z1, t);
337 const uint32_t h = hash(shapeSeed + uint32_t(segment) * 0x9e3779b9u);
338 const float endMask = segment == 0 || segment == wallSegments ? 0.f : 1.f;
339 const float lipScale = rockFace ? 0.26f : 0.12f;
340 const float footScale = rockFace ? 0.34f : 0.16f;
341 const float lipOffset = (float(h & 0xffffu) / 65535.f - 0.42f) *
342 radius * lipScale * endMask;
343 const float footOffset = (float((h >> 16) & 0xffffu) / 65535.f - 0.5f) *
344 radius * footScale * endMask;
345 topX[size_t(segment)] = straightX + nx * lipOffset;
346 topY[size_t(segment)] = top + (rockFace ?
347 (float(hash(h ^ 0x165667b1u) & 0xffffu) / 65535.f - 0.38f) *
348 radius * 0.22f * endMask : 0.f);
349 topZ[size_t(segment)] = straightZ + nz * lipOffset;
350 bottomX[size_t(segment)] = straightX + nx * footOffset;
351 bottomY[size_t(segment)] = bottom + (rockFace ?
352 (float(hash(h ^ 0xd3a2646cu) & 0xffffu) / 65535.f) *
353 std::min(relief * 0.16f, radius * 0.16f) * endMask : 0.f);
354 bottomZ[size_t(segment)] = straightZ + nz * footOffset;
355 }
356 for (int segment = 0; segment < wallSegments; ++segment) {
357 const uint32_t h = hash(shapeSeed ^ uint32_t(segment) * 0x85ebca6bu);
358 const float shadeTurn = (float(h & 0xffffu) / 65535.f - 0.5f) * 0.34f;
359 const float snx = nx + std::cos(a1) * shadeTurn;
360 const float snz = nz + std::sin(a1) * shadeTurn;
361 const uint32_t base = uint32_t(out.getVertexCount());
362 out.addVertex(topX[size_t(segment)], topY[size_t(segment)], topZ[size_t(segment)], snx, 0.f, snz, u, v);
363 out.addVertex(topX[size_t(segment + 1)], topY[size_t(segment + 1)], topZ[size_t(segment + 1)], snx, 0.f, snz, u, v);
364 out.addVertex(bottomX[size_t(segment + 1)], bottomY[size_t(segment + 1)], bottomZ[size_t(segment + 1)], snx, 0.f, snz, u, v);
365 out.addVertex(bottomX[size_t(segment)], bottomY[size_t(segment)], bottomZ[size_t(segment)], snx, 0.f, snz, u, v);
366 out.addTriangle(base, base + 1u, base + 2u);
367 out.addTriangle(base, base + 2u, base + 3u);
368 }
369}
370
371} // namespace
372
374 const int width = params.getInt("width", 32);
375 const int height = params.getInt("height", 24);
376 const int riverCount = params.getInt("riverCount", 8);
377 const uint32_t seed = uint32_t(params.getInt("seed", 1));
378 const float radius = params.getFloat("radius", 1.f);
379 const float sea = params.getFloat("seaLevel", 0.43f);
380 const float heightScale = params.getFloat("heightScale", 4.f);
381 const bool decorations = params.getBool("decorations", true);
382 const float vegetationDensity = params.getFloat("vegetationDensity", 1.f);
383 if (width < 2 || height < 2 || width > 256 || height > 256) {
384 error = "mesh.hexterrain: width and height must be in [2, 256]";
385 return false;
386 }
387 if (!(radius > 0.f) || !(heightScale > 0.f) || sea < 0.f || sea > 1.f || riverCount < 0 ||
388 vegetationDensity < 0.f || vegetationDensity > 2.f) {
389 error = "mesh.hexterrain: invalid radius, heightScale, seaLevel or riverCount";
390 return false;
391 }
392
393 std::vector<Cell> cells(size_t(width * height));
394 for (int r = 0; r < height; ++r) for (int q = 0; q < width; ++q) {
395 Cell& c = cells[size_t(q + r * width)];
396 const float nx = (float(q) / float(width - 1)) * 2.f - 1.f;
397 const float ny = (float(r) / float(height - 1)) * 2.f - 1.f;
398 const float continental = 1.f - std::pow(std::min(1.f, std::sqrt(nx * nx + ny * ny)), 1.7f);
399 c.elevation = std::clamp(0.12f + continental * 0.78f + fbm(q * 0.085f, r * 0.085f, seed) * 0.22f, 0.f, 1.f);
400 c.moisture = std::clamp(0.52f + fbm(q * 0.11f, r * 0.11f, seed ^ 0x51f15e5du) * 0.42f, 0.f, 1.f);
401 const float latitude = std::abs(ny);
402 c.temperature = std::clamp(1.f - latitude * 0.92f - std::max(0.f, c.elevation - sea) * 0.48f +
403 fbm(q * 0.06f, r * 0.06f, seed ^ 0xa53a9d1bu) * 0.1f, 0.f, 1.f);
404 classify(c, sea);
405 }
406
407 std::mt19937 rng(seed ? seed : 1u);
408 std::vector<int> riverSources;
409 for (int river = 0; river < riverCount; ++river) {
410 std::vector<uint8_t> visited(size_t(width * height), 0);
411 int current = -1;
412 int lastEdge = -1;
413 float bestSourceScore = -std::numeric_limits<float>::max();
414 for (int attempt = 0; attempt < width * height; ++attempt) {
415 const int candidate = int(rng() % uint32_t(width * height));
416 if (cells[size_t(candidate)].elevation <= sea + 0.34f) continue;
417 float sourceDistance = float(width + height);
418 const int candidateQ = candidate % width;
419 const int candidateR = candidate / width;
420 for (int source : riverSources) {
421 const int dq = candidateQ - source % width;
422 const int dr = candidateR - source / width;
423 sourceDistance = std::min(sourceDistance, std::sqrt(float(dq * dq + dr * dr)));
424 }
425 const float sourceScore = cells[size_t(candidate)].elevation + sourceDistance * 0.014f;
426 if (sourceScore > bestSourceScore) {
427 bestSourceScore = sourceScore;
428 current = candidate;
429 }
430 }
431 if (current >= 0) riverSources.push_back(current);
432 for (int step = 0; current >= 0 && step < width + height; ++step) {
433 Cell& c = cells[size_t(current)];
434 if (c.elevation <= sea + 0.02f) break;
435 visited[size_t(current)] = 1;
436 c.river = std::max(c.river, std::min(0.95f, 0.35f + float(step) * 0.025f));
437 c.secondary = River;
438 int next = -1;
439 int nextEdge = -1;
440 float bestScore = std::numeric_limits<float>::max();
441 const int q = current % width, r = current / width;
442 const auto ns = neighbours(q, r, width, height);
443 for (int edge = 0; edge < 6; ++edge) {
444 const int n = ns[size_t(edge)];
445 if (n >= 0 && !visited[size_t(n)]) {
446 float score = cells[size_t(n)].elevation;
447 const float routeNoise = float(hash(seed ^ uint32_t(current) * 0x9e3779b9u ^
448 uint32_t(edge) * 0x85ebca6bu) & 0xffffu) /
449 65535.f;
450 score += (routeNoise - 0.5f) * 0.055f;
451 if (lastEdge >= 0) {
452 const int delta = (edge - lastEdge + 6) % 6;
453 const int turnSign = ((step / 3 + river) & 1) == 0 ? 1 : -1;
454 const int preferredEdge = (lastEdge + turnSign + 6) % 6;
455 if (edge == lastEdge) score += 0.045f;
456 if (delta == 3) score += 0.30f;
457 if (edge == preferredEdge) score -= 0.060f;
458 }
459 if (cells[size_t(n)].river > 0.f) score -= 0.018f;
460 if (score >= bestScore) continue;
461 bestScore = score;
462 next = n;
463 nextEdge = edge;
464 }
465 }
466 if (next < 0) break;
467 if (cells[size_t(next)].elevation >= c.elevation + 0.075f && step >= 5 &&
468 c.elevation > sea + 0.10f) {
469 c.lake = true;
470 c.biome = Ocean;
471 c.secondary = River;
472 c.river = std::max(c.river, 0.72f);
473 break;
474 }
475 if (cells[size_t(next)].elevation >= c.elevation)
476 cells[size_t(next)].elevation = std::max(sea + 0.01f, c.elevation - 0.018f);
477 c.riverEdges |= uint8_t(1u << uint32_t(nextEdge));
478 cells[size_t(next)].riverEdges |= uint8_t(1u << uint32_t((nextEdge + 3) % 6));
479 current = next;
480 lastEdge = nextEdge;
481 }
482 }
483
484 for (int r = 0; r < height; ++r) for (int q = 0; q < width; ++q) {
485 Cell& c = cells[size_t(q + r * width)];
486 const auto ns = neighbours(q, r, width, height);
487 float bestDifference = 0.f;
488 for (int n : ns) if (n >= 0 && cells[size_t(n)].biome != c.biome) {
489 const float difference = std::abs(c.elevation - cells[size_t(n)].elevation);
490 if (difference >= bestDifference) {
491 bestDifference = difference;
492 c.secondary = cells[size_t(n)].biome;
493 c.blend = std::clamp(0.18f + (1.f - difference) * 0.32f, 0.f, 0.49f);
494 }
495 }
496 if (c.river > 0.f) c.secondary = River;
497 }
498
499 out.clear();
500 out.reserve(width * height * 18, width * height * 30);
501 std::array<int, 12> biomeCounts{};
502 int cliffEdgeCount = 0;
503 int riverCellCount = 0;
504 int lakeCellCount = 0;
505 const float xStep = radius * 1.5f, zStep = radius * 1.7320508076f;
506 for (int r = 0; r < height; ++r) for (int q = 0; q < width; ++q) {
507 const int idx = q + r * width;
508 const Cell& c = cells[size_t(idx)];
509 const float cx = float(q) * xStep;
510 const float cz = (float(r) + float(q & 1) * 0.5f) * zStep;
511 const float y = surfaceHeight(c, sea, heightScale);
512 const auto ns = neighbours(q, r, width, height);
513 ++biomeCounts[size_t(std::clamp(c.biome, 0, 11))];
514 if (c.riverEdges != 0) ++riverCellCount;
515 if (c.lake) ++lakeCellCount;
516 addTop(out, cx, cz, y, radius, c);
517 if (c.riverEdges != 0) {
518 addRiverJunction(out, cx, y + radius * 0.013f, cz, radius, c.river);
519 for (int edge = 0; edge < 6; ++edge)
520 if ((c.riverEdges & uint8_t(1u << uint32_t(edge))) != 0)
521 {
522 const int neighbourIndex = ns[size_t(edge)];
523 const int safeNeighbour = neighbourIndex >= 0 ? neighbourIndex : idx;
524 const uint32_t lo = uint32_t(std::min(idx, safeNeighbour));
525 const uint32_t hi = uint32_t(std::max(idx, safeNeighbour));
526 addRiverArm(out, cx, y + radius * 0.012f, cz, radius, edge, c.river,
527 hash(seed ^ lo * 0x27d4eb2du ^ hi * 0x85ebca6bu));
528 }
529 }
530 for (int edge = 0; edge < 6; ++edge) {
531 float neighbourY = -sea * heightScale;
532 if (ns[size_t(edge)] >= 0) {
533 const Cell& nc = cells[size_t(ns[size_t(edge)])];
534 neighbourY = surfaceHeight(nc, sea, heightScale);
535 }
536 if (y > neighbourY + 0.025f) {
537 addCliff(out, cx, cz, radius, edge, y, neighbourY, c,
538 hash(seed ^ uint32_t(idx) * 0x9e3779b9u ^ uint32_t(edge)));
539 ++cliffEdgeCount;
540 }
541
542 if (c.biome == Coast && ns[size_t(edge)] >= 0 &&
543 cells[size_t(ns[size_t(edge)])].biome <= Ocean)
544 addShoreBand(out, cx, y + radius * 0.014f, cz, radius, edge);
545 }
546
547 if (decorations && (c.biome == Forest || c.biome == Rainforest || c.biome == Swamp)) {
548 const uint32_t cellSeed = hash(seed ^ uint32_t(idx) * 0x9e3779b9u);
549 const int baseCount = c.biome == Rainforest ? 4 : (c.biome == Forest ? 3 : 2);
550 const int count = int(std::round(float(baseCount) * vegetationDensity));
551 for (int tree = 0; tree < count; ++tree) {
552 const uint32_t h = hash(cellSeed + uint32_t(tree) * 0x85ebca6bu);
553 const float a = float(h & 0xffffu) / 65535.f * 6.2831853072f;
554 const float d = (0.16f + float((h >> 16) & 0xffu) / 255.f * 0.36f) * radius;
555 const float variation = 0.82f + float((h >> 24) & 0xffu) / 255.f * 0.38f;
556 const float scale = radius * variation * (c.biome == Swamp ? 0.75f : 1.25f);
557 const int treeBiome = c.biome == Swamp ? Forest : c.biome;
558 const bool broadleaf = c.biome != Forest || ((h >> 12) & 1u) != 0;
559 addTree(out, cx + std::cos(a) * d, y, cz + std::sin(a) * d, scale,
560 treeBiome, broadleaf);
561 }
562 } else if (decorations && c.biome == Mountain) {
563 const uint32_t mountainSeed = hash(seed ^ uint32_t(idx) * 0x27d4eb2du);
564 const float mainHeight = radius *
565 (1.20f + float(mountainSeed & 0xffu) / 255.f * 0.58f);
566 const float mainRadius = radius *
567 (0.48f + float((mountainSeed >> 8) & 0xffu) / 255.f * 0.12f);
568 const float angle = float((mountainSeed >> 16) & 0xffffu) / 65535.f *
569 6.2831853072f;
570 addCone(out, cx - std::cos(angle) * radius * 0.08f, y - radius * 0.04f,
571 cz - std::sin(angle) * radius * 0.08f, mainRadius, mainHeight,
572 Mountain, 8, mountainSeed);
573 addCone(out, cx + std::cos(angle) * radius * 0.28f, y - radius * 0.025f,
574 cz + std::sin(angle) * radius * 0.28f, radius * 0.31f,
575 mainHeight * 0.64f, Mountain, 7, mountainSeed ^ 0x68bc21ebu);
576 if (((mountainSeed >> 30) & 1u) != 0)
577 addCone(out, cx + std::cos(angle + 2.1f) * radius * 0.25f,
578 y - radius * 0.02f,
579 cz + std::sin(angle + 2.1f) * radius * 0.25f,
580 radius * 0.24f, mainHeight * 0.48f, Mountain, 6,
581 mountainSeed ^ 0x02e5be93u);
582 }
583 }
584 out.setMeta("algorithm", "mesh.hexterrain");
585 out.setMeta("uvEncoding", "u=primaryBiome+blend,v=secondaryBiome+riverCoverage");
586 out.setMeta("biomeCount", "12");
587 out.setMeta("shoreGeometry", "edge-bands");
588 out.setMeta("hydrology", "drainage-rivers+basin-lakes+confluences");
589 out.setMeta("riverGeometry", "seeded-quadratic-ribbons");
590 out.setMeta("cliffGeometry", "seeded-segmented-rock-walls");
591 out.setMeta("mountainGeometry", "seeded-offset-three-ring-peaks");
592 static constexpr const char* biomeNames[10] = {
593 "deepOcean", "ocean", "coast", "grassland", "hills",
594 "mountain", "forest", "swamp", "rainforest", "ice",
595 };
596 for (int biome = 0; biome < 10; ++biome)
597 out.setMeta(std::string("cells.") + biomeNames[biome],
598 std::to_string(biomeCounts[size_t(biome)]));
599 out.setMeta("cells.river", std::to_string(riverCellCount));
600 out.setMeta("cells.lake", std::to_string(lakeCellCount));
601 out.setMeta("edges.cliff", std::to_string(cliffEdgeCount));
602 return !out.empty();
603}
604
605} // namespace eve::procgen
double z
double y
double x
double score
Definition Agent.cpp:51
float cx
Definition CardTypes.cpp:36
float u
Definition Grass.cpp:233
glm::vec3 n
Definition Grass.cpp:63
std::array< double, 10 > q
double r
float moisture
float v
float halfWidth
std::int32_t c
float elevation
uint8_t riverEdges
int secondary
float river
float blend
int biome
bool lake
float temperature
int h
std::vector< Colorf > px
std::uint32_t height
std::uint32_t width
std::array< float, 3 > scale
std::string coverage
Poco::JSON::Object::Ptr result
std::vector< RecipeParam > params
MeleePoint3 b
Definition MeleeHit.cpp:41
double radius
Definition MeleeHit.cpp:42
MeleePoint3 a
Definition MeleeHit.cpp:40
int level
glm::dvec2 center
glm::dvec3 dx
glm::dvec3 dz
std::string error
Definition Package.cpp:174
int idx
std::array< PixelCell, kPixelChunkSize *kPixelChunkSize > cells
Object::Ptr value
std::string hash
float d
float t
ScriptStructure out
std::string_view source
double current
int count
int segments
Definition TreeMesh.cpp:281
float relief
Definition TreeMesh.cpp:274
uint32_t seed
Definition TreeMesh.cpp:276
float step
Definition TreeMesh.cpp:451
float heightScale
float bottom
CPU triangle mesh from procedural mesh recipes (e.g. marching cubes). Positions/normals are xyz-packe...
Definition MeshBuild.h:20
Owning, typed generation parameters.
Definition Params.h:27
SQInteger top
Definition Snapshot.cpp:115
@ Cell
A cell was removed; the out-parameter holds it.
constexpr HexDirection next(HexDirection d) noexcept
The next direction clockwise (NW wraps to NE).
Definition HexMetrics.h:76
double sample(const Heightmap &map, double u, double v)
Sample.
bool generateHexTerrainMesh(const Params &params, MeshBuild &out, std::string &error)
Build a deterministic, flat-top hexagonal continent mesh.
Biome
Biome public API.