11 auto quantize = [](
Vec3 p) {
12 return QuantizedPoint{int(std::lround(
p.x * 1000000.f)), int(std::lround(
p.y * 1000000.f)),
13 int(std::lround(
p.z * 1000000.f)), 0};
16 const bool swap = qa.
x > qb.x || (qa.
x == qb.x && qa.
y > qb.y) || (qa.
x == qb.x && qa.
y == qb.y && qa.
z > qb.z);
17 if (swap) std::swap(qa, qb);
18 return {qa.
x, qa.
y, qa.
z, qb.x, qb.y, qb.z};
22 const float t = std::clamp((
value - edge0) / (edge1 - edge0), 0.f, 1.f);
23 return t *
t * (3.f - 2.f *
t);
27 if (blendRadius <= 0.f)
return std::max(
a,
b);
28 const float h = std::clamp(0.5f + 0.5f * (
a -
b) / blendRadius, 0.f, 1.f);
29 return b + (
a -
b) *
h + blendRadius *
h * (1.f -
h);
34 const float length2 =
dot(segment, segment);
35 const float t = length2 > 1e-8f ? std::clamp(
dot(
sub(
point,
a), segment) / length2, 0.f, 1.f) : 0.f;
37 return std::sqrt(
dot(delta, delta));
42 const float normalizedX =
q.x /
shape.radii.x;
43 const float normalizedY =
q.y /
shape.radii.y;
44 const float normalizedZ =
q.z /
shape.radii.z;
46 (std::sqrt(normalizedX * normalizedX + normalizedY * normalizedY + normalizedZ * normalizedZ) - 1.f) *
48 if (
shape.irregularity <= 0.f)
return base;
49 const float azimuth = std::atan2(normalizedZ, normalizedX);
50 const float lobes = std::sin(azimuth * 3.f +
shape.phase) * 0.58f +
51 std::sin(azimuth * 2.f - normalizedY * 2.4f +
shape.phase * 0.7f) * 0.42f;
52 return base - lobes *
shape.irregularity * std::min({
shape.radii.x,
shape.radii.y,
shape.radii.z}) * 0.13f;
58 const float t = length2 > 1e-8f ? std::clamp(
dot(
sub(
point,
shape.start),
axis) / length2, 0.f, 1.f) : 0.f;
60 delta.
x *= horizontalX;
61 delta.
z *= horizontalZ;
63 const float envelope = std::sin(
t * 3.1415926535f);
65 std::sin(
t *
shape.profileFrequency * 6.283185307f +
shape.profilePhase);
66 return std::sqrt(
dot(delta, delta)) -
radius;
73 const float ripple = std::sin((
v /
shape.halfHeight) *
shape.rippleFrequency +
shape.phase) *
shape.rippleAmplitude;
75 const float ellipsoid =
79 return ellipsoid * std::min({
shape.halfWidth,
shape.halfHeight,
shape.thickness});
85 const float downward = -
q.y;
86 const float wave = std::sin((
u /
shape.halfWidth) *
shape.waveFrequency +
shape.phase) *
shape.waveAmplitude;
88 const float side = std::fabs(
u) -
shape.halfWidth;
89 const float vertical = std::max(-downward, downward -
shape.length);
90 const float sheet = std::fabs(
n) -
shape.thickness;
91 return std::max({side, vertical, sheet});
103 const int ix = int(std::floor(
x));
104 const int iy = int(std::floor(
y));
105 const int iz = int(std::floor(
z));
106 const float fx =
x - float(ix);
107 const float fy =
y - float(iy);
108 const float fz =
z - float(iz);
109 auto smooth = [](
float t) {
return t *
t * (3.f - 2.f *
t); };
111 uint32_t
h =
hash(uint32_t(
sx) * 73856093u ^ uint32_t(
sy) * 19349663u ^ uint32_t(
sz) * 83492791u ^
seed);
112 return float(
h & 0xffffu) / 32767.5f - 1.f;
114 const float ux = smooth(fx), uy = smooth(fy), uz = smooth(fz);
115 auto lerp = [](
float a,
float b,
float t) {
return a + (
b -
a) *
t; };
116 const float x00 = lerp(
sample(ix, iy, iz),
sample(ix + 1, iy, iz), ux);
117 const float x10 = lerp(
sample(ix, iy + 1, iz),
sample(ix + 1, iy + 1, iz), ux);
118 const float x01 = lerp(
sample(ix, iy, iz + 1),
sample(ix + 1, iy, iz + 1), ux);
119 const float x11 = lerp(
sample(ix, iy + 1, iz + 1),
sample(ix + 1, iy + 1, iz + 1), ux);
120 return lerp(lerp(x00, x10, uy), lerp(x01, x11, uy), uz);
132 if (
dot(
normal, referenceNormal) < 0.f) {
138 const uint32_t base = uint32_t(
mesh.getVertexCount());
142 mesh.addTriangle(base, base + 1, base + 2);
146 return style ==
"cavern" || style ==
"tunnels" || style ==
"vertical" || style ==
"labyrinth" || style ==
"mixed";
150 return genesis ==
"epigene" || genesis ==
"hypogene" || genesis ==
"mixed";
std::array< double, 10 > q
std::array< float, 3 > scale
CPU triangle mesh from procedural mesh recipes (e.g. marching cubes). Positions/normals are xyz-packe...
Vec3 projectToDensitySurface(Vec3 meshPoint, const std::vector< float > &density, int nx, int ny, int nz)
void addFacetedTriangle(MeshBuild &mesh, Vec3 a, Vec3 b, Vec3 c, Vec3 referenceNormal)
bool validStyle(const std::string &style)
float latticeNoise(float x, float y, float z, uint32_t seed)
float curtainDistance(Vec3 point, const Curtain &shape)
float ellipsoidDistance(Vec3 point, const Chamber &shape)
float smoothMaximum(float a, float b, float blendRadius)
float taperedSegmentDistance(Vec3 point, const Dripstone &shape, float horizontalX=1.f, float horizontalZ=1.f)
bool validGenesis(const std::string &genesis)
float distanceToSegment(Vec3 point, Vec3 a, Vec3 b)
EdgeKey makeEdgeKey(Vec3 a, Vec3 b)
float flowstoneDistance(Vec3 point, const Flowstone &shape)
Vec3 mul(Vec3 a, float scale)
float smoothstep(float edge0, float edge1, float value)
float dot(Vec3 a, Vec3 b)
double sample(const Heightmap &map, double u, double v)
Sample.
CaveFieldPoint projectToCaveDensitySurface(CaveFieldPoint meshPoint, const std::vector< float > &density, int nx, int ny, int nz)
Project to cave density surface.
CaveFieldPoint public API.
CaveHydrologyVec3 public API.