载入中...
搜索中...
未找到
CaveSupport.cpp
浏览该文件的文档.
2
3#include <cmath>
4#include <cstddef>
5#include <cstdint>
6#include <deque>
7
8namespace eve::procgen {
9
10CaveDetachmentResult detachUnsupportedCaveFragments(std::vector<float>& density, int nx, int ny, int nz,
11 float strength) {
13 if (strength <= 0.f || nx <= 0 || ny <= 0 || nz <= 0) return result;
14
15 const auto index = [nx, ny](int x, int y, int z) {
16 return size_t(x) + size_t(y) * size_t(nx) + size_t(z) * size_t(nx) * size_t(ny);
17 };
18 std::vector<uint8_t> anchored(density.size(), uint8_t(0));
19 std::deque<size_t> frontier;
20 auto anchor = [&](int x, int y, int z) {
21 const size_t voxel = index(x, y, z);
22 if (density[voxel] <= 0.f || anchored[voxel] != 0) return;
23 anchored[voxel] = 1;
24 frontier.push_back(voxel);
25 };
26 for (int z = 0; z < nz; ++z) {
27 for (int y = 0; y < ny; ++y) {
28 anchor(0, y, z);
29 anchor(nx - 1, y, z);
30 }
31 }
32 for (int z = 0; z < nz; ++z) {
33 for (int x = 0; x < nx; ++x) {
34 anchor(x, 0, z);
35 anchor(x, ny - 1, z);
36 }
37 }
38 for (int y = 0; y < ny; ++y) {
39 for (int x = 0; x < nx; ++x) {
40 anchor(x, y, 0);
41 anchor(x, y, nz - 1);
42 }
43 }
44
45 while (!frontier.empty()) {
46 const size_t voxel = frontier.front();
47 frontier.pop_front();
48 const int z = int(voxel / (size_t(nx) * size_t(ny)));
49 const int y = int((voxel / size_t(nx)) % size_t(ny));
50 const int x = int(voxel % size_t(nx));
51 for (int dz = -1; dz <= 1; ++dz) {
52 for (int dy = -1; dy <= 1; ++dy) {
53 for (int dx = -1; dx <= 1; ++dx) {
54 if ((dx == 0 && dy == 0 && dz == 0) || x + dx < 0 || x + dx >= nx || y + dy < 0 || y + dy >= ny ||
55 z + dz < 0 || z + dz >= nz)
56 continue;
57 anchor(x + dx, y + dy, z + dz);
58 }
59 }
60 }
61 }
62
63 for (size_t voxel = 0; voxel < density.size(); ++voxel) {
64 if (density[voxel] <= 0.f || anchored[voxel] != 0) continue;
65 ++result.unsupportedVoxels;
66 const float detached = -std::fabs(density[voxel]) - 1e-4f;
67 density[voxel] += (detached - density[voxel]) * strength;
68 if (density[voxel] <= 0.f) ++result.detachedVoxels;
69 }
70 return result;
71}
72
73} // namespace eve::procgen
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
float z
Definition AnimClip.cpp:738
Vec3 anchor
Definition CaveMesh.cpp:90
float nx
float nz
float ny
float dz
float dy
float dx
uint32_t index
CaveDetachmentResult detachUnsupportedCaveFragments(std::vector< float > &density, int nx, int ny, int nz, float strength)
Detaches unsupported cave fragments.
CaveDetachmentResult public API.
Definition CaveSupport.h:10