aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick3dparticles/qquick3dparticlelineparticle.cpp
blob: c7d2bd0c2825e2fe3329e4bcecbcd0e31312e3d4 (plain)
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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

#include "qquick3dparticlelineparticle_p.h"
#include "qquick3dparticlesystem_p.h"
#include "qquick3dparticlerandomizer_p.h"
#include "qquick3dparticleutils_p.h"
#include <private/qquick3dobject_p.h>

#include <algorithm>

QT_BEGIN_NAMESPACE

/*!
    \qmltype LineParticle3D
    \inherits SpriteParticle3D
    \inqmlmodule QtQuick3D.Particles3D
    \brief Line particle.
    \since 6.4

    The LineParticle3D creates line shaped sprite particles.

    The line is created from the path of the particle when it moves.
    The length of the line is specified either by the \l length parameter
    or the segment count and minimum delta between points. In latter case the
    length of the line may vary if the particles speed varies.
*/

QQuick3DParticleLineParticle::QQuick3DParticleLineParticle(QQuick3DNode *parent)
    : QQuick3DParticleSpriteParticle(parent)
{
}

QQuick3DParticleLineParticle::~QQuick3DParticleLineParticle()
{
}

/*!
    \qmlproperty int LineParticle3D::segmentCount

    This property holds the number of segments in the line. The line is drawn using
    segment + 1 points, where the additional one comes from the particles current position.
    The default value is 1.
*/
int QQuick3DParticleLineParticle::segmentCount() const
{
    return m_segmentCount;
}

/*!
    \qmlproperty real LineParticle3D::alphaFade

    This property holds the alpha fade factor of the line. The alphaFade value range is [0, 1].
    When the value is greater than 0.0, causes the line to fade the further the segment is from the
    first particle segment. The alpha for a segment is calculated like this:
        segmentAlpha(s) = (1.0 - alphaFade) ^ s, where s is the segment index.
    The default value is 0.0.
*/
float QQuick3DParticleLineParticle::alphaFade() const
{
    return m_alphaFade;
}

/*!
    \qmlproperty real LineParticle3D::scaleMultiplier

    This property holds the scale multiplier of the line. The scaleMultiplier value range is [0, 2].
    The scaleMultiplier modifies the line size for the line segments. If the value is less than 1.0,
    the line gets smaller the further a segment is from the first segment and if the value is greater
    than 1.0 the line gets bigger. The size for a segment is calculated like this:
        size(s) = scaleMultiplier ^ s, where s is the segment index.
*/
float QQuick3DParticleLineParticle::scaleMultiplier() const
{
    return m_scaleMultiplier;
}

/*!
    \qmlproperty real LineParticle3D::texcoordMultiplier

    This property holds the texture coordinate multiplier of the line. This value is factored
    to the texture coordinate values of the line. The default value is 1.0.
*/
float QQuick3DParticleLineParticle::texcoordMultiplier() const
{
    return m_texcoordMultiplier;
}

/*!
    \qmlproperty real LineParticle3D::length

    This property holds the length of the line. If the value is set, the lines length is limited
    to the value. In this case the minimum delta of the line is the length divided
    by the segment count. If the value is not set, the line length varies based on the
    speed the particle moves as well as segment count and minimum delta. The default value is -1.0.
*/
float QQuick3DParticleLineParticle::length() const
{
    return m_length;
}

/*!
    \qmlproperty real LineParticle3D::lengthVariation

    This property holds the length variation applied to each line. Variation is applied
    only if the \l length property is also set. The resulting line length clamps to positive
    values.

    The default value is 0.0.
*/

float QQuick3DParticleLineParticle::lengthVariation() const
{
    return m_lengthVariation;
}

/*!
    \qmlproperty real LineParticle3D::lengthDeltaMin

    This property holds the minimum length between segment points. This parameter is
    ignored if the length parameter is set. The default value is 10.0.
*/
float QQuick3DParticleLineParticle::lengthDeltaMin() const
{
    return m_lengthDeltaMin;
}

/*!
    \qmlproperty int LineParticle3D::eolFadeOutDuration

    This property holds the end-of-life fade-out duration of the line. If set, each line remains
    in the place it was when the particle reached end of its lifetime, then fades out during this
    time period. The default value is 0.
*/
int QQuick3DParticleLineParticle::eolFadeOutDuration() const
{
    return m_eolFadeOutDuration;
}

