载入中...
搜索中...
未找到
WaterSplashMesh.cpp
浏览该文件的文档.
2
3#include <algorithm>
4#include <cmath>
5#include <cstdint>
6#include <string>
7#include <vector>
8
9namespace eve::procgen {
10namespace {
11
12constexpr float kPi = 3.14159265358979323846f;
13
14struct LayerSpec {
15 float heightScale; // relative to overall height
16 float baseRadius; // relative to half-width
17 float topRadius; // relative to half-width
18 float flareBias; // 0 = linear, 1 = ease-out flare
19};
20
21// Clean clay-sheet crowns (reference guide mesh): continuous nested flares.
22// Jagged water lips / foam live in the semi-transparent material, not here.
23constexpr LayerSpec kDefaultLayers[] = {
24 {1.00f, 0.10f, 0.045f, 0.15f}, // central jet
25 {0.72f, 0.14f, 0.38f, 0.55f}, // inner crown
26 {0.48f, 0.20f, 0.68f, 0.70f}, // middle crown
27 {0.28f, 0.28f, 1.00f, 0.85f}, // outer ripple crown
28};
29
30float clampf(float v, float lo, float hi) { return std::max(lo, std::min(hi, v)); }
31
32float easeOutCubic(float t) {
33 const float u = 1.f - t;
34 return 1.f - u * u * u;
35}
36
37float radiusAt(const LayerSpec &spec, float halfW, float t) {
38 const float blend = (1.f - spec.flareBias) * t + spec.flareBias * easeOutCubic(t);
39 return halfW * (spec.baseRadius + (spec.topRadius - spec.baseRadius) * blend);
40}
41
42void addFlaredSheet(MeshBuild &out, const LayerSpec &spec, float height, float halfW, int radialSegments,
43 int heightSegments) {
44 const int sides = radialSegments;
45 const int rings = heightSegments;
46 const float layerH = height * spec.heightScale;
47 if (layerH < 1e-4f || halfW < 1e-4f) return;
48
49 const uint32_t base = uint32_t(out.getVertexCount());
50 for (int y = 0; y <= rings; ++y) {
51 const float v = float(y) / float(rings);
52 for (int x = 0; x < sides; ++x) {
53 const float u = float(x) / float(sides);
54 const float angle = u * 2.f * kPi;
55 const float radius = radiusAt(spec, halfW, v);
56 // Mild outward lip curl so crowns read as open sheets, not tubes.
57 const float lip = easeOutCubic(clampf((v - 0.72f) / 0.28f, 0.f, 1.f));
58 const float curl = lip * halfW * 0.035f;
59 const float px = std::cos(angle) * (radius + curl);
60 const float py = layerH * v;
61 const float pz = std::sin(angle) * (radius + curl);
62
63 const float dr = (spec.topRadius - spec.baseRadius) * halfW;
64 float nx = std::cos(angle);
65 float ny = -dr / std::max(layerH, 1e-3f);
66 float nz = std::sin(angle);
67 const float nlen = std::sqrt(nx * nx + ny * ny + nz * nz);
68 nx /= nlen;
69 ny /= nlen;
70 nz /= nlen;
71
72 // UV.v = 0 at ground, 1 at lip — the splash material drives alpha/foam from this.
73 out.addVertex(px, py, pz, nx, ny, nz, u, v);
74 }
75 }
76
77 for (int y = 0; y < rings; ++y) {
78 for (int x = 0; x < sides; ++x) {
79 const int nx = (x + 1) % sides;
80 const uint32_t a = base + uint32_t(y * sides + x);
81 const uint32_t b = base + uint32_t(y * sides + nx);
82 const uint32_t c = base + uint32_t((y + 1) * sides + x);
83 const uint32_t d = base + uint32_t((y + 1) * sides + nx);
84 // Double-sided sheets so the interior of each crown stays visible.
85 out.addTriangle(a, b, c);
86 out.addTriangle(b, d, c);
87 out.addTriangle(c, b, a);
88 out.addTriangle(c, d, b);
89 }
90 }
91}
92
93} // namespace
94
96 const float height = std::max(0.2f, params.getFloat("height", 2.4f));
97 const float width = std::max(0.3f, params.getFloat("width", 3.2f));
98 const float halfW = width * 0.5f;
99 const int layers = std::clamp(params.getInt("layers", 4), 2, 4);
100 // onlyLayer < 0 emits every active layer; 0..layers-1 emits a single crown for
101 // staggered VFX playback without rebuilding the shared silhouette table.
102 const int onlyLayer = params.getInt("onlyLayer", -1);
103 const int sides = std::clamp(params.getInt("radialSegments", 32), 8, 64);
104 const int rings = std::clamp(params.getInt("heightSegments", 12), 3, 32);
105 const float flare = clampf(params.getFloat("flare", 1.0f), 0.35f, 1.75f);
106
107 if (onlyLayer >= layers) {
108 error = "mesh.water_splash: onlyLayer must be < layers";
109 return false;
110 }
111
112 const uint32_t seed = params.getSeed();
113 out.clear();
114 out.setActiveGroup("splash");
115
116 for (int i = 0; i < layers; ++i) {
117 if (onlyLayer >= 0 && i != onlyLayer) continue;
118 LayerSpec spec = kDefaultLayers[i];
119 if (i > 0) {
120 spec.topRadius *= flare;
121 spec.flareBias = clampf(spec.flareBias * (0.65f + 0.35f * flare), 0.f, 1.f);
122 }
123 const std::string group = i == 0 ? "spire" : ("crown" + std::to_string(i));
124 out.setActiveGroup(group);
125 addFlaredSheet(out, spec, height, halfW, sides, rings);
126 }
127
128 out.setMeta("recipe", "mesh.water_splash");
129 out.setMeta("seed", std::to_string(seed));
130 out.setMeta("layers", std::to_string(layers));
131 out.setMeta("sheet", "clay");
132 if (out.empty()) {
133 error = "mesh.water_splash: generated an empty mesh";
134 return false;
135 }
136 return true;
137}
138
139} // namespace eve::procgen
double y
double x
float halfW
float u
Definition Grass.cpp:233
float v
std::int32_t c
float blend
std::vector< Colorf > px
std::uint32_t height
std::uint32_t width
std::vector< RecipeParam > params
MeleePoint3 b
Definition MeleeHit.cpp:41
double radius
Definition MeleeHit.cpp:42
MeleePoint3 a
Definition MeleeHit.cpp:40
std::string error
Definition Package.cpp:174
float d
float t
ScriptStructure out
AbilityActivationGroup group
uint32_t seed
Definition TreeMesh.cpp:276
int sides
Definition TreeMesh.cpp:459
float flareBias
float baseRadius
float heightScale
float topRadius
CPU triangle mesh from procedural mesh recipes (e.g. marching cubes). Positions/normals are xyz-packe...
Definition MeshBuild.h:20
Owning, typed generation parameters.
Definition Params.h:27
constexpr float clampf(float v, float lo, float hi) noexcept
Clampf.
Definition AnimMath.h:34
bool generateWaterSplashMesh(const Params &params, MeshBuild &out, std::string &error)
Build deterministic clay-sheet crowns for a stylized water splash.