跳至內容

跨平臺遊戲程式設計與 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
    }
}
華夏公益教科書