1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
|
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include "qquick3dparticleemitter_p.h"
#include "qquick3dparticlemodelparticle_p.h"
#include "qquick3dparticlerandomizer_p.h"
#include "qquick3dparticleutils_p.h"
#include "qquick3dparticlespritesequence_p.h"
#include "qquick3dparticlemodelblendparticle_p.h"
QT_BEGIN_NAMESPACE
/*!
\qmltype ParticleEmitter3D
\inherits Node
\inqmlmodule QtQuick3D.Particles3D
\brief Emitter for logical particles.
\since 6.2
This element emits logical particles into the \l ParticleSystem3D, with the given starting attributes.
At least one emitter is required to have particles in the \l ParticleSystem3D. There are a few different
ways to control the emitting amount:
\list
\li Set the \l emitRate which controls how many particles per second get emitted continuously.
\li Add \l EmitBurst3D elements into emitBursts property to emit bursts declaratively.
\li Call any of the \l burst() methods to emit bursts immediately.
\endlist
*/
template <typename T,
typename std::enable_if_t<std::is_signed_v<T>, bool> = true,
typename std::enable_if_t<std::is_arithmetic_v<T>, bool> = true>
static constexpr T qSign(T val)
{
return val < T(0) ? T(-1) : T(1);
}
QQuick3DParticleEmitter::QQuick3DParticleEmitter(QQuick3DNode *parent)
: QQuick3DNode(parent)
{
}
QQuick3DParticleEmitter::~QQuick3DParticleEmitter()
{
qDeleteAll(m_emitBursts);
m_emitBursts.clear();
if (m_system)
m_system->unRegisterParticleEmitter(this);
}
/*!
\qmlproperty bool ParticleEmitter3D::enabled
If enabled is set to \c false, this emitter will not emit any particles.
Usually this is used to conditionally turn an emitter on or off.
If you want to continue emitting burst, keep \l emitRate at 0 instead of
toggling this to \c false.
The default value is \c true.
*/
bool QQuick3DParticleEmitter::enabled() const
{
return m_enabled;
}
void QQuick3DParticleEmitter::setEnabled(bool enabled)
{
if (m_enabled == enabled)
return;
if (enabled && m_system) {
// When enabling, we need to reset the
// previous emit time as it might be a long time ago.
m_prevEmitTime = m_system->currentTime();
m_prevBurstTime = m_prevEmitTime;
}
m_enabled = enabled;
Q_EMIT enabledChanged();
}
/*!
\qmlproperty Direction3D ParticleEmitter3D::velocity
This property can be used to set a starting velocity for emitted particles.
If velocity is not set, particles start motionless and velocity comes from
\l {Affector3D}{affectors} if they are used.
*/
QQuick3DParticleDirection *QQuick3DParticleEmitter::velocity() const
{
return m_velocity;
}
void QQuick3DParticleEmitter::setVelocity(QQuick3DParticleDirection *velocity)
{
if (m_velocity == velocity)
return;
m_velocity = velocity;
if (m_velocity && m_system)
m_velocity->m_system = m_system;
Q_EMIT velocityChanged();
}
/*!
\qmlproperty ParticleSystem3D ParticleEmitter3D::system
This property defines the \l ParticleSystem3D for the emitter. If system is direct parent of the emitter,
this property does not need to be defined.
*/
QQuick3DParticleSystem *QQuick3DParticleEmitter::system() const
{
return m_system;
}
void QQuick3DParticleEmitter::setSystem(QQuick3DParticleSystem *system)
{
if (m_system == system)
return;
if (m_system)
m_system->unRegisterParticleEmitter(this);
m_system = system;
if (m_system) {
m_system->registerParticleEmitter(this);
// Reset prev emit time to time of the new system
m_prevEmitTime = m_system->currentTime();
m_prevBurstTime = m_prevEmitTime;
}
if (m_particle)
m_particle->setSystem(m_system);
if (m_shape)
m_shape->m_system = m_system;
if (m_velocity)
m_velocity->m_system = m_system;
m_systemSharedParent = getSharedParentNode(this, m_system);
Q_EMIT systemChanged();
}
/*!
\qmlproperty real ParticleEmitter3D::emitRate
This property defines the constant emitting rate in particles per second.
For example, if the emitRate is 120 and system animates at 60 frames per
second, 2 new particles are emitted at every frame.
The default value is \c 0.
*/
float QQuick3DParticleEmitter::emitRate() const
{
return m_emitRate;
}
void QQuick3DParticleEmitter::setEmitRate(float emitRate)
{
if (qFuzzyCompare(m_emitRate, emitRate))
return;
if (m_emitRate == 0 && m_system) {
// When changing emit rate from 0 we need to reset
// previous emit time as it may be long time ago
m_prevEmitTime = m_system->currentTime();
}
m_emitRate = emitRate;
Q_EMIT emitRateChanged();
}
/*!
\qmlproperty real ParticleEmitter3D::particleScale
This property defines the scale multiplier of the particles at the beginning.
To have variation in the particle sizes, use \l particleScaleVariation.
The default value is \c 1.0.
\sa particleEndScale, particleScaleVariation
*/
float QQuick3DParticleEmitter::particleScale() const
{
return m_particleScale;
}
void QQuick3DParticleEmitter::setParticleScale(float particleScale)
{
if (qFuzzyCompare(m_particleScale, particleScale))
return;
m_particleScale = particleScale;
Q_EMIT particleScaleChanged();
}
/*!
\qmlproperty real ParticleEmitter3D::particleEndScale
This property defines the scale multiplier of the particles at the end
of particle \l lifeSpan. To have variation in the particle end sizes, use
\l particleEndScaleVariation. When the value is negative, end scale is the
same as the \l particleScale, so scale doesn't change during the particle
\l lifeSpan.
The default value is \c -1.0.
\sa particleScale, particleScaleVariation
*/
float QQuick3DParticleEmitter::particleEndScale() const
{
return m_particleEndScale;
}
void QQuick3DParticleEmitter::setParticleEndScale(float particleEndScale)
{
if (qFuzzyCompare(m_particleEndScale, particleEndScale))
return;
m_particleEndScale = particleEndScale;
Q_EMIT particleEndScaleChanged();
}
/*!
\qmlproperty real ParticleEmitter3D::particleScaleVariation
This property defines the scale variation of the particles. For example, to
emit particles at scale 0.5 - 1.5:
\qml
ParticleEmitter3D {
...
particleScale: 1.0
particleScaleVariation: 0.5
}
\endqml
The default value is \c 0.0.
\sa particleScale, particleEndScaleVariation
*/
float QQuick3DParticleEmitter::particleScaleVariation() const
{
return m_particleScaleVariation;
}
void QQuick3DParticleEmitter::setParticleScaleVariation(float particleScaleVariation)
{
if (qFuzzyCompare(m_particleScaleVariation, particleScaleVariation))
return;
m_particleScaleVariation = particleScaleVariation;
Q_EMIT particleScaleVariationChanged();
}
/*!
\qmlproperty real ParticleEmitter3D::particleEndScaleVariation
This property defines the scale variation of the particles in the end.
When the value is negative, \l particleScaleVariation is used also for the
end scale. For example, to emit particles which start at scale 0.5 - 1.5 and end
at scale 1.0 - 5.0:
\qml
ParticleEmitter3D {
...
particleScale: 1.0
particleScaleVariation: 0.5
particleEndScale: 3.0
particleEndScaleVariation: 2.0
}
\endqml
The default value is \c -1.0.
\sa particleEndScale
*/
float QQuick3DParticleEmitter::particleEndScaleVariation() const
{
return m_particleEndScaleVariation;
}
void QQuick3DParticleEmitter::setParticleEndScaleVariation(float particleEndScaleVariation)
{
if (qFuzzyCompare(m_particleEndScaleVariation, particleEndScaleVariation))
return;
m_particleEndScaleVariation = particleEndScaleVariation;
Q_EMIT particleEndScaleVariationChanged();
}
/*!
\qmlproperty int ParticleEmitter3D::lifeSpan
This property defines the lifespan of a single particle in milliseconds.
The default value is \c 1000.
\sa lifeSpanVariation
*/
int QQuick3DParticleEmitter::lifeSpan() const
{
return m_lifeSpan;
}
void QQuick3DParticleEmitter::setLifeSpan(int lifeSpan)
{
if (m_lifeSpan == lifeSpan)
return;
m_lifeSpan = lifeSpan;
Q_EMIT lifeSpanChanged();
}
/*!
\qmlproperty int ParticleEmitter3D::lifeSpanVariation
This property defines the lifespan variation of a single particle in milliseconds.
For example, to emit particles which will exist between 3 and 4 seconds:
\qml
ParticleEmitter3D {
...
lifeSpan: 3500
lifeSpanVariation: 500
}
\endqml
The default value is \c 0.
\sa lifeSpan
*/
int QQuick3DParticleEmitter::lifeSpanVariation() const
{
return m_lifeSpanVariation;
}
void QQuick3DParticleEmitter::setLifeSpanVariation(int lifeSpanVariation)
{
if (m_lifeSpanVariation == lifeSpanVariation)
return;
m_lifeSpanVariation = lifeSpanVariation;
Q_EMIT lifeSpanVariationChanged();
}
/*!
\qmlproperty Particle3D ParticleEmitter3D::particle
This property defines the logical particle which this emitter emits.
Emitter must have a particle defined, or it doesn't emit anything.
Particle can be either \l SpriteParticle3D or \l ModelParticle3D.
*/
QQuick3DParticle *QQuick3DParticleEmitter::particle() const
{
return m_particle;
}
void QQuick3DParticleEmitter::setParticle(QQuick3DParticle *particle)
{
if (m_particle == particle)
return;
if (particle && particle->system() != nullptr && m_system && particle->system() != m_system) {
qWarning("ParticleEmitter3D: Emitter and Particle must be in the same system.");
return;
}
if (m_particle && m_system && !m_system->isShared(m_particle))
m_particle->setSystem(nullptr);
m_particle = particle;
if (particle) {
particle->setDepthBias(m_depthBias);
particle->setSystem(system());
QObject::connect(this, &QQuick3DParticleEmitter::depthBiasChanged, m_particle, [this]() {
m_particle->setDepthBias(m_depthBias);
});
}
Q_EMIT particleChanged();
}
/*!
\qmlproperty ParticleAbstractShape3D ParticleEmitter3D::shape
This property defines optional shape for the emitting area. It can be either
\l ParticleShape3D or \l ParticleModelShape3D. Shape is scaled,
positioned and rotated based on the emitter node properties. When the Shape
\l {ParticleShape3D::fill}{fill} property is set to false, emitting happens
only from the surface of the shape.
When the shape is not defined, emitting is done from the center point of the
emitter node.
*/
QQuick3DParticleAbstractShape *QQuick3DParticleEmitter::shape() const
{
return m_shape;
}
void QQuick3DParticleEmitter::setShape(QQuick3DParticleAbstractShape *shape)
{
if (m_shape == shape)
return;
m_shape = shape;
if (m_shape && m_system)
m_shape->m_system = m_system;
Q_EMIT shapeChanged();
}
/*!
\qmlproperty vector3d ParticleEmitter3D::particleRotation
This property defines the rotation of the particles in the beginning.
Rotation is defined as degrees in euler angles.
\sa particleRotationVariation
*/
QVector3D QQuick3DParticleEmitter::particleRotation() const
{
return m_particleRotation;
}
void QQuick3DParticleEmitter::setParticleRotation(const QVector3D &particleRotation)
{
if (m_particleRotation == particleRotation)
return;
m_particleRotation = particleRotation;
Q_EMIT particleRotationChanged();
}
/*!
\qmlproperty vector3d ParticleEmitter3D::particleRotationVariation
This property defines the rotation variation of the particles in the beginning.
Rotation variation is defined as degrees in euler angles.
For example, to emit particles in fully random rotations:
\qml
ParticleEmitter3D {
...
particleRotationVariation: Qt.vector3d(180, 180, 180)
}
\endqml
\sa particleRotation
*/
QVector3D QQuick3DParticleEmitter::particleRotationVariation() const
{
return m_particleRotationVariation;
}
void QQuick3DParticleEmitter::setParticleRotationVariation(const QVector3D &particleRotationVariation)
{
if (m_particleRotationVariation == particleRotationVariation)
return;
m_particleRotationVariation = particleRotationVariation;
Q_EMIT particleRotationVariationChanged();
}
/*!
\qmlproperty vector3d ParticleEmitter3D::particleRotationVelocity
This property defines the rotation velocity of the particles in the beginning.
Rotation velocity is defined as degrees per second in euler angles.
\sa particleRotationVelocityVariation
*/
QVector3D QQuick3DParticleEmitter::particleRotationVelocity() const
{
return m_particleRotationVelocity;
}
void QQuick3DParticleEmitter::setParticleRotationVelocity(const QVector3D &particleRotationVelocity)
{
if (m_particleRotationVelocity == particleRotationVelocity)
return;
m_particleRotationVelocity = particleRotationVelocity;
Q_EMIT particleRotationVelocityChanged();
}
/*!
\qmlproperty vector3d ParticleEmitter3D::particleRotationVelocityVariation
This property defines the rotation velocity variation of the particles.
Rotation velocity variation is defined as degrees per second in euler angles.
For example, to emit particles in random rotations which have random rotation
velocity between -100 and 100 degrees per second into any directions:
\qml
ParticleEmitter3D {
...
particleRotationVariation: Qt.vector3d(180, 180, 180)
particleRotationVelocityVariation: Qt.vector3d(100, 100, 100)
}
\endqml
\sa particleRotationVelocity
*/
QVector3D QQuick3DParticleEmitter::particleRotationVelocityVariation() const
{
return m_particleRotationVelocityVariation;
}
void QQuick3DParticleEmitter::setParticleRotationVelocityVariation(const QVector3D &particleRotationVelocityVariation)
{
if (m_particleRotationVelocityVariation == particleRotationVelocityVariation)
return;
m_particleRotationVelocityVariation = particleRotationVelocityVariation;
Q_EMIT particleRotationVariationVelocityChanged();
}
/*!
\qmlproperty real ParticleEmitter3D::depthBias
Holds the depth bias of the emitter. Depth bias is added to the object distance from camera when sorting
objects. This can be used to force rendering order between objects close to each other, that
might otherwise be rendered in different order in different frames. Negative values cause the
sorting value to move closer to the camera while positive values move it further from the camera.
*/
float QQuick3DParticleEmitter::depthBias() const
{
return m_depthBias;
}
void QQuick3DParticleEmitter::setDepthBias(float bias)
{
if (qFuzzyCompare(bias, m_depthBias))
return;
m_depthBias = bias;
emit depthBiasChanged();
}
/*!
\qmlproperty bool ParticleEmitter3D::reversed
\since 6.10
This property reverses the particle time i.e. emitted particles are run from
the end time to the start time.
\default false
*/
bool QQuick3DParticleEmitter::reversed() const
{
return m_reversed;
}
void QQuick3DParticleEmitter::setReversed(bool reversed)
{
if (m_reversed == reversed)
return;
m_reversed = reversed;
emit reversedChanged();
}
/*!
\qmlproperty EmitType ParticleEmitter3D::emitType
\since 6.10
This property defines the type of the shape.
\default ParticleEmitter3D.Default
*/
/*!
\qmlproperty enumeration ParticleEmitter3D::EmitType
\since 6.10
Defines the emit type of the emitter with shape.
\value ParticleEmitter3D.Default
Default emit behavior.
\value ParticleEmitter3D.SurfaceNormal
The particles are emitted along the surface normal. Requires a particle shape.
If the emitter is a trail emitter, the surface normal is inherited from the followed particle.
\value ParticleEmitter3D.SurfaceReflected
The particles are emitted along the reflection of the velocity vector and the surface normal.
If the emitter is a trail emitter, the surface normal and the velocity are inherited from the followed particle.
Requires particle shape.
*/
QQuick3DParticleEmitter::EmitMode QQuick3DParticleEmitter::emitMode() const
{
return m_emitMode;
}
void QQuick3DParticleEmitter::setEmitMode(EmitMode mode)
{
if (m_emitMode == mode)
return;
m_emitMode = mode;
emit emitModeChanged();
}
// Called to reset when system stop/continue
void QQuick3DParticleEmitter::reset()
{
m_prevEmitTime = 0;
m_unemittedF = 0.0f;
m_prevBurstTime = 0;
m_burstEmitData.clear();
}
/*!
\qmlmethod vector3d ParticleEmitter3D::burst(int count)
This method emits \a count amount of particles from this emitter immediately.
*/
void QQuick3DParticleEmitter::burst(int count)
{
burst(count, 0, QVector3D());
}
/*!
\qmlmethod vector3d ParticleEmitter3D::burst(int count, int duration)
This method emits \a count amount of particles from this emitter during the
next \a duration milliseconds.
*/
void QQuick3DParticleEmitter::burst(int count, int duration)
{
burst(count, duration, QVector3D());
}
/*!
\qmlmethod vector3d ParticleEmitter3D::burst(int count, int duration, vector3d position)
This method emits \a count amount of particles from this emitter during the
next \a duration milliseconds. The particles are emitted as if the emitter was
at \a position but all other properties are the same.
*/
void QQuick3DParticleEmitter::burst(int count, int duration, const QVector3D &position)
{
if (!m_system)
return;
QQuick3DParticleEmitBurstData burst;
burst.time = m_system->currentTime();
burst.amount = count;
burst.duration = duration;
burst.position = position;
emitParticlesBurst(burst);
}
void QQuick3DParticleEmitter::generateEmitBursts()
{
if (!m_system)
return;
if (!m_particle)
return;
if (m_emitBursts.isEmpty()) {
m_burstGenerated = true;
return;
}
// Generating burst causes all particle data reseting
// as bursts take first particles in the list.
m_particle->reset();
// TODO: In trail emitter case centerPos should be calculated
// taking into account each particle position at emitburst time
QMatrix4x4 transform = calculateParticleTransform(parentNode(), m_systemSharedParent);
QQuaternion rotation = calculateParticleRotation(parentNode(), m_systemSharedParent);
QVector3D centerPos = position();
for (auto emitBurst : std::as_const(m_emitBursts)) {
// Ignore all dynamic bursts here
if (qobject_cast<QQuick3DParticleDynamicBurst *>(emitBurst))
continue;
int emitAmount = emitBurst->amount();
if (emitAmount <= 0)
return;
// Distribute start times between burst time and time+duration.
float startTime = float(emitBurst->time() / 1000.0f);
float timeStep = float(emitBurst->duration() / 1000.0f) / emitAmount;
for (int i = 0; i < emitAmount; i++) {
emitParticle(m_particle, startTime, transform, rotation, centerPos);
startTime += timeStep;
}
// Increase burst index (for statically allocated particles)
m_particle->updateBurstIndex(emitBurst->amount());
}
m_burstGenerated = true;
}
void QQuick3DParticleEmitter::registerEmitBurst(QQuick3DParticleEmitBurst* emitBurst)
{
m_emitBursts.removeAll(emitBurst);
m_emitBursts << emitBurst;
m_burstGenerated = false;
}
void QQuick3DParticleEmitter::unRegisterEmitBurst(QQuick3DParticleEmitBurst* emitBurst)
{
m_emitBursts.removeAll(emitBurst);
m_burstGenerated = false;
}
static QMatrix4x4 rotationFromNormal(const QVector3D &n)
{
static QMatrix4x4 s_cached;
static QVector3D s_cachedN;
if (qFuzzyCompare(s_cachedN.x(), n.x()) && qFuzzyCompare(s_cachedN.y(), n.y()) && qFuzzyCompare(s_cachedN.z(), n.z()))
return s_cached;
QVector3D b;
QVector3D t;
float values[16];
if (qAbs(n.y()) < 1.0f - 0.0001f) {
t = QVector3D::crossProduct(n, QVector3D(0, qSign(n.y()), 0));
b = QVector3D::crossProduct(n, t);
} else {
b = QVector3D(1, 0, 0);
t = QVector3D(0, 0, 1);
}
values[0] = b.x();
values[1] = b.y();
values[2] = b.z();
values[3] = 0.0;
values[4] = t.x();
values[5] = t.y();
values[6] = t.z();
values[7] = 0.0;
values[8] = n.x();
values[9] = n.y();
values[10] = n.z();
values[11] = 0.0;
values[12] = 0.0;
values[13] = 0.0;
values[14] = 0.0;
values[15] = 1.0;
QMatrix4x4 ret = QMatrix4x4(values);
s_cached = ret.transposed();
s_cachedN = n;
return s_cached;
}
static QVector3D reflect(const QVector3D &I, QVector3D &N)
{
return I - 2.0 * QVector3D::dotProduct(N, I) * N;
}
void QQuick3DParticleEmitter::emitParticle(QQuick3DParticle *particle, float startTime, const QMatrix4x4 &transform,
const QQuaternion &parentRotation, const QVector3D ¢erPos, int index,
const QVector3D &velocity, const QVector3D &normal)
{
if (!m_system)
return;
auto rand = m_system->rand();
QQuick3DParticleModelBlendParticle *mbp = qobject_cast<QQuick3DParticleModelBlendParticle *>(particle);
if (mbp && mbp->lastParticle())
return;
int particleDataIndex = index == -1 ? particle->nextCurrentIndex(this) : index;
if (index == -1 && mbp && mbp->emitMode() == QQuick3DParticleModelBlendParticle::Random)
particleDataIndex = mbp->randomIndex(particleDataIndex);
auto d = &particle->m_particleData[particleDataIndex];
int particleIdIndex = m_system->m_particleIdIndex++;
if (m_system->m_particleIdIndex == INT_MAX)
m_system->m_particleIdIndex = 0;
*d = m_clearData; // Reset the data as it might be reused
d->index = particleIdIndex;
d->startTime = startTime;
d->surfaceNormal = normal;
// Life time in seconds
float lifeSpanMs = m_lifeSpanVariation / 1000.0f;
float lifeSpanVariationMs = lifeSpanMs - 2.0f * rand->get(particleIdIndex, QPRand::LifeSpanV) * lifeSpanMs;
d->lifetime = (m_lifeSpan / 1000.0f) + lifeSpanVariationMs;
// Size
float sVar = m_particleScaleVariation - 2.0f * rand->get(particleIdIndex, QPRand::ScaleV) * m_particleScaleVariation;
float endScale = (m_particleEndScale < 0.0f) ? m_particleScale : m_particleEndScale;
float sEndVar = (m_particleEndScaleVariation < 0.0f)
? sVar
: m_particleEndScaleVariation - 2.0f * rand->get(particleIdIndex, QPRand::ScaleEV) * m_particleEndScaleVariation;
d->startSize = std::max(0.0f, float(m_particleScale + sVar));
d->endSize = std::max(0.0f, float(endScale + sEndVar));
// Emiting area/shape
bool normalBasedVelocity = false;
if (mbp && mbp->modelBlendMode() != QQuick3DParticleModelBlendParticle::Construct) {
// We emit from model position unless in construct mode
d->startPosition = mbp->particleCenter(particleDataIndex);
} else {
// When shape is not set, default to node center point.
QVector3D pos = centerPos;
if (m_shape) {
pos += m_shape->getPosition(particleIdIndex);
QVariant fill = m_shape->property("fill");
if (fill.isValid() && !fill.toBool()) {
const auto n = m_shape->getSurfaceNormal(particleIdIndex);
d->surfaceNormal = n;
if (m_emitMode != EmitMode::Default)
normalBasedVelocity = true;
}
} else if (!normal.isNull() && m_emitMode != EmitMode::Default) {
normalBasedVelocity = true;
}
d->startPosition = transform.map(pos);
}
// Velocity
// Rotate velocity based on parent node rotation and emitter rotation
if (m_velocity)
d->startVelocity = parentRotation * rotation() * m_velocity->sample(*d);
if (normalBasedVelocity) {
if (m_emitMode == EmitMode::SurfaceNormal)
d->startVelocity = rotationFromNormal(d->surfaceNormal).mapVector(d->startVelocity);
else if (!velocity.isNull()) // EmitMode::SurfaceReflected
d->startVelocity = rotationFromNormal(-reflect(velocity, d->surfaceNormal)).mapVector(d->startVelocity);
else
d->startVelocity = rotationFromNormal(-reflect(d->startVelocity.normalized(), d->surfaceNormal)).mapVector(d->startVelocity);
}
// Rotation
if (!m_particleRotation.isNull() || !m_particleRotationVariation.isNull()) {
Vector3b rot;
constexpr float step = 127.0f / 360.0f; // +/- 360-degrees as qint8 (-127..127)
rot.x = m_particleRotation.x() * step;
rot.y = m_particleRotation.y() * step;
rot.z = m_particleRotation.z() * step;
rot.x += (m_particleRotationVariation.x() - 2.0f * rand->get(particleIdIndex, QPRand::RotXV) * m_particleRotationVariation.x()) * step;
rot.y += (m_particleRotationVariation.y() - 2.0f * rand->get(particleIdIndex, QPRand::RotYV) * m_particleRotationVariation.y()) * step;
rot.z += (m_particleRotationVariation.z() - 2.0f * rand->get(particleIdIndex, QPRand::RotZV) * m_particleRotationVariation.z()) * step;
d->startRotation = rot;
}
// Rotation velocity
if (!m_particleRotationVelocity.isNull() || !m_particleRotationVelocityVariation.isNull()) {
float rotVelX = m_particleRotationVelocity.x();
float rotVelY = m_particleRotationVelocity.y();
float rotVelZ = m_particleRotationVelocity.z();
rotVelX += (m_particleRotationVelocityVariation.x() - 2.0f * rand->get(particleIdIndex, QPRand::RotXVV) * m_particleRotationVelocityVariation.x());
rotVelY += (m_particleRotationVelocityVariation.y() - 2.0f * rand->get(particleIdIndex, QPRand::RotYVV) * m_particleRotationVelocityVariation.y());
rotVelZ += (m_particleRotationVelocityVariation.z() - 2.0f * rand->get(particleIdIndex, QPRand::RotZVV) * m_particleRotationVelocityVariation.z());
// Particle data rotations are in qint8 vec3 to save memory.
// max value 127*127 = 16129 degrees/second
float sign;
sign = rotVelX < 0.0f ? -1.0f : 1.0f;
rotVelX = std::max(-127.0f, std::min<float>(127.0f, sign * std::sqrt(abs(rotVelX))));
sign = rotVelY < 0.0f ? -1.0f : 1.0f;
rotVelY = std::max(-127.0f, std::min<float>(127.0f, sign * std::sqrt(abs(rotVelY))));
sign = rotVelZ < 0.0f ? -1.0f : 1.0f;
rotVelZ = std::max(-127.0f, std::min<float>(127.0f, sign * std::sqrt(abs(rotVelZ))));
d->startRotationVelocity = { qint8(rotVelX), qint8(rotVelY), qint8(rotVelZ) };
}
// Colors
QColor pc = particle->color();
QVector4D pcv = particle->colorVariation();
uchar r, g, b, a;
if (particle->unifiedColorVariation()) {
// Vary all color channels using the same random amount
const int randVar = int(rand->get(particleIdIndex, QPRand::ColorAV) * 256);
r = pc.red() * (1.0f - pcv.x()) + randVar * pcv.x();
g = pc.green() * (1.0f - pcv.y()) + randVar * pcv.y();
b = pc.blue() * (1.0f - pcv.z()) + randVar * pcv.z();
a = pc.alpha() * (1.0f - pcv.w()) + randVar * pcv.w();
} else {
r = pc.red() * (1.0f - pcv.x()) + int(rand->get(particleIdIndex, QPRand::ColorRV) * 256) * pcv.x();
g = pc.green() * (1.0f - pcv.y()) + int(rand->get(particleIdIndex, QPRand::ColorGV) * 256) * pcv.y();
b = pc.blue() * (1.0f - pcv.z()) + int(rand->get(particleIdIndex, QPRand::ColorBV) * 256) * pcv.z();
a = pc.alpha() * (1.0f - pcv.w()) + int(rand->get(particleIdIndex, QPRand::ColorAV) * 256) * pcv.w();
}
d->startColor = {r, g, b, a};
// Sprite sequence animation
if (auto sequence = particle->m_spriteSequence) {
if (sequence->duration() > 0) {
float animationTimeMs = float(sequence->duration()) / 1000.0f;
float animationTimeVarMs = float(sequence->durationVariation()) / 1000.0f;
animationTimeVarMs = animationTimeVarMs - 2.0f * rand->get(particleIdIndex, QPRand::SpriteAnimationV) * animationTimeVarMs;
// Sequence duration to be at least 1ms
const float MIN_DURATION = 0.001f;
d->animationTime = std::max(MIN_DURATION, animationTimeMs + animationTimeVarMs);
} else {
// Duration not set, so use the lifetime of the particle
d->animationTime = d->lifetime;
}
}
d->reversed = m_reversed;
}
int QQuick3DParticleEmitter::getEmitAmountFromDynamicBursts(int triggerType)
{
int amount = 0;
const int currentTime = m_system->time();
const int prevTime = m_prevBurstTime;
// First go through dynamic bursts and see if any of them tiggers
for (auto *burst : std::as_const(m_emitBursts)) {
auto *burstPtr = qobject_cast<QQuick3DParticleDynamicBurst *>(burst);
if (!burstPtr)
continue;
if (!burstPtr->m_enabled)
continue;
// Trigering on trail emitter start / end
const bool trailTriggering = triggerType && (burstPtr->m_triggerMode) == triggerType;
// Triggering on time for the first time
const bool timeTriggeringStart = !triggerType && currentTime >= burstPtr->m_time && prevTime <= burstPtr->m_time;
if (trailTriggering || timeTriggeringStart) {
int burstAmount = burstPtr->m_amount;
if (burstPtr->m_amountVariation > 0) {
auto rand = m_system->rand();
int randAmount = 2 * rand->get() * burstPtr->m_amountVariation;
burstAmount += burstPtr->m_amountVariation - randAmount;
}
if (burstAmount > 0) {
if (timeTriggeringStart && burstPtr->m_duration > 0) {
// Burst with duration, so generate burst data
BurstEmitData emitData;
emitData.startTime = currentTime;
emitData.endTime = currentTime + burstPtr->m_duration;
emitData.emitAmount = burstAmount;
emitData.prevBurstTime = prevTime;
m_burstEmitData << emitData;
} else {
// Directly trigger the amount
amount += burstAmount;
}
}
}
}
// Then go through the triggered emit bursts list
for (int burstIndex = 0; burstIndex < m_burstEmitData.size(); ++burstIndex) {
auto &burstData = m_burstEmitData[burstIndex];
const int amountLeft = burstData.emitAmount - burstData.emitCounter;
if (currentTime >= burstData.endTime) {
// Burst time has ended, emit all rest of the particles and remove the burst
amount += amountLeft;
m_burstEmitData.removeAt(burstIndex);
} else {
// Otherwise burst correct amount depending on burst duration
const int durationTime = currentTime - burstData.prevBurstTime;
const int burstDurationTime = burstData.endTime - burstData.startTime;
int burstAmount = burstData.emitAmount * (float(durationTime) / float(burstDurationTime));
burstAmount = std::min(amountLeft, burstAmount);
if (burstAmount > 0) {
amount += burstAmount;
burstData.emitCounter += burstAmount;
burstData.prevBurstTime = currentTime;
}
}
}
// Reset the prev burst time
m_prevBurstTime = currentTime;
return amount;
}
int QQuick3DParticleEmitter::getEmitAmount()
{
if (!m_system)
return 0;
if (!m_enabled)
return 0;
if (m_emitRate <= 0.0f)
return 0;
float timeChange = m_system->currentTime() - m_prevEmitTime;
float emitAmountF = timeChange / (1000.0f / m_emitRate);
int emitAmount = floorf(emitAmountF);
// Store the partly unemitted particles
// When emitAmount = 0, we just let the timeChange grow.
if (emitAmount > 0) {
m_unemittedF += (emitAmountF - emitAmount);
// When unemitted grow to a full particle, emit it
// This way if emit rate is 140 emitAmounts can be e.g. 2,2,3,2,2,3 etc.
if (m_unemittedF >= 1.0f) {
emitAmount++;
m_unemittedF--;
}
}
return emitAmount;
}
void QQuick3DParticleEmitter::emitParticlesBurst(const QQuick3DParticleEmitBurstData &burst)
{
if (!m_system)
return;
if (!m_enabled)
return;
if (!m_particle)
return;
QMatrix4x4 transform = calculateParticleTransform(parentNode(), m_systemSharedParent);
QQuaternion rotation = calculateParticleRotation(parentNode(), m_systemSharedParent);
QVector3D centerPos = position() + burst.position;
int emitAmount = std::min(burst.amount, int(m_particle->maxAmount()));
for (int i = 0; i < emitAmount; i++) {
// Distribute evenly between time and time+duration.
float startTime = (burst.time / 1000.0f) + (float(1 + i) / emitAmount) * ((burst.duration) / 1000.0f);
emitParticle(m_particle, startTime, transform, rotation, centerPos);
}
}
// Called to emit set of particles
void QQuick3DParticleEmitter::emitParticles()
{
if (!m_system)
return;
if (!m_enabled)
return;
if (!m_particle)
return;
auto *mbp = qobject_cast<QQuick3DParticleModelBlendParticle *>(m_particle);
if (mbp && mbp->activationNode() && mbp->emitMode() == QQuick3DParticleModelBlendParticle::Activation) {
// The particles are emitted using the activationNode instead of regular emit
emitActivationNodeParticles(mbp);
return;
}
const int systemTime = m_system->currentTime();
if (systemTime < m_prevEmitTime) {
// If we are goint backwards, reset previous emit time to current time.
m_prevEmitTime = systemTime;
} else {
// Keep previous emitting time within max the life span.
// This way emitting is reasonable also with big time jumps.
const int maxLifeSpan = m_lifeSpan + m_lifeSpanVariation;
m_prevEmitTime = std::max(m_prevEmitTime, systemTime - maxLifeSpan);
}
// If bursts have changed, generate them first in the beginning
if (!m_burstGenerated)
generateEmitBursts();
int emitAmount = getEmitAmount() + getEmitAmountFromDynamicBursts();
// With lower emitRates, let timeChange grow until at least 1 particle is emitted
if (emitAmount < 1)
return;
QMatrix4x4 transform = calculateParticleTransform(parentNode(), m_systemSharedParent);
QQuaternion rotation = calculateParticleRotation(parentNode(), m_systemSharedParent);
QVector3D centerPos = position();
emitAmount = std::min(emitAmount, int(m_particle->maxAmount()));
for (int i = 0; i < emitAmount; i++) {
// Distribute evenly between previous and current time, important especially
// when time has jumped a lot (like a starttime).
float startTime = (m_prevEmitTime / 1000.0f) + (float(1+i) / emitAmount) * ((systemTime - m_prevEmitTime) / 1000.0f);
emitParticle(m_particle, startTime, transform, rotation, centerPos);
}
m_prevEmitTime = systemTime;
}
void QQuick3DParticleEmitter::emitActivationNodeParticles(QQuick3DParticleModelBlendParticle *particle)
{
QMatrix4x4 matrix = particle->activationNode()->sceneTransform();
QMatrix4x4 actTransform = sceneTransform().inverted() * matrix;
QVector3D front = actTransform.column(2).toVector3D();
QVector3D pos = actTransform.column(3).toVector3D();
float d = QVector3D::dotProduct(pos, front);
const int systemTime = m_system->currentTime();
// Keep previous emitting time within max the life span.
// This way emitting is reasonable also with big time jumps.
const int maxLifeSpan = m_lifeSpan + m_lifeSpanVariation;
m_prevEmitTime = std::max(m_prevEmitTime, systemTime - maxLifeSpan);
float startTime = systemTime / 1000.0f;
QMatrix4x4 transform = calculateParticleTransform(parentNode(), m_systemSharedParent);
QQuaternion rotation = calculateParticleRotation(parentNode(), m_systemSharedParent);
QVector3D centerPos = position();
for (int i = 0; i < particle->maxAmount(); i++) {
if (particle->m_particleData[i].startTime >= 0)
continue;
const QVector3D pc = particle->particleCenter(i);
if (QVector3D::dotProduct(front, pc) - d > 0.0f)
emitParticle(particle, startTime, transform, rotation, centerPos, i);
}
m_prevEmitTime = systemTime;
}
void QQuick3DParticleEmitter::componentComplete()
{
if (!m_system && qobject_cast<QQuick3DParticleSystem *>(parentItem()))
setSystem(qobject_cast<QQuick3DParticleSystem *>(parentItem()));
// When dynamically creating emitters, start from the current time.
if (m_system)
m_prevEmitTime = m_system->currentTime();
QQuick3DNode::componentComplete();
}
// EmitBursts - list handling
/*!
\qmlproperty List<EmitBurst3D> ParticleEmitter3D::emitBursts
This property takes a list of \l EmitBurst3D elements, to declaratively define bursts.
If the burst starting time, amount, and duration are known beforehand, it is better to
use this property than e.g. calling \l burst() with a \l Timer.
For example, to emit 100 particles at the beginning, and 50 particles at 2 seconds:
\qml
ParticleEmitter3D {
emitBursts: [
EmitBurst3D {
time: 0
amount: 100
},
EmitBurst3D {
time: 2000
amount: 50
}
]
}
\endqml
\sa burst()
*/
QQmlListProperty<QQuick3DParticleEmitBurst> QQuick3DParticleEmitter::emitBursts()
{
return {this, this,
&QQuick3DParticleEmitter::appendEmitBurst,
&QQuick3DParticleEmitter::emitBurstCount,
&QQuick3DParticleEmitter::emitBurst,
&QQuick3DParticleEmitter::clearEmitBursts,
&QQuick3DParticleEmitter::replaceEmitBurst,
&QQuick3DParticleEmitter::removeLastEmitBurst};
}
void QQuick3DParticleEmitter::appendEmitBurst(QQuick3DParticleEmitBurst* n) {
m_emitBursts.append(n);
}
qsizetype QQuick3DParticleEmitter::emitBurstCount() const
{
return m_emitBursts.size();
}
QQuick3DParticleEmitBurst *QQuick3DParticleEmitter::emitBurst(qsizetype index) const
{
return m_emitBursts.at(index);
}
void QQuick3DParticleEmitter::clearEmitBursts() {
m_emitBursts.clear();
}
void QQuick3DParticleEmitter::replaceEmitBurst(qsizetype index, QQuick3DParticleEmitBurst *n)
{
m_emitBursts[index] = n;
}
void QQuick3DParticleEmitter::removeLastEmitBurst()
{
m_emitBursts.removeLast();
}
// EmitBursts - static
void QQuick3DParticleEmitter::appendEmitBurst(QQmlListProperty<QQuick3DParticleEmitBurst> *list, QQuick3DParticleEmitBurst *p) {
reinterpret_cast< QQuick3DParticleEmitter *>(list->data)->appendEmitBurst(p);
}
void QQuick3DParticleEmitter::clearEmitBursts(QQmlListProperty<QQuick3DParticleEmitBurst> *list) {
reinterpret_cast< QQuick3DParticleEmitter *>(list->data)->clearEmitBursts();
}
void QQuick3DParticleEmitter::replaceEmitBurst(QQmlListProperty<QQuick3DParticleEmitBurst> *list, qsizetype i, QQuick3DParticleEmitBurst *p)
{
reinterpret_cast< QQuick3DParticleEmitter *>(list->data)->replaceEmitBurst(i, p);
}
void QQuick3DParticleEmitter::removeLastEmitBurst(QQmlListProperty<QQuick3DParticleEmitBurst> *list)
{
reinterpret_cast< QQuick3DParticleEmitter *>(list->data)->removeLastEmitBurst();
}
QQuick3DParticleEmitBurst* QQuick3DParticleEmitter::emitBurst(QQmlListProperty<QQuick3DParticleEmitBurst> *list, qsizetype i) {
return reinterpret_cast< QQuick3DParticleEmitter *>(list->data)->emitBurst(i);
}
qsizetype QQuick3DParticleEmitter::emitBurstCount(QQmlListProperty<QQuick3DParticleEmitBurst> *list) {
return reinterpret_cast< QQuick3DParticleEmitter *>(list->data)->emitBurstCount();
}
QT_END_NAMESPACE
|