载入中...
搜索中...
未找到
CaveBoundary.cpp
浏览该文件的文档.
2
3#include <algorithm>
4#include <cmath>
5#include <cstddef>
6
7namespace eve::procgen {
8namespace {
9
10bool boundarySample(int x, int y, int z, int nx, int ny, int nz) {
11 return x == 0 || y == 0 || z == 0 || x == nx - 1 || y == ny - 1 || z == nz - 1;
12}
13
14int countBoundaryAir(const std::vector<float>& density, int nx, int ny, int nz) {
15 int count = 0;
16 for (int z = 0; z < nz; ++z)
17 for (int y = 0; y < ny; ++y)
18 for (int x = 0; x < nx; ++x)
19 if (boundarySample(x, y, z, nx, ny, nz) &&
20 density[size_t(x) + size_t(y) * size_t(nx) + size_t(z) * size_t(nx) * size_t(ny)] < 0.f)
21 ++count;
22 return count;
23}
24
25} // namespace
26
27CaveBoundaryClosure closeCaveDensityBoundary(std::vector<float>& density, int nx, int ny, int nz, float strength,
28 uint32_t seed) {
30 result.airSamplesBefore = countBoundaryAir(density, nx, ny, nz);
31 if (strength <= 0.f) {
32 result.airSamplesAfter = result.airSamplesBefore;
33 return result;
34 }
35 const float baseDepth = 0.035f + 0.065f * std::clamp(strength, 0.f, 1.f);
36 const float phase = float(seed & 0xffffu) * (6.28318530718f / 65536.f);
37 for (int z = 0; z < nz; ++z) {
38 const float pz = float(z) / float(nz - 1) * 2.f - 1.f;
39 for (int y = 0; y < ny; ++y) {
40 const float py = float(y) / float(ny - 1) * 2.f - 1.f;
41 for (int x = 0; x < nx; ++x) {
42 const float px = float(x) / float(nx - 1) * 2.f - 1.f;
43 const float inwardDistance = 1.f - std::max({std::fabs(px), std::fabs(py), std::fabs(pz)});
44 const float roughness = 1.f + 0.22f * std::sin(px * 11.3f + py * 7.1f + phase) *
45 std::sin(pz * 9.7f - py * 5.3f + phase * 0.73f);
46 const float envelope = baseDepth * roughness - inwardDistance;
47 float& value = density[size_t(x) + size_t(y) * size_t(nx) + size_t(z) * size_t(nx) * size_t(ny)];
48 if (envelope > value) {
49 value = envelope;
50 ++result.changedVoxels;
51 }
52 }
53 }
54 }
55 result.airSamplesAfter = countBoundaryAir(density, nx, ny, nz);
56 return result;
57}
58
59} // namespace eve::procgen
double value
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
float z
Definition AnimClip.cpp:738
float phase
Definition CaveMesh.cpp:58
float py
float nx
float nz
float ny
float pz
std::vector< Colorf > px
float roughness
std::uint32_t seed
Definition PointSet.cpp:807
std::uint32_t count
CaveBoundaryClosure closeCaveDensityBoundary(std::vector< float > &density, int nx, int ny, int nz, float strength, uint32_t seed)
Apply a deterministic host-rock envelope at the finite density domain boundary.
CaveBoundaryClosure public API.