Luanti 5.11.0-dev
 
Loading...
Searching...
No Matches
particles.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include <vector>
8#include <unordered_map>
10#include "irr_ptr.h"
11#include "../particles.h"
12
13struct ClientEvent;
14class ParticleManager;
16struct MapNode;
17struct ContentFeatures;
18class LocalPlayer;
19class ITextureSource;
20class IGameDef;
21class Client;
22
24{
25 /* per-spawner structure used to store the ParticleTexture structs
26 * that spawned particles will refer to through ClientParticleTexRef */
28 video::ITexture *ref = nullptr;
29
32};
33
35{
36 /* per-particle structure used to avoid massively duplicating the
37 * fairly large ParticleTexture struct */
38 ParticleTexture *tex = nullptr;
39 video::ITexture *ref = nullptr;
40
42
43 /* constructor used by particles spawned from a spawner */
46
47 /* constructor used for node particles */
48 explicit ClientParticleTexRef(video::ITexture *tp): ref(tp) {};
49};
50
51class ParticleSpawner;
52class ParticleBuffer;
53
55{
56public:
58 const ParticleParameters &p,
59 const ClientParticleTexRef &texture,
60 v2f texpos,
61 v2f texsize,
62 video::SColor color,
63 ParticleSpawner *parent = nullptr,
64 std::unique_ptr<ClientParticleTexture> owned_texture = nullptr
65 );
66
67 ~Particle();
68
70
71 void step(float dtime, ClientEnvironment *env);
72
73 bool isExpired () const
74 { return m_expiration < m_time; }
75
76 ParticleSpawner *getParent() const { return m_parent; }
77
78 const ClientParticleTexRef &getTextureRef() const { return m_texture; }
79
82
83 ParticleBuffer *getBuffer() const { return m_buffer; }
84 bool attachToBuffer(ParticleBuffer *buffer);
85
86private:
87 video::SColor updateLight(ClientEnvironment *env);
88 void updateVertices(ClientEnvironment *env, video::SColor color);
89
91 u16 m_index; // index in m_buffer
92
93 float m_time = 0.0f;
95
96 // Color without lighting
97 video::SColor m_base_color;
98
105
107
108 float m_animation_time = 0.0f;
110
112 // Used if not spawned from a particlespawner
113 std::unique_ptr<ClientParticleTexture> m_owned_texture;
114};
115
117{
118public:
120 const ParticleSpawnerParameters &params,
121 u16 attached_id,
122 std::vector<ClientParticleTexture> &&texpool,
123 ParticleManager *p_manager);
124
125 void step(float dtime, ClientEnvironment *env);
126
127 bool getExpired() const
128 { return p.amount <= 0 && p.time != 0; }
129
130 bool hasActive() const { return m_active != 0; }
131 void decrActive() { m_active -= 1; }
132
133private:
134 void spawnParticle(ClientEnvironment *env, float radius,
135 const core::matrix4 *attached_absolute_pos_rot_matrix);
136
137 size_t m_active;
139 float m_time;
142 std::vector<ClientParticleTexture> m_texpool;
143 std::vector<float> m_spawntimes;
145};
146
147class ParticleBuffer : public scene::ISceneNode
148{
149 friend class ParticleManager;
150public:
151 ParticleBuffer(ClientEnvironment *env, const video::SMaterial &material);
152
153 // for pointer stability
155
156
158 std::optional<u16> allocate();
160 void release(u16 index);
161
163 video::S3DVertex *getVertices(u16 index);
164
165 inline bool isEmpty() const {
166 return m_free_list.size() == m_count;
167 }
168
169 virtual video::SMaterial &getMaterial(u32 num) override {
170 return m_mesh_buffer->getMaterial();
171 }
172 virtual u32 getMaterialCount() const override {
173 return 1;
174 }
175
176 virtual const core::aabbox3df &getBoundingBox() const override;
177
178 virtual void render() override;
179
180 virtual void OnRegisterSceneNode() override;
181
182 // we have 16-bit indices
183 static constexpr u16 MAX_PARTICLES_PER_BUFFER = 16000;
184
185private:
186 irr_ptr<scene::SMeshBuffer> m_mesh_buffer;
187 // unused (e.g. expired) particle indices for re-use
188 std::vector<u16> m_free_list;
189 // for automatic deletion when unused for a while. is reset on allocate().
190 float m_usage_timer = 0;
191 // total count of contained particles
192 u16 m_count = 0;
193 mutable bool m_bounding_box_dirty = true;
194};
195
200{
201 friend class ParticleSpawner;
202public:
206
207 void step (float dtime);
208
210 LocalPlayer *player);
211
212 void addDiggingParticles(IGameDef *gamedef, LocalPlayer *player, v3s16 pos,
213 const MapNode &n, const ContentFeatures &f);
214
215 void addNodeParticle(IGameDef *gamedef, LocalPlayer *player, v3s16 pos,
216 const MapNode &n, const ContentFeatures &f);
217
218 void reserveParticleSpace(size_t max_estimate);
219
228 {
230 }
231
232protected:
233 static bool getNodeParticleParams(const MapNode &n, const ContentFeatures &f,
234 ParticleParameters &p, video::ITexture **texture, v2f &texpos,
235 v2f &texsize, video::SColor *color, u8 tilenum = 0);
236
237 static video::SMaterial getMaterialForParticle(const Particle *texture);
238
239 bool addParticle(std::unique_ptr<Particle> toadd);
240
241private:
242 void addParticleSpawner(u64 id, std::unique_ptr<ParticleSpawner> toadd);
243 void deleteParticleSpawner(u64 id);
244
245 void stepParticles(float dtime);
246 void stepSpawners(float dtime);
247 void stepBuffers(float dtime);
248
249 void clearAll();
250
251 std::vector<std::unique_ptr<Particle>> m_particles;
252 std::unordered_map<u64, std::unique_ptr<ParticleSpawner>> m_particle_spawners;
253 std::vector<std::unique_ptr<ParticleSpawner>> m_dying_particle_spawners;
254 std::vector<irr_ptr<ParticleBuffer>> m_particle_buffers;
255
256 // Start the particle spawner ids generated from here after u32_max.
257 // lower values are for server sent spawners.
258 u64 m_next_particle_spawner_id = static_cast<u64>(U32_MAX) + 1;
259
261
263
266};
#define DISABLE_CLASS_COPY(C)
Definition basic_macros.h:26
Definition clientenvironment.h:54
Definition client.h:105
Definition gamedef.h:36
Definition texturesource.h:36
Definition numeric.h:347
Definition localplayer.h:50
Definition particles.h:148
virtual void render() override
Definition particles.cpp:638
bool m_bounding_box_dirty
Definition particles.h:193
ParticleBuffer(ClientEnvironment *env, const video::SMaterial &material)
Definition particles.cpp:548
irr_ptr< scene::SMeshBuffer > m_mesh_buffer
Definition particles.h:186
u16 m_count
Definition particles.h:192
static constexpr u16 MAX_PARTICLES_PER_BUFFER
Definition particles.h:183
void release(u16 index)
Frees the particle at index
Definition particles.cpp:590
float m_usage_timer
Definition particles.h:190
virtual void OnRegisterSceneNode() override
Definition particles.cpp:607
virtual u32 getMaterialCount() const override
Definition particles.h:172
std::optional< u16 > allocate()
Reserves one more slot for a particle (4 vertices, 6 indices)
Definition particles.cpp:559
video::S3DVertex * getVertices(u16 index)
Definition particles.cpp:599
virtual video::SMaterial & getMaterial(u32 num) override
Definition particles.h:169
bool isEmpty() const
Definition particles.h:165
virtual const core::aabbox3df & getBoundingBox() const override
Definition particles.cpp:617
std::vector< u16 > m_free_list
Definition particles.h:188
Class doing particle as well as their spawners handling.
Definition particles.h:200
u64 m_next_particle_spawner_id
Definition particles.h:258
std::mutex m_particle_list_lock
Definition particles.h:264
void stepSpawners(float dtime)
Definition particles.cpp:670
static bool getNodeParticleParams(const MapNode &n, const ContentFeatures &f, ParticleParameters &p, video::ITexture **texture, v2f &texpos, v2f &texsize, video::SColor *color, u8 tilenum=0)
Definition particles.cpp:847
void clearAll()
Definition particles.cpp:750
~ParticleManager()
Definition particles.cpp:658
IntervalLimiter m_buffer_gc
Definition particles.h:262
void addParticleSpawner(u64 id, std::unique_ptr< ParticleSpawner > toadd)
Definition particles.cpp:1057
void stepParticles(float dtime)
Definition particles.cpp:699
void addNodeParticle(IGameDef *gamedef, LocalPlayer *player, v3s16 pos, const MapNode &n, const ContentFeatures &f)
Definition particles.cpp:904
static video::SMaterial getMaterialForParticle(const Particle *texture)
Definition particles.cpp:989
void step(float dtime)
Definition particles.cpp:663
std::vector< irr_ptr< ParticleBuffer > > m_particle_buffers
Definition particles.h:254
void reserveParticleSpace(size_t max_estimate)
Definition particles.cpp:945
u64 generateSpawnerId()
This function is only used by client particle spawners.
Definition particles.h:227
void deleteParticleSpawner(u64 id)
Definition particles.cpp:1071
void addDiggingParticles(IGameDef *gamedef, LocalPlayer *player, v3s16 pos, const MapNode &n, const ContentFeatures &f)
Definition particles.cpp:889
void handleParticleEvent(ClientEvent *event, Client *client, LocalPlayer *player)
Definition particles.cpp:766
std::vector< std::unique_ptr< ParticleSpawner > > m_dying_particle_spawners
Definition particles.h:253
bool addParticle(std::unique_ptr< Particle > toadd)
Definition particles.cpp:1020
ParticleManager(ClientEnvironment *env)
Definition particles.cpp:654
void stepBuffers(float dtime)
Definition particles.cpp:721
std::unordered_map< u64, std::unique_ptr< ParticleSpawner > > m_particle_spawners
Definition particles.h:252
std::vector< std::unique_ptr< Particle > > m_particles
Definition particles.h:251
ClientEnvironment * m_env
Definition particles.h:260
std::mutex m_spawner_list_lock
Definition particles.h:265
Definition particles.h:117
ParticleSpawnerParameters p
Definition particles.h:141
float m_time
Definition particles.h:139
size_t m_active
Definition particles.h:137
std::vector< float > m_spawntimes
Definition particles.h:143
void decrActive()
Definition particles.h:131
bool hasActive() const
Definition particles.h:130
ParticleSpawner(LocalPlayer *player, const ParticleSpawnerParameters &params, u16 attached_id, std::vector< ClientParticleTexture > &&texpool, ParticleManager *p_manager)
Definition particles.cpp:251
std::vector< ClientParticleTexture > m_texpool
Definition particles.h:142
void step(float dtime, ClientEnvironment *env)
Definition particles.cpp:497
u16 m_attached_id
Definition particles.h:144
void spawnParticle(ClientEnvironment *env, float radius, const core::matrix4 *attached_absolute_pos_rot_matrix)
Definition particles.cpp:293
LocalPlayer * m_player
Definition particles.h:140
bool getExpired() const
Definition particles.h:127
ParticleManager * m_particlemanager
Definition particles.h:138
Definition particles.h:55
v3f m_velocity
Definition particles.h:103
void updateVertices(ClientEnvironment *env, video::SColor color)
Definition particles.cpp:183
ParticleSpawner * getParent() const
Definition particles.h:76
Particle(const ParticleParameters &p, const ClientParticleTexRef &texture, v2f texpos, v2f texsize, video::SColor color, ParticleSpawner *parent=nullptr, std::unique_ptr< ClientParticleTexture > owned_texture=nullptr)
Definition particles.cpp:38
u16 m_index
Definition particles.h:91
ParticleBuffer * m_buffer
Definition particles.h:90
v3f m_pos
Definition particles.h:102
float m_animation_time
Definition particles.h:108
const ParticleParameters m_p
Definition particles.h:106
bool attachToBuffer(ParticleBuffer *buffer)
Definition particles.cpp:70
v2f m_texpos
Definition particles.h:100
ParticleParamTypes::BlendMode getBlendMode() const
Definition particles.h:80
ParticleSpawner * m_parent
Definition particles.h:111
float m_time
Definition particles.h:93
const ClientParticleTexRef & getTextureRef() const
Definition particles.h:78
video::SColor updateLight(ClientEnvironment *env)
Definition particles.cpp:159
v3f m_acceleration
Definition particles.h:104
float m_expiration
Definition particles.h:94
v2f m_texsize
Definition particles.h:101
video::SColor m_base_color
Definition particles.h:97
~Particle()
Definition particles.cpp:64
int m_animation_frame
Definition particles.h:109
ClientParticleTexRef m_texture
Definition particles.h:99
void step(float dtime, ClientEnvironment *env)
Definition particles.cpp:81
bool isExpired() const
Definition particles.h:73
ParticleBuffer * getBuffer() const
Definition particles.h:83
std::unique_ptr< ClientParticleTexture > m_owned_texture
Definition particles.h:113
core::vector2d< f32 > v2f
Definition irr_v2d.h:11
core::vector3d< s16 > v3s16
Definition irr_v3d.h:13
core::vector3df v3f
Definition irr_v3d.h:11
#define U32_MAX
Definition irrlichttypes.h:29
BlendMode
Definition particles.h:237
Definition activeobjectmgr.cpp:11
Definition clientevent.h:67
Definition particles.h:35
ClientParticleTexRef(ClientParticleTexture &t)
Definition particles.h:44
ClientParticleTexRef(video::ITexture *tp)
Definition particles.h:48
ClientParticleTexRef()=default
video::ITexture * ref
Definition particles.h:39
ParticleTexture * tex
Definition particles.h:38
Definition particles.h:24
ClientParticleTexture()=default
ParticleTexture tex
Definition particles.h:27
video::ITexture * ref
Definition particles.h:28
ServerParticleTexture texture
Definition particles.h:277
Definition nodedef.h:294
Definition mapnode.h:124
Definition particles.h:304
Definition particles.h:315
u16 amount
Definition particles.h:316
f32 time
Definition particles.h:317
Definition particles.h:254
ParticleParamTypes::BlendMode blendmode
Definition particles.h:256
Definition particles.h:263
static std::string p(std::string path)
Definition test_filesys.cpp:53