Qt Quick 3D - 粒子 3D 试验台示例
演示如何使用Qt Quick 3D Particles3D 模块。
本示例演示了使用QtQuick3D.Particles3D 模块功能的不同方法。Particles Testbed 包含一系列示例,其中一些强调特定功能,而另一些则结合了更多功能,以实现所需的外观。
共同特征
测试平台中的示例具有一些共同特点。要退出示例返回启动界面,请按左上角的返回箭头。
每个示例的右下角都有一个打开日志视图的图标。在该视图中,你可以看到有关粒子的不同调试数据。每个ParticleSystem3D 都有自己的一行,显示其ParticleSystem3DLogging 数据。
大多数示例的右上角都包含一个设置视图,点击图标即可显示或隐藏该视图。这些设置有助于展示个别 API 功能以及粒子系统的动态行为。
下雪示例
让我们通过 Snowing 示例来了解Particles3D 的基础知识。
首先,使用以下语句导入QtQuick3D.Particles3D 模块:
import QtQuick3D.Particles3D
ParticleSystem3D 是粒子系统的根,负责处理系统定时,并将所有其他相关元素(如粒子、发射器和影响器)组合在一起。
ParticleSystem3D { id: psystem // Start so that the snowing is in full steam startTime: 15000
然后,我们创建一个SpriteParticle3D ,这是一个可视化的 2D 纹理粒子。如果我们需要 3D 模型粒子,也可以使用ModelParticle3D 。使用maxAmount 定义粒子数量非常重要,这样可以分配最佳的缓冲区大小。在这里,我们的雪花是白色的,具有不同的不透明度,它们在 1000 毫秒内淡入淡出。
SpriteParticle3D { id: snowParticle sprite: Texture { source: "images/snowflake.png" } maxAmount: 1500 * sliderIntensity.sliderValue color: "#ffffff" colorVariation: Qt.vector4d(0.0, 0.0, 0.0, 0.5); fadeInDuration: 1000 fadeOutDuration: 1000 }
接下来,我们需要ParticleEmitter3D 来发射上述snowParticle
粒子。shape 属性定义了发射区域。在这里,我们定义雪花具有不同的旋转和大小。通过velocity 属性,您可以定义发射粒子的初始速度方向。每个雪花粒子的存在时间为 15 秒,emiting rate 由设置滑块控制。
ParticleEmitter3D { id: emitter particle: snowParticle position: Qt.vector3d(0, 1000, -350) depthBias: -100 scale: Qt.vector3d(15.0, 0.0, 15.0) shape: ParticleShape3D { type: ParticleShape3D.Sphere } particleRotationVariation: Qt.vector3d(180, 180, 180) particleRotationVelocityVariation: Qt.vector3d(50, 50, 50); particleScale: 2.0 particleScaleVariation: 0.5; velocity: VectorDirection3D { direction: Qt.vector3d(0, sliderVelocityY.sliderValue, 0) directionVariation: Qt.vector3d(0, sliderVelocityY.sliderValue * 0.4, 0) } emitRate: sliderEmitRate.sliderValue * sliderIntensity.sliderValue lifeSpan: 15000 }
通常还会使用一些影响器来使粒子的运动更加有趣。在这个下雪示例中,我们使用Wander3D 让雪花以波浪形曲线飞舞,并使用PointRotator3D 来模拟大风天气。
Wander3D { enabled: checkBoxWanderEnabled.checked globalAmount: Qt.vector3d(sliderWanderGlobalAmount.sliderValue, 0, sliderWanderGlobalAmount.sliderValue) globalPace: Qt.vector3d(sliderWanderGlobalPace.sliderValue, 0, sliderWanderGlobalPace.sliderValue) uniqueAmount: Qt.vector3d(sliderWanderUniqueAmount.sliderValue, 0, sliderWanderUniqueAmount.sliderValue) uniquePace: Qt.vector3d(sliderWanderUniquePace.sliderValue, 0, sliderWanderUniquePace.sliderValue) uniqueAmountVariation: sliderWanderUniqueAmountVariation.sliderValue uniquePaceVariation: sliderWanderUniquePaceVariation.sliderValue } PointRotator3D { enabled: checkBoxRotatorEnabled.checked pivotPoint: Qt.vector3d(0, 0, -350) direction: Qt.vector3d(0, 1, 0) magnitude: sliderRotatorMagnitude.sliderValue }
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.