载入中...
搜索中...
未找到
LightningGeometry.cpp
浏览该文件的文档.
1#include <algorithm>
2#include <array>
3#include <cmath>
4#include "weather/Lightning.h"
5namespace eve::weather {
7 if (!std::isfinite(s.width) || s.width <= 0 || s.width > 100 || !std::isfinite(s.displacement) ||
8 s.displacement < 0 || s.displacement > 100 || !std::isfinite(s.duration) || s.duration <= 0 ||
9 s.duration > 60 || s.subdivisions < 1 || s.subdivisions > 8 || s.branches < 0 || s.branches > 16 ||
10 s.variants < 1 || s.variants > 16)
12 Diagnostic::error(DiagnosticCode::InvalidArgument, "Invalid lightning settings", "weather.lightning"));
13 return Result<void>::success();
14}
15float lightningEnvelope(float age) {
16 if (!std::isfinite(age) || age < 0 || age >= 1) return 0;
17 const float t = age * .5f;
18 if (t < .055f) return 1.f - t * 4.f;
19 if (t < .095f) return .06f;
20 if (t < .19f) return .72f * (1.f - (t - .095f) / .095f);
21 return std::max(0.f, .22f * (1.f - (t - .19f) / .31f));
22}
25 if (!valid) return Result<LightningGeometry>::failure(valid.status());
26 using Pt = std::array<float, 3>;
27 std::uint32_t rng = static_cast<std::uint32_t>(s.seed) + variant * 0x9e3779b9u;
28 auto random = [&]() {
29 rng = rng * 1664525u + 1013904223u;
30 return float(rng >> 8u) / 16777216.f;
31 };
32 auto refine = [&](std::vector<Pt>& path, float displacement, int levels) {
33 for (int level = 0; level < levels; ++level) {
34 std::vector<Pt> next;
35 for (size_t i = 0; i + 1 < path.size(); ++i) {
36 const auto a = path[i], b = path[i + 1];
37 next.push_back(a);
38 next.push_back({(a[0] + b[0]) * .5f + (random() * 2 - 1) * displacement, (a[1] + b[1]) * .5f,
39 (a[2] + b[2]) * .5f + (random() * 2 - 1) * displacement});
40 }
41 next.push_back(path.back());
42 path.swap(next);
43 displacement *= .48f;
44 }
45 };
46 std::vector<Pt> main{{0, 0, 0}, {0, 1, 0}};
47 refine(main, s.displacement, s.subdivisions);
48 std::vector<std::vector<Pt>> paths{main};
49 for (int b = 0; b < s.branches; ++b) {
50 auto a = main[1 + (static_cast<size_t>(b + 1) * (main.size() - 2)) / (static_cast<size_t>(s.branches) + 1)];
51 std::vector<Pt> branch{
52 a,
53 {a[0] + (random() * 2 - 1) * s.displacement * 1.8f, std::min(1.f, a[1] + .12f + random() * .15f),
54 a[2] + (random() * 2 - 1) * s.displacement * 1.8f}};
55 refine(branch, s.displacement * .34f, 3);
56 paths.push_back(std::move(branch));
57 }
59 for (size_t p = 0; p < paths.size(); ++p) {
60 const auto& path = paths[p];
61 for (size_t i = 0; i + 1 < path.size(); ++i)
62 for (int plane = 0; plane < 2; ++plane) {
63 const float t = float(i) / float(path.size() - 1);
64 const float w = s.width * (p == 0 ? 1.f : .55f) * (1.f - .9f * t);
65 const auto base = static_cast<std::uint32_t>(g.positions.size() / 3);
66 for (int c = 0; c < 4; ++c) {
67 auto point = path[i + (c >= 2 ? 1 : 0)];
68 point[plane == 0 ? 0 : 2] += (c == 0 || c == 3 ? w : -w);
69 g.positions.insert(g.positions.end(), point.begin(), point.end());
70 g.normals.insert(g.normals.end(), {0, 1, 0});
71 g.uv.insert(g.uv.end(), {c == 0 || c == 3 ? -1.f : 1.f, t});
72 }
73 g.indices.insert(g.indices.end(), {base, base + 1, base + 2, base, base + 2, base + 3});
74 }
75 }
76 return Result<LightningGeometry>::success(std::move(g));
77}
78} // namespace eve::weather
float w
Definition AnimClip.cpp:738
const std::string & s
std::string variant
glm::vec4 p[6]
tensor::Graph g
Definition GpuGraph.cpp:7
std::int32_t c
std::vector< std::filesystem::path > paths
bool valid
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
int level
glm::dvec3 displacement
std::string path
Definition PlayHost.cpp:111
float t
Battle::Random random
glm::vec3 point
static Diagnostic error(DiagnosticCode code, std::string message, std::string path={})
Construct an error diagnostic with the standard error severity.
Move-only operation result carrying either a value or Status.
Definition Result.h:176
static Result success(T value)
Construct a successful result owning value.
Definition Result.h:185
static Result failure(Status status)
Construct a failed result from a structured status.
Definition Result.h:196
int main(int argc, char **argv)
Definition main.cpp:46
EVENGINE_API_ORCHESTRATION float lightningEnvelope(float age)
Weather return-stroke envelope, normalized age [0,1]; side-effect free.
EVENGINE_API_ORCHESTRATION Result< void > validateLightning(const LightningSettings &settings)
Validate bounded finite settings without modifying live instances.
EVENGINE_API_ORCHESTRATION Result< LightningGeometry > buildLightningGeometry(const LightningSettings &settings, unsigned variant)
Deterministic CPU preparation, explicit variant stream; identical inputs give identical geometry on o...
Owned CPU crossed ribbons along local +Y in [0,1]; no graphics dependency.
Definition Lightning.h:26
Copied preparation settings. Width and displacement are world units, independent of endpoint distance...
Definition Lightning.h:16