8#include "core/reflect.h"
9#include "core/math/random.h"
11namespace Vkm::Engine::Math {
26 float draw(
Rng& rng)
const {
return min == max ? min : rng.nextFloat(min, max); }
44template <
typename Key,
typename Value,
typename Read>
45Value sampleKeys(
const std::vector<Key>& keys,
float t,
const Value& none, Read&& value) {
46 const Key* below =
nullptr;
47 const Key* above =
nullptr;
48 for (
const Key& key : keys) {
49 if (key.time <= t && (!below || key.time >= below->time)) below = &key;
50 if (key.time >= t && (!above || key.time < above->time)) above = &key;
52 if (!below && !above)
return none;
53 if (!below)
return value(*above);
54 if (!above || above->time <= below->time)
return value(*below);
55 return glm::mix(value(*below), value(*above), (t - below->time) / (above->time - below->time));
73 std::vector<CurveKey> keys;
81 float at(
float t)
const {
82 return sampleKeys(keys, t, 1.0f, [](
const CurveKey& key) {
return key.value; });
91 glm::vec4 color{1.0f};
101 std::vector<GradientKey> keys;
109 glm::vec4
at(
float t)
const {
110 return sampleKeys(keys, t, glm::vec4(1.0f), [](
const GradientKey& key) {
return key.color; });
121VKM_REFLECT_BEGIN(::Vkm::Engine::Math::CurveKey)
126VKM_REFLECT_BEGIN(::Vkm::Engine::Math::Curve)
130VKM_REFLECT_BEGIN(::Vkm::Engine::Math::GradientKey)
135VKM_REFLECT_BEGIN(::Vkm::Engine::Math::Gradient)
PCG32 pseudo-random generator (O'Neill, 2014).
Definition random.h:12
One key of a Curve: its value at a time between 0 and 1.
Definition curve.h:61
A value over a span from 0 to 1 - a particle's life, say - straight between its keys.
Definition curve.h:72
float at(float t) const
The value at t.
Definition curve.h:81
A value drawn once, uniformly between min and max; equal ends are a constant.
Definition curve.h:16
float draw(Rng &rng) const
One draw from the range.
Definition curve.h:26
One key of a Gradient: its colour at a time between 0 and 1.
Definition curve.h:89
A colour over a span from 0 to 1, straight between its keys in linear RGBA.
Definition curve.h:100
glm::vec4 at(float t) const
The colour at t.
Definition curve.h:109