blob: 6ba952f53144ef153f636fe1e91a3011c0763597 (
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
|
// Copyright (C) 2021 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include "qquick3dparticlecustomshape_p.h"
#include "qquick3dparticlerandomizer_p.h"
#include "qquick3dparticlesystem_p.h"
#include "qquick3dparticleutils_p.h"
#include "qquick3dparticleshapedatautils_p.h"
#include <QtQml/qqmlcontext.h>
#include <QtQml/qqmlfile.h>
#include <QtCore/qfile.h>
QT_BEGIN_NAMESPACE
/*!
\qmltype ParticleCustomShape3D
\inherits ParticleAbstractShape3D
\inqmlmodule QtQuick3D.Particles3D
\brief Loads custom particle shapes for emitters and affectors.
\since 6.3
The ParticleCustomShape3D element can be used to load custom particle shapes.
For example, to emit particles from positions defined in heart.cbor:
\qml
ParticleEmitter3D {
shape: ParticleCustomShape3D {
source: "heart.cbor"
}
...
}
\endqml
The format of CBOR shape files is following:
\badcode
[
"QQ3D_SHAPE", // string
version, // integer
[
posX, // float
posY, // float
posZ, // float
posX, // float
...
]
]
\endcode
To assist in generating these shape files you can use the shapegen tool.
*/
QQuick3DParticleCustomShape::QQuick3DParticleCustomShape(QObject *parent)
: QQuick3DParticleAbstractShape(parent)
{
}
/*!
\qmlproperty url ParticleCustomShape3D::source
This property holds the location of the shape file.
\warning \a source is expected to contain trusted content. Application
developers are advised to carefully consider the potential implications
before passing in user-provided source files that are not part of the
application.
*/
QUrl QQuick3DParticleCustomShape::source() const
{
return m_source;
}
/*!
\qmlproperty bool ParticleCustomShape3D::randomizeData
This property holds whether the particles are used in random order instead
of in the order they are specified in the source.
The default value is \c false.
*/
bool QQuick3DParticleCustomShape::randomizeData() const
{
return m_random;
}
void QQuick3DParticleCustomShape::setSource(const QUrl &source)
{
if (m_source == source)
return;
m_source = source;
loadFromSource();
Q_EMIT sourceChanged();
}
void QQuick3DParticleCustomShape::setRandomizeData(bool random)
{
if (m_random == random)
return;
m_random = random;
if (m_random)
m_randomizeDirty = true;
Q_EMIT randomizeDataChanged();
}
void QQuick3DParticleCustomShape::loadFromSource()
{
m_positions.clear();
// Get path to file
const QQmlContext *context = qmlContext(this);
QString dataFilePath = QQmlFile::urlToLocalFileOrQrc(context ? context->resolvedUrl(m_source) : m_source);
QFile dataFile(dataFilePath);
if (!dataFile.open(QIODevice::ReadOnly)) {
// Invalid file
qWarning() << "Unable to open file:" << dataFilePath;
return;
}
QCborStreamReader reader(&dataFile);
// Check that file is proper CBOR and get the version
int version = QQuick3DParticleShapeDataUtils::readShapeHeader(reader);
if (version == -1) {
// Invalid file
qWarning() << "Invalid shape data version:" << version;
return;
}
// Start positions array
reader.enterContainer();
m_center = QVector3D();
while (reader.lastError() == QCborError::NoError && reader.hasNext()) {
QVector3D pos = QQuick3DParticleShapeDataUtils::readValue(reader, QMetaType::QVector3D).value<QVector3D>();
m_positions.append(pos);
m_center += pos;
}
if (!m_positions.isEmpty())
m_center *= 1.0f / float(m_positions.size());
// Leave positions array
reader.leaveContainer();
// Leave root array
reader.leaveContainer();
if (m_random)
m_randomizeDirty = true;
}
void QQuick3DParticleCustomShape::doRandomizeData()
{
if (!m_system || m_positions.isEmpty())
return;
auto rand = m_system->rand();
int seed = rand->get(0, QPRand::Shape1) * float(INT_MAX);
std::shuffle(m_positions.begin(), m_positions.end(), std::default_random_engine(seed));
m_randomizeDirty = false;
}
QVector3D QQuick3DParticleCustomShape::getPosition(int particleIndex)
{
auto *parent = parentNode();
if (!parent || m_positions.isEmpty())
return QVector3D();
if (m_randomizeDirty)
doRandomizeData();
int index = particleIndex % m_positions.size();
return m_positions.at(index) * parent->scale();
}
QVector3D QQuick3DParticleCustomShape::getSurfaceNormal(int particleIndex)
{
auto *parent = parentNode();
if (!parent || m_positions.isEmpty())
return QVector3D();
if (m_randomizeDirty)
doRandomizeData();
int index = particleIndex % m_positions.size();
return (m_positions.at(index) - m_center).normalized();
}
QT_END_NAMESPACE
|