载入中...
搜索中...
未找到
ParticleCurve.h
浏览该文件的文档.
1#pragma once
2
3#include <algorithm>
4#include <cstddef>
5#include <vector>
6
7namespace eve::particles {
8
10struct ColorStop {
11 float t = 0.f;
12 float r = 1.f;
13 float g = 1.f;
14 float b = 1.f;
15 float a = 1.f;
16};
17
19struct CurvePoint {
20 float t = 0.f;
21 float v = 0.f;
22};
23
30public:
32 void clear() { stops.clear(); }
33
35 void add(float t, float r, float g, float b, float a) {
36 stops.push_back(ColorStop{t, r, g, b, a});
38 std::sort(stops.begin(), stops.end(),
39 [](const ColorStop &x, const ColorStop &y) { return x.t < y.t; });
40 }
41
43 bool empty() const { return stops.empty(); }
45 size_t size() const { return stops.size(); }
47 const ColorStop &at(size_t i) const { return stops[i]; }
48
50 void sample(float t, float &r, float &g, float &b, float &a) const {
51 if (stops.empty()) {
52 r = g = b = a = 1.f;
53 return;
54 }
55 t = t < 0.f ? 0.f : (t > 1.f ? 1.f : t);
56 if (t <= stops.front().t) {
57 const ColorStop &s = stops.front();
58 r = s.r;
59 g = s.g;
60 b = s.b;
61 a = s.a;
62 return;
63 }
64 if (t >= stops.back().t) {
65 const ColorStop &s = stops.back();
66 r = s.r;
67 g = s.g;
68 b = s.b;
69 a = s.a;
70 return;
71 }
72 for (size_t i = 1; i < stops.size(); ++i) {
73 if (t > stops[i].t) continue;
74 const ColorStop &p = stops[i - 1];
75 const ColorStop &n = stops[i];
76 const float span = n.t - p.t;
77 const float f = span > 0.f ? (t - p.t) / span : 0.f;
78 r = p.r + (n.r - p.r) * f;
79 g = p.g + (n.g - p.g) * f;
80 b = p.b + (n.b - p.b) * f;
81 a = p.a + (n.a - p.a) * f;
82 return;
83 }
84 r = g = b = a = 1.f;
85 }
86
87private:
88 std::vector<ColorStop> stops;
89};
90
96public:
98 void clear() { points.clear(); }
99
101 void add(float t, float v) {
102 points.push_back(CurvePoint{t, v});
104 std::sort(points.begin(), points.end(),
105 [](const CurvePoint &x, const CurvePoint &y) { return x.t < y.t; });
106 }
107
109 bool empty() const { return points.empty(); }
111 size_t size() const { return points.size(); }
113 const CurvePoint &at(size_t i) const { return points[i]; }
114
116 float sample(float t, float fallback) const {
117 if (points.empty()) return fallback;
118 t = t < 0.f ? 0.f : (t > 1.f ? 1.f : t);
119 if (t <= points.front().t) return points.front().v;
120 if (t >= points.back().t) return points.back().v;
121 for (size_t i = 1; i < points.size(); ++i) {
122 if (t > points[i].t) continue;
123 const CurvePoint &p = points[i - 1];
124 const CurvePoint &n = points[i];
125 const float span = n.t - p.t;
126 const float f = span > 0.f ? (t - p.t) / span : 0.f;
127 return p.v + (n.v - p.v) * f;
128 }
129 return fallback;
130 }
131
132private:
133 std::vector<CurvePoint> points;
134};
135
136} // namespace eve::particles
float y
Definition AnimClip.cpp:738
float x
Definition AnimClip.cpp:738
const std::string & s
glm::vec4 p[6]
tensor::Graph g
Definition GpuGraph.cpp:7
glm::vec3 n
Definition Grass.cpp:63
double r
float v
MeleePoint3 b
Definition MeleeHit.cpp:41
MeleePoint3 a
Definition MeleeHit.cpp:40
float f
float t
Multi-point scalar curve sampled with linear interpolation between points. Empty curve means "use the...
const CurvePoint & at(size_t i) const
At.
size_t size() const
Returns the size of .
void add(float t, float v)
Adds .
float sample(float t, float fallback) const
Sample at normalized t; fallback is returned when the curve is empty.
Multi-stop color gradient sampled with linear interpolation between stops. Stops are kept sorted by t...
const ColorStop & at(size_t i) const
At.
void sample(float t, float &r, float &g, float &b, float &a) const
Sample.
size_t size() const
Returns the size of .
void add(float t, float r, float g, float b, float a)
Adds .
Color gradient stop at normalized lifetime t in [0, 1].
Scalar curve point at normalized lifetime t in [0, 1].