Qt6 QML Book/图形效果/粒子绘制器

Particle Painters

粒子绘制器

Until now we have only used the image based particle painter to visualize particles. Qt comes also with other particle painters:

到目前为止,我们只使用基于图像的粒子绘制器来可视化粒子。Qt还附带了其他粒子绘制工具:

  • ItemParticle: delegate based particle painter
  • ItemParticle: 基于委托的粒子绘制器
  • CustomParticle: shader based particle painter
  • CustomParticle: 基于着色器的粒子绘制器

The ItemParticle can be used to emit QML items as particles. For this, you need to specify your own delegate to the particle.

ItemParticle可用于将QML项作为粒子发射。为此,需要为粒子指定自己的委托项。

ItemParticle {
    id: particle
    system: particleSystem
    delegate: itemDelegate
}

Our delegate, in this case, is a random image (using Math.random()), visualized with a white border and a random size.

在本例中,我们的委托项是一个随机图像(使用Math.random()),以白色边框和随机大小显示。

Component {
    id: itemDelegate

    Item {
        id: container
        width: 32 * Math.ceil(Math.random() * 3)
        height: width
        Image {
            anchors.fill: parent
            anchors.margins: 4
            source: 'assets/' + root.images[Math.floor(Math.random() * 9)]
        }
    }
}

We emit 4 images per second with a lifespan of 4 seconds each. The particles fade automatically in and out.

我们每秒发射4个图像,每个图像的寿命为4秒。粒子会自动淡入淡出。

Qt6 QML Book/图形效果/粒子绘制器

For more dynamic cases it is also possible to create an item on your own and let the particle take control of it with take(item, priority). By this, the particle simulation takes control of your particle and handles the item like an ordinary particle. You can get back control of the item by using give(item). You can influence item particles even more by halt their life progression using freeze(item) and resume their life using unfreeze(item).

对于更动态的情况,也可以自己创建一个项,并让粒子通过take(item, priority)控制它。通过这种方式,“粒子模拟”可以控制粒子,并像处理普通粒子一样处理该项目。您可以通过使用give(item)恢复对该项的控制。通过使用freeze(item)停止物品粒子的生命进程,并使用unfreeze(item)恢复其生命,可以对项粒子做更多操作。

 示例源码下载

上一篇:个人版机房收费——数据库设计


下一篇:实现无限滚动效果Vue