载入中...
搜索中...
未找到
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:
31 void clear() { stops.clear(); }
32
33 void add(float t, float r, float g, float b, float a) {
34 stops.push_back(ColorStop{t, r, g, b, a});
35 std::sort(stops.begin(), stops.end(),
36 [](const ColorStop &x, const ColorStop &y) { return x.t < y.t; });
37 }
38
39 bool empty() const { return stops.empty(); }
40 size_t size() const { return stops.size(); }
41 const ColorStop &at(size_t i) const { return stops[i]; }
42
43 void sample(float t, float &r, float &g, float &b, float &a) const {
44 if (stops.empty()) {
45 r = g = b = a = 1.f;
46 return;
47 }
48 t = t < 0.f ? 0.f : (t > 1.f ? 1.f : t);
49 if (t <= stops.front().t) {
50 const ColorStop &s = stops.front();
51 r = s.r;
52 g = s.g;
53 b = s.b;
54 a = s.a;
55 return;
56 }
57 if (t >= stops.back().t) {
58 const ColorStop &s = stops.back();
59 r = s.r;
60 g = s.g;
61 b = s.b;
62 a = s.a;
63 return;
64 }
65 for (size_t i = 1; i < stops.size(); ++i) {
66 if (t > stops[i].t) continue;
67 const ColorStop &p = stops[i - 1];
68 const ColorStop &n = stops[i];
69 const float span = n.t - p.t;
70 const float f = span > 0.f ? (t - p.t) / span : 0.f;
71 r = p.r + (n.r - p.r) * f;
72 g = p.g + (n.g - p.g) * f;
73 b = p.b + (n.b - p.b) * f;
74 a = p.a + (n.a - p.a) * f;
75 return;
76 }
77 r = g = b = a = 1.f;
78 }
79
80private:
81 std::vector<ColorStop> stops;
82};
83
89public:
90 void clear() { points.clear(); }
91
92 void add(float t, float v) {
93 points.push_back(CurvePoint{t, v});
94 std::sort(points.begin(), points.end(),
95 [](const CurvePoint &x, const CurvePoint &y) { return x.t < y.t; });
96 }
97
98 bool empty() const { return points.empty(); }
99 size_t size() const { return points.size(); }
100 const CurvePoint &at(size_t i) const { return points[i]; }
101
103 float sample(float t, float fallback) const {
104 if (points.empty()) return fallback;
105 t = t < 0.f ? 0.f : (t > 1.f ? 1.f : t);
106 if (t <= points.front().t) return points.front().v;
107 if (t >= points.back().t) return points.back().v;
108 for (size_t i = 1; i < points.size(); ++i) {
109 if (t > points[i].t) continue;
110 const CurvePoint &p = points[i - 1];
111 const CurvePoint &n = points[i];
112 const float span = n.t - p.t;
113 const float f = span > 0.f ? (t - p.t) / span : 0.f;
114 return p.v + (n.v - p.v) * f;
115 }
116 return fallback;
117 }
118
119private:
120 std::vector<CurvePoint> points;
121};
122
123} // namespace eve::particles
int y
Definition Grass.cpp:135
int x
Definition Grass.cpp:135
glm::vec3 n
Definition Grass.cpp:64
uint32_t a
uint32_t b
float f
glm::vec4 p[6]
int v
uint32_t s
Definition Weather.cpp:28
Multi-point scalar curve sampled with linear interpolation between points. Empty curve means "use the...
const CurvePoint & at(size_t i) const
void add(float t, float v)
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
void sample(float t, float &r, float &g, float &b, float &a) const
void add(float t, float r, float g, float b, float a)
Color gradient stop at normalized lifetime t in [0, 1].
Scalar curve point at normalized lifetime t in [0, 1].