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
qquick3dparticlelineparticle.cpp
Go to the documentation of this file.
1// Copyright (C) 2022 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
10#include <private/qquick3dobject_p.h>
11
12#include <algorithm>
13
14QT_BEGIN_NAMESPACE
15
16/*!
17 \qmltype LineParticle3D
18 \inherits SpriteParticle3D
19 \inqmlmodule QtQuick3D.Particles3D
20 \brief Line particle.
21 \since 6.4
22
23 The LineParticle3D creates line shaped sprite particles.
24
25 The line is created from the path of the particle when it moves.
26 The length of the line is specified either by the \l length parameter
27 or the segment count and minimum delta between points. In latter case the
28 length of the line may vary if the particles speed varies.
29*/
30
31QQuick3DParticleLineParticle::QQuick3DParticleLineParticle(QQuick3DNode *parent)
32 : QQuick3DParticleSpriteParticle(parent)
33{
34}
35
36QQuick3DParticleLineParticle::~QQuick3DParticleLineParticle()
37{
38}
39
40/*!
41 \qmlproperty int LineParticle3D::segmentCount
42
43 This property holds the number of segments in the line. The line is drawn using
44 segment + 1 points, where the additional one comes from the particles current position.
45 The default value is 1.
46*/
47int QQuick3DParticleLineParticle::segmentCount() const
48{
49 return m_segmentCount;
50}
51
52/*!
53 \qmlproperty real LineParticle3D::alphaFade
54
55 This property holds the alpha fade factor of the line. The alphaFade value range is [0, 1].
56 When the value is greater than 0.0, causes the line to fade the further the segment is from the
57 first particle segment. The alpha for a segment is calculated like this:
58 segmentAlpha(s) = (1.0 - alphaFade) ^ s, where s is the segment index.
59 The default value is 0.0.
60*/
61float QQuick3DParticleLineParticle::alphaFade() const
62{
63 return m_alphaFade;
64}
65
66/*!
67 \qmlproperty real LineParticle3D::scaleMultiplier
68
69 This property holds the scale multiplier of the line. The scaleMultiplier value range is [0, 2].
70 The scaleMultiplier modifies the line size for the line segments. If the value is less than 1.0,
71 the line gets smaller the further a segment is from the first segment and if the value is greater
72 than 1.0 the line gets bigger. The size for a segment is calculated like this:
73 size(s) = scaleMultiplier ^ s, where s is the segment index.
74*/
75float QQuick3DParticleLineParticle::scaleMultiplier() const
76{
77 return m_scaleMultiplier;
78}
79
80/*!
81 \qmlproperty real LineParticle3D::texcoordMultiplier
82
83 This property holds the texture coordinate multiplier of the line. This value is factored
84 to the texture coordinate values of the line. The default value is 1.0.
85*/
86float QQuick3DParticleLineParticle::texcoordMultiplier() const
87{
88 return m_texcoordMultiplier;
89}
90
91/*!
92 \qmlproperty real LineParticle3D::length
93
94 This property holds the length of the line. If the value is set, the lines length is limited
95 to the value. In this case the minimum delta of the line is the length divided
96 by the segment count. If the value is not set, the line length varies based on the
97 speed the particle moves as well as segment count and minimum delta. The default value is -1.0.
98*/
99float QQuick3DParticleLineParticle::length() const
100{
101 return m_length;
102}
103
104/*!
105 \qmlproperty real LineParticle3D::lengthVariation
106
107 This property holds the length variation applied to each line. Variation is applied
108 only if the \l length property is also set. The resulting line length clamps to positive
109 values.
110
111 The default value is 0.0.
112*/
113
114float QQuick3DParticleLineParticle::lengthVariation() const
115{
116 return m_lengthVariation;
117}
118
119/*!
120 \qmlproperty real LineParticle3D::lengthDeltaMin
121
122 This property holds the minimum length between segment points. This parameter is
123 ignored if the length parameter is set. The default value is 10.0.
124*/
125float QQuick3DParticleLineParticle::lengthDeltaMin() const
126{
127 return m_lengthDeltaMin;
128}
129
130/*!
131 \qmlproperty int LineParticle3D::eolFadeOutDuration
132
133 This property holds the end-of-life fade-out duration of the line. If set, each line remains
134 in the place it was when the particle reached end of its lifetime, then fades out during this
135 time period. The default value is 0.
136*/
137int QQuick3DParticleLineParticle::eolFadeOutDuration() const
138{
139 return m_eolFadeOutDuration;
140}
141
142/*!
143 \qmlproperty enumeration LineParticle3D::TexcoordMode
144
145 Defines the texture coordinate mode of line particle.
146
147 \value LineParticle3D.Absolute
148 Texture coordinates are specified relative to the world position.
149 \value LineParticle3D.Relative
150 Texture coordinates are specified relative to line first line point.
151 \value LineParticle3D.Fill
152 Texture coordinates are specified such that the texture fills the whole line.
153*/
154
155/*!
156 \qmlproperty TexcoordMode LineParticle3D::texcoordMode
157
158 This property holds the texture coordinate mode of the line.
159*/
160QQuick3DParticleLineParticle::TexcoordMode QQuick3DParticleLineParticle::texcoordMode() const
161{
162 return m_texcoordMode;
163}
164
165void QQuick3DParticleLineParticle::setSegmentCount(int count)
166{
167 count = qMax(1, count);
168 if (m_segmentCount == count)
169 return;
170 m_segmentCount = count;
171 handleSegmentCountChanged();
172 Q_EMIT segmentCountChanged();
173}
174
175void QQuick3DParticleLineParticle::setAlphaFade(float fade)
176{
177 fade = qBound(0.0f, fade, 1.0f);
178 if (qFuzzyCompare(m_alphaFade, fade))
179 return;
180 m_alphaFade = fade;
181 Q_EMIT alphaFadeChanged();
182}
183
184void QQuick3DParticleLineParticle::setScaleMultiplier(float multiplier)
185{
186 multiplier = qBound(0.0f, multiplier, 2.0f);
187 if (qFuzzyCompare(m_scaleMultiplier, multiplier))
188 return;
189 m_scaleMultiplier = multiplier;
190 Q_EMIT scaleMultiplierChanged();
191}
192
193void QQuick3DParticleLineParticle::setTexcoordMultiplier(float multiplier)
194{
195 if (qFuzzyCompare(m_texcoordMultiplier, multiplier))
196 return;
197 m_texcoordMultiplier = multiplier;
198 Q_EMIT texcoordMultiplierChanged();
199}
200
201void QQuick3DParticleLineParticle::setLength(float length)
202{
203 length = length != -1.0f ? qMax(length, 0.0f) : -1.0f;
204 if (qFuzzyCompare(m_length, length))
205 return;
206 m_length = length;
207 Q_EMIT lengthChanged();
208}
209
210void QQuick3DParticleLineParticle::setLengthVariation(float lengthVariation)
211{
212 lengthVariation = qMax(lengthVariation, 0.0f);
213 if (qFuzzyCompare(m_lengthVariation, lengthVariation))
214 return;
215 m_lengthVariation = lengthVariation;
216 Q_EMIT lengthVariationChanged();
217}
218
219void QQuick3DParticleLineParticle::setLengthDeltaMin(float min)
220{
221 min = qMax(min, 0.0f);
222 if (qFuzzyCompare(m_lengthDeltaMin, min))
223 return;
224 m_lengthDeltaMin = min;
225 Q_EMIT lengthDeltaMinChanged();
226}
227
228void QQuick3DParticleLineParticle::setEolFadeOutDuration(int duration)
229{
230 duration = qMax(0, duration);
231 if (duration == m_eolFadeOutDuration)
232 return;
233 m_eolFadeOutDuration = duration;
234 Q_EMIT eolFadeOutDurationChanged();
235}
236
237void QQuick3DParticleLineParticle::setTexcoordMode(TexcoordMode mode)
238{
239 if (mode == m_texcoordMode)
240 return;
241 m_texcoordMode = mode;
242 Q_EMIT texcoordModeChanged();
243}
244
245QSSGRenderParticles::FeatureLevel lineFeatureLevel(QQuick3DParticleSpriteParticle::FeatureLevel in)
246{
247 switch (in) {
248 case QQuick3DParticleSpriteParticle::FeatureLevel::Simple:
249 return QSSGRenderParticles::FeatureLevel::Line;
250 case QQuick3DParticleSpriteParticle::FeatureLevel::Mapped:
251 return QSSGRenderParticles::FeatureLevel::LineMapped;
252 case QQuick3DParticleSpriteParticle::FeatureLevel::Animated:
253 return QSSGRenderParticles::FeatureLevel::LineAnimated;
254 case QQuick3DParticleSpriteParticle::FeatureLevel::SimpleVLight:
255 return QSSGRenderParticles::FeatureLevel::LineVLight;
256 case QQuick3DParticleSpriteParticle::FeatureLevel::MappedVLight:
257 return QSSGRenderParticles::FeatureLevel::LineMappedVLight;
258 case QQuick3DParticleSpriteParticle::FeatureLevel::AnimatedVLight:
259 return QSSGRenderParticles::FeatureLevel::LineAnimatedVLight;
260 }
261 return QSSGRenderParticles::FeatureLevel::Line;
262}
263
264QSSGRenderGraphObject *QQuick3DParticleLineParticle::updateLineNode(QSSGRenderGraphObject *node)
265{
266 auto particles = static_cast<QSSGRenderParticles *>(node);
267
268 float frames = 1.0f;
269 if (sprite() && spriteSequence())
270 frames = float(spriteSequence()->frameCount());
271
272 particles->m_sizeModifier = m_scaleMultiplier;
273 particles->m_alphaFade = 1.0f - m_alphaFade;
274 if (m_texcoordMode == TexcoordMode::Fill)
275 particles->m_texcoordScale = frames * m_texcoordMultiplier;
276 else
277 particles->m_texcoordScale = frames / particleScale() * m_texcoordMultiplier;
278 particles->m_featureLevel = lineFeatureLevel(m_featureLevel);
279
280 return particles;
281}
282
283void QQuick3DParticleLineParticle::handleMaxAmountChanged(int amount)
284{
285 if (m_lineData.size() == amount)
286 return;
287
288 m_lineData.resize(m_segmentCount * amount);
289 m_lineHeaderData.resize(amount);
290 QQuick3DParticleSpriteParticle::handleMaxAmountChanged(amount);
291}
292
293void QQuick3DParticleLineParticle::handleSystemChanged(QQuick3DParticleSystem *system)
294{
295 for (PerEmitterData &value : m_perEmitterData) {
296 delete value.particleUpdateNode;
297 value.particleUpdateNode = new LineParticleUpdateNode(system);
298 value.particleUpdateNode->m_particle = this;
299 }
300 updateNodeLayers();
301}
302
303QSSGRenderGraphObject *QQuick3DParticleLineParticle::LineParticleUpdateNode::updateSpatialNode(QSSGRenderGraphObject *node)
304{
305 if (m_particle) {
306 QQuick3DParticleLineParticle *lineParticle = qobject_cast<QQuick3DParticleLineParticle *>(m_particle);
307 node = lineParticle->updateParticleNode(this, node);
308 lineParticle->updateLineNode(node);
309 QQuick3DNode::updateSpatialNode(node);
310 Q_QUICK3D_PROFILE_ASSIGN_ID_SG(lineParticle, node);
311 auto particles = static_cast<QSSGRenderParticles *>(node);
312
313 lineParticle->updateLineBuffer(this, particles);
314
315 m_nodeDirty = false;
316 }
317 return node;
318}
319
320void QQuick3DParticleLineParticle ::reset()
321{
322 QQuick3DParticleSpriteParticle::reset();
323 m_lineData.fill({});
324 m_lineHeaderData.fill({});
325 m_fadeOutData.clear();
326}
327
328void QQuick3DParticleLineParticle::commitParticles(float time)
329{
330 QQuick3DParticleSpriteParticle::commitParticles(time);
331
332 for (auto iter = m_fadeOutData.begin(); iter != m_fadeOutData.end(); ) {
333 if (time >= iter->beginTime && time < iter->endTime)
334 iter++;
335 else
336 iter = m_fadeOutData.erase(iter);
337 }
338}
339
340int QQuick3DParticleLineParticle::nextCurrentIndex(const QQuick3DParticleEmitter *emitter)
341{
342 if (!m_perEmitterData.contains(emitter)) {
343 m_perEmitterData.insert(emitter, PerEmitterData());
344 auto &perEmitter = m_perEmitterData[emitter];
345 perEmitter.particleUpdateNode = new LineParticleUpdateNode(system());
346 perEmitter.emitter = emitter;
347 perEmitter.particleUpdateNode->m_particle = this;
348 perEmitter.emitterIndex = m_nextEmitterIndex++;
349 updateNodeLayers();
350 }
351 int index = QQuick3DParticleSpriteParticle::nextCurrentIndex(emitter);
352 clearSegment(index);
353 m_lineHeaderData[index].emitterIndex = m_perEmitterData[emitter].emitterIndex;
354 if (m_length > 0.0f)
355 m_lineHeaderData[index].length = qMax(0.0f, m_length + m_lengthVariation * (system()->rand()->get(index) - 0.5f));
356 return index;
357}
358
359void QQuick3DParticleLineParticle::setParticleData(int particleIndex,
360 const QVector3D &position,
361 const QVector3D &rotation,
362 const QVector4D &color,
363 float size, float age,
364 float animationFrame)
365{
366 auto &dst = m_spriteParticleData[particleIndex];
367 bool update = size > 0.0f || dst.size > 0.0f;
368 QQuick3DParticleSpriteParticle::setParticleData(particleIndex, position, rotation, color, size, age, animationFrame);
369 if (update)
370 updateLineSegment(particleIndex);
371}
372
373void QQuick3DParticleLineParticle::resetParticleData(int particleIndex)
374{
375 LineDataHeader *header = m_lineHeaderData.data() + particleIndex;
376 if (header->pointCount) {
377 header->currentIndex = 0;
378 header->pointCount = 0;
379 }
380 QQuick3DParticleSpriteParticle::resetParticleData(particleIndex);
381}
382
383void QQuick3DParticleLineParticle::saveLineSegment(int particleIndex, float time)
384{
385 if (m_eolFadeOutDuration > 0 && m_lineHeaderData[particleIndex].pointCount > 0) {
386 FadeOutLineData data;
387 data.endPoint = m_spriteParticleData[particleIndex];
388 data.beginTime = time;
389 data.endTime = time + m_eolFadeOutDuration * 0.001f;
390 data.timeFactor = 1000.0f / ((float)m_eolFadeOutDuration);
391 data.header = m_lineHeaderData[particleIndex];
392 data.lineData = m_lineData.mid(particleIndex * m_segmentCount, m_segmentCount);
393 data.emitterIndex = m_spriteParticleData[particleIndex].emitterIndex;
394 m_fadeOutData.emplaceBack(data);
395 clearSegment(particleIndex);
396 }
397}
398
399static QVector3D qt_normalFromRotation(const QVector3D &eulerRotation)
400{
401 float x = qDegreesToRadians(eulerRotation.x());
402 float y = qDegreesToRadians(eulerRotation.y());
403 if (qFuzzyIsNull(x) && qFuzzyIsNull(y))
404 return QVector3D(0, 0, -1);
405 float a = qCos(x);
406 float b = qSin(x);
407 float c = qCos(y);
408 float d = qSin(y);
409 return QVector3D(d, -b * c, a * c);
410}
411
412void QQuick3DParticleLineParticle::updateLineBuffer(LineParticleUpdateNode *updateNode, QSSGRenderGraphObject *spatialNode)
413{
414 const auto &perEmitter = perEmitterData(updateNode);
415 QSSGRenderParticles *node = static_cast<QSSGRenderParticles *>(spatialNode);
416 if (!node)
417 return;
418
419 int lineCount = 0;
420 for (int i = 0; i < m_lineHeaderData.size(); i++) {
421 if (m_lineHeaderData[i].pointCount && m_lineHeaderData[i].emitterIndex == perEmitter.emitterIndex)
422 lineCount++;
423 }
424 int totalCount = lineCount;
425 if (m_perEmitterData.size() > 1) {
426 for (int i = 0; i < m_fadeOutData.size(); i++) {
427 if (m_fadeOutData[i].emitterIndex == perEmitter.emitterIndex)
428 totalCount++;
429 }
430 } else {
431 totalCount += m_fadeOutData.size();
432 }
433
434 // particleCount() is in points (lines * points per line), not lines
435 const int pointsPerLine = m_segmentCount + 1;
436 if (node->m_particleBuffer.particleCount() != totalCount * pointsPerLine
437 || node->m_particleBuffer.segments() != pointsPerLine) {
438 node->m_particleBuffer.resizeLine(totalCount, pointsPerLine);
439 }
440
441 if (!totalCount) return;
442
443 const int segments = m_segmentCount;
444 const int particlesPerSlice = node->m_particleBuffer.particlesPerSlice();
445 const int sliceStride = node->m_particleBuffer.sliceStride();
446 int sliceParticleIdx = 0;
447 int slice = 0;
448 char *dest = node->m_particleBuffer.pointer();
449 QSSGBounds3 bounds;
450
451 const LineDataHeader *header = m_lineHeaderData.constData();
452 const LineData *lineData = m_lineData.constData();
453 const SpriteParticleData *src = m_spriteParticleData.constData();
454
455 auto nextParticle = [](char *&buffer, int &slice, int &sliceParticleIdx, int particlesPerSlice, int sliceStride) -> QSSGLineParticle* {
456 QSSGLineParticle *ret = reinterpret_cast<QSSGLineParticle *>(buffer) + sliceParticleIdx;
457 sliceParticleIdx++;
458 if (sliceParticleIdx == particlesPerSlice) {
459 slice++;
460 buffer += sliceStride;
461 sliceParticleIdx = 0;
462 }
463 return ret;
464 };
465
466 auto genLine = [&](const SpriteParticleData &sdata, const LineDataHeader &header, const LineData *tdata,
467 QSSGBounds3 &bounds, int segments, float particleScale, float alpha,
468 char *&buffer, int &slice, int &sliceParticleIdx, int particlesPerSlice, int sliceStride, bool absolute, bool fill) {
469 QSSGLineParticle *particle = nextParticle(buffer, slice, sliceParticleIdx, particlesPerSlice, sliceStride);
470 int idx = header.currentIndex;
471 particle->color = sdata.color;
472 particle->color.setW(sdata.color.w() * alpha);
473 QVector3D tangent = (tdata[idx].position - sdata.position).normalized();
474 QVector3D binormal = QVector3D::crossProduct(qt_normalFromRotation(sdata.rotation), tangent);
475 particle->binormal = binormal;
476 particle->position = sdata.position;
477 particle->age = sdata.age;
478 particle->animationFrame = sdata.animationFrame;
479 particle->size = sdata.size * particleScale;
480 float partialLength = (tdata[idx].position - sdata.position).length();
481 float length0 = tdata[idx].length + partialLength;
482 particle->length = 0.0f;
483 float lineLength = header.length;
484 int lastIdx = (idx + 1 + segments - header.pointCount) % segments;
485 float lengthScale = -1.0f;
486
487 if (absolute) {
488 particle->length = length0;
489 length0 = 0;
490 }
491
492 if (fill) {
493 if (lineLength > 0.0f) {
494 lengthScale = -1.0f / lineLength;
495 } else {
496 float totalLength = tdata[idx].length - tdata[lastIdx].length;
497 if (header.pointCount < segments)
498 totalLength += partialLength;
499 if (!qFuzzyIsNull(totalLength))
500 lengthScale = -1.0f / totalLength;
501 }
502 }
503 bounds.include(sdata.position);
504
505 QSSGLineParticle *prevGood = particle;
506 int segmentIdx = 0;
507 int prevIdx = 0;
508 Q_ASSERT(header.pointCount <= m_segmentCount);
509
510 if (header.length >= 0.0f) {
511 float totalLength = 0;
512 float prevLength = tdata[idx].length + partialLength;
513 for (segmentIdx = 0; segmentIdx < header.pointCount && totalLength < header.length; segmentIdx++) {
514 particle = nextParticle(buffer, slice, sliceParticleIdx, particlesPerSlice, sliceStride);
515 particle->size = tdata[idx].size * particleScale;
516 if (particle->size > 0.0f) {
517 bounds.include(tdata[idx].position);
518 particle->color = tdata[idx].color;
519 particle->color.setW(tdata[idx].color.w() * alpha);
520 particle->binormal = tdata[idx].binormal;
521 particle->position = tdata[idx].position;
522 particle->animationFrame = sdata.animationFrame;
523 particle->age = sdata.age;
524 particle->length = (length0 - tdata[idx].length) * lengthScale;
525 float segmentLength = prevLength - tdata[idx].length;
526 prevLength = tdata[idx].length;
527 if (totalLength + segmentLength > header.length) {
528 float diff = totalLength + segmentLength - header.length;
529 particle->position -= tdata[idx].tangent * diff;
530 particle->length -= diff * lengthScale;
531 segmentLength -= diff;
532 }
533 totalLength += segmentLength;
534 prevGood = particle;
535 prevIdx = idx;
536 }
537 idx = idx ? (idx - 1) : (segments - 1);
538 }
539 } else {
540 for (segmentIdx = 0; segmentIdx < header.pointCount; segmentIdx++) {
541 particle = nextParticle(buffer, slice, sliceParticleIdx, particlesPerSlice, sliceStride);
542 particle->size = tdata[idx].size * particleScale;
543 if (particle->size > 0.0f) {
544 bounds.include(tdata[idx].position);
545 particle->color = tdata[idx].color;
546 particle->color.setW(tdata[idx].color.w() * alpha);
547 particle->binormal = tdata[idx].binormal;
548 particle->position = tdata[idx].position;
549 particle->animationFrame = sdata.animationFrame;
550 particle->age = sdata.age;
551 particle->length = (length0 - tdata[idx].length) * lengthScale;
552 prevGood = particle;
553 prevIdx = idx;
554 }
555 idx = idx ? (idx - 1) : (segments - 1);
556 }
557 }
558 for (;segmentIdx < segments; segmentIdx++) {
559 particle = nextParticle(buffer, slice, sliceParticleIdx, particlesPerSlice, sliceStride);
560 *particle = *prevGood;
561 particle->size = 0.0f;
562 particle->length = 0.0f;
563 idx = idx ? (idx - 1) : (segments - 1);
564 }
565 // Do only for full segment
566 if (prevGood == particle && header.length < 0.0f && segments > 1) {
567 prevGood->position -= tdata[prevIdx].tangent * partialLength;
568 if (!fill)
569 prevGood->length -= partialLength * lengthScale;
570 }
571 };
572
573 const bool absolute = m_texcoordMode == TexcoordMode::Absolute;
574 const bool fill = m_texcoordMode == TexcoordMode::Fill;
575 int i = 0;
576 while (i < lineCount) {
577 if (header->pointCount && header->emitterIndex == perEmitter.emitterIndex) {
578 genLine(*src, *header, lineData, bounds, segments, particleScale(), 1.0f, dest,
579 slice, sliceParticleIdx, particlesPerSlice, sliceStride, absolute, fill);
580 i++;
581 }
582 header++;
583 lineData += segments;
584 src++;
585 }
586
587 float time = system()->currentTime() * 0.001f;
588 for (const FadeOutLineData &fdata : std::as_const(m_fadeOutData)) {
589 if (fdata.emitterIndex == perEmitter.emitterIndex) {
590 float factor = 1.0f - (time - fdata.beginTime) * fdata.timeFactor;
591 genLine(fdata.endPoint, fdata.header, fdata.lineData.data(), bounds, segments,
592 particleScale(), factor, dest, slice, sliceParticleIdx, particlesPerSlice,
593 sliceStride, absolute, fill);
594 }
595 }
596 node->m_particleBuffer.setBounds(bounds);
597}
598
599void QQuick3DParticleLineParticle::handleSegmentCountChanged()
600{
601 markNodesDirty();
602 m_lineData.resize(m_segmentCount * m_maxAmount);
603 m_lineData.fill({});
604 m_lineHeaderData.resize(m_maxAmount);
605 m_lineHeaderData.fill({});
606 m_fadeOutData.clear();
607 if (!m_spriteParticleData.isEmpty()) {
608 auto count = qMin(m_maxAmount, m_spriteParticleData.size());
609 for (int i = 0; i < count; i++)
610 m_lineHeaderData[i].emitterIndex = m_spriteParticleData[i].emitterIndex;
611 }
612}
613
614void QQuick3DParticleLineParticle::updateLineSegment(int particleIndex)
615{
616 if (m_lineData.isEmpty()) {
617 qWarning () << "Line particle updated before having been initialized";
618 return;
619 }
620 LineDataHeader *header = m_lineHeaderData.data() + particleIndex;
621 int idx = header->currentIndex;
622 LineData *cur = m_lineData.data() + particleIndex * m_segmentCount;
623 LineData *prev = header->pointCount ? cur + idx : nullptr;
624 const SpriteParticleData &src = m_spriteParticleData.at(particleIndex);
625
626 if (prev && m_segmentCount > 1) {
627 float length = (prev->position - src.position).length();
628 float minLength = m_lengthDeltaMin;
629 if (header->length >= 0.0f)
630 minLength = header->length / (m_segmentCount - 1);
631 if (length < minLength)
632 return;
633 }
634
635 if (header->pointCount < m_segmentCount)
636 header->pointCount++;
637
638 if (prev)
639 idx = (idx + 1) % m_segmentCount;
640 header->currentIndex = idx;
641 cur = cur + idx;
642
643 cur->color = src.color;
644 cur->size = src.size;
645 cur->normal = qt_normalFromRotation(src.rotation);
646
647 if (prev && m_segmentCount == 1) {
648 QVector3D t = prev->position - src.position;
649 float l = t.length();
650 t.normalize();
651 float minLength = m_lengthDeltaMin;
652 if (header->length >= 0.0f)
653 minLength = header->length;
654 cur->position = src.position + minLength * t;
655 cur->length += l;
656 cur->tangent = t;
657 cur->binormal = QVector3D::crossProduct(cur->normal, t);
658 } else {
659 cur->position = src.position;
660 cur->length = 0.0f;
661 }
662
663 if (prev && prev != cur) {
664 prev->tangent = prev->position - src.position;
665 cur->length = prev->tangent.length();
666 prev->tangent /= cur->length;
667 cur->length += prev->length;
668 cur->binormal = QVector3D::crossProduct(cur->normal, prev->tangent);
669 if (header->pointCount == 1)
670 prev->binormal = cur->binormal;
671 else
672 prev->binormal = (prev->binormal + cur->binormal).normalized();
673 }
674}
675
676void QQuick3DParticleLineParticle::clearSegment(int particleIndex)
677{
678 if (m_lineData.isEmpty())
679 return;
680 LineDataHeader *header = m_lineHeaderData.data() + particleIndex;
681 if (header->pointCount) {
682 auto data = m_lineData.begin() + particleIndex * m_segmentCount;
683 std::fill_n(data, m_segmentCount, LineData());
684 }
685 header->emitterIndex = -1;
686 header->currentIndex = 0;
687 header->pointCount = 0;
688 header->length = -1.0f;
689}
690
691QT_END_NAMESPACE
QSSGRenderParticles::FeatureLevel lineFeatureLevel(QQuick3DParticleSpriteParticle::FeatureLevel in)
static QVector3D qt_normalFromRotation(const QVector3D &eulerRotation)