载入中...
搜索中...
未找到
CaveGeometry.cpp
浏览该文件的文档.
2
4
5float dot(Vec3 a, Vec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
6Vec3 sub(Vec3 a, Vec3 b) { return {a.x - b.x, a.y - b.y, a.z - b.z}; }
7Vec3 add(Vec3 a, Vec3 b) { return {a.x + b.x, a.y + b.y, a.z + b.z}; }
8Vec3 mul(Vec3 a, float scale) { return {a.x * scale, a.y * scale, a.z * scale}; }
9
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};
14 };
15 QuantizedPoint qa = quantize(a), qb = quantize(b);
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};
19}
20
21float smoothstep(float edge0, float edge1, float value) {
22 const float t = std::clamp((value - edge0) / (edge1 - edge0), 0.f, 1.f);
23 return t * t * (3.f - 2.f * t);
24}
25
26float smoothMaximum(float a, float b, float blendRadius) {
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);
30}
31
33 const Vec3 segment = sub(b, a);
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;
36 const Vec3 delta = sub(point, add(a, mul(segment, t)));
37 return std::sqrt(dot(delta, delta));
38}
39
41 const Vec3 q = sub(point, shape.center);
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;
45 const float base =
46 (std::sqrt(normalizedX * normalizedX + normalizedY * normalizedY + normalizedZ * normalizedZ) - 1.f) *
47 std::min({shape.radii.x, shape.radii.y, shape.radii.z});
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;
53}
54
55float taperedSegmentDistance(Vec3 point, const Dripstone& shape, float horizontalX, float horizontalZ) {
56 const Vec3 axis = sub(shape.end, shape.start);
57 const float length2 = dot(axis, axis);
58 const float t = length2 > 1e-8f ? std::clamp(dot(sub(point, shape.start), axis) / length2, 0.f, 1.f) : 0.f;
59 Vec3 delta = sub(point, add(shape.start, mul(axis, t)));
60 delta.x *= horizontalX;
61 delta.z *= horizontalZ;
62 float radius = shape.startRadius + (shape.endRadius - shape.startRadius) * t;
63 const float envelope = std::sin(t * 3.1415926535f);
64 radius *= 1.f + shape.profileAmplitude * envelope *
65 std::sin(t * shape.profileFrequency * 6.283185307f + shape.profilePhase);
66 return std::sqrt(dot(delta, delta)) - radius;
67}
68
70 const Vec3 q = sub(point, shape.center);
71 const float u = dot(q, shape.tangent);
72 const float v = q.y;
73 const float ripple = std::sin((v / shape.halfHeight) * shape.rippleFrequency + shape.phase) * shape.rippleAmplitude;
74 const float n = dot(q, shape.normal) - ripple;
75 const float ellipsoid =
76 std::sqrt((u * u) / (shape.halfWidth * shape.halfWidth) + (v * v) / (shape.halfHeight * shape.halfHeight) +
77 (n * n) / (shape.thickness * shape.thickness)) -
78 1.f;
79 return ellipsoid * std::min({shape.halfWidth, shape.halfHeight, shape.thickness});
80}
81
83 const Vec3 q = sub(point, shape.anchor);
84 const float u = dot(q, shape.tangent);
85 const float downward = -q.y;
86 const float wave = std::sin((u / shape.halfWidth) * shape.waveFrequency + shape.phase) * shape.waveAmplitude;
87 const float n = dot(q, shape.normal) - wave;
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});
92}
93
94uint32_t hash(uint32_t value) {
95 value ^= value >> 16u;
96 value *= 0x7feb352du;
97 value ^= value >> 15u;
98 value *= 0x846ca68bu;
99 return value ^ (value >> 16u);
100}
101
102float latticeNoise(float x, float y, float z, uint32_t seed) {
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); };
110 auto sample = [seed](int sx, int sy, int sz) {
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;
113 };
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);
121}
122
123Vec3 projectToDensitySurface(Vec3 meshPoint, const std::vector<float>& density, int nx, int ny, int nz) {
124 const CaveFieldPoint point =
125 projectToCaveDensitySurface({meshPoint.x, meshPoint.y, meshPoint.z}, density, nx, ny, nz);
126 return {point.x, point.y, point.z};
127}
128
129void addFacetedTriangle(MeshBuild& mesh, Vec3 a, Vec3 b, Vec3 c, Vec3 referenceNormal) {
130 const Vec3 ab = sub(b, a), ac = sub(c, a);
131 Vec3 normal{ab.y * ac.z - ab.z * ac.y, ab.z * ac.x - ab.x * ac.z, ab.x * ac.y - ab.y * ac.x};
132 if (dot(normal, referenceNormal) < 0.f) {
133 std::swap(b, c);
134 normal = mul(normal, -1.f);
135 }
136 const float length = std::sqrt(dot(normal, normal));
137 if (length > 1e-8f) normal = mul(normal, 1.f / length);
138 const uint32_t base = uint32_t(mesh.getVertexCount());
139 mesh.addVertex(a.x, a.y, a.z, normal.x, normal.y, normal.z, a.x + 0.5f, a.y + 0.5f);
140 mesh.addVertex(b.x, b.y, b.z, normal.x, normal.y, normal.z, b.x + 0.5f, b.y + 0.5f);
141 mesh.addVertex(c.x, c.y, c.z, normal.x, normal.y, normal.z, c.x + 0.5f, c.y + 0.5f);
142 mesh.addTriangle(base, base + 1, base + 2);
143}
144
145bool validStyle(const std::string& style) {
146 return style == "cavern" || style == "tunnels" || style == "vertical" || style == "labyrinth" || style == "mixed";
147}
148
149bool validGenesis(const std::string& genesis) {
150 return genesis == "epigene" || genesis == "hypogene" || genesis == "mixed";
151}
152
153
154} // namespace eve::procgen::cave_detail
double z
double y
double x
glm::vec4 p[6]
float density
ShaderImageInput shape
float u
Definition Grass.cpp:233
glm::vec3 n
Definition Grass.cpp:63
std::array< double, 10 > q
std::uint32_t ab
std::uint32_t ac
float v
std::int32_t c
int h
float length
JointPoint2D axis
std::array< float, 3 > scale
MeleePoint3 b
Definition MeleeHit.cpp:41
double radius
Definition MeleeHit.cpp:42
MeleePoint3 a
Definition MeleeHit.cpp:40
Texture * normal
OceanMeshSettings mesh
Object::Ptr value
std::string hash
float t
uint32_t seed
Definition TreeMesh.cpp:276
glm::vec3 point
CPU triangle mesh from procedural mesh recipes (e.g. marching cubes). Positions/normals are xyz-packe...
Definition MeshBuild.h:20
Vec3 sub(Vec3 a, Vec3 b)
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)
Vec3 add(Vec3 a, Vec3 b)
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.