terrain_renderer.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. #include "terrain_renderer.h"
  2. #include "../../game/map/visibility_service.h"
  3. #include "../gl/mesh.h"
  4. #include "../gl/render_constants.h"
  5. #include "../gl/resources.h"
  6. #include "../scene_renderer.h"
  7. #include "ground/terrain_gpu.h"
  8. #include "ground_utils.h"
  9. #include "map/terrain.h"
  10. #include <QDebug>
  11. #include <QElapsedTimer>
  12. #include <QQuaternion>
  13. #include <QVector2D>
  14. #include <QtGlobal>
  15. #include <algorithm>
  16. #include <cmath>
  17. #include <cstddef>
  18. #include <cstdint>
  19. #include <limits>
  20. #include <memory>
  21. #include <qelapsedtimer.h>
  22. #include <qglobal.h>
  23. #include <qmatrix4x4.h>
  24. #include <qvectornd.h>
  25. #include <unordered_map>
  26. #include <utility>
  27. #include <vector>
  28. namespace {
  29. using std::uint32_t;
  30. using namespace Render::GL::BitShift;
  31. using namespace Render::GL::Geometry;
  32. using namespace Render::GL::HashXorShift;
  33. using namespace Render::Ground;
  34. const QMatrix4x4 k_identity_matrix;
  35. inline auto applyTint(const QVector3D &color, float tint) -> QVector3D {
  36. QVector3D const c = color * tint;
  37. return {std::clamp(c.x(), 0.0F, 1.0F), std::clamp(c.y(), 0.0F, 1.0F),
  38. std::clamp(c.z(), 0.0F, 1.0F)};
  39. }
  40. inline auto clamp01(const QVector3D &c) -> QVector3D {
  41. return {std::clamp(c.x(), 0.0F, 1.0F), std::clamp(c.y(), 0.0F, 1.0F),
  42. std::clamp(c.z(), 0.0F, 1.0F)};
  43. }
  44. inline auto linstep(float a, float b, float x) -> float {
  45. return std::clamp((x - a) / std::max(1e-6F, (b - a)), 0.0F, 1.0F);
  46. }
  47. inline auto smooth(float a, float b, float x) -> float {
  48. float const t = linstep(a, b, x);
  49. return t * t * (3.0F - 2.0F * t);
  50. }
  51. inline auto valueNoise(float x, float z, uint32_t salt = 0U) -> float {
  52. int const x0 = int(std::floor(x));
  53. int const z0 = int(std::floor(z));
  54. int const x1 = x0 + 1;
  55. int const z1 = z0 + 1;
  56. float const tx = x - float(x0);
  57. float const tz = z - float(z0);
  58. float const n00 = hash_to_01(hash_coords(x0, z0, salt));
  59. float const n10 = hash_to_01(hash_coords(x1, z0, salt));
  60. float const n01 = hash_to_01(hash_coords(x0, z1, salt));
  61. float const n11 = hash_to_01(hash_coords(x1, z1, salt));
  62. float const nx0 = n00 * (1 - tx) + n10 * tx;
  63. float const nx1 = n01 * (1 - tx) + n11 * tx;
  64. return nx0 * (1 - tz) + nx1 * tz;
  65. }
  66. } // namespace
  67. namespace Render::GL {
  68. TerrainRenderer::TerrainRenderer() = default;
  69. TerrainRenderer::~TerrainRenderer() = default;
  70. void TerrainRenderer::configure(const Game::Map::TerrainHeightMap &height_map,
  71. const Game::Map::BiomeSettings &biomeSettings) {
  72. m_width = height_map.getWidth();
  73. m_height = height_map.getHeight();
  74. m_tile_size = height_map.getTileSize();
  75. m_heightData = height_map.getHeightData();
  76. m_terrain_types = height_map.getTerrainTypes();
  77. m_biomeSettings = biomeSettings;
  78. m_noiseSeed = biomeSettings.seed;
  79. buildMeshes();
  80. }
  81. void TerrainRenderer::submit(Renderer &renderer, ResourceManager *resources) {
  82. if (m_chunks.empty()) {
  83. return;
  84. }
  85. Q_UNUSED(resources);
  86. auto &visibility = Game::Map::VisibilityService::instance();
  87. const bool use_visibility = visibility.isInitialized();
  88. for (const auto &chunk : m_chunks) {
  89. if (!chunk.mesh) {
  90. continue;
  91. }
  92. if (use_visibility) {
  93. bool any_visible = false;
  94. for (int gz = chunk.minZ; gz <= chunk.maxZ && !any_visible; ++gz) {
  95. for (int gx = chunk.minX; gx <= chunk.maxX; ++gx) {
  96. if (visibility.stateAt(gx, gz) ==
  97. Game::Map::VisibilityState::Visible) {
  98. any_visible = true;
  99. break;
  100. }
  101. }
  102. }
  103. if (!any_visible) {
  104. continue;
  105. }
  106. }
  107. renderer.terrainChunk(chunk.mesh.get(), k_identity_matrix, chunk.params,
  108. 0x0080U, true, 0.0F);
  109. }
  110. }
  111. auto TerrainRenderer::sectionFor(Game::Map::TerrainType type) -> int {
  112. switch (type) {
  113. case Game::Map::TerrainType::Mountain:
  114. return 2;
  115. case Game::Map::TerrainType::Hill:
  116. return 1;
  117. case Game::Map::TerrainType::Flat:
  118. default:
  119. return 0;
  120. }
  121. }
  122. void TerrainRenderer::buildMeshes() {
  123. QElapsedTimer timer;
  124. timer.start();
  125. m_chunks.clear();
  126. if (m_width < 2 || m_height < 2 || m_heightData.empty()) {
  127. return;
  128. }
  129. float min_h = std::numeric_limits<float>::infinity();
  130. float max_h = -std::numeric_limits<float>::infinity();
  131. for (float const h : m_heightData) {
  132. min_h = std::min(min_h, h);
  133. max_h = std::max(max_h, h);
  134. }
  135. const float height_range = std::max(1e-4F, max_h - min_h);
  136. const float half_width = m_width * 0.5F - 0.5F;
  137. const float half_height = m_height * 0.5F - 0.5F;
  138. const int vertex_count = m_width * m_height;
  139. std::vector<QVector3D> positions(vertex_count);
  140. std::vector<QVector3D> normals(vertex_count, QVector3D(0.0F, 0.0F, 0.0F));
  141. std::vector<QVector3D> face_accum(vertex_count, QVector3D(0, 0, 0));
  142. for (int z = 0; z < m_height; ++z) {
  143. for (int x = 0; x < m_width; ++x) {
  144. int const idx = z * m_width + x;
  145. float const world_x = (x - half_width) * m_tile_size;
  146. float const world_z = (z - half_height) * m_tile_size;
  147. positions[idx] = QVector3D(world_x, m_heightData[idx], world_z);
  148. }
  149. }
  150. auto accumulate_normal = [&](int i0, int i1, int i2) {
  151. const QVector3D &v0 = positions[i0];
  152. const QVector3D &v1 = positions[i1];
  153. const QVector3D &v2 = positions[i2];
  154. QVector3D const normal = QVector3D::crossProduct(v1 - v0, v2 - v0);
  155. normals[i0] += normal;
  156. normals[i1] += normal;
  157. normals[i2] += normal;
  158. };
  159. auto sample_height_at = [&](float gx, float gz) {
  160. gx = std::clamp(gx, 0.0F, float(m_width - 1));
  161. gz = std::clamp(gz, 0.0F, float(m_height - 1));
  162. int const x0 = int(std::floor(gx));
  163. int const z0 = int(std::floor(gz));
  164. int const x1 = std::min(x0 + 1, m_width - 1);
  165. int const z1 = std::min(z0 + 1, m_height - 1);
  166. float const tx = gx - float(x0);
  167. float const tz = gz - float(z0);
  168. float const h00 = m_heightData[z0 * m_width + x0];
  169. float const h10 = m_heightData[z0 * m_width + x1];
  170. float const h01 = m_heightData[z1 * m_width + x0];
  171. float const h11 = m_heightData[z1 * m_width + x1];
  172. float const h0 = h00 * (1.0F - tx) + h10 * tx;
  173. float const h1 = h01 * (1.0F - tx) + h11 * tx;
  174. return h0 * (1.0F - tz) + h1 * tz;
  175. };
  176. auto normal_from_heights_at = [&](float gx, float gz) {
  177. float const gx0 = std::clamp(gx - 1.0F, 0.0F, float(m_width - 1));
  178. float const gx1 = std::clamp(gx + 1.0F, 0.0F, float(m_width - 1));
  179. float const gz0 = std::clamp(gz - 1.0F, 0.0F, float(m_height - 1));
  180. float const gz1 = std::clamp(gz + 1.0F, 0.0F, float(m_height - 1));
  181. float const h_l = sample_height_at(gx0, gz);
  182. float const h_r = sample_height_at(gx1, gz);
  183. float const h_d = sample_height_at(gx, gz0);
  184. float const h_u = sample_height_at(gx, gz1);
  185. QVector3D const dx(2.0F * m_tile_size, h_r - h_l, 0.0F);
  186. QVector3D const dz(0.0F, h_u - h_d, 2.0F * m_tile_size);
  187. QVector3D n = QVector3D::crossProduct(dz, dx);
  188. if (n.lengthSquared() > 0.0F) {
  189. n.normalize();
  190. }
  191. return n.isNull() ? QVector3D(0, 1, 0) : n;
  192. };
  193. for (int z = 0; z < m_height - 1; ++z) {
  194. for (int x = 0; x < m_width - 1; ++x) {
  195. int const idx0 = z * m_width + x;
  196. int const idx1 = idx0 + 1;
  197. int const idx2 = (z + 1) * m_width + x;
  198. int const idx3 = idx2 + 1;
  199. accumulate_normal(idx0, idx1, idx2);
  200. accumulate_normal(idx2, idx1, idx3);
  201. }
  202. }
  203. for (int i = 0; i < vertex_count; ++i) {
  204. normals[i].normalize();
  205. if (normals[i].isNull()) {
  206. normals[i] = QVector3D(0.0F, 1.0F, 0.0F);
  207. }
  208. face_accum[i] = normals[i];
  209. }
  210. {
  211. std::vector<QVector3D> filtered = normals;
  212. auto get_n = [&](int x, int z) -> QVector3D & {
  213. return normals[z * m_width + x];
  214. };
  215. for (int z = 1; z < m_height - 1; ++z) {
  216. for (int x = 1; x < m_width - 1; ++x) {
  217. const int idx = z * m_width + x;
  218. const float h0 = m_heightData[idx];
  219. const float nh = (h0 - min_h) / height_range;
  220. const float h_l = m_heightData[z * m_width + (x - 1)];
  221. const float h_r = m_heightData[z * m_width + (x + 1)];
  222. const float h_d = m_heightData[(z - 1) * m_width + x];
  223. const float h_u = m_heightData[(z + 1) * m_width + x];
  224. const float avg_nbr = 0.25F * (h_l + h_r + h_d + h_u);
  225. const float convexity = h0 - avg_nbr;
  226. const QVector3D n0 = normals[idx];
  227. const float slope = 1.0F - std::clamp(n0.y(), 0.0F, 1.0F);
  228. const float ridge_s = smooth(0.35F, 0.70F, slope);
  229. const float ridge_c = smooth(0.00F, 0.20F, convexity);
  230. const float ridge_factor =
  231. std::clamp(0.5F * ridge_s + 0.5F * ridge_c, 0.0F, 1.0F);
  232. const float base_boost = 0.6F * (1.0F - nh);
  233. QVector3D acc(0, 0, 0);
  234. float wsum = 0.0F;
  235. for (int dz = -1; dz <= 1; ++dz) {
  236. for (int dx = -1; dx <= 1; ++dx) {
  237. const int nx = x + dx;
  238. const int nz = z + dz;
  239. const int n_idx = nz * m_width + nx;
  240. const float dh = std::abs(m_heightData[n_idx] - h0);
  241. const QVector3D nn = get_n(nx, nz);
  242. const float ndot = std::max(0.0F, QVector3D::dotProduct(n0, nn));
  243. const float w_h = 1.0F / (1.0F + 2.0F * dh);
  244. const float w_n = std::pow(ndot, 8.0F);
  245. const float w_b = 1.0F + base_boost;
  246. const float w_r = 1.0F - ridge_factor * 0.85F;
  247. const float w = w_h * w_n * w_b * w_r;
  248. acc += nn * w;
  249. wsum += w;
  250. }
  251. }
  252. QVector3D n_filtered = (wsum > 0.0F) ? (acc / wsum) : n0;
  253. n_filtered.normalize();
  254. const QVector3D n_orig = face_accum[idx];
  255. const QVector3D n_final =
  256. (ridge_factor > 0.0F)
  257. ? (n_filtered * (1.0F - ridge_factor) + n_orig * ridge_factor)
  258. : n_filtered;
  259. filtered[idx] = n_final.normalized();
  260. }
  261. }
  262. normals.swap(filtered);
  263. }
  264. auto quad_section = [&](Game::Map::TerrainType a, Game::Map::TerrainType b,
  265. Game::Map::TerrainType c, Game::Map::TerrainType d) {
  266. int const priority_a = sectionFor(a);
  267. int const priority_b = sectionFor(b);
  268. int const priority_c = sectionFor(c);
  269. int const priority_d = sectionFor(d);
  270. int result = priority_a;
  271. result = std::max(result, priority_b);
  272. result = std::max(result, priority_c);
  273. result = std::max(result, priority_d);
  274. return result;
  275. };
  276. const int chunk_size = DefaultChunkSize;
  277. std::size_t total_triangles = 0;
  278. for (int chunk_z = 0; chunk_z < m_height - 1; chunk_z += chunk_size) {
  279. int const chunk_max_z = std::min(chunk_z + chunk_size, m_height - 1);
  280. for (int chunk_x = 0; chunk_x < m_width - 1; chunk_x += chunk_size) {
  281. int const chunk_max_x = std::min(chunk_x + chunk_size, m_width - 1);
  282. struct SectionData {
  283. std::vector<Vertex> vertices;
  284. std::vector<unsigned int> indices;
  285. std::unordered_map<int, unsigned int> remap;
  286. float heightSum = 0.0F;
  287. int heightCount = 0;
  288. float rotationDeg = 0.0F;
  289. bool flipU = false;
  290. float tint = 1.0F;
  291. QVector3D normalSum = QVector3D(0, 0, 0);
  292. float slopeSum = 0.0F;
  293. float heightVarSum = 0.0F;
  294. int statCount = 0;
  295. float aoSum = 0.0F;
  296. int aoCount = 0;
  297. };
  298. SectionData sections[3];
  299. uint32_t const chunk_seed = hash_coords(chunk_x, chunk_z, m_noiseSeed);
  300. uint32_t const variant_seed = chunk_seed ^ k_golden_ratio;
  301. constexpr int k_rotation_shift = 5;
  302. constexpr int k_rotation_mask = 3;
  303. constexpr float k_rotation_step_degrees = 90.0F;
  304. constexpr int k_flip_shift = 7;
  305. constexpr int k_tint_shift = 12;
  306. constexpr int k_tint_variant_count = 7;
  307. float const rotation_step =
  308. static_cast<float>((variant_seed >> k_rotation_shift) &
  309. k_rotation_mask) *
  310. k_rotation_step_degrees;
  311. bool const flip = ((variant_seed >> k_flip_shift) & 1U) != 0U;
  312. static const float tint_variants[k_tint_variant_count] = {
  313. 0.9F, 0.94F, 0.97F, 1.0F, 1.03F, 1.06F, 1.1F};
  314. float const tint =
  315. tint_variants[(variant_seed >> k_tint_shift) % k_tint_variant_count];
  316. for (auto &section : sections) {
  317. section.rotationDeg = rotation_step;
  318. section.flipU = flip;
  319. section.tint = tint;
  320. }
  321. auto ensure_vertex = [&](SectionData &section,
  322. int globalIndex) -> unsigned int {
  323. auto it = section.remap.find(globalIndex);
  324. if (it != section.remap.end()) {
  325. return it->second;
  326. }
  327. Vertex v{};
  328. const QVector3D &pos = positions[globalIndex];
  329. const QVector3D &normal = normals[globalIndex];
  330. v.position[0] = pos.x();
  331. v.position[1] = pos.y();
  332. v.position[2] = pos.z();
  333. v.normal[0] = normal.x();
  334. v.normal[1] = normal.y();
  335. v.normal[2] = normal.z();
  336. float const tex_scale = 0.2F / std::max(1.0F, m_tile_size);
  337. float uu = pos.x() * tex_scale;
  338. float const vv = pos.z() * tex_scale;
  339. if (section.flipU) {
  340. uu = -uu;
  341. }
  342. float ru = uu;
  343. float rv = vv;
  344. switch (static_cast<int>(section.rotationDeg)) {
  345. case 90: {
  346. float const t = ru;
  347. ru = -rv;
  348. rv = t;
  349. } break;
  350. case 180:
  351. ru = -ru;
  352. rv = -rv;
  353. break;
  354. case 270: {
  355. float const t = ru;
  356. ru = rv;
  357. rv = -t;
  358. } break;
  359. default:
  360. break;
  361. }
  362. v.tex_coord[0] = ru;
  363. v.tex_coord[1] = rv;
  364. section.vertices.push_back(v);
  365. auto const local_index =
  366. static_cast<unsigned int>(section.vertices.size() - 1);
  367. section.remap.emplace(globalIndex, local_index);
  368. section.normalSum += normal;
  369. return local_index;
  370. };
  371. for (int z = chunk_z; z < chunk_max_z; ++z) {
  372. for (int x = chunk_x; x < chunk_max_x; ++x) {
  373. int const idx0 = z * m_width + x;
  374. int const idx1 = idx0 + 1;
  375. int const idx2 = (z + 1) * m_width + x;
  376. int const idx3 = idx2 + 1;
  377. int const section_index =
  378. quad_section(m_terrain_types[idx0], m_terrain_types[idx1],
  379. m_terrain_types[idx2], m_terrain_types[idx3]);
  380. if (section_index > 0) {
  381. SectionData &section = sections[section_index];
  382. unsigned int const v0 = ensure_vertex(section, idx0);
  383. unsigned int const v1 = ensure_vertex(section, idx1);
  384. unsigned int const v2 = ensure_vertex(section, idx2);
  385. unsigned int const v3 = ensure_vertex(section, idx3);
  386. section.indices.push_back(v0);
  387. section.indices.push_back(v1);
  388. section.indices.push_back(v2);
  389. section.indices.push_back(v2);
  390. section.indices.push_back(v1);
  391. section.indices.push_back(v3);
  392. float const quad_height =
  393. (m_heightData[idx0] + m_heightData[idx1] + m_heightData[idx2] +
  394. m_heightData[idx3]) *
  395. 0.25F;
  396. section.heightSum += quad_height;
  397. section.heightCount += 1;
  398. float const n_y = (normals[idx0].y() + normals[idx1].y() +
  399. normals[idx2].y() + normals[idx3].y()) *
  400. 0.25F;
  401. float const slope = 1.0F - std::clamp(n_y, 0.0F, 1.0F);
  402. section.slopeSum += slope;
  403. float const hmin =
  404. std::min(std::min(m_heightData[idx0], m_heightData[idx1]),
  405. std::min(m_heightData[idx2], m_heightData[idx3]));
  406. float const hmax =
  407. std::max(std::max(m_heightData[idx0], m_heightData[idx1]),
  408. std::max(m_heightData[idx2], m_heightData[idx3]));
  409. section.heightVarSum += (hmax - hmin);
  410. section.statCount += 1;
  411. auto h = [&](int gx, int gz) {
  412. gx = std::clamp(gx, 0, m_width - 1);
  413. gz = std::clamp(gz, 0, m_height - 1);
  414. return m_heightData[gz * m_width + gx];
  415. };
  416. int const cx = x;
  417. int const cz = z;
  418. float const h_c = quad_height;
  419. float ao = 0.0F;
  420. ao += std::max(0.0F, h(cx - 1, cz) - h_c);
  421. ao += std::max(0.0F, h(cx + 1, cz) - h_c);
  422. ao += std::max(0.0F, h(cx, cz - 1) - h_c);
  423. ao += std::max(0.0F, h(cx, cz + 1) - h_c);
  424. ao = std::clamp(ao * 0.15F, 0.0F, 1.0F);
  425. section.aoSum += ao;
  426. section.aoCount += 1;
  427. }
  428. }
  429. }
  430. for (int i = 0; i < 3; ++i) {
  431. SectionData const &section = sections[i];
  432. if (section.indices.empty()) {
  433. continue;
  434. }
  435. auto mesh = std::make_unique<Mesh>(section.vertices, section.indices);
  436. if (!mesh) {
  437. continue;
  438. }
  439. ChunkMesh chunk;
  440. chunk.mesh = std::move(mesh);
  441. chunk.minX = chunk_x;
  442. chunk.maxX = chunk_max_x - 1;
  443. chunk.minZ = chunk_z;
  444. chunk.maxZ = chunk_max_z - 1;
  445. chunk.type = (i == 0) ? Game::Map::TerrainType::Flat
  446. : (i == 1) ? Game::Map::TerrainType::Hill
  447. : Game::Map::TerrainType::Mountain;
  448. chunk.averageHeight =
  449. (section.heightCount > 0)
  450. ? section.heightSum / float(section.heightCount)
  451. : 0.0F;
  452. const float nh_chunk = (chunk.averageHeight - min_h) / height_range;
  453. const float avg_slope =
  454. (section.statCount > 0)
  455. ? (section.slopeSum / float(section.statCount))
  456. : 0.0F;
  457. const float roughness =
  458. (section.statCount > 0)
  459. ? (section.heightVarSum / float(section.statCount))
  460. : 0.0F;
  461. const float center_gx = 0.5F * (chunk.minX + chunk.maxX);
  462. const float center_gz = 0.5F * (chunk.minZ + chunk.maxZ);
  463. auto hgrid = [&](int gx, int gz) {
  464. gx = std::clamp(gx, 0, m_width - 1);
  465. gz = std::clamp(gz, 0, m_height - 1);
  466. return m_heightData[gz * m_width + gx];
  467. };
  468. const int cxi = int(center_gx);
  469. const int czi = int(center_gz);
  470. const float h_c = hgrid(cxi, czi);
  471. const float h_l = hgrid(cxi - 1, czi);
  472. const float h_r = hgrid(cxi + 1, czi);
  473. const float h_d = hgrid(cxi, czi - 1);
  474. const float h_u = hgrid(cxi, czi + 1);
  475. const float convexity = h_c - 0.25F * (h_l + h_r + h_d + h_u);
  476. const float edge_factor = smooth(0.25F, 0.55F, avg_slope);
  477. const float entrance_factor =
  478. (1.0F - edge_factor) * smooth(0.00F, 0.15F, -convexity);
  479. const float plateau_flat = 1.0F - smooth(0.10F, 0.25F, avg_slope);
  480. const float plateau_height = smooth(0.60F, 0.80F, nh_chunk);
  481. const float plateau_factor = plateau_flat * plateau_height;
  482. QVector3D const base_color =
  483. getTerrainColor(chunk.type, chunk.averageHeight);
  484. QVector3D const rock_tint = m_biomeSettings.rockLow;
  485. float slope_mix = std::clamp(
  486. avg_slope * ((chunk.type == Game::Map::TerrainType::Flat) ? 0.30F
  487. : (chunk.type == Game::Map::TerrainType::Hill)
  488. ? 0.60F
  489. : 0.90F),
  490. 0.0F, 1.0F);
  491. slope_mix += 0.15F * edge_factor;
  492. slope_mix -= 0.10F * entrance_factor;
  493. slope_mix -= 0.08F * plateau_factor;
  494. slope_mix = std::clamp(slope_mix, 0.0F, 1.0F);
  495. float const center_wx = (center_gx - half_width) * m_tile_size;
  496. float const center_wz = (center_gz - half_height) * m_tile_size;
  497. float const macro = valueNoise(center_wx * 0.02F, center_wz * 0.02F,
  498. m_noiseSeed ^ 0x51C3U);
  499. float const macro_shade = 0.9F + 0.2F * macro;
  500. float const ao_avg = (section.aoCount > 0)
  501. ? (section.aoSum / float(section.aoCount))
  502. : 0.0F;
  503. float const ao_shade = 1.0F - 0.35F * ao_avg;
  504. QVector3D avg_n = section.normalSum;
  505. if (avg_n.lengthSquared() > 0.0F) {
  506. avg_n.normalize();
  507. }
  508. QVector3D const north(0, 0, 1);
  509. float const northness = std::clamp(
  510. QVector3D::dotProduct(avg_n, north) * 0.5F + 0.5F, 0.0F, 1.0F);
  511. QVector3D const cool_tint(0.96F, 1.02F, 1.04F);
  512. QVector3D const warm_tint(1.03F, 1.0F, 0.97F);
  513. QVector3D const aspect_tint =
  514. cool_tint * northness + warm_tint * (1.0F - northness);
  515. float const feature_bright =
  516. 1.0F + 0.08F * plateau_factor - 0.05F * entrance_factor;
  517. QVector3D const feature_tint =
  518. QVector3D(1.0F + 0.03F * plateau_factor - 0.03F * entrance_factor,
  519. 1.0F + 0.01F * plateau_factor - 0.01F * entrance_factor,
  520. 1.0F - 0.02F * plateau_factor + 0.03F * entrance_factor);
  521. chunk.tint = section.tint;
  522. QVector3D color =
  523. base_color * (1.0F - slope_mix) + rock_tint * slope_mix;
  524. color = applyTint(color, chunk.tint);
  525. color *= macro_shade;
  526. color.setX(color.x() * aspect_tint.x() * feature_tint.x());
  527. color.setY(color.y() * aspect_tint.y() * feature_tint.y());
  528. color.setZ(color.z() * aspect_tint.z() * feature_tint.z());
  529. color *= ao_shade * feature_bright;
  530. color = color * 0.96F + QVector3D(0.04F, 0.04F, 0.04F);
  531. chunk.color = clamp01(color);
  532. TerrainChunkParams params;
  533. auto tint_color = [&](const QVector3D &base) {
  534. return clamp01(applyTint(base, chunk.tint));
  535. };
  536. params.grassPrimary = tint_color(m_biomeSettings.grassPrimary);
  537. params.grassSecondary = tint_color(m_biomeSettings.grassSecondary);
  538. params.grassDry = tint_color(m_biomeSettings.grassDry);
  539. params.soilColor = tint_color(m_biomeSettings.soilColor);
  540. params.rockLow = tint_color(m_biomeSettings.rockLow);
  541. params.rockHigh = tint_color(m_biomeSettings.rockHigh);
  542. params.tile_size = std::max(0.001F, m_tile_size);
  543. params.macroNoiseScale = m_biomeSettings.terrainMacroNoiseScale;
  544. params.detail_noiseScale = m_biomeSettings.terrainDetailNoiseScale;
  545. float slope_threshold = m_biomeSettings.terrainRockThreshold;
  546. float sharpness_mul = 1.0F;
  547. if (chunk.type == Game::Map::TerrainType::Hill) {
  548. slope_threshold -= 0.08F;
  549. sharpness_mul = 1.25F;
  550. } else if (chunk.type == Game::Map::TerrainType::Mountain) {
  551. slope_threshold -= 0.16F;
  552. sharpness_mul = 1.60F;
  553. }
  554. slope_threshold -= 0.05F * edge_factor;
  555. slope_threshold += 0.04F * entrance_factor;
  556. slope_threshold = std::clamp(
  557. slope_threshold - std::clamp(avg_slope * 0.20F, 0.0F, 0.12F), 0.05F,
  558. 0.9F);
  559. params.slopeRockThreshold = slope_threshold;
  560. params.slopeRockSharpness = std::max(
  561. 1.0F, m_biomeSettings.terrainRockSharpness * sharpness_mul);
  562. float soil_height = m_biomeSettings.terrainSoilHeight;
  563. if (chunk.type == Game::Map::TerrainType::Hill) {
  564. soil_height -= 0.06F;
  565. } else if (chunk.type == Game::Map::TerrainType::Mountain) {
  566. soil_height -= 0.12F;
  567. }
  568. soil_height += 0.05F * entrance_factor - 0.03F * plateau_factor;
  569. params.soilBlendHeight = soil_height;
  570. params.soilBlendSharpness =
  571. std::max(0.75F, m_biomeSettings.terrainSoilSharpness *
  572. (chunk.type == Game::Map::TerrainType::Mountain
  573. ? 0.80F
  574. : 0.95F));
  575. const uint32_t noise_key_a =
  576. hash_coords(chunk.minX, chunk.minZ, m_noiseSeed ^ 0xB5297A4DU);
  577. const uint32_t noise_key_b =
  578. hash_coords(chunk.minX, chunk.minZ, m_noiseSeed ^ 0x68E31DA4U);
  579. constexpr float k_noise_offset_scale = 256.0F;
  580. params.noiseOffset =
  581. QVector2D(hash_to_01(noise_key_a) * k_noise_offset_scale,
  582. hash_to_01(noise_key_b) * k_noise_offset_scale);
  583. float base_amp =
  584. m_biomeSettings.heightNoiseAmplitude *
  585. (0.7F + 0.3F * std::clamp(roughness * 0.6F, 0.0F, 1.0F));
  586. if (chunk.type == Game::Map::TerrainType::Mountain) {
  587. base_amp *= 1.25F;
  588. }
  589. base_amp *= (1.0F + 0.10F * edge_factor - 0.08F * plateau_factor -
  590. 0.06F * entrance_factor);
  591. params.heightNoiseStrength = base_amp;
  592. params.heightNoiseFrequency = m_biomeSettings.heightNoiseFrequency;
  593. params.ambientBoost =
  594. m_biomeSettings.terrainAmbientBoost *
  595. ((chunk.type == Game::Map::TerrainType::Mountain) ? 0.90F : 0.95F);
  596. params.rockDetailStrength =
  597. m_biomeSettings.terrainRockDetailStrength *
  598. (0.75F + 0.35F * std::clamp(avg_slope * 1.2F, 0.0F, 1.0F) +
  599. 0.15F * edge_factor - 0.10F * plateau_factor -
  600. 0.08F * entrance_factor);
  601. params.tint = clamp01(QVector3D(chunk.tint, chunk.tint, chunk.tint));
  602. params.light_direction = QVector3D(0.35F, 0.8F, 0.45F);
  603. chunk.params = params;
  604. total_triangles += chunk.mesh->getIndices().size() / 3;
  605. m_chunks.push_back(std::move(chunk));
  606. }
  607. }
  608. }
  609. }
  610. auto TerrainRenderer::getTerrainColor(Game::Map::TerrainType type,
  611. float height) const -> QVector3D {
  612. switch (type) {
  613. case Game::Map::TerrainType::Mountain:
  614. if (height > 4.0F) {
  615. return m_biomeSettings.rockHigh;
  616. }
  617. return m_biomeSettings.rockLow;
  618. case Game::Map::TerrainType::Hill: {
  619. float const t = std::clamp(height / 3.0F, 0.0F, 1.0F);
  620. QVector3D const grass = m_biomeSettings.grassSecondary * (1.0F - t) +
  621. m_biomeSettings.grassDry * t;
  622. QVector3D const rock =
  623. m_biomeSettings.rockLow * (1.0F - t) + m_biomeSettings.rockHigh * t;
  624. float const rock_blend = std::clamp(0.25F + 0.5F * t, 0.0F, 0.75F);
  625. return grass * (1.0F - rock_blend) + rock * rock_blend;
  626. }
  627. case Game::Map::TerrainType::Flat:
  628. default: {
  629. float const moisture = std::clamp((height - 0.5F) * 0.2F, 0.0F, 0.4F);
  630. QVector3D const base = m_biomeSettings.grassPrimary * (1.0F - moisture) +
  631. m_biomeSettings.grassSecondary * moisture;
  632. float const dry_blend = std::clamp((height - 2.0F) * 0.12F, 0.0F, 0.3F);
  633. return base * (1.0F - dry_blend) + m_biomeSettings.grassDry * dry_blend;
  634. }
  635. }
  636. }
  637. } // namespace Render::GL