3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "particles.h"
23 #include "collision.h"
24 #include "client/content_cao.h"
25 #include "client/clientevent.h"
26 #include "client/renderingengine.h"
27 #include "util/numeric.h"
29 #include "environment.h"
30 #include "clientmap.h"
40 static f32 random_f32(f32 min, f32 max)
42 return rand() / (float)RAND_MAX * (max - min) + min;
45 static v3f random_v3f(v3f min, v3f max)
48 random_f32(min.X, max.X),
49 random_f32(min.Y, max.Y),
50 random_f32(min.Z, max.Z));
60 ClientEnvironment *env,
61 const ParticleParameters &p,
62 video::ITexture *texture,
67 scene::ISceneNode(RenderingEngine::get_scene_manager()->getRootSceneNode(),
68 RenderingEngine::get_scene_manager())
75 m_material.setFlag(video::EMF_LIGHTING, false);
76 m_material.setFlag(video::EMF_BACK_FACE_CULLING, false);
77 m_material.setFlag(video::EMF_BILINEAR_FILTER, false);
78 m_material.setFlag(video::EMF_FOG_ENABLE, true);
79 m_material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
80 m_material.setTexture(0, texture);
83 m_animation = p.animation;
92 m_acceleration = p.acc;
93 m_expiration = p.expirationtime;
96 m_collisiondetection = p.collisiondetection;
97 m_collision_removal = p.collision_removal;
98 m_object_collision = p.object_collision;
99 m_vertical = p.vertical;
103 const float c = p.size / 2;
104 m_collisionbox = aabb3f(-c, -c, -c, c, c, c);
105 this->setAutomaticCulling(scene::EAC_OFF);
114 void Particle::OnRegisterSceneNode()
117 SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT_EFFECT);
119 ISceneNode::OnRegisterSceneNode();
122 void Particle::render()
124 video::IVideoDriver *driver = SceneManager->getVideoDriver();
125 driver->setMaterial(m_material);
126 driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
128 u16 indices[] = {0,1,2, 2,3,0};
129 driver->drawVertexPrimitiveList(m_vertices, 4,
130 indices, 2, video::EVT_STANDARD,
131 scene::EPT_TRIANGLES, video::EIT_16BIT);
134 void Particle::step(float dtime)
137 if (m_collisiondetection) {
138 aabb3f box = m_collisionbox;
139 v3f p_pos = m_pos * BS;
140 v3f p_velocity = m_velocity * BS;
141 collisionMoveResult r = collisionMoveSimple(m_env, m_gamedef, BS * 0.5f,
142 box, 0.0f, dtime, &p_pos, &p_velocity, m_acceleration * BS, nullptr,
144 if (m_collision_removal && r.collides) {
145 // force expiration of the particle
149 m_velocity = p_velocity / BS;
152 m_velocity += m_acceleration * dtime;
153 m_pos += m_velocity * dtime;
155 if (m_animation.type != TAT_NONE) {
156 m_animation_time += dtime;
157 int frame_length_i, frame_count;
158 m_animation.determineParams(
159 m_material.getTexture(0)->getSize(),
160 &frame_count, &frame_length_i, NULL);
161 float frame_length = frame_length_i / 1000.0;
162 while (m_animation_time > frame_length) {
164 m_animation_time -= frame_length;
175 void Particle::updateLight()
185 MapNode n = m_env->getClientMap().getNode(p, &pos_ok);
187 light = n.getLightBlend(m_env->getDayNightRatio(), m_gamedef->ndef());
189 light = blend_light(m_env->getDayNightRatio(), LIGHT_SUN, 0);
191 u8 m_light = decode_light(light + m_glow);
193 m_light * m_base_color.getRed() / 255,
194 m_light * m_base_color.getGreen() / 255,
195 m_light * m_base_color.getBlue() / 255);
198 void Particle::updateVertices()
200 f32 tx0, tx1, ty0, ty1;
202 if (m_animation.type != TAT_NONE) {
203 const v2u32 texsize = m_material.getTexture(0)->getSize();
204 v2f texcoord, framesize_f;
206 texcoord = m_animation.getTextureCoords(texsize, m_animation_frame);
207 m_animation.determineParams(texsize, NULL, NULL, &framesize);
208 framesize_f = v2f(framesize.X / (float) texsize.X, framesize.Y / (float) texsize.Y);
210 tx0 = m_texpos.X + texcoord.X;
211 tx1 = m_texpos.X + texcoord.X + framesize_f.X * m_texsize.X;
212 ty0 = m_texpos.Y + texcoord.Y;
213 ty1 = m_texpos.Y + texcoord.Y + framesize_f.Y * m_texsize.Y;
216 tx1 = m_texpos.X + m_texsize.X;
218 ty1 = m_texpos.Y + m_texsize.Y;
221 m_vertices[0] = video::S3DVertex(-m_size / 2, -m_size / 2,
222 0, 0, 0, 0, m_color, tx0, ty1);
223 m_vertices[1] = video::S3DVertex(m_size / 2, -m_size / 2,
224 0, 0, 0, 0, m_color, tx1, ty1);
225 m_vertices[2] = video::S3DVertex(m_size / 2, m_size / 2,
226 0, 0, 0, 0, m_color, tx1, ty0);
227 m_vertices[3] = video::S3DVertex(-m_size / 2, m_size / 2,
228 0, 0, 0, 0, m_color, tx0, ty0);
230 v3s16 camera_offset = m_env->getCameraOffset();
231 for (video::S3DVertex &vertex : m_vertices) {
233 v3f ppos = m_player->getPosition()/BS;
234 vertex.Pos.rotateXZBy(std::atan2(ppos.Z - m_pos.Z, ppos.X - m_pos.X) /
235 core::DEGTORAD + 90);
237 vertex.Pos.rotateYZBy(m_player->getPitch());
238 vertex.Pos.rotateXZBy(m_player->getYaw());
240 m_box.addInternalPoint(vertex.Pos);
241 vertex.Pos += m_pos*BS - intToFloat(camera_offset, BS);
249 ParticleSpawner::ParticleSpawner(
252 const ParticleSpawnerParameters &p,
254 video::ITexture *texture,
255 ParticleManager *p_manager
257 m_particlemanager(p_manager), p(p)
261 m_attached_id = attached_id;
265 m_spawntimes.reserve(p.amount + 1);
266 for (u16 i = 0; i <= p.amount; i++) {
267 float spawntime = rand() / (float)RAND_MAX * p.time;
268 m_spawntimes.push_back(spawntime);
272 void ParticleSpawner::spawnParticle(ClientEnvironment *env, float radius,
273 const core::matrix4 *attached_absolute_pos_rot_matrix)
275 v3f ppos = m_player->getPosition() / BS;
276 v3f pos = random_v3f(p.minpos, p.maxpos);
278 // Need to apply this first or the following check
279 // will be wrong for attached spawners
280 if (attached_absolute_pos_rot_matrix) {
282 attached_absolute_pos_rot_matrix->transformVect(pos);
284 v3s16 camera_offset = m_particlemanager->m_env->getCameraOffset();
285 pos.X += camera_offset.X;
286 pos.Y += camera_offset.Y;
287 pos.Z += camera_offset.Z;
290 if (pos.getDistanceFrom(ppos) > radius)
293 // Parameters for the single particle we're about to spawn
294 ParticleParameters pp;
297 pp.vel = random_v3f(p.minvel, p.maxvel);
298 pp.acc = random_v3f(p.minacc, p.maxacc);
300 if (attached_absolute_pos_rot_matrix) {
301 // Apply attachment rotation
302 attached_absolute_pos_rot_matrix->rotateVect(pp.vel);
303 attached_absolute_pos_rot_matrix->rotateVect(pp.acc);
306 pp.expirationtime = random_f32(p.minexptime, p.maxexptime);
309 video::ITexture *texture;
311 video::SColor color(0xFFFFFFFF);
313 if (p.node.getContent() != CONTENT_IGNORE) {
314 const ContentFeatures &f =
315 m_particlemanager->m_env->getGameDef()->ndef()->get(p.node);
316 if (!ParticleManager::getNodeParticleParams(p.node, f, pp, &texture,
317 texpos, texsize, &color, p.node_tile))
321 texpos = v2f(0.0f, 0.0f);
322 texsize = v2f(1.0f, 1.0f);
325 // Allow keeping default random size
326 if (p.maxsize > 0.0f)
327 pp.size = random_f32(p.minsize, p.maxsize);
329 m_particlemanager->addParticle(new Particle(
341 void ParticleSpawner::step(float dtime, ClientEnvironment *env)
345 static thread_local const float radius =
346 g_settings->getS16("max_block_send_distance") * MAP_BLOCKSIZE;
348 bool unloaded = false;
349 const core::matrix4 *attached_absolute_pos_rot_matrix = nullptr;
351 if (GenericCAO *attached = dynamic_cast<GenericCAO *>(env->getActiveObject(m_attached_id))) {
352 attached_absolute_pos_rot_matrix = &attached->getAbsolutePosRotMatrix();
359 // Spawner exists for a predefined timespan
360 for (auto i = m_spawntimes.begin(); i != m_spawntimes.end(); ) {
361 if ((*i) <= m_time && p.amount > 0) {
364 // Pretend to, but don't actually spawn a particle if it is
365 // attached to an unloaded object or distant from player.
367 spawnParticle(env, radius, attached_absolute_pos_rot_matrix);
369 i = m_spawntimes.erase(i);
375 // Spawner exists for an infinity timespan, spawn on a per-second base
377 // Skip this step if attached to an unloaded object
381 for (int i = 0; i <= p.amount; i++) {
382 if (rand() / (float)RAND_MAX < dtime)
383 spawnParticle(env, radius, attached_absolute_pos_rot_matrix);
392 ParticleManager::ParticleManager(ClientEnvironment *env) :
396 ParticleManager::~ParticleManager()
401 void ParticleManager::step(float dtime)
403 stepParticles (dtime);
404 stepSpawners (dtime);
407 void ParticleManager::stepSpawners(float dtime)
409 MutexAutoLock lock(m_spawner_list_lock);
410 for (auto i = m_particle_spawners.begin(); i != m_particle_spawners.end();) {
411 if (i->second->get_expired()) {
413 m_particle_spawners.erase(i++);
415 i->second->step(dtime, m_env);
421 void ParticleManager::stepParticles(float dtime)
423 MutexAutoLock lock(m_particle_list_lock);
424 for (auto i = m_particles.begin(); i != m_particles.end();) {
425 if ((*i)->get_expired()) {
428 i = m_particles.erase(i);
436 void ParticleManager::clearAll()
438 MutexAutoLock lock(m_spawner_list_lock);
439 MutexAutoLock lock2(m_particle_list_lock);
440 for (auto i = m_particle_spawners.begin(); i != m_particle_spawners.end();) {
442 m_particle_spawners.erase(i++);
445 for(auto i = m_particles.begin(); i != m_particles.end();)
449 i = m_particles.erase(i);
453 void ParticleManager::handleParticleEvent(ClientEvent *event, Client *client,
456 switch (event->type) {
457 case CE_DELETE_PARTICLESPAWNER: {
458 deleteParticleSpawner(event->delete_particlespawner.id);
459 // no allocated memory in delete event
462 case CE_ADD_PARTICLESPAWNER: {
463 deleteParticleSpawner(event->add_particlespawner.id);
465 const ParticleSpawnerParameters &p = *event->add_particlespawner.p;
467 video::ITexture *texture =
468 client->tsrc()->getTextureForMesh(p.texture);
470 auto toadd = new ParticleSpawner(client, player,
472 event->add_particlespawner.attached_id,
476 addParticleSpawner(event->add_particlespawner.id, toadd);
478 delete event->add_particlespawner.p;
481 case CE_SPAWN_PARTICLE: {
482 ParticleParameters &p = *event->spawn_particle;
484 video::ITexture *texture;
486 video::SColor color(0xFFFFFFFF);
488 f32 oldsize = p.size;
490 if (p.node.getContent() != CONTENT_IGNORE) {
491 const ContentFeatures &f = m_env->getGameDef()->ndef()->get(p.node);
492 if (!getNodeParticleParams(p.node, f, p, &texture, texpos,
493 texsize, &color, p.node_tile))
496 texture = client->tsrc()->getTextureForMesh(p.texture);
497 texpos = v2f(0.0f, 0.0f);
498 texsize = v2f(1.0f, 1.0f);
501 // Allow keeping default random size
506 Particle *toadd = new Particle(client, player, m_env,
507 p, texture, texpos, texsize, color);
512 delete event->spawn_particle;
519 bool ParticleManager::getNodeParticleParams(const MapNode &n,
520 const ContentFeatures &f, ParticleParameters &p, video::ITexture **texture,
521 v2f &texpos, v2f &texsize, video::SColor *color, u8 tilenum)
523 // No particles for "airlike" nodes
524 if (f.drawtype == NDT_AIRLIKE)
529 if (tilenum > 0 && tilenum <= 6)
533 const TileLayer &tile = f.tiles[texid].layers[0];
534 p.animation.type = TAT_NONE;
536 // Only use first frame of animated texture
537 if (tile.material_flags & MATERIAL_FLAG_ANIMATION)
538 *texture = (*tile.frames)[0].texture;
540 *texture = tile.texture;
542 float size = (rand() % 8) / 64.0f;
546 texsize = v2f(size * 2.0f, size * 2.0f);
547 texpos.X = (rand() % 64) / 64.0f - texsize.X;
548 texpos.Y = (rand() % 64) / 64.0f - texsize.Y;
553 n.getColor(f, color);
558 // The final burst of particles when a node is finally dug, *not* particles
559 // spawned during the digging of a node.
561 void ParticleManager::addDiggingParticles(IGameDef *gamedef,
562 LocalPlayer *player, v3s16 pos, const MapNode &n, const ContentFeatures &f)
564 // No particles for "airlike" nodes
565 if (f.drawtype == NDT_AIRLIKE)
568 for (u16 j = 0; j < 16; j++) {
569 addNodeParticle(gamedef, player, pos, n, f);
573 // During the digging of a node particles are spawned individually by this
574 // function, called from Game::handleDigging() in game.cpp.
576 void ParticleManager::addNodeParticle(IGameDef *gamedef,
577 LocalPlayer *player, v3s16 pos, const MapNode &n, const ContentFeatures &f)
579 ParticleParameters p;
580 video::ITexture *texture;
584 if (!getNodeParticleParams(n, f, p, &texture, texpos, texsize, &color))
587 p.expirationtime = (rand() % 100) / 100.0f;
591 (rand() % 150) / 50.0f - 1.5f,
592 (rand() % 150) / 50.0f,
593 (rand() % 150) / 50.0f - 1.5f
597 -player->movement_gravity * player->physics_override_gravity / BS,
601 (f32)pos.X + (rand() % 100) / 200.0f - 0.25f,
602 (f32)pos.Y + (rand() % 100) / 200.0f - 0.25f,
603 (f32)pos.Z + (rand() % 100) / 200.0f - 0.25f
606 Particle *toadd = new Particle(
619 void ParticleManager::addParticle(Particle *toadd)
621 MutexAutoLock lock(m_particle_list_lock);
622 m_particles.push_back(toadd);
626 void ParticleManager::addParticleSpawner(u64 id, ParticleSpawner *toadd)
628 MutexAutoLock lock(m_spawner_list_lock);
629 m_particle_spawners[id] = toadd;
632 void ParticleManager::deleteParticleSpawner(u64 id)
634 MutexAutoLock lock(m_spawner_list_lock);
635 auto it = m_particle_spawners.find(id);
636 if (it != m_particle_spawners.end()) {
638 m_particle_spawners.erase(it);