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
qquick3dparticle.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
9
10/*!
11 \qmltype Particle3D
12 \inherits Object3D
13 \inqmlmodule QtQuick3D.Particles3D
14 \brief Abstract logical particle.
15 \since 6.2
16
17 This element defines the common properties of the logical particles.
18 Particle3D is an abstract base class of particles, use \l ModelParticle3D or \l SpriteParticle3D instead.
19
20 \note Unlike the materials used with the models, particles default to being rendered with assuming
21 semi-transparency, and so with blending enabled. This is the desired behavior most of the time due
22 to particle textures, color (alpha) variations, fadings etc. If you don't need the blending,
23 set the \l hasTransparency to \c false for possible performance gains.
24*/
25
26QQuick3DParticle::QQuick3DParticle(QQuick3DObject *parent)
27 : QQuick3DObject(parent)
28 , m_color(255, 255, 255, 255)
29 , m_colorVariation(0, 0, 0, 0)
30{
31}
32
33QQuick3DParticle::QQuick3DParticle(QQuick3DObjectPrivate &dd, QQuick3DNode *parent)
34 : QQuick3DObject(dd, parent)
35 , m_color(255, 255, 255, 255)
36 , m_colorVariation(0, 0, 0, 0)
37{
38
39}
40
41QQuick3DParticle::~QQuick3DParticle()
42{
43 if (m_system)
44 m_system->unRegisterParticle(this);
45}
46
47QQuick3DParticleSystem *QQuick3DParticle::system() const
48{
49 return m_system;
50}
51
52void QQuick3DParticle::setSystem(QQuick3DParticleSystem *system)
53{
54 if (m_system == system)
55 return;
56
57 if (m_system)
58 m_system->unRegisterParticle(this);
59
60 m_system = system;
61 if (m_system)
62 m_system->registerParticle(this);
63 Q_EMIT systemChanged();
64}
65
66/*!
67 \qmlproperty int Particle3D::maxAmount
68
69 This property defines the maximum amount of particles that can exist at the same time.
70 You can use \l {ParticleSystem3DLogging::particlesUsed}{particlesUsed} for debugging how
71 efficiently the allocated particles are used. If the maxAmount is too small, particles
72 are reused before they reach the end of their \l {ParticleEmitter3D::lifeSpan}{lifeSpan}.
73 If the maxAmount is too big, unnecessary memory is allocated for the particles.
74
75 \note Changing the maxAmount resets all the particles in the particle system.
76
77 The default value is \c 100.
78*/
79int QQuick3DParticle::maxAmount() const
80{
81 return m_maxAmount;
82}
83
84void QQuick3DParticle::setMaxAmount(int maxAmount)
85{
86 if (m_maxAmount == maxAmount)
87 return;
88
89 doSetMaxAmount(maxAmount);
90}
91
92void QQuick3DParticle::doSetMaxAmount(int amount)
93{
94 m_maxAmount = amount;
95 Q_EMIT maxAmountChanged();
96}
97
98/*!
99 \qmlproperty color Particle3D::color
100
101 This property defines the base color that is used for colorizing the particles.
102
103 The default value is \c "#FFFFFF" (white).
104*/
105QColor QQuick3DParticle::color() const
106{
107 return m_color;
108}
109
110float QQuick3DParticle::opacity() const
111{
112 return m_color.alphaF();
113}
114
115
116void QQuick3DParticle::setColor(QColor color)
117{
118 if (m_color == color)
119 return;
120
121 m_color = color;
122 Q_EMIT colorChanged();
123}
124
125// When setting color to undefined, reset particle
126// to use its own color instead
127void QQuick3DParticle::resetColor()
128{
129 m_color = QColor(255, 255, 255, 255);
130 m_colorVariation = QVector4D(0, 0, 0, 0);
131}
132
133/*!
134 \qmlproperty vector4d Particle3D::colorVariation
135
136 This property defines the color variation that is used for colorizing the particles.
137 The values are in RGBA order and each value should be between 0.0 (no variation) and 1.0
138 (full variation).
139
140 For example, to create particles which will have translucent red colors between
141 \c #ff0000 and \c #e50000, with 40% to 60% opacity:
142
143 \qml
144 ModelParticle3D {
145 ...
146 color: "#7fff0000"
147 colorVariation: Qt.vector4d(0.1, 0.0, 0.0, 0.2)
148 }
149 \endqml
150
151 The default value is \c (0, 0, 0, 0) (no variation).
152
153 \sa unifiedColorVariation
154*/
155QVector4D QQuick3DParticle::colorVariation() const
156{
157 return m_colorVariation;
158}
159
160void QQuick3DParticle::setColorVariation(QVector4D colorVariation)
161{
162 if (m_colorVariation == colorVariation)
163 return;
164
165 m_colorVariation = colorVariation;
166 Q_EMIT colorVariationChanged();
167}
168
169/*!
170 \qmlproperty bool Particle3D::unifiedColorVariation
171
172 This property defines if the \l colorVariation should be applied uniformly for all
173 the color channels. This means that all variations are applied with the same
174 random amount.
175
176 For example, to create particles which will have yellow colors between
177 \c #ffff00 and \c #7f7f00, so that the values of \c R and \c G color channels are
178 always the same:
179
180 \qml
181 ModelParticle3D {
182 ...
183 color: "#ffff00"
184 colorVariation: Qt.vector4d(0.5, 0.5, 0.0, 0.0)
185 unifiedColorVariation: true
186 }
187 \endqml
188
189 The default value is \c false.
190
191 \sa colorVariation
192*/
193bool QQuick3DParticle::unifiedColorVariation() const
194{
195 return m_unifiedColorVariation;
196}
197
198void QQuick3DParticle::setUnifiedColorVariation(bool unified)
199{
200 if (m_unifiedColorVariation == unified)
201 return;
202
203 m_unifiedColorVariation = unified;
204 Q_EMIT unifiedColorVariationChanged();
205}
206
207/*!
208 \qmlproperty enumeration Particle3D::FadeType
209
210 Defines the type of the fading effect.
211
212 \value Particle3D.FadeNone
213 No fading.
214 \value Particle3D.FadeOpacity
215 Fade the particle opacity from/to 0.0.
216 \value Particle3D.FadeScale
217 Fade the particle scale from/to 0.0.
218*/
219
220/*!
221 \qmlproperty FadeType Particle3D::fadeInEffect
222
223 This property defines the fading effect used when the particles appear.
224
225 The default value is \c Particle3D.FadeOpacity.
226
227 \sa fadeInDuration, fadeOutEffect
228*/
229QQuick3DParticle::FadeType QQuick3DParticle::fadeInEffect() const
230{
231 return m_fadeInEffect;
232}
233
234void QQuick3DParticle::setFadeInEffect(FadeType fadeInEffect)
235{
236 if (m_fadeInEffect == fadeInEffect)
237 return;
238
239 m_fadeInEffect = fadeInEffect;
240 Q_EMIT fadeInEffectChanged();
241}
242
243/*!
244 \qmlproperty FadeType Particle3D::fadeOutEffect
245
246 This property defines the fading effect used when the particles reach their
247 \l {ParticleEmitter3D::lifeSpan}{lifeSpan} and disappear.
248
249 The default value is \c Particle3D.FadeOpacity.
250
251 \sa fadeOutDuration, fadeInEffect
252*/
253QQuick3DParticle::FadeType QQuick3DParticle::fadeOutEffect() const
254{
255 return m_fadeOutEffect;
256}
257
258void QQuick3DParticle::setFadeOutEffect(FadeType fadeOutEffect)
259{
260 if (m_fadeOutEffect == fadeOutEffect)
261 return;
262
263 m_fadeOutEffect = fadeOutEffect;
264 Q_EMIT fadeOutEffectChanged();
265}
266
267/*!
268 \qmlproperty int Particle3D::fadeInDuration
269
270 This property defines the duration in milliseconds for the fading in effect.
271
272 \note The fading durations are part of the particles
273 \l {ParticleEmitter3D::lifeSpan}{lifeSpan}. So e.g. if \c lifeSpan is 3000,
274 \c fadeInDuration is 500 and \c fadeOutDuration is 500, the fully visible
275 time of the particle is 2000ms.
276
277 The default value is \c 250.
278
279 \sa fadeInEffect, fadeOutDuration
280*/
281int QQuick3DParticle::fadeInDuration() const
282{
283 return m_fadeInDuration;
284}
285
286void QQuick3DParticle::setFadeInDuration(int fadeInDuration)
287{
288 if (m_fadeInDuration == fadeInDuration)
289 return;
290
291 m_fadeInDuration = fadeInDuration;
292 Q_EMIT fadeInDurationChanged();
293}
294
295/*!
296 \qmlproperty int Particle3D::fadeOutDuration
297
298 This property defines the duration in milliseconds for the fading out effect.
299
300 The default value is \c 250.
301
302 \sa fadeOutEffect, fadeInDuration
303*/
304int QQuick3DParticle::fadeOutDuration() const
305{
306 return m_fadeOutDuration;
307}
308
309void QQuick3DParticle::setFadeOutDuration(int fadeOutDuration)
310{
311 if (m_fadeOutDuration == fadeOutDuration)
312 return;
313
314 m_fadeOutDuration = fadeOutDuration;
315 Q_EMIT fadeOutDurationChanged();
316}
317
318/*!
319 \qmlproperty enumeration Particle3D::AlignMode
320
321 Defines the type of the alignment.
322
323 \value Particle3D.AlignNone
324 No alignment. Particles rotation can be defined with \l {ParticleEmitter3D::particleRotation}{particleRotation}.
325 \value Particle3D.AlignTowardsTarget
326 Align the particles towards \l alignTargetPosition direction.
327 \value Particle3D.AlignTowardsStartVelocity
328 Align the particles towards their starting \l {ParticleEmitter3D::velocity}{velocity}
329 direction.
330*/
331
332/*!
333 \qmlproperty AlignMode Particle3D::alignMode
334
335 This property defines the align mode used for the particles.
336 Particle alignment means the direction that particles face.
337
338 \note When the \l SpriteParticle3D \l {SpriteParticle3D::billboard}{billboard}
339 property is set to \c true, alignMode does not have an effect.
340
341 The default value is \c Particle3D.AlignNone.
342
343 \sa alignTargetPosition
344*/
345QQuick3DParticle::AlignMode QQuick3DParticle::alignMode() const
346{
347 return m_alignMode;
348}
349
350/*!
351 \qmlproperty vector3d Particle3D::alignTargetPosition
352
353 This property defines the position particles are aligned to.
354 This property has effect only when the \l alignMode is set to
355 \c Particle3D.AlignTowardsTarget.
356
357 \sa alignMode
358*/
359QVector3D QQuick3DParticle::alignTargetPosition() const
360{
361 return m_alignTarget;
362}
363
364/*!
365 \qmlproperty bool Particle3D::hasTransparency
366
367 This property defines if the particle has any transparency and should
368 be blended with the background. Usually this should be true, like when
369 the particle color doesn't have full alpha, texture contains semi-transparent
370 pixels or particles opacity is faded in or out. Setting this to false can be
371 an optimization in specific cases.
372
373 The default value is \c true.
374
375 \sa color, fadeInEffect, fadeOutEffect
376*/
377bool QQuick3DParticle::hasTransparency() const
378{
379 return m_hasTransparency;
380}
381
382void QQuick3DParticle::setHasTransparency(bool transparency)
383{
384 if (m_hasTransparency == transparency)
385 return;
386
387 m_hasTransparency = transparency;
388 Q_EMIT hasTransparencyChanged();
389}
390
391void QQuick3DParticle::setAlignMode(AlignMode alignMode)
392{
393 if (m_alignMode == alignMode)
394 return;
395
396 m_alignMode = alignMode;
397 Q_EMIT alignModeChanged();
398}
399
400void QQuick3DParticle::setAlignTargetPosition(const QVector3D &alignPosition)
401{
402 if (m_alignTarget == alignPosition)
403 return;
404
405 m_alignTarget = alignPosition;
406 Q_EMIT alignTargetPositionChanged();
407}
408
409/*!
410 \qmlproperty enumeration Particle3D::SortMode
411
412 Defines the sorting mode of the particle. The sorting mode determines
413 the order in which the particles are drawn.
414
415 \value Particle3D.SortNone
416 No sorting.
417 \value Particle3D.SortNewest
418 Sort based on particle lifetime, newest first.
419 \value Particle3D.SortOldest
420 Sort based on particle lifetime, oldest first.
421 \value Particle3D.SortDistance
422 Sort based on distance to the camera, farthest first.
423*/
424
425
426/*!
427 \qmlproperty SortMode Particle3D::sortMode
428
429 This property defines the sort mode used for the particles.
430
431 The default value is \c Particle3D.SortNone.
432*/
433QQuick3DParticle::SortMode QQuick3DParticle::sortMode() const
434{
435 return m_sortMode;
436}
437
438void QQuick3DParticle::setSortMode(QQuick3DParticle::SortMode mode)
439{
440 if (m_sortMode == mode)
441 return;
442 m_sortMode = mode;
443 Q_EMIT sortModeChanged();
444}
445
446void QQuick3DParticle::updateBurstIndex(int amount)
447{
448 m_lastBurstIndex += amount;
449}
450
451int QQuick3DParticle::nextCurrentIndex(const QQuick3DParticleEmitter *)
452{
453 m_currentIndex = (m_currentIndex < m_maxAmount - 1) ? m_currentIndex + 1 : m_lastBurstIndex;
454 return m_currentIndex;
455}
456
457void QQuick3DParticle::componentComplete()
458{
459 QQuick3DObject::componentComplete();
460 // Make sure the default amount gets initialized, even if user doesn't set it
461 Q_EMIT maxAmountChanged();
462}
463
464void QQuick3DParticle::reset() {
465 m_currentIndex = -1;
466 m_lastBurstIndex = 0;
467
468 // Reset all particles data
469 m_particleData.fill({});
470}
471
472QT_END_NAMESPACE
Combined button and popup list for selecting options.