Qt Quick 3D - Particles 3D 测试平台示例
演示如何使用Qt Quick 3D 的Particles3D模块。

本示例演示了使用QtQuick3D.Particles3D 模块功能的多种方式。Particles测试平台包含一系列示例,其中一些侧重于特定功能,另一些则结合了多种功能以实现所需的外观效果。
常见功能
测试平台中的示例具有一些共同特征。要退出示例并返回启动界面,请点击左上角的返回箭头。
每个示例的右下角都有一个图标,点击可打开日志视图。在此视图中,您可以查看有关粒子的各种调试数据。每个“ParticleSystem3D ”都有独立的一行,显示其ParticleSystem3DLogging 数据。

大多数示例的右上角都包含一个设置视图,点击其图标即可显示或隐藏。这些设置有助于演示 API 的各项功能以及粒子系统的动态行为。

“飘雪”示例
让我们通过“飘雪”示例来了解Particles3D 的基础知识。

首先,使用以下语句导入QtQuick3D.Particles3D 模块:
import QtQuick3D.Particles3DParticleSystem3D 是粒子系统的根节点,负责处理系统的计时,并将粒子、发射器和影响器等所有相关元素聚合在一起。
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
}通常还会使用一些效果器(affector)来使粒子的运动更加生动有趣。在这个“飘雪”示例中,我们使用“随机偏移”(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
}© 2026 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.