粒子模拟的机制有点复杂,暂时还没法理解透彻,所以 “粒子模拟”纯粹是一个学习的过程!
简单的例子模拟
简单例子模拟需要做的任务:
- 绑定所有元素到一个模拟的粒子系统(ParticleSystem)。
- 一个向系统发射粒子的发射器(Emitter)。
- 一个ParticlePainter派生元素,用来实现粒子的可视化。
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Particles 2.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Item{
anchors.fill: parent
Rectangle{
id:root
anchors.fill: parent
color: "#1f1f1f"
ParticleSystem{
id:particleSystem
}
Emitter{
id:emitter
anchors.centerIn: parent
width: 160
height: 80
system: particleSystem
// 每秒发射10个粒子
emitRate: 10
// 每个粒子的生命周期
lifeSpan:1000
// 一个已发射粒子的生命周期变化是 500毫秒
lifeSpanVariation: 500
// 一个粒子开始的大小
size:16
// 一个粒子结束时候的大小
endSize: 52
// 绿色边框的矩形是一个跟踪元素,
// 用来显示发射器的几何形状
Tracer{color:"green"}
}
ImageParticle{
source:"qrc:/new/preimg/particle.png"
system:particleSystem
}
}
}
}
发射器发射逻辑粒子。
一个逻辑粒子的可视化使用粒子画笔(ParticlePainter)来 实现,在这个例子中我们使用了图像粒子(ImageParticle),使用一个图片链接作 为源属性。
ImageParticle派生自ParticlePainter!
参数
前面一节初步认识了粒子模拟,其中有很多参数可以控制粒子的显示行为,这节来试验:
代码:
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Particles 2.0
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Item{
anchors.fill: parent
Rectangle{
id:root
anchors.fill: parent
color: "#1f1f1f"
ParticleSystem{
id:particleSystem
}
Emitter{
id:emitter
anchors.centerIn: parent
anchors.fill: parent
system: particleSystem
// 每秒发射10个粒子
emitRate: 10
// 每个粒子的生命周期
lifeSpan:5000
// 一个已发射粒子的生命周期变化是 500毫秒
lifeSpanVariation: 3000
// 一个粒子开始的大小
size:16
// 一个粒子结束时候的大小
endSize: 152
}
ImageParticle{
// 改变粒子图片
source:"qrc:/new/preImg/star.png"
system:particleSystem
// 使用金色来初始化
color: '#FFD700'
// 不同的粒子颜色变化范围为+/- 50%。
colorVariation: 0.3
// 每个粒子首先按顺时针旋转 0 度
rotation: 0
// 不同 的粒子在+/-45度之间变化
rotationVariation: 45
// 每个例子会不断的以每秒15度旋转
rotationVelocity: 15
// 每个粒子的旋转速 度在+/-15度之间变化
rotationVelocityVariation: 15
// 粒子的入场有缩放效果
entryEffect: ImageParticle.Scale
}
}
}
}
主要修改的部分:
ImageParticle{
// 改变粒子图片
source:"qrc:/new/preImg/star.png"
system:particleSystem
// 使用金色来初始化
color: '#FFD700'
// 不同的粒子颜色变化范围为+/- 50%。
colorVariation: 0.3
// 每个粒子首先按顺时针旋转 0 度
rotation: 0
// 不同 的粒子在+/-45度之间变化
rotationVariation: 45
// 每个例子会不断的以每秒15度旋转
rotationVelocity: 15
// 每个粒子的旋转速 度在+/-15度之间变化
rotationVelocityVariation: 15
// 粒子的入场有缩放效果
entryEffect: ImageParticle.Scale
}
运行>>