11constexpr float Pi = 3.1415926535f;
13float barDistance(
float x,
float y,
float z,
const CaveSedimentBar& bar) {
14 const float dx =
x - bar.x,
dy =
y - bar.y,
dz =
z - bar.z;
15 const float along =
dx * bar.flowX +
dz * bar.flowZ;
16 const float across = -
dx * bar.flowZ +
dz * bar.flowX;
18 std::sqrt((along * along) / (bar.length * bar.length) + (
across *
across) / (bar.width * bar.width) +
19 (
dy *
dy) / (bar.thickness * bar.thickness));
20 return (
q - 1.f) * std::min({bar.length, bar.width, bar.thickness});
23float clastDistance(
float x,
float y,
float z,
const CaveSedimentClast& clast) {
24 const float dx =
x - clast.x,
dy =
y - clast.y,
dz =
z - clast.z;
25 const float across = -
dx * clast.flowZ +
dz * clast.flowX;
26 const float along =
dx * clast.flowX +
dz * clast.flowZ;
27 const float c = std::cos(clast.pitch);
28 const float s = std::sin(clast.pitch);
29 const float tiltedAlong = along *
c -
dy *
s;
30 const float tiltedUp = along *
s +
dy *
c;
31 const float q = std::sqrt((
across *
across) / (clast.longRadius * clast.longRadius) +
32 (tiltedUp * tiltedUp) / (clast.thickRadius * clast.thickRadius) +
33 (tiltedAlong * tiltedAlong) / (clast.shortRadius * clast.shortRadius));
34 return (
q - 1.f) * std::min({clast.longRadius, clast.thickRadius, clast.shortRadius});
37float parageneticChannelDistance(
float x,
float y,
float z,
const CaveSedimentBar& bar,
float strength) {
38 const float dx =
x - bar.x,
dy =
y - bar.ceilingY,
dz =
z - bar.z;
39 const float along =
dx * bar.flowX +
dz * bar.flowZ;
40 const float clamped = std::clamp(along, -bar.channelHalfLength, bar.channelHalfLength);
41 const float endDistance = along - clamped;
42 const float phase = clamped / std::max(bar.channelHalfLength, 1e-5f);
43 const float meander = std::sin(
phase * Pi) * bar.channelMeander *
strength;
44 const float across = -
dx * bar.flowZ +
dz * bar.flowX - meander;
46 std::sqrt((endDistance * endDistance +
across *
across) / (bar.channelHalfWidth * bar.channelHalfWidth) +
47 (
dy *
dy) / (bar.channelLift * bar.channelLift));
48 return (
q - 1.f) * std::min(bar.channelHalfWidth, bar.channelLift);
51float alluvialNotchDistance(
float x,
float y,
float z,
const CaveSedimentBar& bar) {
52 const float dx =
x - bar.x,
dy =
y - bar.palaeofillY,
dz =
z - bar.z;
53 const float along =
dx * bar.flowX +
dz * bar.flowZ;
54 const float clamped = std::clamp(along, -bar.notchHalfLength, bar.notchHalfLength);
55 const float endDistance = along - clamped;
56 const float phase = clamped / std::max(bar.notchHalfLength, 1e-5f);
57 const float across = -
dx * bar.flowZ +
dz * bar.flowX - std::sin(
phase * Pi) * bar.channelMeander * 0.45f;
58 const float q = std::sqrt((endDistance * endDistance) / (bar.notchHalfWidth * bar.notchHalfWidth) +
59 (
across *
across) / (bar.notchHalfWidth * bar.notchHalfWidth) +
60 (
dy *
dy) / (bar.notchHalfHeight * bar.notchHalfHeight));
61 return (
q - 1.f) * std::min(bar.notchHalfWidth, bar.notchHalfHeight);
67 float paragenesis, uint32_t
seed) {
71 std::mt19937 rng(
seed ^ 0x6c8e9cf5u);
72 std::uniform_real_distribution<float>
unit(0.f, 1.f);
73 const float boundedStrength = std::clamp(
strength, 0.f, 1.f);
74 result.paragenesisStrength = std::clamp(paragenesis, 0.f, 1.f);
75 float totalPitch = 0.f;
76 float totalChannelWidth = 0.f;
77 float totalPalaeofillRatio = 0.f;
78 float totalNotchThickness = 0.f;
79 result.bars.reserve(
size_t(barCount));
80 for (
int i = 0; i < barCount; ++i) {
81 const size_t segment = size_t(1 + rng() % uint32_t(
path.size() - 1));
83 const auto&
end =
path[segment];
86 const float length = std::sqrt(flowX * flowX + flowZ * flowZ);
94 const float t = 0.3f +
unit(rng) * 0.4f;
105 const float floorY = centerY -
radius * 0.92f;
109 if (
result.paragenesisStrength > 0.f) {
121 ++
result.parageneticChannels;
129 const int clastCount = 3 + int(rng() % 4u);
130 bar.
clasts.reserve(
size_t(clastCount));
131 for (
int clastIndex = 0; clastIndex < clastCount; ++clastIndex) {
138 const float along = (
unit(rng) * 2.f - 1.f) * bar.
length * 0.72f;
140 clast.
x = bar.
x + flowX * along - flowZ *
across;
141 clast.
z = bar.
z + flowZ * along + flowX *
across;
142 const float pitchDegrees = 9.f + boundedStrength * (8.f +
unit(rng) * 11.f);
143 clast.
pitch = -pitchDegrees * Pi / 180.f;
145 totalPitch += pitchDegrees;
148 bar.
clasts.push_back(clast);
151 result.bars.push_back(std::move(bar));
153 if (
result.clastCount > 0)
result.meanImbricationDegrees = totalPitch / float(
result.clastCount);
154 if (
result.parageneticChannels > 0)
155 result.meanParageneticWidth = totalChannelWidth / float(
result.parageneticChannels);
156 if (
result.parageneticChannels > 0)
157 result.meanPalaeofillRatio = totalPalaeofillRatio / float(
result.parageneticChannels);
158 if (
result.parageneticChannels > 0)
159 result.meanNotchThickness = totalNotchThickness / float(
result.parageneticChannels);
167 alluvialNotchDistance(
x,
y,
z, bar)});
181 if (std::fabs(barDistance(
x,
y,
z, bar)) <= tolerance)
return true;
183 if (std::fabs(clastDistance(
x,
y,
z, clast)) <= tolerance)
return true;
std::array< double, 10 > q
CaveSedimentSet createCaveSediment(const std::vector< CaveSedimentPathPoint > &path, int barCount, float strength, float paragenesis, uint32_t seed)
Create flow-aligned cave sediment bars with upstream-imbricated gravel.
float carveCaveParagenesis(float x, float y, float z, float current, const CaveSedimentSet &sediment)
Carve flow-aligned antigravitative ceiling channels above generated sediment bars.
bool isCaveSedimentSurface(float x, float y, float z, float tolerance, const CaveSedimentSet &sediment)
Test whether a point lies on a generated sediment surface.
float addCaveSediment(float x, float y, float z, float current, const CaveSedimentSet &sediment)
Union sediment bars and imbricated clasts into a cave SDF.
CaveSedimentBar public API.
std::vector< CaveSedimentClast > clasts
CaveSedimentClast public API.
CaveSedimentSet public API.