Qt6 QML Book/图形效果/简单仿真

Simple Simulation

简单仿真

Let us have a look at a very simple simulation to get started. Qt Quick makes it actually very simple to get started with particle rendering. For this we need:

让我们来看一个非常简单的仿真开始。Qt Quick使粒子渲染的入门变得非常简单。为此,我们需要:

  • ParticleSystem which binds all elements to a simulation
  • 将所有元素绑定到仿真的粒子系统ParticleSystem
  • An Emitter which emits particles into the system
  • 向系统发射粒子的发射器Emitter
  • ParticlePainter derived element, which visualizes the particles
  • 由ParticlePaint产生元素对象,用于可视化粒子
import QtQuick
import QtQuick.Particles

Rectangle {
    id: root
    width: 480; height: 160
    color: "#1f1f1f"

    ParticleSystem {
        id: particleSystem
    }

    Emitter {
        id: emitter
        anchors.centerIn: parent
        width: 160; height: 80
        system: particleSystem
        emitRate: 10
        lifeSpan: 1000
        lifeSpanVariation: 500
        size: 16
        endSize: 32
        Tracer { color: 'green' }
    }

    ImageParticle {
        source: "assets/particle.png"
        system: particleSystem
    }
}

The outcome of the example will look like this:

该示例的运行结果如下所示:

We start with an 80x80 pixel dark rectangle as our root element and background. Therein we declare a ParticleSystem. This is always the first step as the system binds all other elements together. Typically the next element is the Emitter, which defines the emitting area based on it’s bounding box and basic parameters for them to be emitted particles. The emitter is bound to the system using the system property.

我们以一个80x80像素的黑色矩形作为根元素和背景。在这里我们声明一个ParticleSystem。这始终是系统将所有其他元素绑定在一起的第一步。通常,下一个元素是发射器Emitter,它根据其边界框和发射粒子的基本参数定义发射区域。发射器使用系统属性system绑定到系统。

The emitter in this example emits 10 particles per second (emitRate: 10) over the area of the emitter with each a lifespan of 1000msec (lifeSpan : 1000) and a lifespan variation between emitted particles of 500 msec (lifeSpanVariation: 500). A particle shall start with a size of 16px (size: 16) and at the end of its life shall be 32px (endSize: 32).

本例中的发射器在发射器区域内每秒发射10个粒子(emitRate: 10),每个粒子的寿命为1000毫秒(lifeSpan : 1000),发射粒子之间的寿命变化为500毫秒(lifeSpanVariation: 500)。颗粒开始时的尺寸应为16px(size: 16),结束时的尺寸应为32px(endSize: 32)。

The green bordered rectangle is a tracer element to show the geometry of the emitter. This visualizes that also while the particles are emitted inside the emitters bounding box the rendering is not limited to the emitters bounding box. The rendering position depends upon life-span and direction of the particle. This will get more clear when we look into how to change the direction particles.

绿色边框矩形是一个示踪元素,用于显示发射器的几何图形。这将可视化显示,当粒子在发射器边界框内发射时,渲染并不限于发射器边界框。渲染位置取决于粒子的寿命和方向。当我们研究如何改变粒子的方向时,这将变得更加清楚。

The emitter emits logical particles. A logical particle is visualized using a ParticlePainter in this example we use an ImageParticle, which takes an image URL as the source property. The image particle has also several other properties, which control the appearance of the average particle.

发射器发射逻辑粒子。使用particlePaint可视化逻辑粒子,在本例中我们使用ImageParticle,它将图像URL作为源属性。图像粒子还具有其他几个属性,这些属性控制平均粒子的外观。

  • emitRate: particles emitted per second (defaults to 10 per second)
  • emitRate:每秒发射的粒子数(默认为每秒10个)
  • lifeSpan: milliseconds the particle should last for (defaults to 1000 msec)
  • lifeSpan:粒子应持续毫秒(默认为1000毫秒)
  • sizeendSize: size of the particles at the beginning and end of their life (defaults to 16 px)
  • sizeendSize: 粒子寿命开始和结束时的大小(默认为16 px)

Changing these properties can influence the result in a drastically way

更改这些属性可以极大地影响结果

Emitter {
    id: emitter
    anchors.centerIn: parent
    width: 20; height: 20
    system: particleSystem
    emitRate: 40
    lifeSpan: 2000
    lifeSpanVariation: 500
    size: 64
    sizeVariation: 32
    Tracer { color: 'green' }
}

Besides increasing the emit rate to 40 and the lifespan to 2 seconds the size now starts at 64 pixels and decreases 32 pixels at the end of a particle lifespan.

除了将发射速率增加到40,寿命增加到2秒之外,该大小现在从64像素开始,在粒子寿命结束时减少32像素。

Increasing the endSize even more would lead to a more or less white background. Please note also when the particles are only emitted in the area defined by the emitter the rendering is not constrained to it.

进一步增大endSize将导致或多或少的白色背景。还请注意,当粒子仅在发射器定义的区域发射时,渲染不受其限制。

示例源码下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值