| 1 | // Copyright (C) 2021 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef QQUICK3DPARTICLEEMITTER_H |
| 5 | #define QQUICK3DPARTICLEEMITTER_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtQuick3D/private/qquick3dnode_p.h> |
| 19 | #include <QtQuick3DParticles/private/qquick3dparticledirection_p.h> |
| 20 | #include <QtQuick3DParticles/private/qquick3dparticlesystem_p.h> |
| 21 | #include <QtQuick3DParticles/private/qquick3dparticlemodelparticle_p.h> |
| 22 | #include <QtQuick3DParticles/private/qquick3dparticleabstractshape_p.h> |
| 23 | #include <QtQuick3DParticles/private/qquick3dparticleemitburst_p.h> |
| 24 | #include <QtQuick3DParticles/private/qquick3dparticledynamicburst_p.h> |
| 25 | |
| 26 | QT_BEGIN_NAMESPACE |
| 27 | |
| 28 | class Q_QUICK3DPARTICLES_EXPORT QQuick3DParticleEmitter : public QQuick3DNode |
| 29 | { |
| 30 | Q_OBJECT |
| 31 | Q_PROPERTY(QQuick3DParticleSystem *system READ system WRITE setSystem NOTIFY systemChanged) |
| 32 | Q_PROPERTY(QQmlListProperty<QQuick3DParticleEmitBurst> emitBursts READ emitBursts) |
| 33 | Q_PROPERTY(QQuick3DParticleDirection *velocity READ velocity WRITE setVelocity NOTIFY velocityChanged) |
| 34 | Q_PROPERTY(QQuick3DParticle *particle READ particle WRITE setParticle NOTIFY particleChanged) |
| 35 | Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged) |
| 36 | Q_PROPERTY(QQuick3DParticleAbstractShape *shape READ shape WRITE setShape NOTIFY shapeChanged) |
| 37 | Q_PROPERTY(float emitRate READ emitRate WRITE setEmitRate NOTIFY emitRateChanged) |
| 38 | Q_PROPERTY(int lifeSpan READ lifeSpan WRITE setLifeSpan NOTIFY lifeSpanChanged) |
| 39 | Q_PROPERTY(int lifeSpanVariation READ lifeSpanVariation WRITE setLifeSpanVariation NOTIFY lifeSpanVariationChanged) |
| 40 | Q_PROPERTY(float particleScale READ particleScale WRITE setParticleScale NOTIFY particleScaleChanged) |
| 41 | Q_PROPERTY(float particleEndScale READ particleEndScale WRITE setParticleEndScale NOTIFY particleEndScaleChanged) |
| 42 | Q_PROPERTY(float particleScaleVariation READ particleScaleVariation WRITE setParticleScaleVariation NOTIFY particleScaleVariationChanged) |
| 43 | Q_PROPERTY(float particleEndScaleVariation READ particleEndScaleVariation WRITE setParticleEndScaleVariation NOTIFY particleEndScaleVariationChanged) |
| 44 | Q_PROPERTY(QVector3D particleRotation READ particleRotation WRITE setParticleRotation NOTIFY particleRotationChanged) |
| 45 | Q_PROPERTY(QVector3D particleRotationVariation READ particleRotationVariation WRITE setParticleRotationVariation NOTIFY particleRotationVariationChanged) |
| 46 | Q_PROPERTY(QVector3D particleRotationVelocity READ particleRotationVelocity WRITE setParticleRotationVelocity NOTIFY particleRotationVelocityChanged) |
| 47 | Q_PROPERTY(QVector3D particleRotationVelocityVariation READ particleRotationVelocityVariation WRITE setParticleRotationVelocityVariation NOTIFY particleRotationVariationVelocityChanged) |
| 48 | Q_PROPERTY(float depthBias READ depthBias WRITE setDepthBias NOTIFY depthBiasChanged) |
| 49 | |
| 50 | QML_NAMED_ELEMENT(ParticleEmitter3D) |
| 51 | QML_ADDED_IN_VERSION(6, 2) |
| 52 | |
| 53 | public: |
| 54 | QQuick3DParticleEmitter(QQuick3DNode *parent = nullptr); |
| 55 | ~QQuick3DParticleEmitter() override; |
| 56 | |
| 57 | bool enabled() const; |
| 58 | QQuick3DParticleDirection *velocity() const; |
| 59 | QQuick3DParticleSystem *system() const; |
| 60 | float emitRate() const; |
| 61 | float particleScale() const; |
| 62 | float particleEndScale() const; |
| 63 | float particleScaleVariation() const; |
| 64 | float particleEndScaleVariation() const; |
| 65 | int lifeSpan() const; |
| 66 | int lifeSpanVariation() const; |
| 67 | QQuick3DParticle *particle() const; |
| 68 | QQuick3DParticleAbstractShape *shape() const; |
| 69 | QVector3D particleRotation() const; |
| 70 | QVector3D particleRotationVariation() const; |
| 71 | QVector3D particleRotationVelocity() const; |
| 72 | QVector3D particleRotationVelocityVariation() const; |
| 73 | float depthBias() const; |
| 74 | |
| 75 | QQmlListProperty<QQuick3DParticleEmitBurst> emitBursts(); |
| 76 | |
| 77 | Q_INVOKABLE virtual void burst(int count); |
| 78 | Q_INVOKABLE virtual void burst(int count, int duration); |
| 79 | Q_INVOKABLE virtual void burst(int count, int duration, const QVector3D &position); |
| 80 | |
| 81 | public Q_SLOTS: |
| 82 | void setEnabled(bool enabled); |
| 83 | void setVelocity(QQuick3DParticleDirection *velocity); |
| 84 | void setSystem(QQuick3DParticleSystem *system); |
| 85 | void setEmitRate(float emitRate); |
| 86 | void setParticleScale(float particleScale); |
| 87 | void setParticleEndScale(float particleEndScale); |
| 88 | void setParticleScaleVariation(float particleScaleVariation); |
| 89 | void setParticleEndScaleVariation(float particleEndScaleVariation); |
| 90 | void setLifeSpan(int lifeSpan); |
| 91 | void setLifeSpanVariation(int lifeSpanVariation); |
| 92 | void setParticle(QQuick3DParticle *particle); |
| 93 | void setShape(QQuick3DParticleAbstractShape *shape); |
| 94 | void setParticleRotation(const QVector3D &particleRotation); |
| 95 | void setParticleRotationVariation(const QVector3D &particleRotationVariation); |
| 96 | void setParticleRotationVelocity(const QVector3D &particleRotationVelocity); |
| 97 | void setParticleRotationVelocityVariation(const QVector3D &particleRotationVelocityVariation); |
| 98 | void setDepthBias(float bias); |
| 99 | |
| 100 | Q_SIGNALS: |
| 101 | void velocityChanged(); |
| 102 | void systemChanged(); |
| 103 | void emitRateChanged(); |
| 104 | void particleScaleChanged(); |
| 105 | void particleEndScaleChanged(); |
| 106 | void particleScaleVariationChanged(); |
| 107 | void particleEndScaleVariationChanged(); |
| 108 | void lifeSpanChanged(); |
| 109 | void lifeSpanVariationChanged(); |
| 110 | void particleChanged(); |
| 111 | void shapeChanged(); |
| 112 | void particleRotationChanged(); |
| 113 | void particleRotationVariationChanged(); |
| 114 | void particleRotationVelocityChanged(); |
| 115 | void particleRotationVariationVelocityChanged(); |
| 116 | void enabledChanged(); |
| 117 | void depthBiasChanged(); |
| 118 | |
| 119 | protected: |
| 120 | friend class QQuick3DParticleSystem; |
| 121 | friend class QQuick3DParticleEmitBurst; |
| 122 | friend class QQuick3DParticleTrailEmitter; |
| 123 | |
| 124 | void componentComplete() override; |
| 125 | void registerEmitBurst(QQuick3DParticleEmitBurst *emitBurst); |
| 126 | void unRegisterEmitBurst(QQuick3DParticleEmitBurst *emitBurst); |
| 127 | void generateEmitBursts(); |
| 128 | void emitParticle(QQuick3DParticle *particle, float startTime, const QMatrix4x4 &transform, const QQuaternion &parentRotation, const QVector3D ¢erPos, int index = -1); |
| 129 | void emitParticles(); |
| 130 | void emitActivationNodeParticles(QQuick3DParticleModelBlendParticle *particle); |
| 131 | void emitParticlesBurst(const QQuick3DParticleEmitBurstData &burst); |
| 132 | int getEmitAmount(); |
| 133 | int getEmitAmountFromDynamicBursts(int triggerType = 0); |
| 134 | |
| 135 | void reset(); |
| 136 | |
| 137 | // EmitBursts - list handling |
| 138 | void appendEmitBurst(QQuick3DParticleEmitBurst *); |
| 139 | qsizetype emitBurstCount() const; |
| 140 | QQuick3DParticleEmitBurst *emitBurst(qsizetype) const; |
| 141 | void clearEmitBursts(); |
| 142 | void replaceEmitBurst(qsizetype, QQuick3DParticleEmitBurst *); |
| 143 | void removeLastEmitBurst(); |
| 144 | |
| 145 | // EmitBursts - static |
| 146 | static void appendEmitBurst(QQmlListProperty<QQuick3DParticleEmitBurst> *, QQuick3DParticleEmitBurst *); |
| 147 | static qsizetype emitBurstCount(QQmlListProperty<QQuick3DParticleEmitBurst> *); |
| 148 | static QQuick3DParticleEmitBurst *emitBurst(QQmlListProperty<QQuick3DParticleEmitBurst> *, qsizetype); |
| 149 | static void clearEmitBursts(QQmlListProperty<QQuick3DParticleEmitBurst> *); |
| 150 | static void replaceEmitBurst(QQmlListProperty<QQuick3DParticleEmitBurst> *, qsizetype, QQuick3DParticleEmitBurst *); |
| 151 | static void removeLastEmitBurst(QQmlListProperty<QQuick3DParticleEmitBurst> *); |
| 152 | |
| 153 | private: |
| 154 | struct BurstEmitData |
| 155 | { |
| 156 | int startTime; |
| 157 | int endTime; |
| 158 | int emitAmount; |
| 159 | int emitCounter = 0; |
| 160 | int prevBurstTime; |
| 161 | }; |
| 162 | QQuick3DParticleDirection *m_velocity = nullptr; |
| 163 | QQuick3DParticleSystem *m_system = nullptr; |
| 164 | float m_emitRate = 0.0f; |
| 165 | // Time in ms when emitting last time happened |
| 166 | int m_prevEmitTime = 0; |
| 167 | // Time in ms when dynamic burst last time happened |
| 168 | int m_prevBurstTime = 0; |
| 169 | float m_particleScale = 1.0f; |
| 170 | float m_particleEndScale = -1.0f; |
| 171 | float m_particleScaleVariation = 0.0f; |
| 172 | float m_particleEndScaleVariation = -1.0f; |
| 173 | int m_lifeSpan = 1000; |
| 174 | int m_lifeSpanVariation = 0; |
| 175 | float m_unemittedF = 0.0f; |
| 176 | float m_depthBias = 0.0f; |
| 177 | QQuick3DParticle *m_particle = nullptr; |
| 178 | QQuick3DParticleAbstractShape *m_shape = nullptr; |
| 179 | QVector3D m_particleRotation; |
| 180 | QVector3D m_particleRotationVariation; |
| 181 | QVector3D m_particleRotationVelocity; |
| 182 | QVector3D m_particleRotationVelocityVariation; |
| 183 | bool m_enabled = true; |
| 184 | const QQuick3DParticleData m_clearData = {}; |
| 185 | bool m_burstGenerated = false; |
| 186 | QQuick3DNode *m_systemSharedParent = nullptr; |
| 187 | // This list contains all emit bursts (both QQuick3DParticleEmitBurst and QQuick3DParticleDynamicBurst) |
| 188 | QList<QQuick3DParticleEmitBurst *> m_emitBursts; |
| 189 | QList<BurstEmitData> m_burstEmitData; |
| 190 | }; |
| 191 | |
| 192 | QT_END_NAMESPACE |
| 193 | |
| 194 | #endif // QQUICK3DPARTICLEEMITTER_H |
| 195 | |