载入中...
搜索中...
未找到
ParticleAppearance.cpp
浏览该文件的文档.
1// eve-hot-path
3#include <algorithm>
4#include <cmath>
5#include <limits>
6namespace eve::particles {
7namespace {
8constexpr float kRad2Deg = 180.f / 3.14159265358979323846f;
9void sampleColor(const ParticleEmitter::Config &cfg, float t, Color &out) {
10 if (!cfg.colorGradient.empty()) {
11 float r, g, b, a;
12 cfg.colorGradient.sample(t, r, g, b, a);
13 out = Color(r, g, b, a);
14 return;
15 }
16 t = t < 0.f ? 0.f : (t > 1.f ? 1.f : t);
17 out.r = cfg.colorStart.r + (cfg.colorEnd.r - cfg.colorStart.r) * t;
18 out.g = cfg.colorStart.g + (cfg.colorEnd.g - cfg.colorStart.g) * t;
19 out.b = cfg.colorStart.b + (cfg.colorEnd.b - cfg.colorStart.b) * t;
20 out.a = cfg.colorStart.a + (cfg.colorEnd.a - cfg.colorStart.a) * t;
21}
22
23void sampleScale(const ParticleEmitter::Config &cfg, float t, float &scale) {
24 if (!cfg.sizeCurve.empty()) {
25 scale = cfg.sizeCurve.sample(t, 1.f);
26 return;
27 }
28 t = t < 0.f ? 0.f : (t > 1.f ? 1.f : t);
29 scale = cfg.sizeStart + (cfg.sizeEnd - cfg.sizeStart) * t;
30}
31
32void flipbookUV(const ParticleEmitter::Config &cfg, float frame, float &u0, float &v0, float &u1, float &v1) {
33 const auto total = std::int64_t(cfg.hframes) * std::int64_t(cfg.vframes);
34 if (total <= 1 || cfg.hframes <= 0 || cfg.vframes <= 0) {
35 u0 = 0.f;
36 v0 = 0.f;
37 u1 = 1.f;
38 v1 = 1.f;
39 return;
40 }
41 if (!std::isfinite(frame)) {
42 u0 = v0 = u1 = v1 = std::numeric_limits<float>::quiet_NaN();
43 return;
44 }
45 double wrapped = std::fmod(std::floor(double(frame)), double(total));
46 if (wrapped < 0) wrapped += double(total);
47 const auto fi = static_cast<std::int64_t>(wrapped) % total;
48 const auto col = fi % cfg.hframes;
49 const auto row = fi / cfg.hframes;
50 const float iw = 1.f / float(cfg.hframes);
51 const float ih = 1.f / float(cfg.vframes);
52 u0 = float(col) * iw;
53 v0 = float(row) * ih;
54 u1 = u0 + iw;
55 v1 = v0 + ih;
56}
57
58} // namespace
60 const float t = p.lifetime > 0.f ? 1.f - (p.life / p.lifetime) : 1.f;
61 Color c;
62 sampleColor(cfg, t, c);
63 float scale;
64 sampleScale(cfg, t, scale);
65 scale *= p.size > 0.f ? p.size : 1.f;
66 const float w = cfg.particleW * scale;
67 const float h = cfg.particleH * scale;
68
70 item.x = p.x - w * 0.5f;
71 item.y = p.y - h * 0.5f;
72 item.w = w;
73 item.h = h;
74 if (cfg.renderMode == "stretched") {
75 // Elongate along the velocity direction (comet / streak style).
76 const float speed = std::sqrt(p.vx * p.vx + p.vy * p.vy);
77 const float len = std::max(w, speed * cfg.stretchFactor);
78 item.w = len;
79 item.rotation = std::atan2(p.vy, p.vx) * kRad2Deg;
80 } else if (cfg.renderMode == "axis") {
81 item.rotation = cfg.renderAxisDegrees;
82 } else {
83 item.rotation = p.rot * kRad2Deg;
84 if (!cfg.rotationCurve.empty()) item.rotation += cfg.rotationCurve.sample(t, 0.f);
85 }
86 item.color = c;
87 flipbookUV(cfg, p.frame, item.u0, item.v0, item.u1, item.v1);
88 for (const float value : {item.x, item.y, item.w, item.h, item.rotation, item.color.r, item.color.g, item.color.b,
89 item.color.a, item.u0, item.v0, item.u1, item.v1}) {
90 if (!std::isfinite(value))
92 DiagnosticCode::InvalidArgument, "nonfinite particle appearance", "particleAppearance"));
93 }
95}
96} // namespace eve::particles
float w
Definition AnimClip.cpp:738
glm::vec4 p[6]
tensor::Graph g
Definition GpuGraph.cpp:7
double r
std::int32_t c
int h
std::array< float, 3 > scale
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
ssq::Object frame
Object::Ptr value
float t
ScriptStructure out
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
float sample(float t, float fallback) const
Sample at normalized t; fallback is returned when the curve is empty.
Result< ParticleAppearance > evaluateParticleAppearance(const ParticleEmitter::Config &cfg, const Particle &p)
Evaluate the same CPU sprite curves and render mode for live rendering and bake capture....
WidgetDesc row(std::vector< WidgetDesc > children, std::string id)
Horizontal elastic layout row.
Definition Widget.cpp:870
glm::vec4 Color
Render-neutral RGBA color shared by graphics-facing modules.
Definition RenderTypes.h:8
Owning CPU sprite appearance, with pixel rectangle, degree rotation, color and normalized atlas UV.
ParticleCurve rotationCurve
Optional extra rotation (degrees) over lifetime; added on top of spin.
float renderAxisDegrees
Fixed screen-space angle in degrees used by axis mode.
std::string renderMode
"billboard" | "axis" | "stretched" | "ribbon".
Single live particle (CPU simulation).