Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
ParticleEmitter.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
3#pragma once
4
7#include "../../math/Math.hpp"
9
10namespace esengine::ecs {
11
12// Local +Y is the axis every directed shape points along, so an unrotated
13// emitter aims up — where every 2D cone has always aimed. Values are serialized.
14ES_ENUM(stability=beta)
15enum class EmitterShape : i32 {
16 Point = 0,
17 Circle = 1,
18 Box = 2,
19 Cone = 3,
20 Sphere = 4,
22};
23
24// Single source of the easing choice serialized by sizeEasing/colorEasing; the
25// particle sim consumes it as particle::EasingType (alias) and the editor
26// dropdown + TS const are EHT-generated from here.
27ES_ENUM(stability=beta)
28enum class ParticleEasing : i32 {
29 Linear = 0,
30 EaseIn = 1,
33};
34
37ES_ENUM(stability=beta)
38enum class ParticleRenderMode : i32 {
41};
42
43ES_ENUM(stability=beta)
44enum class SimulationSpace : i32 {
45 World = 0,
46 Local = 1,
47};
48
49// When a sub-emitter fires the referenced child emitter's burst: as each parent
50// particle is born, dies, or collides. Serialized values remain stable.
51ES_ENUM(stability=beta)
52enum class SubEmitterTrigger : i32 {
53 Death = 0,
54 Birth = 1,
56};
57
58ES_ENUM(stability=beta)
60 Floor = 0,
63 Depth = 3,
64 Sdf = 4,
65};
66
67ES_ENUM(stability=beta)
69 Floor = 0,
70 Skip = 1,
71};
72
73ES_COMPONENT(renderable=enabled, stability=beta)
75 // Emission
76 ES_PROPERTY(min=0, category=Emission)
77 f32 rate{10.0f};
78
79 ES_PROPERTY(min=0, step=1, category=Emission)
81
82 ES_PROPERTY(min=0, category=Emission)
84
85 ES_PROPERTY(min=0, category=Emission)
87
88 ES_PROPERTY(min=0, category=Emission, tooltip="How fast this emitter's own time runs: 2 plays it twice as fast, 0.5 at half, 0 holds it. Scales emission, lifetimes and motion alike.")
90
91 ES_PROPERTY(min=0, category=Emission, tooltip="Seconds after the emitter starts playing before it emits anything. The duration counts from the end of the delay.")
93
94 ES_PROPERTY(category=Emission)
95 bool looping{true};
96
97 ES_PROPERTY(category=Emission)
98 bool playOnStart{true};
99
100 ES_PROPERTY(min=1, step=1, category=Emission)
102
103 // Simulated by a compute kernel (WebGPU); elsewhere the emitter runs on the CPU, capped. Gravity, damping,
104 // noise, force fields, over-life curves, the floor plane, depth and distance-function collision,
105 // sub-emitters and trails apply; a Physics2D/Physics3D scene query is CPU-only.
106 ES_PROPERTY(category=Emission, tooltip="Simulate on the GPU (WebGPU compute). Falls back to a capped CPU simulation elsewhere. Sub-emitters, trails and physics collision are CPU-only.")
107 bool gpuSimulation{false};
108
109 // Lifetime
110 ES_PROPERTY(min=0, category=Lifetime)
112
113 ES_PROPERTY(min=0, category=Lifetime)
115
116 // Shape kind — i32 storage, dropdown generated from the EmitterShape enum.
117 ES_PROPERTY(enum=EmitterShape, category=Shape)
119
120 ES_PROPERTY(min=0, category=Shape, shown_when=shape:Circle|Cone|Sphere|Hemisphere)
122
123 // A filled VOLUME, not a surface: shapeSize.z of 0 is the flat rectangle every
124 // 2D emitter spawned from, so one field spells both.
125 ES_PROPERTY(category=Shape, shown_when=shape:Box)
126 glm::vec3 shapeSize{100.0f, 100.0f, 0.0f};
127
128 ES_PROPERTY(unit="°", category=Shape, shown_when=shape:Cone)
130
131 // Velocity
132 ES_PROPERTY(category=Velocity)
133 f32 speedMin{500.0f};
134
135 ES_PROPERTY(category=Velocity)
136 f32 speedMax{500.0f};
137
138 // Aim for the shapes that HAVE no aim of their own (Point, Box): an angle in
139 // the emitter's local XY plane. Circle, Cone, Sphere and Hemisphere carry a
140 // direction intrinsically and ignore these.
141 ES_PROPERTY(unit="°", category=Velocity, shown_when=shape:Point|Box)
143
144 ES_PROPERTY(unit="°", category=Velocity, shown_when=shape:Point|Box)
146
147 // Size
148 ES_PROPERTY(min=0, category=Size)
150
151 ES_PROPERTY(min=0, category=Size)
153
154 ES_PROPERTY(min=0, category=Size)
156
157 ES_PROPERTY(min=0, category=Size)
159
160 // Easing — i32 storage, dropdown generated from the ParticleEasing enum.
161 ES_PROPERTY(enum=ParticleEasing, category=Size)
163
164 // Color
165 ES_PROPERTY(category=Color)
166 glm::vec4 startColor{1.0f, 1.0f, 1.0f, 1.0f};
167
168 ES_PROPERTY(category=Color)
169 glm::vec4 endColor{1.0f, 1.0f, 1.0f, 0.0f};
170
171 ES_PROPERTY(enum=ParticleEasing, category=Color)
173
174 // Rotation
175 ES_PROPERTY(unit="°", category=Rotation)
177
178 ES_PROPERTY(unit="°", category=Rotation)
180
181 ES_PROPERTY(category=Rotation)
183
184 ES_PROPERTY(category=Rotation)
186
187 // Forces (grouped under the Velocity category in the inspector)
188 ES_PROPERTY(category=Velocity)
189 glm::vec3 gravity{0.0f, 0.0f, 0.0f};
190
191 ES_PROPERTY(min=0, category=Velocity)
193
194 ES_PROPERTY(category=Velocity, tooltip="A velocity each particle carries on top of its own, chosen between the two ends when it is born. Gravity, damping, noise and the speed limit do not act on it. Run by the CPU simulation.")
195 glm::vec3 addedVelocityMin{0.0f, 0.0f, 0.0f};
196
197 ES_PROPERTY(category=Velocity)
198 glm::vec3 addedVelocityMax{0.0f, 0.0f, 0.0f};
199
200 ES_PROPERTY(min=0, category=Velocity, tooltip="The fastest a particle may move under its own velocity; 0 is no limit. A faster particle is pulled back toward the limit. Run by the CPU simulation.")
202
203 ES_PROPERTY(min=0, max=1, category=Velocity, tooltip="How much of the excess over the limit is removed per 1/60 s: 1 clamps at once, a small value brakes gently.")
205
206 // Noise / Turbulence — a divergence-free curl-noise flow field advects each
207 // particle, layered on top of velocity/gravity. Strength 0 disables it (no
208 // field is sampled), so the module is free when unused.
209 ES_PROPERTY(min=0, category=Noise)
211
212 ES_PROPERTY(min=0, category=Noise)
214
215 ES_PROPERTY(category=Noise)
217
218 ES_PROPERTY(min=1, max=8, step=1, category=Noise)
220
221 // Texture
222 ES_PROPERTY(asset = texture, category=Texture)
224
225 ES_PROPERTY(min=1, step=1, category=Texture)
227
228 ES_PROPERTY(min=1, step=1, category=Texture)
230
231 ES_PROPERTY(min=0, category=Texture)
233
234 ES_PROPERTY(category=Texture)
235 bool spriteLoop{true};
236
237 // Rendering — blendMode keeps a TS override (BlendMode is a renderer-side enum,
238 // not a component ES_ENUM); simulationSpace's dropdown is generated (see below).
239 ES_PROPERTY(category=Rendering)
241
242 ES_PROPERTY(step=1, enum_source=sortingLayers, category=Rendering)
244
245 ES_PROPERTY(asset = material, category=Rendering)
247
248 // Space — i32 storage, dropdown generated from the SimulationSpace enum.
249 ES_PROPERTY(enum=SimulationSpace, category=Rendering)
251
252 ES_PROPERTY(enum=ParticleRenderMode, category=Rendering, tooltip="Billboard faces the view. Stretched lays the quad's long axis (its Y) along the particle's velocity and lengthens it with speed. Drawn by the CPU simulation; a GPU-simulated emitter draws as Billboard.")
254
255 ES_PROPERTY(min=0, category=Rendering, shown_when=renderMode:Stretched, tooltip="Stretched: how much of the particle's speed is added to the quad's length, in world units per unit of speed.")
257
258 ES_PROPERTY(min=0, category=Rendering, shown_when=renderMode:Stretched, tooltip="Stretched: the quad's own length is its size times this.")
260
261 ES_PROPERTY(animatable, min=0, category=Rendering, tooltip="Multiplies the particle's colour (not its alpha) as it is drawn, past 1 where the picture is HDR: what makes a spark bright enough to bloom. A material's fragment receives the colour already multiplied. The CPU simulation's per-particle trail is drawn without it.")
263
264 // State
266 bool enabled{true};
267
268 // Sub-emitter — on each parent particle's birth or death, fire the referenced
269 // child emitter's burst at that particle's position. The child is an ordinary
270 // ParticleEmitter entity used as a template (typically rate=0, playOnStart=false
271 // so it only emits when triggered); its burstCount sets the sub-burst size.
272 // `subEmitter` is the child entity's raw id (0 = none). It is stored as u32
273 // rather than Entity so the pointer layout keeps a heap accessor (Entity is
274 // truncated out of the layout); the entity_ref tag still drives the editor
275 // picker and the scene/prefab editor→runtime id remapping.
276 ES_PROPERTY(enum=SubEmitterTrigger, category=SubEmitter)
278
279 ES_PROPERTY(min=0, max=1, category=SubEmitter)
281
282 ES_PROPERTY(min=0, max=1, category=SubEmitter)
284
285 ES_PROPERTY(entity_ref, category=SubEmitter)
287
288 // Per-particle trail — each particle drags a tapering ribbon along its recent
289 // path (comet tails, sparks, magic streaks). The ribbon reuses the same
290 // triangle-strip batch path as the standalone TrailRenderer; its head takes the
291 // particle's current colour and `trailWidth`, fading to a transparent point at
292 // the tail. Off by default (no history is recorded when disabled).
293 ES_PROPERTY(category=Trail)
294 bool trailEnabled{false};
295
296 ES_PROPERTY(min=0, category=Trail, shown_when=trailEnabled:true)
298
299 ES_PROPERTY(min=2, max=12, step=1, category=Trail, shown_when=trailEnabled:true)
301
302 ES_PROPERTY(min=0, category=Trail, shown_when=trailEnabled:true)
304
305 // Collision is opt-in. Floor remains the default for existing scenes; optional
306 // 2D/3D adapters query the swept segment in project world units.
307 ES_PROPERTY(category=Collision)
308 bool collisionEnabled{false};
309
310 ES_PROPERTY(category=Collision, shown_when=collisionEnabled:true)
312
313 // Scene queries use the same response/events as the legacy plane. Results are
314 // exchanged as a bounded batch, so the core depends on no physics backend.
315 ES_PROPERTY(enum=ParticleCollisionMode, category=Collision, shown_when=collisionEnabled:true)
317
318 ES_PROPERTY(bitmask_source=collisionLayers, min=0, max=65535, step=1, category=Collision, shown_when=collisionEnabled:true)
320
321 ES_PROPERTY(min=0, max=4096, step=1, category=Collision, shown_when=collisionEnabled:true)
323
324 // World units from the nearest active camera; 0 means no distance culling.
325 ES_PROPERTY(min=0, category=Collision, shown_when=collisionEnabled:true)
327
328 ES_PROPERTY(enum=ParticleCollisionFallback, category=Collision, shown_when=collisionEnabled:true)
330
331 ES_PROPERTY(category=Collision, shown_when=collisionEnabled:true)
332 bool collisionEvents{false};
333
334 ES_PROPERTY(min=0, max=1024, step=1, category=Collision, shown_when=collisionEnabled:true)
336
337 // Depth mode: how far behind what the camera saw a particle still counts as having hit it, in world units.
338 // Past this it has gone round the back, and passes.
339 ES_PROPERTY(min=0, category=Collision, shown_when=collisionEnabled:true)
341
342 // Restitution: 0 = particles stop dead on the floor, 1 = a full elastic bounce.
343 ES_PROPERTY(min=0, max=1, category=Collision, shown_when=collisionEnabled:true)
345
346 // Tangential friction applied on each bounce (0 = frictionless slide, 1 = stop
347 // horizontal motion).
348 ES_PROPERTY(min=0, max=1, category=Collision, shown_when=collisionEnabled:true)
350
351 // Fraction of a particle's remaining life burned on each bounce (0 = none, 1 =
352 // dies on first contact) — fades splashes/sparks out as they settle.
353 ES_PROPERTY(min=0, max=1, category=Collision, shown_when=collisionEnabled:true)
355
356 ParticleEmitter() = default;
357};
358
359} // namespace esengine::ecs
Type-safe resource handle system.
Math utilities and GLM configuration for ESEngine.
Reflection and metadata system for automatic binding generation.
#define ES_COMPONENT(...)
Mark a struct/class as a component that should be exposed to scripts.
#define ES_ENUM(...)
Mark an enum that should be exposed to scripts.
#define ES_PROPERTY(...)
Mark a field as a property that should be exposed to scripts.
Core type definitions and utilities for ESEngine.
2D texture for GPU rendering
Definition Texture.hpp:116
std::int32_t i32
Signed 32-bit integer.
Definition Types.hpp:54
std::uint32_t u32
Unsigned 32-bit integer.
Definition Types.hpp:45
float f32
32-bit floating point
Definition Types.hpp:59
Context passed to every System::update call.
Definition World.hpp:40
Turns the MeshRenderer beside it to face each view that draws it.
Definition Billboard.hpp:37
resource::TextureHandle texture
Velocity component for simple kinematic motion.
Definition Velocity.hpp:41