| 1 | // Copyright (C) 2021 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include "qquick3dparticlevectordirection_p.h" |
| 5 | #include "qquick3dparticlerandomizer_p.h" |
| 6 | #include "qquick3dparticlesystem_p.h" |
| 7 | |
| 8 | QT_BEGIN_NAMESPACE |
| 9 | |
| 10 | /*! |
| 11 | \qmltype VectorDirection3D |
| 12 | \inherits Direction3D |
| 13 | \inqmlmodule QtQuick3D.Particles3D |
| 14 | \brief For specifying a direction towards the target direction. |
| 15 | \since 6.2 |
| 16 | |
| 17 | This element sets emitted particle velocity towards the target direction vector. |
| 18 | The length of the direction vector is used as the velocity magnitude. |
| 19 | |
| 20 | For example, to emit particles towards some random direction within |
| 21 | x: 50..150, y: -20..20, z: 0: |
| 22 | |
| 23 | \qml |
| 24 | ParticleEmitter3D { |
| 25 | ... |
| 26 | velocity: VectorDirection3D { |
| 27 | direction: Qt.vector3d(100, 0, 0) |
| 28 | directionVariation: Qt.vector3d(50, 20, 0) |
| 29 | } |
| 30 | } |
| 31 | \endqml |
| 32 | |
| 33 | */ |
| 34 | |
| 35 | QQuick3DParticleVectorDirection::QQuick3DParticleVectorDirection(QObject *parent) |
| 36 | : QQuick3DParticleDirection(parent) |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | /*! |
| 41 | \qmlproperty vector3d VectorDirection3D::direction |
| 42 | |
| 43 | This property defines the direction for particles target. |
| 44 | |
| 45 | The default value is \c (0, 100, 0) (upwards on the y-axis). |
| 46 | |
| 47 | \sa directionVariation |
| 48 | */ |
| 49 | QVector3D QQuick3DParticleVectorDirection::direction() const |
| 50 | { |
| 51 | return m_direction; |
| 52 | } |
| 53 | |
| 54 | /*! |
| 55 | \qmlproperty vector3d VectorDirection3D::directionVariation |
| 56 | |
| 57 | This property defines the direction variation for particles target. |
| 58 | |
| 59 | The default value is \c (0, 0, 0) (no variation). |
| 60 | |
| 61 | \sa direction |
| 62 | */ |
| 63 | QVector3D QQuick3DParticleVectorDirection::directionVariation() const |
| 64 | { |
| 65 | return m_directionVariation; |
| 66 | } |
| 67 | |
| 68 | /*! |
| 69 | \qmlproperty bool VectorDirection3D::normalized |
| 70 | |
| 71 | This property defines if the direction should be normalized after applying the variation. |
| 72 | When this is false, variation affects the magnitude of the particles velocity. |
| 73 | When set to true, variation affects the direction, but the magnitude is determined |
| 74 | by the original direction length. |
| 75 | |
| 76 | The default value is \c false. |
| 77 | */ |
| 78 | bool QQuick3DParticleVectorDirection::normalized() const |
| 79 | { |
| 80 | return m_normalized; |
| 81 | } |
| 82 | |
| 83 | void QQuick3DParticleVectorDirection::setDirection(const QVector3D &direction) |
| 84 | { |
| 85 | if (m_direction == direction) |
| 86 | return; |
| 87 | |
| 88 | m_direction = direction; |
| 89 | Q_EMIT directionChanged(); |
| 90 | } |
| 91 | |
| 92 | void QQuick3DParticleVectorDirection::setDirectionVariation(const QVector3D &directionVariation) |
| 93 | { |
| 94 | if (m_directionVariation == directionVariation) |
| 95 | return; |
| 96 | |
| 97 | m_directionVariation = directionVariation; |
| 98 | Q_EMIT directionVariationChanged(); |
| 99 | } |
| 100 | |
| 101 | void QQuick3DParticleVectorDirection::setNormalized(bool normalized) |
| 102 | { |
| 103 | if (m_normalized == normalized) |
| 104 | return; |
| 105 | |
| 106 | m_normalized = normalized; |
| 107 | Q_EMIT normalizedChanged(); |
| 108 | } |
| 109 | |
| 110 | QVector3D QQuick3DParticleVectorDirection::sample(const QQuick3DParticleData &d) |
| 111 | { |
| 112 | QVector3D ret; |
| 113 | if (!m_system) |
| 114 | return ret; |
| 115 | auto rand = m_system->rand(); |
| 116 | ret.setX(m_direction.x() - m_directionVariation.x() + rand->get(particleIndex: d.index, user: QPRand::VDirXV) * m_directionVariation.x() * 2.0f); |
| 117 | ret.setY(m_direction.y() - m_directionVariation.y() + rand->get(particleIndex: d.index, user: QPRand::VDirYV) * m_directionVariation.y() * 2.0f); |
| 118 | ret.setZ(m_direction.z() - m_directionVariation.z() + rand->get(particleIndex: d.index, user: QPRand::VDirZV) * m_directionVariation.z() * 2.0f); |
| 119 | if (m_normalized) |
| 120 | ret = m_direction.length() * ret.normalized(); |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | QT_END_NAMESPACE |
| 125 | |