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