Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquick3dparticletrailemitter.cpp
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
5
6#include <QtGui/qquaternion.h>
7
9
10/*!
11 \qmltype TrailEmitter3D
12 \inherits ParticleEmitter3D
13 \inqmlmodule QtQuick3D.Particles3D
14 \brief Emitter for logical particles from other particles.
15 \since 6.2
16
17 TrailEmitter3D is a special emitter for emitting particles with starting positions inherited from
18 other logical particles.
19*/
20
21QQuick3DParticleTrailEmitter::QQuick3DParticleTrailEmitter(QQuick3DNode *parent)
22 : QQuick3DParticleEmitter(parent)
23{
24}
25
26/*!
27 \qmlproperty Particle3D TrailEmitter3D::follow
28
29 This property defines the logical particle which this emitter follows.
30 When the TrailEmitter3D emits particles, center position of those particles
31 will become from the \l Particle3D the emitter follows.
32*/
33QQuick3DParticle *QQuick3DParticleTrailEmitter::follow() const
34{
35 return m_follow;
36}
37
38void QQuick3DParticleTrailEmitter::setFollow(QQuick3DParticle *follow)
39{
40 if (m_follow == follow)
41 return;
42
43 m_follow = follow;
44 Q_EMIT followChanged();
45}
46
47/*!
48 \qmlmethod vector3d TrailEmitter3D::burst(int count)
49
50 This method emits \a count amount of particles from this emitter immediately.
51
52 \note TrailEmitter3D doesn't support other bursting methods. Position always comes
53 from the particle defined with the \l follow property.
54*/
55void QQuick3DParticleTrailEmitter::burst(int count)
56{
57 if (!system())
58 return;
59 QQuick3DParticleEmitBurstData burst;
60 burst.time = system()->currentTime();
61 burst.amount = count;
62 m_bursts << burst;
63}
64
65// Returns true if there are any dynamic bursts
66bool QQuick3DParticleTrailEmitter::hasBursts() const
67{
68 bool dynamicBursts = false;
69 for (auto *burst : std::as_const(m_emitBursts)) {
70 if (qobject_cast<QQuick3DParticleDynamicBurst *>(burst)) {
71 dynamicBursts = true;
72 break;
73 }
74 }
75 return !m_bursts.empty() || dynamicBursts;
76}
77
78// Called to emit set of particles
79void QQuick3DParticleTrailEmitter::emitTrailParticles(const QVector3D &centerPos, int emitAmount, int triggerType, const QVector3D &normal, const QVector3D &velocity)
80{
81 if (!system())
82 return;
83
84 if (!enabled())
85 return;
86
87 const int systemTime = system()->currentTime();
88 for (auto particle : std::as_const(m_system->m_particles)) {
89 if (particle == m_particle) {
90 emitAmount += getEmitAmountFromDynamicBursts(triggerType);
91 emitAmount = std::min(emitAmount, int(particle->maxAmount()));
92 float addTime = ((systemTime - m_prevEmitTime) / 1000.0f) / emitAmount;
93 for (int i = 0; i < emitAmount; i++) {
94 // Distribute evenly between previous and current time, important especially
95 // when time has jumped a lot (like a starttime).
96 float startTime = (m_prevEmitTime / 1000.0f) + addTime * float(i);
97 emitParticle(particle, startTime, QMatrix4x4(), QQuaternion(), centerPos, -1, velocity, normal);
98 }
99 // Emit bursts, if any
100 for (auto burst : std::as_const(m_bursts)) {
101 int burstAmount = std::min(burst.amount, int(particle->maxAmount()));
102 float burstTime = float(burst.time / 1000.0f);
103 for (int i = 0; i < burstAmount; i++)
104 emitParticle(particle, burstTime, QMatrix4x4(), QQuaternion(), centerPos, -1, velocity, normal);
105 }
106 }
107 }
108
109 m_prevEmitTime = systemTime;
110}
111
112void QQuick3DParticleTrailEmitter::clearBursts()
113{
114 // After bursts have been emitted, clear the list
115 m_bursts.clear();
116}
117
118QT_END_NAMESPACE