/*!
    \qmlproperty enumeration LineParticle3D::TexcoordMode

    Defines the texture coordinate mode of line particle.

    \value LineParticle3D.Absolute
        Texture coordinates are specified relative to the world position.
    \value LineParticle3D.Relative
        Texture coordinates are specified relative to line first line point.
    \value LineParticle3D.Fill
        Texture coordinates are specified such that the texture fills the whole line.
*/

/*!
    \qmlproperty TexcoordMode LineParticle3D::texcoordMode

    This property holds the texture coordinate mode of the line.
*/
QQuick3DParticleLineParticle::TexcoordMode QQuick3DParticleLineParticle::texcoordMode() const
{
    return m_texcoordMode;
}

void QQuick3DParticleLineParticle::setSegmentCount(int count)
{
    count = qMax(1, count);
    if (m_segmentCount == count)
        return;
    m_segmentCount = count;
    handleSegmentCountChanged();
    Q_EMIT segmentCountChanged();
}

void QQuick3DParticleLineParticle::setAlphaFade(float fade)
{
    fade = qBound(0.0f, fade, 1.0f);
    if (qFuzzyCompare(m_alphaFade, fade))
        return;
    m_alphaFade = fade;
    Q_EMIT alphaFadeChanged();
}

void QQuick3DParticleLineParticle::setScaleMultiplier(float multiplier)
{
    multiplier = qBound(0.0f, multiplier, 2.0f);
    if (qFuzzyCompare(m_scaleMultiplier, multiplier))
        return;
    m_scaleMultiplier = multiplier;
    Q_EMIT scaleMultiplierChanged();
}

void QQuick3DParticleLineParticle::setTexcoordMultiplier(float multiplier)
{
    if (qFuzzyCompare(m_texcoordMultiplier, multiplier))
        return;
    m_texcoordMultiplier = multiplier;
    Q_EMIT texcoordMultiplierChanged();
}

void QQuick3DParticleLineParticle::setLength(float length)
{
    length = length != -1.0f ? qMax(length, 0.0f) : -1.0f;
    if (qFuzzyCompare(m_length, length))
        return;
    m_length = length;
    Q_EMIT lengthChanged();
}

void QQuick3DParticleLineParticle::setLengthVariation(float lengthVariation)
{
    lengthVariation = qMax(lengthVariation, 0.0f);
    if (qFuzzyCompare(m_lengthVariation, lengthVariation))
        return;
    m_lengthVariation = lengthVariation;
    Q_EMIT lengthVariationChanged();
}

void QQuick3DParticleLineParticle::setLengthDeltaMin(float min)
{
    min = qMax(min, 0.0f);
    if (qFuzzyCompare(m_lengthDeltaMin, min))
        return;
    m_lengthDeltaMin = min;
    Q_EMIT lengthDeltaMinChanged();
}

void QQuick3DParticleLineParticle::setEolFadeOutDuration(int duration)
{
    duration = qMax(0, duration);
    if (duration == m_eolFadeOutDuration)
        return;
    m_eolFadeOutDuration = duration;
    Q_EMIT eolFadeOutDurationChanged();
}

void QQuick3DParticleLineParticle::setTexcoordMode(TexcoordMode mode)
{
    if (mode == m_texcoordMode)
        return;
    m_texcoordMode = mode;
    Q_EMIT texcoordModeChanged();
}

QSSGRenderParticles::FeatureLevel lineFeatureLevel(QQuick3DParticleSpriteParticle::FeatureLevel in)
{
    switch (in) {
    case QQuick3DParticleSpriteParticle::FeatureLevel::Simple:
         return QSSGRenderParticles::FeatureLevel::Line;
    case QQuick3DParticleSpriteParticle::FeatureLevel::Mapped:
         return QSSGRenderParticles::FeatureLevel::LineMapped;
    case QQuick3DParticleSpriteParticle::FeatureLevel::Animated:
         return QSSGRenderParticles::FeatureLevel::LineAnimated;
    case QQuick3DParticleSpriteParticle::FeatureLevel::SimpleVLight:
         return QSSGRenderParticles::FeatureLevel::LineVLight;
    case QQuick3DParticleSpriteParticle::FeatureLevel::MappedVLight:
         return QSSGRenderParticles::FeatureLevel::LineMappedVLight;
    case QQuick3DParticleSpriteParticle::FeatureLevel::AnimatedVLight:
         return QSSGRenderParticles::FeatureLevel::LineAnimatedVLight;
    }
    return QSSGRenderParticles::FeatureLevel::Line;
}

