跳至内容

跨平台游戏编程与 gameplay3d/粒子

来自 Wikibooks,开放世界的开放书籍

gameplay::ParticleEmitter 类定义了模拟和渲染粒子系统所需的所有信息。粒子发射器可以用来表示多种视觉效果,例如烟雾、蒸汽、火焰和爆炸;以及其他大气效果,例如雨和闪电。创建后,发射器可以设置在 gameplay::Node 上以跟随一个物体,或者可以放置在场景中。

创建粒子发射器

[编辑 | 编辑源代码]

使用 ParticleEmitter::create() 方法创建粒子发射器。

此函数有 3 个重载,但您最常使用的可能是从 .particle 文件创建粒子发射器的重载,其函数签名如下

static ParticleEmitter* create (const char *url);

如果您想以编程方式创建粒子发射器,可以使用以下重载,它将创建一个未初始化的 ParticleEmitter。然后,可以使用 ParticleEmitter 类中的成员函数来配置新的粒子发射器。

static ParticleEmitter* create (const char *texturePath, TextureBlending textureBlending, unsigned int particleCountMax);

控制粒子发射器

[编辑 | 编辑源代码]

.particle 文件

[编辑 | 编辑源代码]

以下是使用“sample-particles”示例项目创建的 .particle 文件的注释示例,该项目是与 gameplay3d 一起打包的实用粒子编辑器。

鉴于 .particle 文件中需要完成的字段数量,并且因为在编辑时看到粒子发射器的视觉表示很有帮助,所以“sample-particles”项目可能是开始创建和配置粒子发射器的最简单方法。

particle smoke
{
    sprite
    {
        path = smoke.png        // A path to the image to use as this ParticleEmitter's texture.
        width = 64              // The height of the first frame of the sprite.
        height = 64             // The width of the first frame of the sprite.
        blending = ADDITIVE     // Other options are OPAQUE, TRANSPARENT AND MULTIPLIED.
        animated = false        // Specifies whether particles cycle through the sprite frames.
        looped = false          // Specifies whether sprites are set to loop,
        frameCount = 1          // The number of frames to use for the particle emitter's sprite.
        frameRandomOffset = 0   // Set to 0 for all particles to start on the first frame. Otherwise the starting frame will be a random frame not higher than this value.
        frameDuration = 0       // The duration of a single sprite frame, in milliseconds.
    }

    particleCountMax = 5000             // The number of live particles at any one time will not exceed this value.
    emissionRate = 20                   // The emission rate, measured in particles per second.
    ellipsoid = false                   // An ellipsoid particle emitter spawns particles inside a ellipsoidal domain. (Using an ellipsoidal domain
                                                  is slightly more expensive than the default cuboid domain.)
    orbitPosition = false               // Whether to rotate initial particle positions by the node's rotation matrix.
    orbitVelocity = false               // Whether to rotate initial particle velocity vectors by the node's rotation matrix.
    orbitAcceleration = false           // Whether to rotate initial particle acceleration vectors by the node's rotation matrix.
    sizeStartMin = 3.5                  // The minimum size that each particle can be at the time when it is started.
    sizeStartMax = 3.5                  // The maximum size that each particle can be at the time when it is started.
    sizeEndMin = 15                     // The minimum size that each particle can be at the end of its lifetime.
    sizeEndMax = 15                     // The maximum size that each particle can be at the end of its lifetime.
    energyMin = 4000                    // The minimum lifetime of each particle, measured in milliseconds.
    energyMax = 5000                    // The maximum lifetime of each particle, measured in milliseconds.
    colorStart = 0.5, 0.5, 0.5, 1       // The base start color of emitted particles.
    colorStartVar = 0, 0, 0, 0          // The variance of start color of emitted particles.
    colorEnd = 1, 0, 0, 0               // The base end color of emitted particles.
    colorEndVar = 0, 0, 0, 0            // The variance of end color of emitted particles.
    position = 0, 0, 0                  // The position of new particles, relative to the emitter's transform.
    positionVar = 0, 0, 0               // The position variance of new particles.
    velocity = 2.5, 5, 0                // The initial velocity of new particles.
    velocityVar = 0, 0, 0               // The initial velocity variance of new particles.
    acceleration = -10, 5, 0            // The base acceleration vector of emitted particles.
    accelerationVar = 2.5, 5, 0         // The variance allowed in the acceleration of emitted particles.
    rotationPerParticleSpeedMin = -1.5  // The minimum rotation speed (per particle).
    rotationPerParticleSpeedMax = 1.5   // The maximum rotation speed (per particle).

    editor                              // Particle editor-specific properties
    {
        cameraTranslation = -0.0200001, 4.06, 0
        cameraZoom = 0, 0, 16.8
        cameraRotation = 0, 0, 0, 0
        sizeMax = 20
        energyMax = 5000
    }
}
华夏公益教科书