vkmEngine 1.0.15
A C++ game engine · vkmengine.com
Loading...
Searching...
No Matches
particle_emitter.h
1#pragma once
2
3#include <algorithm>
4#include <cmath>
5#include <cstdint>
6
7#include <glm/glm.hpp>
8
9#include "core/reflect.h"
10#include "core/math/curve.h"
11#include "ecs/component/render/mesh.h"
12#include "resource/asset/texture_asset.h"
13
14namespace Vkm::Engine {
15
19enum class EmitShape : uint8_t {
20 Point,
21 Sphere,
22 Hemisphere,
23 Cone,
24 Box,
25 Disc,
26 Count
27};
28
32enum class ParticleBlend : uint8_t {
33 Additive,
34 Alpha,
35 Distortion,
36 Count
37};
38
42enum class ParticleRender : uint8_t {
43 Billboard,
44 Stretched,
45 Ribbon,
46 Trail,
47 Count
48};
49
53enum class ParticleEvent : uint8_t {
54 Birth,
55 Death,
56 Collision,
57 Count
58};
59
75 bool emitting = true;
76 float rate = 30.0f;
77 float ratePerMetre = 0.0f;
78 uint32_t maxParticles = 256;
79
81 uint32_t burst = 0;
82
85 bool destroyWhenSpent = false;
86
88 EmitShape shape = EmitShape::Cone;
89 float radius = 0.0f;
90 float angle = glm::radians(25.0f);
91 glm::vec3 halfExtents{0.5f};
92 bool fromSurface = false;
93
95 Math::FloatRange speed{2.0f, 2.0f};
98 float inheritVelocity = 0.0f;
99 glm::vec3 acceleration{0.0f, -2.0f, 0.0f};
100 float drag = 0.0f;
101 float turbulence = 0.0f;
102 float turbulenceScale = 1.0f;
103
104 Math::FloatRange size{0.25f, 0.25f};
105 Math::Curve sizeOverLife{{{0.0f, 1.0f}, {1.0f, 0.08f}}};
106 Math::Gradient color{{{0.0f, {1.0f, 0.8f, 0.4f, 1.0f}}, {1.0f, {1.0f, 0.2f, 0.0f, 0.0f}}}};
107 float intensity = 1.0f;
109 Math::FloatRange spin{0.0f, 0.0f};
110 float softness = 1.0f;
111
112 ParticleBlend blend = ParticleBlend::Additive;
113 float emission = 0.0f;
114 float distortion = 0.03f;
115
120 float lightIntensity = 0.0f;
121 glm::vec3 lightColor{1.0f, 0.6f, 0.3f};
122 float lightRadius = 8.0f;
123
124 ParticleRender render = ParticleRender::Billboard;
125 float stretch = 0.05f;
126 float trailLength = 0.3f;
127
130 TextureHandle texture;
131
134 TextureHandle normalMap;
135
137 uint32_t sheetColumns = 1;
138 uint32_t sheetRows = 1;
139 float sheetFps = 0.0f;
140 bool randomFrame = false;
141 bool sheetBlend = false;
142
145 bool collide = false;
146 float bounce = 0.3f;
147 float lifeLostOnHit = 0.0f;
148
153 SeenBy seenBy = SeenBy::All;
154};
155
166 ParticleEvent event = ParticleEvent::Death;
167 uint32_t count = 4;
168};
169
176inline uint32_t sheetFrames(const ParticleEmitter& emitter) {
177 return std::max(emitter.sheetColumns, 1u) * std::max(emitter.sheetRows, 1u);
178}
179
184 uint32_t frame = 0;
185 uint32_t next = 0;
186 float blend = 0.0f;
187};
188
200inline SheetFrame sheetFrameAt(const ParticleEmitter& emitter, float age, float lifetime, uint32_t start) {
201 const uint32_t frames = sheetFrames(emitter);
202 const auto count = static_cast<float>(frames);
203 const bool loops = emitter.sheetFps > 0.0f;
204 float position = 0.0f;
205 if (loops) {
206 position = std::fmod(std::max(age * emitter.sheetFps, 0.0f), count);
207 } else {
208 const float share = lifetime > 0.0f ? std::clamp(age / lifetime, 0.0f, 1.0f) : 1.0f;
209 position = share * (emitter.sheetBlend ? count - 1.0f : count);
210 }
211 // Held on the last frame at the very end of a life, where the share reaches one.
212 const uint32_t ahead = std::min(static_cast<uint32_t>(position), frames - 1);
213 const uint32_t after = loops ? (ahead + 1) % frames : std::min(ahead + 1, frames - 1);
214
215 SheetFrame sheet;
216 sheet.frame = (start + ahead) % frames;
217 sheet.next = (start + after) % frames;
218 sheet.blend = emitter.sheetBlend && after != ahead ? position - static_cast<float>(ahead) : 0.0f;
219 return sheet;
220}
221
222} // namespace Vkm::Engine
223
224VKM_ENUM_NAMES(::Vkm::Engine::EmitShape, "Point", "Sphere", "Hemisphere", "Cone", "Box", "Disc")
225VKM_ENUM_NAMES(::Vkm::Engine::ParticleBlend, "Additive", "Alpha", "Distortion")
226VKM_ENUM_NAMES(::Vkm::Engine::ParticleRender, "Billboard", "Stretched", "Ribbon", "Trail")
227VKM_ENUM_NAMES(::Vkm::Engine::ParticleEvent, "Birth", "Death", "Collision")
228
229VKM_REFLECT_BEGIN(::Vkm::Engine::ParticleEmitter)
230 VKM_F(emitting)
231 VKM_F(rate)
232 VKM_F(ratePerMetre)
233 VKM_F(maxParticles)
234 VKM_F(burst)
235 VKM_F(destroyWhenSpent)
236 VKM_F(shape)
237 VKM_F(radius)
238 VKM_F(angle)
239 VKM_F(halfExtents)
240 VKM_F(fromSurface)
241 VKM_F(lifetime)
242 VKM_F(speed)
243 VKM_F(inheritVelocity)
244 VKM_F(acceleration)
245 VKM_F(drag)
246 VKM_F(turbulence)
247 VKM_F(turbulenceScale)
248 VKM_F(size)
249 VKM_F(sizeOverLife)
250 VKM_F(color)
251 VKM_F(intensity)
252 VKM_F(rotation)
253 VKM_F(spin)
254 VKM_F(softness)
255 VKM_F(blend)
256 VKM_F(emission)
257 VKM_F(distortion)
258 VKM_F(lightIntensity)
259 VKM_F(lightColor)
260 VKM_F(lightRadius)
261 VKM_F(render)
262 VKM_F(stretch)
263 VKM_F(trailLength)
264 VKM_F(texture)
265 VKM_F(normalMap)
266 VKM_F(sheetColumns)
267 VKM_F(sheetRows)
268 VKM_F(sheetFps)
269 VKM_F(randomFrame)
270 VKM_F(sheetBlend)
271 VKM_F(collide)
272 VKM_F(bounce)
273 VKM_F(lifeLostOnHit)
274 VKM_F(seenBy)
275VKM_REFLECT_END()
276
277VKM_REFLECT_BEGIN(::Vkm::Engine::ParticleTrigger)
278 VKM_F(event)
279 VKM_F(count)
280VKM_REFLECT_END()
One entity's view of a CollisionEvent, as its behaviors' collision hooks receive it.
Definition physics_events.h:47
A value over a span from 0 to 1 - a particle's life, say - straight between its keys.
Definition curve.h:72
A value drawn once, uniformly between min and max; equal ends are a constant.
Definition curve.h:16
A colour over a span from 0 to 1, straight between its keys in linear RGBA.
Definition curve.h:100
CPU-simulated particle emitter: fire, smoke, sparks, impacts, trails, heat haze.
Definition particle_emitter.h:74
float softness
The disc's edge, 1 soft to 0 hard. Unread with a texture.
Definition particle_emitter.h:110
float lifeLostOnHit
The share of its whole life a strike takes; 1 ends it.
Definition particle_emitter.h:147
Math::FloatRange spin
Radians a second it turns.
Definition particle_emitter.h:109
float turbulenceScale
Metres the swirls span.
Definition particle_emitter.h:102
EmitShape shape
Where particles are born and which way they leave, in the emitter's frame.
Definition particle_emitter.h:88
float rate
Particles a second.
Definition particle_emitter.h:76
Math::FloatRange rotation
Radians it is born turned by, about the view.
Definition particle_emitter.h:108
uint32_t burst
Particles to spawn at once on the next update, besides the rates; set again to fire again.
Definition particle_emitter.h:81
float intensity
Multiplies the colour: past 1, an emitting one blooms.
Definition particle_emitter.h:107
float bounce
The share of its speed into the surface it keeps, turned back.
Definition particle_emitter.h:146
Math::FloatRange speed
Definition particle_emitter.h:95
float sheetFps
Frames a second, looping; 0 plays the sheet once over a life.
Definition particle_emitter.h:139
uint32_t sheetColumns
The texture as a sheet of frames, columns by rows, read left to right from the top.
Definition particle_emitter.h:137
float ratePerMetre
Particles a metre the emitter travels, besides the rate.
Definition particle_emitter.h:77
bool destroyWhenSpent
Definition particle_emitter.h:85
bool fromSurface
Born on the shape's surface or rim, not inside it.
Definition particle_emitter.h:92
float turbulence
Scales the swirl it accelerates by (ParticleSystem::swirlAt).
Definition particle_emitter.h:101
float inheritVelocity
Definition particle_emitter.h:98
TextureHandle texture
Definition particle_emitter.h:130
float lightRadius
Metres the light reaches.
Definition particle_emitter.h:122
glm::vec3 acceleration
World space, metres a second squared: gravity.
Definition particle_emitter.h:99
float distortion
Distortion: how far it bends what is behind, in view heights.
Definition particle_emitter.h:114
float lightIntensity
Definition particle_emitter.h:120
float stretch
Stretched: seconds of its travel the card spans.
Definition particle_emitter.h:125
float trailLength
Trail: seconds of where it was the strip spans.
Definition particle_emitter.h:126
bool collide
Definition particle_emitter.h:145
Math::FloatRange size
Half the card's width, metres, drawn at birth.
Definition particle_emitter.h:104
SeenBy seenBy
Definition particle_emitter.h:153
float drag
Velocity lost a second: it keeps exp(-drag) of it.
Definition particle_emitter.h:100
bool randomFrame
Each particle starts on a frame of its own.
Definition particle_emitter.h:140
uint32_t maxParticles
Cap on live particles.
Definition particle_emitter.h:78
glm::vec3 halfExtents
Box, metres.
Definition particle_emitter.h:91
TextureHandle normalMap
Definition particle_emitter.h:134
float emission
Alpha: the share of its colour it gives off - fire in smoke.
Definition particle_emitter.h:113
float angle
Cone and Box: radians off +Y a particle may leave at.
Definition particle_emitter.h:90
glm::vec3 lightColor
The light's colour.
Definition particle_emitter.h:121
bool sheetBlend
Each frame fades into the next: a slow flipbook moves smoothly.
Definition particle_emitter.h:141
Math::FloatRange lifetime
Seconds.
Definition particle_emitter.h:94
Math::Curve sizeOverLife
Multiplies the drawn size.
Definition particle_emitter.h:105
float radius
Sphere, Hemisphere, Cone and Disc, metres.
Definition particle_emitter.h:89
Makes a child entity's ParticleEmitter a sub-emitter of its parent's.
Definition particle_emitter.h:165
Where in its sheet a particle is: the frame it shows, and how far it has faded into the next.
Definition particle_emitter.h:183
uint32_t frame
The frame it shows.
Definition particle_emitter.h:184
float blend
How far it has faded into next, 0..1; 0 unless sheetBlend.
Definition particle_emitter.h:186
uint32_t next
The frame after it.
Definition particle_emitter.h:185