QSSGRenderGraphObject *QQuick3DParticleLineParticle::updateLineNode(QSSGRenderGraphObject *node)
{
    auto particles = static_cast<QSSGRenderParticles *>(node);

    float frames = 1.0f;
    if (sprite() && spriteSequence())
        frames = float(spriteSequence()->frameCount());

    particles->m_sizeModifier = m_scaleMultiplier;
    particles->m_alphaFade = 1.0f - m_alphaFade;
    if (m_texcoordMode == TexcoordMode::Fill)
        particles->m_texcoordScale = frames * m_texcoordMultiplier;
    else
        particles->m_texcoordScale = frames / particleScale() * m_texcoordMultiplier;
    particles->m_featureLevel = lineFeatureLevel(m_featureLevel);

    return particles;
}

void QQuick3DParticleLineParticle::handleMaxAmountChanged(int amount)
{
    if (m_lineData.size() == amount)
        return;

    m_lineData.resize(m_segmentCount * amount);
    m_lineHeaderData.resize(amount);
    QQuick3DParticleSpriteParticle::handleMaxAmountChanged(amount);
}

void QQuick3DParticleLineParticle::handleSystemChanged(QQuick3DParticleSystem *system)
{
    for (PerEmitterData &value : m_perEmitterData) {
        delete value.particleUpdateNode;
        value.particleUpdateNode = new LineParticleUpdateNode(system);
        value.particleUpdateNode->m_particle = this;
    }
}

QSSGRenderGraphObject *QQuick3DParticleLineParticle::LineParticleUpdateNode::updateSpatialNode(QSSGRenderGraphObject *node)
{
    if (m_particle) {
        QQuick3DParticleLineParticle *lineParticle = qobject_cast<QQuick3DParticleLineParticle *>(m_particle);
        node = lineParticle->updateParticleNode(this, node);
        lineParticle->updateLineNode(node);
        QQuick3DNode::updateSpatialNode(node);
        Q_QUICK3D_PROFILE_ASSIGN_ID_SG(lineParticle, node);
        auto particles = static_cast<QSSGRenderParticles *>(node);

        lineParticle->updateLineBuffer(this, particles);

        m_nodeDirty = false;
    }
    return node;
}

void QQuick3DParticleLineParticle ::reset()
{
    QQuick3DParticleSpriteParticle::reset();
    m_lineData.fill({});
    m_lineHeaderData.fill({});
    m_fadeOutData.clear();
}

void QQuick3DParticleLineParticle::commitParticles(float time)
{
    QQuick3DParticleSpriteParticle::commitParticles(time);

    for (auto iter = m_fadeOutData.begin(); iter != m_fadeOutData.end(); ) {
        if (time >= iter->beginTime && time < iter->endTime)
            iter++;
        else
            iter = m_fadeOutData.erase(iter);
    }
}

int QQuick3DParticleLineParticle::nextCurrentIndex(const QQuick3DParticleEmitter *emitter)
{
    if (!m_perEmitterData.contains(emitter)) {
        m_perEmitterData.insert(emitter, PerEmitterData());
        auto &perEmitter = m_perEmitterData[emitter];
        perEmitter.particleUpdateNode = new LineParticleUpdateNode(system());
        perEmitter.emitter = emitter;
        perEmitter.particleUpdateNode->m_particle = this;
        perEmitter.emitterIndex = m_nextEmitterIndex++;
    }
    int index = QQuick3DParticleSpriteParticle::nextCurrentIndex(emitter);
    clearSegment(index);
    m_lineHeaderData[index].emitterIndex = m_perEmitterData[emitter].emitterIndex;
    if (m_length > 0.0f)
        m_lineHeaderData[index].length = qMax(0.0f, m_length + m_lengthVariation * (system()->rand()->get(index) - 0.5f));
    return index;
}

void QQuick3DParticleLineParticle::setParticleData(int particleIndex,
                     const QVector3D &position,
                     const QVector3D &rotation,
                     const QVector4D &color,
                     float size, float age,
                     float animationFrame)
{
    auto &dst = m_spriteParticleData[particleIndex];
    bool update = size > 0.0f || dst.size > 0.0f;
    QQuick3DParticleSpriteParticle::setParticleData(particleIndex, position, rotation, color, size, age, animationFrame);
    if (update)
        updateLineSegment(particleIndex);
}

