XNA在更新时更新VertexBuffer中顶点的位置

时间:2013-04-09 18:08:28

标签: c# 3d xna-4.0

我正在尝试在XNA 4.0中制作的游戏中创建一个粒子系统。 在初始化发射器的世界中渲染粒子。 问题是我想在Update()上更新粒子的位置,但它不起作用,粒子被渲染,但只在世界的中心位置0,0,0。

每个粒子由一组顶点组成,上面有纹理。

任何人都可以帮助我吗? 这是粒子发射器的类:

public class ParticleEmitter : RenderableGameObject
{
    VertexBuffer vertexBuffer;
    VertexDeclaration vertexDeclaration;
    Effect bbEffect;

    VertexPositionTexture[] vertices;

    Texture2D texture;
    int noVertices = 0;

    struct Particle
    {
        public Vector3 position;
    }
    Particle[] particles = new Particle[5];

    public ParticleEmitter(Game1 game)
        : base(game)
    {
        SetupParticles();

        SetupVertices();

        bbEffect = game.Content.Load<Effect>("Effects/Particle");
        texture = game.Content.Load<Texture2D>("Textures/fire");
    }

    private void SetupVertices()
    {
        int vertexIndex = 0;
        vertices = new VertexPositionTexture[particles.Length * 6];

        for (int i = 0; i < particles.Length; ++i)
        {
            vertices[vertexIndex++] = new VertexPositionTexture(particles[i].position, new Vector2(0, 0));
            vertices[vertexIndex++] = new VertexPositionTexture(particles[i].position, new Vector2(1, 0));
            vertices[vertexIndex++] = new VertexPositionTexture(particles[i].position, new Vector2(1, 1));

            vertices[vertexIndex++] = new VertexPositionTexture(particles[i].position, new Vector2(0, 0));
            vertices[vertexIndex++] = new VertexPositionTexture(particles[i].position, new Vector2(1, 1));
            vertices[vertexIndex++] = new VertexPositionTexture(particles[i].position, new Vector2(0, 1));
        }

        noVertices = vertexIndex;

        vertexBuffer = new VertexBuffer(game.GraphicsDevice, typeof(VertexPositionTexture), vertices.Length, BufferUsage.WriteOnly);
        vertexBuffer.SetData(vertices);
        vertexDeclaration = new VertexDeclaration(VertexPositionTexture.VertexDeclaration.GetVertexElements());
    }

    private void SetupParticles()
    {
        for (int i = 0; i < particles.Length; ++i)
        {
            particles[i] = new Particle();
            particles[i].position = Position * i;
        }
    }

    private void UpdateVertices()
    {
        for (int i = 0; i < particles.Length; ++i)
        {
            vertices[i] = new VertexPositionTexture(particles[i].position, new Vector2(0, 0));
            vertices[i] = new VertexPositionTexture(particles[i].position, new Vector2(1, 0));
            vertices[i] = new VertexPositionTexture(particles[i].position, new Vector2(1, 1));

            vertices[i] = new VertexPositionTexture(particles[i].position, new Vector2(0, 0));
            vertices[i] = new VertexPositionTexture(particles[i].position, new Vector2(1, 1));
            vertices[i] = new VertexPositionTexture(particles[i].position, new Vector2(0, 1));
        }

        game.GraphicsDevice.SetVertexBuffer(null);
        vertexBuffer.SetData(vertices);
    }

    private void UpdateParticles()
    {
        for (int i = 0; i < particles.Length; ++i)
        {
            particles[i].position = Position;
        }
    }

    public override void Update(GameTime gameTime)
    {
        UpdateParticles();
        UpdateVertices();

        base.Update(gameTime);
    }

    public override void Draw()
    {
        DrawParticles(game.camera.viewMatrix);
    }

    private void DrawParticles(Matrix currentViewMatrix)
    {
        bbEffect.CurrentTechnique = bbEffect.Techniques["CylBillboard"];
        bbEffect.Parameters["xWorld"].SetValue(Matrix.Identity);
        bbEffect.Parameters["xView"].SetValue(currentViewMatrix);
        bbEffect.Parameters["xProjection"].SetValue(game.camera.projectionMatrix);
        bbEffect.Parameters["xCamPos"].SetValue(game.camera.Position);
        bbEffect.Parameters["xAllowedRotDir"].SetValue(new Vector3(0, 1, 0));
        bbEffect.Parameters["xBillboardTexture"].SetValue(texture);

        foreach (EffectPass pass in bbEffect.CurrentTechnique.Passes)
        {
            game.GraphicsDevice.BlendState = BlendState.AlphaBlend;

            pass.Apply();
            game.GraphicsDevice.SetVertexBuffer(vertexBuffer);
            int noTriangles = noVertices / 3;
            game.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, noTriangles);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

在这种方法中,你实际上并没有真正改变粒子位置:

private void UpdateParticles()
{
    for (int i = 0; i < particles.Length; ++i)
    {
        particles[i].position = Position;
    }
}

您将每个粒子设置为相同的位置,可能是原点。

您可能想要做的就是为每个粒子添加速度:

struct Particle
{
    public Vector3 position;
    public Vector3 velocity;
}

然后用随机速度初始化每个粒子:

// Pass a shared, per-thread instance of Random into this method
private void SetupParticles(Random random)
{
    for (int i = 0; i < particles.Length; ++i)
    {
        particles[i] = new Particle() {
            position = Vector.Zero,
            velocity = new Vector3((float)(random.NextDouble() * 2 - 1),
                                   (float)(random.NextDouble() * 2 - 1),
                                   (float)(random.NextDouble() * 2 - 1))
        };
    }
}

然后根据某些物理模型更新它们(即:由于重力导致的速度和加速度,随着时间的推移)。

Vector3 gravity = new Vector3(0, -9.8f, 0);

private void UpdateParticles(float time)
{
    for (int i = 0; i < particles.Length; ++i)
    {
        particles[i].velocity += gravity * time;
        particles[i].position += particles[i].velocity * time;
    }
}

(注意:这个答案中的所有代码都是手写的 - 注意语法错误等)。