2012-12-31 19:33:36 +01:00
|
|
|
/*
|
2013-02-24 18:40:43 +01:00
|
|
|
Minetest
|
2013-02-24 19:38:45 +01:00
|
|
|
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
2012-12-31 19:33:36 +01:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "particles.h"
|
2018-04-04 07:42:40 +02:00
|
|
|
#include <cmath>
|
2017-01-09 20:39:22 +01:00
|
|
|
#include "client.h"
|
2012-12-31 19:33:36 +01:00
|
|
|
#include "collision.h"
|
2019-08-09 10:51:17 +02:00
|
|
|
#include "client/content_cao.h"
|
2017-08-28 20:02:23 +02:00
|
|
|
#include "client/clientevent.h"
|
2017-06-26 20:11:17 +02:00
|
|
|
#include "client/renderingengine.h"
|
2012-12-31 19:33:36 +01:00
|
|
|
#include "util/numeric.h"
|
|
|
|
#include "light.h"
|
|
|
|
#include "environment.h"
|
|
|
|
#include "clientmap.h"
|
|
|
|
#include "mapnode.h"
|
2017-08-18 19:25:07 +02:00
|
|
|
#include "nodedef.h"
|
2015-01-05 18:34:59 +01:00
|
|
|
#include "client.h"
|
2017-05-20 16:46:35 +02:00
|
|
|
#include "settings.h"
|
2012-12-31 19:33:36 +01:00
|
|
|
|
2020-05-22 13:23:25 +02:00
|
|
|
/*
|
|
|
|
Particle
|
|
|
|
*/
|
|
|
|
|
2012-12-31 19:33:36 +01:00
|
|
|
Particle::Particle(
|
|
|
|
IGameDef *gamedef,
|
|
|
|
LocalPlayer *player,
|
2015-01-05 18:34:59 +01:00
|
|
|
ClientEnvironment *env,
|
2020-05-22 13:23:25 +02:00
|
|
|
const ParticleParameters &p,
|
2022-07-13 11:57:12 +02:00
|
|
|
const ClientTexRef& texture,
|
2013-06-24 04:17:50 +02:00
|
|
|
v2f texpos,
|
2017-01-14 16:48:49 +01:00
|
|
|
v2f texsize,
|
2017-01-12 15:46:30 +01:00
|
|
|
video::SColor color
|
2012-12-31 19:33:36 +01:00
|
|
|
):
|
2021-04-29 09:40:56 +02:00
|
|
|
scene::ISceneNode(((Client *)gamedef)->getSceneManager()->getRootSceneNode(),
|
2022-07-13 11:57:12 +02:00
|
|
|
((Client *)gamedef)->getSceneManager()),
|
|
|
|
m_texture(texture)
|
2012-12-31 19:33:36 +01:00
|
|
|
{
|
|
|
|
// Misc
|
|
|
|
m_gamedef = gamedef;
|
2015-01-05 18:34:59 +01:00
|
|
|
m_env = env;
|
2012-12-31 19:33:36 +01:00
|
|
|
|
2022-07-13 11:57:12 +02:00
|
|
|
// translate blend modes to GL blend functions
|
|
|
|
video::E_BLEND_FACTOR bfsrc, bfdst;
|
|
|
|
video::E_BLEND_OPERATION blendop;
|
|
|
|
const auto blendmode = texture.tex != nullptr
|
|
|
|
? texture.tex -> blendmode
|
|
|
|
: ParticleParamTypes::BlendMode::alpha;
|
|
|
|
|
|
|
|
switch (blendmode) {
|
|
|
|
case ParticleParamTypes::BlendMode::add:
|
|
|
|
bfsrc = video::EBF_SRC_ALPHA;
|
|
|
|
bfdst = video::EBF_DST_ALPHA;
|
|
|
|
blendop = video::EBO_ADD;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ParticleParamTypes::BlendMode::sub:
|
|
|
|
bfsrc = video::EBF_SRC_ALPHA;
|
|
|
|
bfdst = video::EBF_DST_ALPHA;
|
|
|
|
blendop = video::EBO_REVSUBTRACT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ParticleParamTypes::BlendMode::screen:
|
|
|
|
bfsrc = video::EBF_ONE;
|
|
|
|
bfdst = video::EBF_ONE_MINUS_SRC_COLOR;
|
|
|
|
blendop = video::EBO_ADD;
|
|
|
|
break;
|
|
|
|
|
2022-07-13 22:45:05 +02:00
|
|
|
default: // includes ParticleParamTypes::BlendMode::alpha
|
|
|
|
bfsrc = video::EBF_SRC_ALPHA;
|
|
|
|
bfdst = video::EBF_ONE_MINUS_SRC_ALPHA;
|
|
|
|
blendop = video::EBO_ADD;
|
|
|
|
break;
|
2022-07-13 11:57:12 +02:00
|
|
|
}
|
|
|
|
|
2012-12-31 19:33:36 +01:00
|
|
|
// Texture
|
|
|
|
m_material.setFlag(video::EMF_LIGHTING, false);
|
|
|
|
m_material.setFlag(video::EMF_BACK_FACE_CULLING, false);
|
|
|
|
m_material.setFlag(video::EMF_BILINEAR_FILTER, false);
|
|
|
|
m_material.setFlag(video::EMF_FOG_ENABLE, true);
|
2022-07-13 11:57:12 +02:00
|
|
|
|
|
|
|
// correctly render layered transparent particles -- see #10398
|
|
|
|
m_material.setFlag(video::EMF_ZWRITE_ENABLE, true);
|
|
|
|
|
|
|
|
// enable alpha blending and set blend mode
|
|
|
|
m_material.MaterialType = video::EMT_ONETEXTURE_BLEND;
|
|
|
|
m_material.MaterialTypeParam = video::pack_textureBlendFunc(
|
|
|
|
bfsrc, bfdst,
|
|
|
|
video::EMFN_MODULATE_1X,
|
|
|
|
video::EAS_TEXTURE | video::EAS_VERTEX_COLOR);
|
|
|
|
m_material.BlendOperation = blendop;
|
|
|
|
m_material.setTexture(0, m_texture.ref);
|
2013-06-24 04:17:50 +02:00
|
|
|
m_texpos = texpos;
|
|
|
|
m_texsize = texsize;
|
2020-05-22 13:23:25 +02:00
|
|
|
m_animation = p.animation;
|
2012-12-31 19:33:36 +01:00
|
|
|
|
2017-01-12 15:46:30 +01:00
|
|
|
// Color
|
|
|
|
m_base_color = color;
|
|
|
|
m_color = color;
|
|
|
|
|
2012-12-31 19:33:36 +01:00
|
|
|
// Particle related
|
2020-05-22 13:23:25 +02:00
|
|
|
m_pos = p.pos;
|
|
|
|
m_velocity = p.vel;
|
|
|
|
m_acceleration = p.acc;
|
2022-07-13 11:57:12 +02:00
|
|
|
m_drag = p.drag;
|
|
|
|
m_jitter = p.jitter;
|
|
|
|
m_bounce = p.bounce;
|
2020-05-22 13:23:25 +02:00
|
|
|
m_expiration = p.expirationtime;
|
2012-12-31 19:33:36 +01:00
|
|
|
m_player = player;
|
2020-05-22 13:23:25 +02:00
|
|
|
m_size = p.size;
|
|
|
|
m_collisiondetection = p.collisiondetection;
|
|
|
|
m_collision_removal = p.collision_removal;
|
|
|
|
m_object_collision = p.object_collision;
|
|
|
|
m_vertical = p.vertical;
|
|
|
|
m_glow = p.glow;
|
2022-07-13 11:57:12 +02:00
|
|
|
m_alpha = 0;
|
|
|
|
m_parent = nullptr;
|
2012-12-31 19:33:36 +01:00
|
|
|
|
2013-01-23 18:32:02 +01:00
|
|
|
// Irrlicht stuff
|
2020-05-22 13:23:25 +02:00
|
|
|
const float c = p.size / 2;
|
|
|
|
m_collisionbox = aabb3f(-c, -c, -c, c, c, c);
|
2012-12-31 19:33:36 +01:00
|
|
|
this->setAutomaticCulling(scene::EAC_OFF);
|
2013-01-23 18:32:02 +01:00
|
|
|
|
|
|
|
// Init lighting
|
2014-01-26 11:40:21 +01:00
|
|
|
updateLight();
|
2013-01-23 18:32:02 +01:00
|
|
|
|
|
|
|
// Init model
|
|
|
|
updateVertices();
|
2012-12-31 19:33:36 +01:00
|
|
|
}
|
|
|
|
|
2022-07-13 11:57:12 +02:00
|
|
|
Particle::~Particle()
|
|
|
|
{
|
|
|
|
/* if our textures aren't owned by a particlespawner, we need to clean
|
|
|
|
* them up ourselves when the particle dies */
|
|
|
|
if (m_parent == nullptr)
|
|
|
|
delete m_texture.tex;
|
|
|
|
}
|
|
|
|
|
2012-12-31 19:33:36 +01:00
|
|
|
void Particle::OnRegisterSceneNode()
|
|
|
|
{
|
|
|
|
if (IsVisible)
|
2015-02-26 14:45:31 +01:00
|
|
|
SceneManager->registerNodeForRendering(this, scene::ESNRP_TRANSPARENT_EFFECT);
|
2012-12-31 19:33:36 +01:00
|
|
|
|
|
|
|
ISceneNode::OnRegisterSceneNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Particle::render()
|
|
|
|
{
|
2019-08-17 08:39:40 +02:00
|
|
|
video::IVideoDriver *driver = SceneManager->getVideoDriver();
|
2012-12-31 19:33:36 +01:00
|
|
|
driver->setMaterial(m_material);
|
|
|
|
driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
|
|
|
|
|
|
|
|
u16 indices[] = {0,1,2, 2,3,0};
|
2013-01-23 18:32:02 +01:00
|
|
|
driver->drawVertexPrimitiveList(m_vertices, 4,
|
|
|
|
indices, 2, video::EVT_STANDARD,
|
|
|
|
scene::EPT_TRIANGLES, video::EIT_16BIT);
|
2012-12-31 19:33:36 +01:00
|
|
|
}
|
|
|
|
|
2014-01-26 11:40:21 +01:00
|
|
|
void Particle::step(float dtime)
|
2012-12-31 19:33:36 +01:00
|
|
|
{
|
|
|
|
m_time += dtime;
|
2022-07-13 11:57:12 +02:00
|
|
|
|
|
|
|
// apply drag (not handled by collisionMoveSimple) and brownian motion
|
|
|
|
v3f av = vecAbsolute(m_velocity);
|
|
|
|
av -= av * (m_drag * dtime);
|
|
|
|
m_velocity = av*vecSign(m_velocity) + v3f(m_jitter.pickWithin())*dtime;
|
|
|
|
|
2016-05-28 06:08:23 +02:00
|
|
|
if (m_collisiondetection) {
|
2016-02-11 15:21:21 +01:00
|
|
|
aabb3f box = m_collisionbox;
|
2016-05-28 06:08:23 +02:00
|
|
|
v3f p_pos = m_pos * BS;
|
|
|
|
v3f p_velocity = m_velocity * BS;
|
2018-09-08 01:38:35 +02:00
|
|
|
collisionMoveResult r = collisionMoveSimple(m_env, m_gamedef, BS * 0.5f,
|
|
|
|
box, 0.0f, dtime, &p_pos, &p_velocity, m_acceleration * BS, nullptr,
|
|
|
|
m_object_collision);
|
2022-07-13 11:57:12 +02:00
|
|
|
|
|
|
|
f32 bounciness = m_bounce.pickWithin();
|
|
|
|
if (r.collides && (m_collision_removal || bounciness > 0)) {
|
|
|
|
if (m_collision_removal) {
|
|
|
|
// force expiration of the particle
|
|
|
|
m_expiration = -1.0f;
|
|
|
|
} else if (bounciness > 0) {
|
|
|
|
/* cheap way to get a decent bounce effect is to only invert the
|
|
|
|
* largest component of the velocity vector, so e.g. you don't
|
|
|
|
* have a rock immediately bounce back in your face when you try
|
|
|
|
* to skip it across the water (as would happen if we simply
|
|
|
|
* downscaled and negated the velocity vector). this means
|
|
|
|
* bounciness will work properly for cubic objects, but meshes
|
|
|
|
* with diagonal angles and entities will not yield the correct
|
|
|
|
* visual. this is probably unavoidable */
|
|
|
|
if (av.Y > av.X && av.Y > av.Z) {
|
|
|
|
m_velocity.Y = -(m_velocity.Y * bounciness);
|
|
|
|
} else if (av.X > av.Y && av.X > av.Z) {
|
|
|
|
m_velocity.X = -(m_velocity.X * bounciness);
|
|
|
|
} else if (av.Z > av.Y && av.Z > av.X) {
|
|
|
|
m_velocity.Z = -(m_velocity.Z * bounciness);
|
|
|
|
} else { // well now we're in a bit of a pickle
|
|
|
|
m_velocity = -(m_velocity * bounciness);
|
|
|
|
}
|
|
|
|
}
|
2016-05-28 06:08:23 +02:00
|
|
|
} else {
|
|
|
|
m_velocity = p_velocity / BS;
|
|
|
|
}
|
2022-07-13 11:57:12 +02:00
|
|
|
m_pos = p_pos / BS;
|
2016-05-28 06:08:23 +02:00
|
|
|
} else {
|
2022-09-20 10:55:51 +02:00
|
|
|
// apply velocity and acceleration to position
|
|
|
|
m_pos += (m_velocity + m_acceleration * 0.5f * dtime) * dtime;
|
|
|
|
// apply acceleration to velocity
|
2013-01-23 18:32:02 +01:00
|
|
|
m_velocity += m_acceleration * dtime;
|
|
|
|
}
|
2022-07-13 11:57:12 +02:00
|
|
|
|
2017-01-14 16:48:49 +01:00
|
|
|
if (m_animation.type != TAT_NONE) {
|
|
|
|
m_animation_time += dtime;
|
|
|
|
int frame_length_i, frame_count;
|
|
|
|
m_animation.determineParams(
|
|
|
|
m_material.getTexture(0)->getSize(),
|
|
|
|
&frame_count, &frame_length_i, NULL);
|
|
|
|
float frame_length = frame_length_i / 1000.0;
|
|
|
|
while (m_animation_time > frame_length) {
|
|
|
|
m_animation_frame++;
|
|
|
|
m_animation_time -= frame_length;
|
|
|
|
}
|
|
|
|
}
|
2012-12-31 19:33:36 +01:00
|
|
|
|
2022-07-13 11:57:12 +02:00
|
|
|
// animate particle alpha in accordance with settings
|
|
|
|
if (m_texture.tex != nullptr)
|
|
|
|
m_alpha = m_texture.tex -> alpha.blend(m_time / (m_expiration+0.1f));
|
|
|
|
else
|
|
|
|
m_alpha = 1.f;
|
|
|
|
|
2012-12-31 19:33:36 +01:00
|
|
|
// Update lighting
|
2014-01-26 11:40:21 +01:00
|
|
|
updateLight();
|
2013-01-23 18:32:02 +01:00
|
|
|
|
|
|
|
// Update model
|
|
|
|
updateVertices();
|
2022-07-13 11:57:12 +02:00
|
|
|
|
|
|
|
// Update position -- see #10398
|
|
|
|
v3s16 camera_offset = m_env->getCameraOffset();
|
|
|
|
setPosition(m_pos*BS - intToFloat(camera_offset, BS));
|
2013-01-23 18:32:02 +01:00
|
|
|
}
|
|
|
|
|
2014-01-26 11:40:21 +01:00
|
|
|
void Particle::updateLight()
|
2013-01-23 18:32:02 +01:00
|
|
|
{
|
2012-12-31 19:33:36 +01:00
|
|
|
u8 light = 0;
|
2014-11-14 09:05:34 +01:00
|
|
|
bool pos_ok;
|
|
|
|
|
|
|
|
v3s16 p = v3s16(
|
|
|
|
floor(m_pos.X+0.5),
|
|
|
|
floor(m_pos.Y+0.5),
|
|
|
|
floor(m_pos.Z+0.5)
|
|
|
|
);
|
2019-08-10 19:45:44 +02:00
|
|
|
MapNode n = m_env->getClientMap().getNode(p, &pos_ok);
|
2014-11-14 09:05:34 +01:00
|
|
|
if (pos_ok)
|
2022-10-09 16:50:26 +02:00
|
|
|
light = n.getLightBlend(m_env->getDayNightRatio(),
|
|
|
|
m_gamedef->ndef()->getLightingFlags(n));
|
2014-11-14 09:05:34 +01:00
|
|
|
else
|
2014-01-26 11:40:21 +01:00
|
|
|
light = blend_light(m_env->getDayNightRatio(), LIGHT_SUN, 0);
|
2014-11-14 09:05:34 +01:00
|
|
|
|
2017-01-12 15:46:30 +01:00
|
|
|
u8 m_light = decode_light(light + m_glow);
|
2022-07-13 11:57:12 +02:00
|
|
|
m_color.set(m_alpha*255,
|
2017-01-12 15:46:30 +01:00
|
|
|
m_light * m_base_color.getRed() / 255,
|
|
|
|
m_light * m_base_color.getGreen() / 255,
|
|
|
|
m_light * m_base_color.getBlue() / 255);
|
2012-12-31 19:33:36 +01:00
|
|
|
}
|
|
|
|
|
2013-01-23 18:32:02 +01:00
|
|
|
void Particle::updateVertices()
|
|
|
|
{
|
2017-01-14 16:48:49 +01:00
|
|
|
f32 tx0, tx1, ty0, ty1;
|
2022-07-13 11:57:12 +02:00
|
|
|
v2f scale;
|
|
|
|
|
|
|
|
if (m_texture.tex != nullptr)
|
|
|
|
scale = m_texture.tex -> scale.blend(m_time / (m_expiration+0.1));
|
|
|
|
else
|
|
|
|
scale = v2f(1.f, 1.f);
|
2017-01-14 16:48:49 +01:00
|
|
|
|
|
|
|
if (m_animation.type != TAT_NONE) {
|
|
|
|
const v2u32 texsize = m_material.getTexture(0)->getSize();
|
|
|
|
v2f texcoord, framesize_f;
|
|
|
|
v2u32 framesize;
|
|
|
|
texcoord = m_animation.getTextureCoords(texsize, m_animation_frame);
|
|
|
|
m_animation.determineParams(texsize, NULL, NULL, &framesize);
|
|
|
|
framesize_f = v2f(framesize.X / (float) texsize.X, framesize.Y / (float) texsize.Y);
|
|
|
|
|
|
|
|
tx0 = m_texpos.X + texcoord.X;
|
|
|
|
tx1 = m_texpos.X + texcoord.X + framesize_f.X * m_texsize.X;
|
|
|
|
ty0 = m_texpos.Y + texcoord.Y;
|
|
|
|
ty1 = m_texpos.Y + texcoord.Y + framesize_f.Y * m_texsize.Y;
|
|
|
|
} else {
|
|
|
|
tx0 = m_texpos.X;
|
|
|
|
tx1 = m_texpos.X + m_texsize.X;
|
|
|
|
ty0 = m_texpos.Y;
|
|
|
|
ty1 = m_texpos.Y + m_texsize.Y;
|
|
|
|
}
|
2013-06-24 04:17:50 +02:00
|
|
|
|
2022-07-13 11:57:12 +02:00
|
|
|
auto half = m_size * .5f,
|
|
|
|
hx = half * scale.X,
|
|
|
|
hy = half * scale.Y;
|
|
|
|
m_vertices[0] = video::S3DVertex(-hx, -hy,
|
2017-01-12 15:46:30 +01:00
|
|
|
0, 0, 0, 0, m_color, tx0, ty1);
|
2022-07-13 11:57:12 +02:00
|
|
|
m_vertices[1] = video::S3DVertex(hx, -hy,
|
2017-01-12 15:46:30 +01:00
|
|
|
0, 0, 0, 0, m_color, tx1, ty1);
|
2022-07-13 11:57:12 +02:00
|
|
|
m_vertices[2] = video::S3DVertex(hx, hy,
|
2017-01-12 15:46:30 +01:00
|
|
|
0, 0, 0, 0, m_color, tx1, ty0);
|
2022-07-13 11:57:12 +02:00
|
|
|
m_vertices[3] = video::S3DVertex(-hx, hy,
|
2017-01-12 15:46:30 +01:00
|
|
|
0, 0, 0, 0, m_color, tx0, ty0);
|
2013-01-23 18:32:02 +01:00
|
|
|
|
2022-07-13 11:57:12 +02:00
|
|
|
|
|
|
|
// see #10398
|
|
|
|
// v3s16 camera_offset = m_env->getCameraOffset();
|
|
|
|
// particle position is now handled by step()
|
|
|
|
m_box.reset(v3f());
|
|
|
|
|
2017-08-20 19:37:29 +02:00
|
|
|
for (video::S3DVertex &vertex : m_vertices) {
|
2013-04-22 20:35:10 +02:00
|
|
|
if (m_vertical) {
|
|
|
|
v3f ppos = m_player->getPosition()/BS;
|
2018-04-04 07:42:40 +02:00
|
|
|
vertex.Pos.rotateXZBy(std::atan2(ppos.Z - m_pos.Z, ppos.X - m_pos.X) /
|
|
|
|
core::DEGTORAD + 90);
|
2013-04-22 20:35:10 +02:00
|
|
|
} else {
|
2017-08-20 19:37:29 +02:00
|
|
|
vertex.Pos.rotateYZBy(m_player->getPitch());
|
|
|
|
vertex.Pos.rotateXZBy(m_player->getYaw());
|
2013-04-22 20:35:10 +02:00
|
|
|
}
|
2017-08-20 19:37:29 +02:00
|
|
|
m_box.addInternalPoint(vertex.Pos);
|
2013-01-23 18:32:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
ParticleSpawner
|
|
|
|
*/
|
|
|
|
|
2018-09-08 01:38:35 +02:00
|
|
|
ParticleSpawner::ParticleSpawner(
|
|
|
|
IGameDef *gamedef,
|
|
|
|
LocalPlayer *player,
|
2020-05-22 13:23:25 +02:00
|
|
|
const ParticleSpawnerParameters &p,
|
2018-09-08 01:38:35 +02:00
|
|
|
u16 attached_id,
|
2022-07-13 11:57:12 +02:00
|
|
|
std::unique_ptr<ClientTexture[]>& texpool,
|
|
|
|
size_t texcount,
|
2018-09-08 01:38:35 +02:00
|
|
|
ParticleManager *p_manager
|
|
|
|
):
|
2020-05-22 13:23:25 +02:00
|
|
|
m_particlemanager(p_manager), p(p)
|
2013-01-23 18:32:02 +01:00
|
|
|
{
|
|
|
|
m_gamedef = gamedef;
|
|
|
|
m_player = player;
|
2016-08-04 22:09:21 +02:00
|
|
|
m_attached_id = attached_id;
|
2022-07-13 11:57:12 +02:00
|
|
|
m_texpool = std::move(texpool);
|
|
|
|
m_texcount = texcount;
|
2013-01-23 18:32:02 +01:00
|
|
|
m_time = 0;
|
2022-07-13 11:57:12 +02:00
|
|
|
m_active = 0;
|
|
|
|
m_dying = false;
|
2013-01-23 18:32:02 +01:00
|
|
|
|
2020-05-22 13:23:25 +02:00
|
|
|
m_spawntimes.reserve(p.amount + 1);
|
|
|
|
for (u16 i = 0; i <= p.amount; i++) {
|
2022-07-13 11:57:12 +02:00
|
|
|
float spawntime = myrand_float() * p.time;
|
2013-01-23 18:32:02 +01:00
|
|
|
m_spawntimes.push_back(spawntime);
|
|
|
|
}
|
2022-07-13 11:57:12 +02:00
|
|
|
|
|
|
|
size_t max_particles = 0; // maximum number of particles likely to be visible at any given time
|
|
|
|
if (p.time != 0) {
|
|
|
|
auto maxGenerations = p.time / std::min(p.exptime.start.min, p.exptime.end.min);
|
|
|
|
max_particles = p.amount / maxGenerations;
|
|
|
|
} else {
|
|
|
|
auto longestLife = std::max(p.exptime.start.max, p.exptime.end.max);
|
|
|
|
max_particles = p.amount * longestLife;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_manager->reserveParticleSpace(max_particles * 1.2);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
GenericCAO *findObjectByID(ClientEnvironment *env, u16 id) {
|
|
|
|
if (id == 0)
|
|
|
|
return nullptr;
|
|
|
|
return env->getGenericCAO(id);
|
|
|
|
}
|
2013-01-23 18:32:02 +01:00
|
|
|
}
|
|
|
|
|
2017-10-02 20:40:59 +02:00
|
|
|
void ParticleSpawner::spawnParticle(ClientEnvironment *env, float radius,
|
2019-08-09 10:51:17 +02:00
|
|
|
const core::matrix4 *attached_absolute_pos_rot_matrix)
|
2017-10-02 20:40:59 +02:00
|
|
|
{
|
2022-07-13 11:57:12 +02:00
|
|
|
float fac = 0;
|
|
|
|
if (p.time != 0) { // ensure safety from divide-by-zeroes
|
|
|
|
fac = m_time / (p.time+0.1f);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto r_pos = p.pos.blend(fac);
|
|
|
|
auto r_vel = p.vel.blend(fac);
|
|
|
|
auto r_acc = p.acc.blend(fac);
|
|
|
|
auto r_drag = p.drag.blend(fac);
|
|
|
|
auto r_radius = p.radius.blend(fac);
|
|
|
|
auto r_jitter = p.jitter.blend(fac);
|
|
|
|
auto r_bounce = p.bounce.blend(fac);
|
|
|
|
v3f attractor_origin = p.attractor_origin.blend(fac);
|
|
|
|
v3f attractor_direction = p.attractor_direction.blend(fac);
|
|
|
|
auto attractor_obj = findObjectByID(env, p.attractor_attachment);
|
|
|
|
auto attractor_direction_obj = findObjectByID(env, p.attractor_direction_attachment);
|
|
|
|
|
|
|
|
auto r_exp = p.exptime.blend(fac);
|
|
|
|
auto r_size = p.size.blend(fac);
|
|
|
|
auto r_attract = p.attract.blend(fac);
|
|
|
|
auto attract = r_attract.pickWithin();
|
|
|
|
|
2017-10-02 20:40:59 +02:00
|
|
|
v3f ppos = m_player->getPosition() / BS;
|
2022-07-13 11:57:12 +02:00
|
|
|
v3f pos = r_pos.pickWithin();
|
|
|
|
v3f sphere_radius = r_radius.pickWithin();
|
2017-10-02 20:40:59 +02:00
|
|
|
|
|
|
|
// Need to apply this first or the following check
|
|
|
|
// will be wrong for attached spawners
|
2019-08-09 10:51:17 +02:00
|
|
|
if (attached_absolute_pos_rot_matrix) {
|
|
|
|
pos *= BS;
|
|
|
|
attached_absolute_pos_rot_matrix->transformVect(pos);
|
|
|
|
pos /= BS;
|
|
|
|
v3s16 camera_offset = m_particlemanager->m_env->getCameraOffset();
|
|
|
|
pos.X += camera_offset.X;
|
|
|
|
pos.Y += camera_offset.Y;
|
|
|
|
pos.Z += camera_offset.Z;
|
2017-10-02 20:40:59 +02:00
|
|
|
}
|
|
|
|
|
2022-07-13 11:57:12 +02:00
|
|
|
if (pos.getDistanceFromSQ(ppos) > radius*radius)
|
2017-10-02 20:40:59 +02:00
|
|
|
return;
|
|
|
|
|
2020-05-22 13:23:25 +02:00
|
|
|
// Parameters for the single particle we're about to spawn
|
|
|
|
ParticleParameters pp;
|
|
|
|
pp.pos = pos;
|
|
|
|
|
2022-07-13 11:57:12 +02:00
|
|
|
pp.vel = r_vel.pickWithin();
|
|
|
|
pp.acc = r_acc.pickWithin();
|
|
|
|
pp.drag = r_drag.pickWithin();
|
|
|
|
pp.jitter = r_jitter;
|
|
|
|
pp.bounce = r_bounce;
|
2017-10-02 20:40:59 +02:00
|
|
|
|
2019-08-09 10:51:17 +02:00
|
|
|
if (attached_absolute_pos_rot_matrix) {
|
|
|
|
// Apply attachment rotation
|
2020-05-22 13:23:25 +02:00
|
|
|
attached_absolute_pos_rot_matrix->rotateVect(pp.vel);
|
|
|
|
attached_absolute_pos_rot_matrix->rotateVect(pp.acc);
|
2017-10-02 20:40:59 +02:00
|
|
|
}
|
|
|
|
|
2022-07-13 11:57:12 +02:00
|
|
|
if (attractor_obj)
|
|
|
|
attractor_origin += attractor_obj->getPosition() / BS;
|
|
|
|
if (attractor_direction_obj) {
|
|
|
|
auto *attractor_absolute_pos_rot_matrix = attractor_direction_obj->getAbsolutePosRotMatrix();
|
|
|
|
if (attractor_absolute_pos_rot_matrix)
|
|
|
|
attractor_absolute_pos_rot_matrix->rotateVect(attractor_direction);
|
|
|
|
}
|
|
|
|
|
|
|
|
pp.expirationtime = r_exp.pickWithin();
|
|
|
|
|
|
|
|
if (sphere_radius != v3f()) {
|
|
|
|
f32 l = sphere_radius.getLength();
|
|
|
|
v3f mag = sphere_radius;
|
|
|
|
mag.normalize();
|
|
|
|
|
|
|
|
v3f ofs = v3f(l,0,0);
|
|
|
|
ofs.rotateXZBy(myrand_range(0.f,360.f));
|
|
|
|
ofs.rotateYZBy(myrand_range(0.f,360.f));
|
|
|
|
ofs.rotateXYBy(myrand_range(0.f,360.f));
|
|
|
|
|
|
|
|
pp.pos += ofs * mag;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p.attractor_kind != ParticleParamTypes::AttractorKind::none && attract != 0) {
|
|
|
|
v3f dir;
|
|
|
|
f32 dist = 0; /* =0 necessary to silence warning */
|
|
|
|
switch (p.attractor_kind) {
|
|
|
|
case ParticleParamTypes::AttractorKind::none:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ParticleParamTypes::AttractorKind::point: {
|
|
|
|
dist = pp.pos.getDistanceFrom(attractor_origin);
|
|
|
|
dir = pp.pos - attractor_origin;
|
|
|
|
dir.normalize();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ParticleParamTypes::AttractorKind::line: {
|
|
|
|
// https://github.com/minetest/minetest/issues/11505#issuecomment-915612700
|
|
|
|
const auto& lorigin = attractor_origin;
|
|
|
|
v3f ldir = attractor_direction;
|
|
|
|
ldir.normalize();
|
|
|
|
auto origin_to_point = pp.pos - lorigin;
|
|
|
|
auto scalar_projection = origin_to_point.dotProduct(ldir);
|
|
|
|
auto point_on_line = lorigin + (ldir * scalar_projection);
|
|
|
|
|
|
|
|
dist = pp.pos.getDistanceFrom(point_on_line);
|
|
|
|
dir = (point_on_line - pp.pos);
|
|
|
|
dir.normalize();
|
|
|
|
dir *= -1; // flip it around so strength=1 attracts, not repulses
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ParticleParamTypes::AttractorKind::plane: {
|
|
|
|
// https://github.com/minetest/minetest/issues/11505#issuecomment-915612700
|
|
|
|
const v3f& porigin = attractor_origin;
|
|
|
|
v3f normal = attractor_direction;
|
|
|
|
normal.normalize();
|
|
|
|
v3f point_to_origin = porigin - pp.pos;
|
|
|
|
f32 factor = normal.dotProduct(point_to_origin);
|
|
|
|
if (numericAbsolute(factor) == 0.0f) {
|
|
|
|
dir = normal;
|
|
|
|
} else {
|
|
|
|
factor = numericSign(factor);
|
|
|
|
dir = normal * factor;
|
|
|
|
}
|
|
|
|
dist = numericAbsolute(normal.dotProduct(pp.pos - porigin));
|
|
|
|
dir *= -1; // flip it around so strength=1 attracts, not repulses
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
f32 speedTowards = numericAbsolute(attract) * dist;
|
|
|
|
v3f avel = dir * speedTowards;
|
|
|
|
if (attract > 0 && speedTowards > 0) {
|
|
|
|
avel *= -1;
|
|
|
|
if (p.attractor_kill) {
|
|
|
|
// make sure the particle dies after crossing the attractor threshold
|
|
|
|
f32 timeToCenter = dist / speedTowards;
|
|
|
|
if (timeToCenter < pp.expirationtime)
|
|
|
|
pp.expirationtime = timeToCenter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pp.vel += avel;
|
|
|
|
}
|
|
|
|
|
2020-05-22 13:23:25 +02:00
|
|
|
p.copyCommon(pp);
|
2017-10-02 20:40:59 +02:00
|
|
|
|
2022-07-13 11:57:12 +02:00
|
|
|
ClientTexRef texture;
|
2020-05-22 14:17:03 +02:00
|
|
|
v2f texpos, texsize;
|
|
|
|
video::SColor color(0xFFFFFFFF);
|
|
|
|
|
|
|
|
if (p.node.getContent() != CONTENT_IGNORE) {
|
|
|
|
const ContentFeatures &f =
|
|
|
|
m_particlemanager->m_env->getGameDef()->ndef()->get(p.node);
|
2022-07-13 11:57:12 +02:00
|
|
|
if (!ParticleManager::getNodeParticleParams(p.node, f, pp, &texture.ref,
|
2020-05-22 14:17:03 +02:00
|
|
|
texpos, texsize, &color, p.node_tile))
|
|
|
|
return;
|
|
|
|
} else {
|
2022-07-13 11:57:12 +02:00
|
|
|
if (m_texcount == 0)
|
|
|
|
return;
|
|
|
|
texture = decltype(texture)(m_texpool[m_texcount == 1 ? 0 : myrand_range(0,m_texcount-1)]);
|
2020-05-22 14:17:03 +02:00
|
|
|
texpos = v2f(0.0f, 0.0f);
|
|
|
|
texsize = v2f(1.0f, 1.0f);
|
2022-07-13 11:57:12 +02:00
|
|
|
if (texture.tex->animated)
|
|
|
|
pp.animation = texture.tex->animation;
|
|
|
|
}
|
|
|
|
|
|
|
|
// synchronize animation length with particle life if desired
|
|
|
|
if (pp.animation.type != TAT_NONE) {
|
2022-09-04 16:22:12 +02:00
|
|
|
// FIXME: this should be moved into a TileAnimationParams class method
|
2022-07-13 11:57:12 +02:00
|
|
|
if (pp.animation.type == TAT_VERTICAL_FRAMES &&
|
|
|
|
pp.animation.vertical_frames.length < 0) {
|
|
|
|
auto& a = pp.animation.vertical_frames;
|
|
|
|
// we add a tiny extra value to prevent the first frame
|
|
|
|
// from flickering back on just before the particle dies
|
|
|
|
a.length = (pp.expirationtime / -a.length) + 0.1;
|
|
|
|
} else if (pp.animation.type == TAT_SHEET_2D &&
|
|
|
|
pp.animation.sheet_2d.frame_length < 0) {
|
|
|
|
auto& a = pp.animation.sheet_2d;
|
|
|
|
auto frames = a.frames_w * a.frames_h;
|
|
|
|
auto runtime = (pp.expirationtime / -a.frame_length) + 0.1;
|
|
|
|
pp.animation.sheet_2d.frame_length = frames / runtime;
|
|
|
|
}
|
2020-05-22 14:17:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Allow keeping default random size
|
2022-07-13 11:57:12 +02:00
|
|
|
if (p.size.start.max > 0.0f || p.size.end.max > 0.0f)
|
|
|
|
pp.size = r_size.pickWithin();
|
2020-05-22 14:17:03 +02:00
|
|
|
|
2022-07-13 11:57:12 +02:00
|
|
|
++m_active;
|
|
|
|
auto pa = new Particle(
|
2017-10-02 20:40:59 +02:00
|
|
|
m_gamedef,
|
|
|
|
m_player,
|
|
|
|
env,
|
2020-05-22 13:23:25 +02:00
|
|
|
pp,
|
2020-05-22 14:17:03 +02:00
|
|
|
texture,
|
|
|
|
texpos,
|
|
|
|
texsize,
|
|
|
|
color
|
2022-07-13 11:57:12 +02:00
|
|
|
);
|
|
|
|
pa->m_parent = this;
|
|
|
|
m_particlemanager->addParticle(pa);
|
2017-10-02 20:40:59 +02:00
|
|
|
}
|
|
|
|
|
2019-08-17 08:39:40 +02:00
|
|
|
void ParticleSpawner::step(float dtime, ClientEnvironment *env)
|
2013-01-23 18:32:02 +01:00
|
|
|
{
|
|
|
|
m_time += dtime;
|
2016-11-14 15:28:06 +01:00
|
|
|
|
2017-06-11 13:58:43 +02:00
|
|
|
static thread_local const float radius =
|
2017-05-20 16:46:35 +02:00
|
|
|
g_settings->getS16("max_block_send_distance") * MAP_BLOCKSIZE;
|
|
|
|
|
2016-08-04 22:09:21 +02:00
|
|
|
bool unloaded = false;
|
2019-08-09 10:51:17 +02:00
|
|
|
const core::matrix4 *attached_absolute_pos_rot_matrix = nullptr;
|
|
|
|
if (m_attached_id) {
|
2022-07-13 11:57:12 +02:00
|
|
|
if (GenericCAO *attached = env->getGenericCAO(m_attached_id)) {
|
2020-06-19 19:29:47 +02:00
|
|
|
attached_absolute_pos_rot_matrix = attached->getAbsolutePosRotMatrix();
|
2016-10-24 19:56:27 +02:00
|
|
|
} else {
|
2016-08-04 22:09:21 +02:00
|
|
|
unloaded = true;
|
2016-10-24 19:56:27 +02:00
|
|
|
}
|
2016-08-04 22:09:21 +02:00
|
|
|
}
|
|
|
|
|
2020-05-22 13:23:25 +02:00
|
|
|
if (p.time != 0) {
|
2017-10-02 20:40:59 +02:00
|
|
|
// Spawner exists for a predefined timespan
|
2020-05-22 13:23:25 +02:00
|
|
|
for (auto i = m_spawntimes.begin(); i != m_spawntimes.end(); ) {
|
|
|
|
if ((*i) <= m_time && p.amount > 0) {
|
|
|
|
--p.amount;
|
2013-01-23 18:32:02 +01:00
|
|
|
|
2017-05-20 16:46:35 +02:00
|
|
|
// Pretend to, but don't actually spawn a particle if it is
|
|
|
|
// attached to an unloaded object or distant from player.
|
2017-10-02 20:40:59 +02:00
|
|
|
if (!unloaded)
|
2019-08-09 10:51:17 +02:00
|
|
|
spawnParticle(env, radius, attached_absolute_pos_rot_matrix);
|
2017-10-02 20:40:59 +02:00
|
|
|
|
2017-05-20 16:46:35 +02:00
|
|
|
i = m_spawntimes.erase(i);
|
2017-10-02 20:40:59 +02:00
|
|
|
} else {
|
2017-05-20 16:46:35 +02:00
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
2017-10-02 20:40:59 +02:00
|
|
|
} else {
|
|
|
|
// Spawner exists for an infinity timespan, spawn on a per-second base
|
|
|
|
|
2017-05-20 16:46:35 +02:00
|
|
|
// Skip this step if attached to an unloaded object
|
|
|
|
if (unloaded)
|
|
|
|
return;
|
2017-10-02 20:40:59 +02:00
|
|
|
|
2020-05-22 13:23:25 +02:00
|
|
|
for (int i = 0; i <= p.amount; i++) {
|
2022-07-13 11:57:12 +02:00
|
|
|
if (myrand_float() < dtime)
|
2019-08-09 10:51:17 +02:00
|
|
|
spawnParticle(env, radius, attached_absolute_pos_rot_matrix);
|
2013-01-23 18:32:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-22 13:23:25 +02:00
|
|
|
/*
|
|
|
|
ParticleManager
|
|
|
|
*/
|
2015-01-05 18:34:59 +01:00
|
|
|
|
2019-08-17 08:39:40 +02:00
|
|
|
ParticleManager::ParticleManager(ClientEnvironment *env) :
|
2015-01-05 18:34:59 +01:00
|
|
|
m_env(env)
|
|
|
|
{}
|
|
|
|
|
|
|
|
ParticleManager::~ParticleManager()
|
|
|
|
{
|
|
|
|
clearAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleManager::step(float dtime)
|
2013-01-23 18:32:02 +01:00
|
|
|
{
|
2015-01-05 18:34:59 +01:00
|
|
|
stepParticles (dtime);
|
|
|
|
stepSpawners (dtime);
|
|
|
|
}
|
|
|
|
|
2019-08-09 10:51:17 +02:00
|
|
|
void ParticleManager::stepSpawners(float dtime)
|
2015-01-05 18:34:59 +01:00
|
|
|
{
|
2015-04-07 12:13:12 +02:00
|
|
|
MutexAutoLock lock(m_spawner_list_lock);
|
2019-03-01 20:16:11 +01:00
|
|
|
for (auto i = m_particle_spawners.begin(); i != m_particle_spawners.end();) {
|
2022-07-13 11:57:12 +02:00
|
|
|
if (i->second->getExpired()) {
|
|
|
|
// the particlespawner owns the textures, so we need to make
|
|
|
|
// sure there are no active particles before we free it
|
|
|
|
if (i->second->m_active == 0) {
|
|
|
|
delete i->second;
|
|
|
|
m_particle_spawners.erase(i++);
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
2019-03-01 20:16:11 +01:00
|
|
|
} else {
|
2015-01-05 18:34:59 +01:00
|
|
|
i->second->step(dtime, m_env);
|
2015-08-25 22:23:05 +02:00
|
|
|
++i;
|
2013-01-23 18:32:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 10:51:17 +02:00
|
|
|
void ParticleManager::stepParticles(float dtime)
|
2013-01-23 18:32:02 +01:00
|
|
|
{
|
2015-04-07 12:13:12 +02:00
|
|
|
MutexAutoLock lock(m_particle_list_lock);
|
2019-03-01 20:16:11 +01:00
|
|
|
for (auto i = m_particles.begin(); i != m_particles.end();) {
|
|
|
|
if ((*i)->get_expired()) {
|
2022-07-13 11:57:12 +02:00
|
|
|
if ((*i)->m_parent) {
|
|
|
|
assert((*i)->m_parent->m_active != 0);
|
|
|
|
--(*i)->m_parent->m_active;
|
|
|
|
}
|
2015-01-05 18:34:59 +01:00
|
|
|
(*i)->remove();
|
|
|
|
delete *i;
|
|
|
|
i = m_particles.erase(i);
|
2019-03-01 20:16:11 +01:00
|
|
|
} else {
|
2015-01-05 18:34:59 +01:00
|
|
|
(*i)->step(dtime);
|
2015-08-25 22:23:05 +02:00
|
|
|
++i;
|
2015-01-05 18:34:59 +01:00
|
|
|
}
|
2013-01-23 18:32:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 10:51:17 +02:00
|
|
|
void ParticleManager::clearAll()
|
2013-01-23 18:32:02 +01:00
|
|
|
{
|
2015-04-07 12:13:12 +02:00
|
|
|
MutexAutoLock lock(m_spawner_list_lock);
|
|
|
|
MutexAutoLock lock2(m_particle_list_lock);
|
2019-03-01 20:16:11 +01:00
|
|
|
for (auto i = m_particle_spawners.begin(); i != m_particle_spawners.end();) {
|
2013-01-23 18:32:02 +01:00
|
|
|
delete i->second;
|
2015-01-05 18:34:59 +01:00
|
|
|
m_particle_spawners.erase(i++);
|
2013-01-23 18:32:02 +01:00
|
|
|
}
|
|
|
|
|
2019-08-09 10:51:17 +02:00
|
|
|
for(auto i = m_particles.begin(); i != m_particles.end();)
|
2013-01-23 18:32:02 +01:00
|
|
|
{
|
|
|
|
(*i)->remove();
|
|
|
|
delete *i;
|
2015-01-05 18:34:59 +01:00
|
|
|
i = m_particles.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 20:39:22 +01:00
|
|
|
void ParticleManager::handleParticleEvent(ClientEvent *event, Client *client,
|
2017-06-26 20:11:17 +02:00
|
|
|
LocalPlayer *player)
|
2015-01-05 18:34:59 +01:00
|
|
|
{
|
2016-03-30 00:06:47 +02:00
|
|
|
switch (event->type) {
|
|
|
|
case CE_DELETE_PARTICLESPAWNER: {
|
2020-05-22 13:23:25 +02:00
|
|
|
deleteParticleSpawner(event->delete_particlespawner.id);
|
2016-03-30 00:06:47 +02:00
|
|
|
// no allocated memory in delete event
|
|
|
|
break;
|
2015-01-05 18:34:59 +01:00
|
|
|
}
|
2016-03-30 00:06:47 +02:00
|
|
|
case CE_ADD_PARTICLESPAWNER: {
|
2020-05-22 13:23:25 +02:00
|
|
|
deleteParticleSpawner(event->add_particlespawner.id);
|
|
|
|
|
|
|
|
const ParticleSpawnerParameters &p = *event->add_particlespawner.p;
|
2015-01-05 18:34:59 +01:00
|
|
|
|
2022-07-13 11:57:12 +02:00
|
|
|
// texture pool
|
|
|
|
std::unique_ptr<ClientTexture[]> texpool = nullptr;
|
|
|
|
size_t txpsz = 0;
|
|
|
|
if (!p.texpool.empty()) {
|
|
|
|
txpsz = p.texpool.size();
|
|
|
|
texpool = decltype(texpool)(new ClientTexture [txpsz]);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < txpsz; ++i) {
|
|
|
|
texpool[i] = ClientTexture(p.texpool[i], client->tsrc());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// no texpool in use, use fallback texture
|
|
|
|
txpsz = 1;
|
|
|
|
texpool = decltype(texpool)(new ClientTexture[1] {
|
|
|
|
ClientTexture(p.texture, client->tsrc())
|
|
|
|
});
|
|
|
|
}
|
2016-03-30 00:06:47 +02:00
|
|
|
|
2019-03-01 20:16:11 +01:00
|
|
|
auto toadd = new ParticleSpawner(client, player,
|
2020-05-22 13:23:25 +02:00
|
|
|
p,
|
2016-08-04 22:09:21 +02:00
|
|
|
event->add_particlespawner.attached_id,
|
2022-07-13 11:57:12 +02:00
|
|
|
texpool,
|
|
|
|
txpsz,
|
2016-03-30 00:06:47 +02:00
|
|
|
this);
|
|
|
|
|
2020-05-22 13:23:25 +02:00
|
|
|
addParticleSpawner(event->add_particlespawner.id, toadd);
|
|
|
|
|
|
|
|
delete event->add_particlespawner.p;
|
2016-03-30 00:06:47 +02:00
|
|
|
break;
|
2015-01-05 18:34:59 +01:00
|
|
|
}
|
2016-03-30 00:06:47 +02:00
|
|
|
case CE_SPAWN_PARTICLE: {
|
2020-05-22 14:17:03 +02:00
|
|
|
ParticleParameters &p = *event->spawn_particle;
|
2016-03-30 00:06:47 +02:00
|
|
|
|
2022-07-13 11:57:12 +02:00
|
|
|
ClientTexRef texture;
|
2020-05-22 14:17:03 +02:00
|
|
|
v2f texpos, texsize;
|
|
|
|
video::SColor color(0xFFFFFFFF);
|
|
|
|
|
|
|
|
f32 oldsize = p.size;
|
|
|
|
|
|
|
|
if (p.node.getContent() != CONTENT_IGNORE) {
|
|
|
|
const ContentFeatures &f = m_env->getGameDef()->ndef()->get(p.node);
|
2022-07-13 11:57:12 +02:00
|
|
|
getNodeParticleParams(p.node, f, p, &texture.ref, texpos,
|
|
|
|
texsize, &color, p.node_tile);
|
2020-05-22 14:17:03 +02:00
|
|
|
} else {
|
2022-07-13 11:57:12 +02:00
|
|
|
/* with no particlespawner to own the texture, we need
|
|
|
|
* to save it on the heap. it will be freed when the
|
|
|
|
* particle is destroyed */
|
|
|
|
auto texstore = new ClientTexture(p.texture, client->tsrc());
|
|
|
|
|
|
|
|
texture = ClientTexRef(*texstore);
|
2020-05-22 14:17:03 +02:00
|
|
|
texpos = v2f(0.0f, 0.0f);
|
|
|
|
texsize = v2f(1.0f, 1.0f);
|
|
|
|
}
|
2015-01-05 18:34:59 +01:00
|
|
|
|
2020-05-22 14:17:03 +02:00
|
|
|
// Allow keeping default random size
|
|
|
|
if (oldsize > 0.0f)
|
|
|
|
p.size = oldsize;
|
|
|
|
|
2022-07-13 11:57:12 +02:00
|
|
|
if (texture.ref) {
|
2020-05-22 14:17:03 +02:00
|
|
|
Particle *toadd = new Particle(client, player, m_env,
|
|
|
|
p, texture, texpos, texsize, color);
|
|
|
|
|
|
|
|
addParticle(toadd);
|
|
|
|
}
|
2015-01-05 18:34:59 +01:00
|
|
|
|
2020-05-22 13:23:25 +02:00
|
|
|
delete event->spawn_particle;
|
2016-03-30 00:06:47 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: break;
|
2013-12-23 15:11:53 +01:00
|
|
|
}
|
2012-12-31 19:33:36 +01:00
|
|
|
}
|
2015-01-05 18:34:59 +01:00
|
|
|
|
2020-05-22 13:23:25 +02:00
|
|
|
bool ParticleManager::getNodeParticleParams(const MapNode &n,
|
2020-05-22 14:17:03 +02:00
|
|
|
const ContentFeatures &f, ParticleParameters &p, video::ITexture **texture,
|
|
|
|
v2f &texpos, v2f &texsize, video::SColor *color, u8 tilenum)
|
2020-05-22 13:23:25 +02:00
|
|
|
{
|
|
|
|
// No particles for "airlike" nodes
|
|
|
|
if (f.drawtype == NDT_AIRLIKE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Texture
|
2020-05-22 14:17:03 +02:00
|
|
|
u8 texid;
|
|
|
|
if (tilenum > 0 && tilenum <= 6)
|
|
|
|
texid = tilenum - 1;
|
|
|
|
else
|
2022-07-13 11:57:12 +02:00
|
|
|
texid = myrand_range(0,5);
|
2020-05-22 13:23:25 +02:00
|
|
|
const TileLayer &tile = f.tiles[texid].layers[0];
|
|
|
|
p.animation.type = TAT_NONE;
|
|
|
|
|
|
|
|
// Only use first frame of animated texture
|
|
|
|
if (tile.material_flags & MATERIAL_FLAG_ANIMATION)
|
|
|
|
*texture = (*tile.frames)[0].texture;
|
|
|
|
else
|
|
|
|
*texture = tile.texture;
|
|
|
|
|
2022-07-13 11:57:12 +02:00
|
|
|
float size = (myrand_range(0,8)) / 64.0f;
|
2020-05-22 13:23:25 +02:00
|
|
|
p.size = BS * size;
|
|
|
|
if (tile.scale)
|
|
|
|
size /= tile.scale;
|
|
|
|
texsize = v2f(size * 2.0f, size * 2.0f);
|
2022-07-13 11:57:12 +02:00
|
|
|
texpos.X = (myrand_range(0,64)) / 64.0f - texsize.X;
|
|
|
|
texpos.Y = (myrand_range(0,64)) / 64.0f - texsize.Y;
|
2020-05-22 13:23:25 +02:00
|
|
|
|
|
|
|
if (tile.has_color)
|
|
|
|
*color = tile.color;
|
|
|
|
else
|
|
|
|
n.getColor(f, color);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-04 05:13:41 +02:00
|
|
|
// The final burst of particles when a node is finally dug, *not* particles
|
|
|
|
// spawned during the digging of a node.
|
|
|
|
|
2019-08-17 08:39:40 +02:00
|
|
|
void ParticleManager::addDiggingParticles(IGameDef *gamedef,
|
2017-06-26 20:11:17 +02:00
|
|
|
LocalPlayer *player, v3s16 pos, const MapNode &n, const ContentFeatures &f)
|
2015-01-05 18:34:59 +01:00
|
|
|
{
|
2017-09-08 18:39:24 +02:00
|
|
|
// No particles for "airlike" nodes
|
|
|
|
if (f.drawtype == NDT_AIRLIKE)
|
|
|
|
return;
|
|
|
|
|
2018-10-04 05:13:41 +02:00
|
|
|
for (u16 j = 0; j < 16; j++) {
|
2017-06-26 20:11:17 +02:00
|
|
|
addNodeParticle(gamedef, player, pos, n, f);
|
2015-01-05 18:34:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-04 05:13:41 +02:00
|
|
|
// During the digging of a node particles are spawned individually by this
|
|
|
|
// function, called from Game::handleDigging() in game.cpp.
|
|
|
|
|
2019-08-17 08:39:40 +02:00
|
|
|
void ParticleManager::addNodeParticle(IGameDef *gamedef,
|
2017-06-26 20:11:17 +02:00
|
|
|
LocalPlayer *player, v3s16 pos, const MapNode &n, const ContentFeatures &f)
|
2015-01-05 18:34:59 +01:00
|
|
|
{
|
2020-05-22 13:23:25 +02:00
|
|
|
ParticleParameters p;
|
2022-07-13 11:57:12 +02:00
|
|
|
video::ITexture *ref = nullptr;
|
2020-05-22 13:23:25 +02:00
|
|
|
v2f texpos, texsize;
|
|
|
|
video::SColor color;
|
2015-01-05 18:34:59 +01:00
|
|
|
|
2022-07-13 11:57:12 +02:00
|
|
|
if (!getNodeParticleParams(n, f, p, &ref, texpos, texsize, &color))
|
2020-05-22 13:23:25 +02:00
|
|
|
return;
|
2015-01-05 18:34:59 +01:00
|
|
|
|
2022-07-13 11:57:12 +02:00
|
|
|
p.expirationtime = myrand_range(0, 100) / 100.0f;
|
2015-01-05 18:34:59 +01:00
|
|
|
|
|
|
|
// Physics
|
2020-05-22 13:23:25 +02:00
|
|
|
p.vel = v3f(
|
2022-07-13 11:57:12 +02:00
|
|
|
myrand_range(-1.5f,1.5f),
|
|
|
|
myrand_range(0.f,3.f),
|
|
|
|
myrand_range(-1.5f,1.5f)
|
2018-10-04 05:13:41 +02:00
|
|
|
);
|
2020-05-22 13:23:25 +02:00
|
|
|
p.acc = v3f(
|
2018-10-04 05:13:41 +02:00
|
|
|
0.0f,
|
2022-08-12 11:17:02 +02:00
|
|
|
-player->movement_gravity * player->physics_override.gravity / BS,
|
2018-10-04 05:13:41 +02:00
|
|
|
0.0f
|
|
|
|
);
|
2020-05-22 13:23:25 +02:00
|
|
|
p.pos = v3f(
|
2022-07-13 11:57:12 +02:00
|
|
|
(f32)pos.X + myrand_range(0.f, .5f) - .25f,
|
|
|
|
(f32)pos.Y + myrand_range(0.f, .5f) - .25f,
|
|
|
|
(f32)pos.Z + myrand_range(0.f, .5f) - .25f
|
2015-01-05 18:34:59 +01:00
|
|
|
);
|
|
|
|
|
2019-08-17 08:39:40 +02:00
|
|
|
Particle *toadd = new Particle(
|
2015-01-05 18:34:59 +01:00
|
|
|
gamedef,
|
|
|
|
player,
|
|
|
|
m_env,
|
2020-05-22 13:23:25 +02:00
|
|
|
p,
|
2022-07-13 11:57:12 +02:00
|
|
|
ClientTexRef(ref),
|
2015-01-05 18:34:59 +01:00
|
|
|
texpos,
|
2017-01-14 16:48:49 +01:00
|
|
|
texsize,
|
2017-01-12 15:46:30 +01:00
|
|
|
color);
|
2015-01-05 18:34:59 +01:00
|
|
|
|
|
|
|
addParticle(toadd);
|
|
|
|
}
|
|
|
|
|
2022-07-13 11:57:12 +02:00
|
|
|
void ParticleManager::reserveParticleSpace(size_t max_estimate)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(m_particle_list_lock);
|
|
|
|
m_particles.reserve(m_particles.size() + max_estimate);
|
|
|
|
}
|
|
|
|
|
2019-08-17 08:39:40 +02:00
|
|
|
void ParticleManager::addParticle(Particle *toadd)
|
2015-01-05 18:34:59 +01:00
|
|
|
{
|
2015-04-07 12:13:12 +02:00
|
|
|
MutexAutoLock lock(m_particle_list_lock);
|
2015-01-05 18:34:59 +01:00
|
|
|
m_particles.push_back(toadd);
|
|
|
|
}
|
2020-05-22 13:23:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
void ParticleManager::addParticleSpawner(u64 id, ParticleSpawner *toadd)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(m_spawner_list_lock);
|
|
|
|
m_particle_spawners[id] = toadd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleManager::deleteParticleSpawner(u64 id)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(m_spawner_list_lock);
|
|
|
|
auto it = m_particle_spawners.find(id);
|
|
|
|
if (it != m_particle_spawners.end()) {
|
2022-07-13 11:57:12 +02:00
|
|
|
it->second->setDying();
|
2020-05-22 13:23:25 +02:00
|
|
|
}
|
|
|
|
}
|