void QQuick3DParticleLineParticle::resetParticleData(int particleIndex)
{
    LineDataHeader *header = m_lineHeaderData.data() + particleIndex;
    if (header->pointCount) {
        header->currentIndex = 0;
        header->pointCount = 0;
    }
    QQuick3DParticleSpriteParticle::resetParticleData(particleIndex);
}

void QQuick3DParticleLineParticle::saveLineSegment(int particleIndex, float time)
{
    if (m_eolFadeOutDuration > 0 && m_lineHeaderData[particleIndex].pointCount > 0) {
        FadeOutLineData data;
        data.endPoint = m_spriteParticleData[particleIndex];
        data.beginTime = time;
        data.endTime = time + m_eolFadeOutDuration * 0.001f;
        data.timeFactor = 1000.0f / ((float)m_eolFadeOutDuration);
        data.header = m_lineHeaderData[particleIndex];
        data.lineData = m_lineData.mid(particleIndex * m_segmentCount, m_segmentCount);
        data.emitterIndex = m_spriteParticleData[particleIndex].emitterIndex;
        m_fadeOutData.emplaceBack(data);
        clearSegment(particleIndex);
    }
}

static QVector3D qt_normalFromRotation(const QVector3D &eulerRotation)
{
    float x = qDegreesToRadians(eulerRotation.x());
    float y = qDegreesToRadians(eulerRotation.y());
    if (qFuzzyIsNull(x) && qFuzzyIsNull(y))
        return QVector3D(0, 0, -1);
    float a = qCos(x);
    float b = qSin(x);
    float c = qCos(y);
    float d = qSin(y);
    return QVector3D(d, -b * c, a * c);
}

void QQuick3DParticleLineParticle::updateLineBuffer(LineParticleUpdateNode *updateNode, QSSGRenderGraphObject *spatialNode)
{
    const auto &perEmitter = perEmitterData(updateNode);
    QSSGRenderParticles *node = static_cast<QSSGRenderParticles *>(spatialNode);
    if (!node)
        return;

    int lineCount = 0;
    for (int i = 0; i < m_lineHeaderData.size(); i++) {
        if (m_lineHeaderData[i].pointCount && m_lineHeaderData[i].emitterIndex == perEmitter.emitterIndex)
            lineCount++;
    }
    int totalCount = lineCount;
    if (m_perEmitterData.size() > 1) {
        for (int i = 0; i < m_fadeOutData.size(); i++) {
            if (m_fadeOutData[i].emitterIndex == perEmitter.emitterIndex)
                totalCount++;
        }
    } else {
        totalCount += m_fadeOutData.size();
    }

    if (node->m_particleBuffer.particleCount() != totalCount)
        node->m_particleBuffer.resizeLine(totalCount, m_segmentCount + 1);

    if (!totalCount) return;

    const int segments = m_segmentCount;
    const int particlesPerSlice = node->m_particleBuffer.particlesPerSlice();
    const int sliceStride = node->m_particleBuffer.sliceStride();
    int sliceParticleIdx = 0;
    int slice = 0;
    char *dest = node->m_particleBuffer.pointer();
    QSSGBounds3 bounds;

    const LineDataHeader *header = m_lineHeaderData.constData();
    const LineData *lineData = m_lineData.constData();
    const SpriteParticleData *src = m_spriteParticleData.constData();

    auto nextParticle = [](char *&buffer, int &slice, int &sliceParticleIdx, int particlesPerSlice, int sliceStride) -> QSSGLineParticle* {
        QSSGLineParticle *ret = reinterpret_cast<QSSGLineParticle *>(buffer) + sliceParticleIdx;
        sliceParticleIdx++;
        if (sliceParticleIdx == particlesPerSlice) {
            slice++;
            buffer += sliceStride;
            sliceParticleIdx = 0;
        }
        return ret;
    };

    auto genLine = [&](const SpriteParticleData &sdata, const LineDataHeader &header, const LineData *tdata,
            QSSGBounds3 &bounds, int segments, float particleScale, float alpha,
            char *&buffer, int &slice, int &sliceParticleIdx, int particlesPerSlice, int sliceStride, bool absolute, bool fill) {
        QSSGLineParticle *particle = nextParticle(buffer, slice, sliceParticleIdx, particlesPerSlice, sliceStride);
        int idx = header.currentIndex;
        particle->color = sdata.color;
        particle->color.setW(sdata.color.w() * alpha);
        QVector3D tangent = (tdata[idx].position - sdata.position).normalized();
        QVector3D binormal = QVector3D::crossProduct(qt_normalFromRotation(sdata.rotation), tangent);
        particle->binormal = binormal;
        particle->position = sdata.position;
        particle->age = sdata.age;
        particle->animationFrame = sdata.animationFrame;
        particle->size = sdata.size * particleScale;
        float partialLength = (tdata[idx].position - sdata.position).length();
        float length0 = tdata[idx].length + partialLength;
        particle->length = 0.0f;
        float lineLength = header.length;
        int lastIdx = (idx + 1 + segments - header.pointCount) % segments;
        float lengthScale = -1.0f;

        if (absolute) {
            particle->length = length0;
            length0 = 0;
        }

        if (fill) {
            if (lineLength > 0.0f) {
                lengthScale = -1.0f / lineLength;
            } else {
                float totalLength = tdata[idx].length - tdata[lastIdx].length;
                if (header.pointCount < segments)
                    totalLength += partialLength;
                if (!qFuzzyIsNull(totalLength))
                    lengthScale = -1.0f / totalLength;
            }
        }
        bounds.include(sdata.position);

        QSSGLineParticle *prevGood = particle;
        int segmentIdx = 0;
        int prevIdx = 0;
        Q_ASSERT(header.pointCount <= m_segmentCount);

        if (header.length >= 0.0f) {
            float totalLength = 0;
            float prevLength = tdata[idx].length + partialLength;
            for (segmentIdx = 0; segmentIdx < header.pointCount && totalLength < header.length; segmentIdx++) {
                particle = nextParticle(buffer, slice, sliceParticleIdx, particlesPerSlice, sliceStride);
                particle->size = tdata[idx].size * particleScale;
                if (particle->size > 0.0f) {
                    bounds.include(tdata[idx].position);
                    particle->color = tdata[idx].color;
                    particle->color.setW(tdata[idx].color.w() * alpha);
                    particle->binormal = tdata[idx].binormal;
                    particle->position = tdata[idx].position;
                    particle->animationFrame = sdata.animationFrame;
                    particle->age = sdata.age;
                    particle->length = (length0 - tdata[idx].length) * lengthScale;
                    float segmentLength = prevLength - tdata[idx].length;
                    prevLength = tdata[idx].length;
                    if (totalLength + segmentLength > header.length) {
                        float diff = totalLength + segmentLength - header.length;
                        particle->position -= tdata[idx].tangent * diff;
                        particle->length -= diff * lengthScale;
                        segmentLength -= diff;
                    }
                    totalLength += segmentLength;
                    prevGood = particle;
                    prevIdx = idx;
                }
                idx = idx ? (idx - 1) : (segments - 1);
            }
        } else {
            for (segmentIdx = 0; segmentIdx < header.pointCount; segmentIdx++) {
                particle = nextParticle(buffer, slice, sliceParticleIdx, particlesPerSlice, sliceStride);
                particle->size = tdata[idx].size * particleScale;
                if (particle->size > 0.0f) {
                    bounds.include(tdata[idx].position);
                    particle->color = tdata[idx].color;
                    particle->color.setW(tdata[idx].color.w() * alpha);
                    particle->binormal = tdata[idx].binormal;
                    particle->position = tdata[idx].position;
                    particle->animationFrame = sdata.animationFrame;
                    particle->age = sdata.age;
                    particle->length = (length0 - tdata[idx].length) * lengthScale;
                    prevGood = particle;
                    prevIdx = idx;
                }
                idx = idx ? (idx - 1) : (segments - 1);
            }
        }
        for (;segmentIdx < segments; segmentIdx++) {
            particle = nextParticle(buffer, slice, sliceParticleIdx, particlesPerSlice, sliceStride);
            *particle = *prevGood;
            particle->size = 0.0f;
            particle->length = 0.0f;
            idx = idx ? (idx - 1) : (segments - 1);
        }
        // Do only for full segment
        if (prevGood == particle && header.length < 0.0f && segments > 1) {
            prevGood->position -= tdata[prevIdx].tangent * partialLength;
            if (!fill)
                prevGood->length -= partialLength * lengthScale;
        }
    };

    const bool absolute = m_texcoordMode == TexcoordMode::Absolute;
    const bool fill = m_texcoordMode == TexcoordMode::Fill;
    int i = 0;
    while (i < lineCount) {
        if (header->pointCount && header->emitterIndex == perEmitter.emitterIndex) {
            genLine(*src, *header, lineData, bounds, segments, particleScale(), 1.0f, dest,
                    slice, sliceParticleIdx, particlesPerSlice, sliceStride, absolute, fill);
            i++;
        }
        header++;
        lineData += segments;
        src++;
    }

    float time = system()->currentTime() * 0.001f;
    for (const FadeOutLineData &fdata : m_fadeOutData) {
        if (fdata.emitterIndex == perEmitter.emitterIndex) {
            float factor = 1.0f - (time - fdata.beginTime) * fdata.timeFactor;
            genLine(fdata.endPoint, fdata.header, fdata.lineData.data(), bounds, segments,
                    particleScale(), factor, dest, slice, sliceParticleIdx, particlesPerSlice,
                    sliceStride, absolute, fill);
        }
    }
    node->m_particleBuffer.setBounds(bounds);
}

void QQuick3DParticleLineParticle::handleSegmentCountChanged()
{
    markNodesDirty();
    m_lineData.resize(m_segmentCount * m_maxAmount);
    m_lineData.fill({});
    m_lineHeaderData.resize(m_maxAmount);
    m_lineHeaderData.fill({});
    m_fadeOutData.clear();
    if (!m_spriteParticleData.isEmpty()) {
        auto count = qMin(m_maxAmount, m_spriteParticleData.size());
        for (int i = 0; i < count; i++)
            m_lineHeaderData[i].emitterIndex = m_spriteParticleData[i].emitterIndex;
    }
}

void QQuick3DParticleLineParticle::updateLineSegment(int particleIndex)
{
    if (m_lineData.isEmpty()) {
        qWarning () << "Line particle updated before having been initialized";
        return;
    }
    LineDataHeader *header = m_lineHeaderData.data() + particleIndex;
    int idx = header->currentIndex;
    LineData *cur = m_lineData.data() + particleIndex * m_segmentCount;
    LineData *prev = header->pointCount ? cur + idx : nullptr;
    const SpriteParticleData &src = m_spriteParticleData.at(particleIndex);

    if (prev && m_segmentCount > 1) {
        float length = (prev->position - src.position).length();
        float minLength = m_lengthDeltaMin;
        if (header->length >= 0.0f)
            minLength = header->length / (m_segmentCount - 1);
        if (length < minLength)
            return;
    }

    if (header->pointCount < m_segmentCount)
        header->pointCount++;

    if (prev)
        idx = (idx + 1) % m_segmentCount;
    header->currentIndex = idx;
    cur = cur + idx;

    cur->color = src.color;
    cur->size = src.size;
    cur->normal = qt_normalFromRotation(src.rotation);

    if (prev && m_segmentCount == 1) {
        QVector3D t = prev->position - src.position;
        float l = t.length();
        t.normalize();
        float minLength = m_lengthDeltaMin;
        if (header->length >= 0.0f)
            minLength = header->length;
        cur->position = src.position + minLength * t;
        cur->length += l;
        cur->tangent = t;
        cur->binormal = QVector3D::crossProduct(cur->normal, t);
    } else {
        cur->position = src.position;
        cur->length = 0.0f;
    }

    if (prev && prev != cur) {
        prev->tangent = prev->position - src.position;
        cur->length = prev->tangent.length();
        prev->tangent /= cur->length;
        cur->length += prev->length;
        cur->binormal = QVector3D::crossProduct(cur->normal, prev->tangent);
        if (header->pointCount == 1)
            prev->binormal = cur->binormal;
        else
            prev->binormal = (prev->binormal + cur->binormal).normalized();
    }
}

void QQuick3DParticleLineParticle::clearSegment(int particleIndex)
{
    if (m_lineData.isEmpty())
        return;
    LineDataHeader *header = m_lineHeaderData.data() + particleIndex;
    if (header->pointCount) {
        auto data = m_lineData.begin() + particleIndex * m_segmentCount;
        std::fill_n(data, m_segmentCount, LineData());
    }
    header->emitterIndex = -1;
    header->currentIndex = 0;
    header->pointCount = 0;
    header->length = -1.0f;
}

QT_END_NAMESPACE