WP7在绘制大量原始立方体时滞后

时间:2013-08-27 20:14:44

标签: c# windows-phone-7 3d windows-phone-8 xna

我正在研究3D Arkanoid克隆,我正在使用基元绘制立方体。

我有6行,每行有49个块,总共294个块。 通常情况下,我不打算关注验光,但这款游戏在低端手机上的速度要慢得多。我知道问题在于绘制块,因为游戏运行正常,删除了代码。

我设置基元的代码是

VertexPositionNormalTexture[] vertices1 = new VertexPositionNormalTexture[4];
VertexPositionNormalTexture[] vertices2 = new VertexPositionNormalTexture[4];
VertexPositionNormalTexture[] vertices3 = new VertexPositionNormalTexture[4];
VertexPositionNormalTexture[] vertices4 = new VertexPositionNormalTexture[4];
VertexPositionNormalTexture[] vertices5 = new VertexPositionNormalTexture[4];
VertexPositionNormalTexture[] vertices6 = new VertexPositionNormalTexture[4];

public void SetupVertices(){
 vertices1[0].Position = new Vector3(position.X, position.Y, position.Z);
        vertices1[0].TextureCoordinate = new Vector2(1, 1);
        vertices1[1].Position = new Vector3(position.X, position.Y + size.Y, position.Z);
        vertices1[1].TextureCoordinate = new Vector2(1, 0);
        vertices1[2].Position = new Vector3(position.X + size.X, position.Y + size.Y, position.Z);
        vertices1[2].TextureCoordinate = new Vector2(0, 0);
        vertices1[3].Position = new Vector3(position.X + size.X, position.Y, position.Z);
        vertices1[3].TextureCoordinate = new Vector2(0, 1);
        //Another side

        vertices2[0].Position = new Vector3(position.X, position.Y, position.Z);
        vertices2[0].TextureCoordinate = new Vector2(0, 1);
        vertices2[1].Position = new Vector3(position.X, position.Y + size.Y, position.Z);
        vertices2[1].TextureCoordinate = new Vector2(0, 0);
        vertices2[2].Position = new Vector3(position.X, position.Y + size.Y, position.Z + size.Z);
        vertices2[2].TextureCoordinate = new Vector2(1, 0);
        vertices2[3].Position = new Vector3(position.X, position.Y, position.Z + size.Z);
        vertices2[3].TextureCoordinate = new Vector2(1, 1);
        //ANOTHER SIDE
        vertices3[0].Position = new Vector3(position.X + size.X, position.Y, position.Z + size.Z);
        vertices3[0].TextureCoordinate = new Vector2(0, 1);
        vertices3[1].Position = new Vector3(position.X + size.X, position.Y + size.Y, position.Z + size.Z);
        vertices3[1].TextureCoordinate = new Vector2(0, 0);
        vertices3[2].Position = new Vector3(position.X + size.X, position.Y + size.Y, position.Z);
        vertices3[2].TextureCoordinate = new Vector2(1, 0);
        vertices3[3].Position = new Vector3(position.X + size.X, position.Y, position.Z);
        vertices3[3].TextureCoordinate = new Vector2(1, 1);

        vertices4[0].Position = new Vector3(position.X, position.Y, position.Z + size.Z);
        vertices4[0].TextureCoordinate = new Vector2(0, 1);
        vertices4[1].Position = new Vector3(position.X, position.Y + size.Y, position.Z + size.Z);
        vertices4[1].TextureCoordinate = new Vector2(0, 0);
        vertices4[2].Position = new Vector3(position.X + size.X, position.Y + size.Y, position.Z + size.Z);
        vertices4[2].TextureCoordinate = new Vector2(1, 0);
        vertices4[3].Position = new Vector3(position.X + size.X, position.Y, position.Z + size.Z);
        vertices4[3].TextureCoordinate = new Vector2(1, 1);


        vertices5[0].Position = new Vector3(position.X, position.Y + size.Y, position.Z);
        vertices5[0].TextureCoordinate = new Vector2(0, 0);
        vertices5[1].Position = new Vector3(position.X + size.X, position.Y + size.Y, position.Z);
        vertices5[1].TextureCoordinate = new Vector2(1, 0);
        vertices5[2].Position = new Vector3(position.X + size.X, position.Y + size.Y, position.Z + size.Z);
        vertices5[2].TextureCoordinate = new Vector2(1, 1);
        vertices5[3].Position = new Vector3(position.X, position.Y + size.Y, position.Z + size.Z);
        vertices5[3].TextureCoordinate = new Vector2(0, 1);

        //bottom
        vertices6[0].Position = new Vector3(position.X, position.Y, position.Z);
        vertices6[0].TextureCoordinate = new Vector2(0, 0);
        vertices6[1].Position = new Vector3(position.X + size.X, position.Y, position.Z);
        vertices6[1].TextureCoordinate = new Vector2(1, 0);
        vertices6[2].Position = new Vector3(position.X + size.X, position.Y, position.Z + size.Z);
        vertices6[2].TextureCoordinate = new Vector2(1, 1);
        vertices6[3].Position = new Vector3(position.X, position.Y, position.Z + size.Z);
        vertices6[3].TextureCoordinate = new Vector2(0, 1);
        numTriangles = 4;
        indices = new short[numTriangles + 2];

        int i = 0;
        indices[i++] = 0;
        indices[i++] = 1;
        indices[i++] = 3;
        indices[i++] = 2;
}

我绘制块的代码是基本的,但我不知道如何对其进行反光。

//Game1.CS Code
GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp; // need to do this on reach     devices to allow non 2^n textures


        #region GamePlaying
        if (gameState == State.BREAKOUT || gameState == State.GAMEOVERBREAKOUT)
        {
            foreach (Block block in lastBlocks)
            {
                block.Draw(camera);
            }
            foreach (Block block in fourthBlocks)
            {
                block.Draw(camera);
            }
            foreach (Block block in thirdBlocks)
            {
                block.Draw(camera);
            }
            foreach (Block block in secondBlocks)
            {
                block.Draw(camera);
            }
            foreach (Block block in firstBlocks)
            {
                block.Draw(camera);
            }
            foreach (Block block in fifthBlocks)
            {
                block.Draw(camera);
            }
}

Block.CS Draw Code

public void Draw(Camera camera)
    {
        effect.View = camera.View;
        effect.Projection = camera.Projection;
        effect.World = rotationMatrix;


        foreach (EffectPass pass in effect.CurrentTechnique.Passes)
        {
            pass.Apply();
            graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleStrip, vertices1, 0, 4, indices, 0, numTriangles);
            graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleStrip, vertices2, 0, 4, indices, 0, numTriangles);
            graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleStrip, vertices3, 0, 4, indices, 0, numTriangles);
            graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleStrip, vertices4, 0, 4, indices, 0, numTriangles);
            graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleStrip, vertices5, 0, 4, indices, 0, numTriangles);
            graphicsDevice.DrawUserIndexedPrimitives(PrimitiveType.TriangleStrip, vertices6, 0, 4, indices, 0, numTriangles);
        }

    }

1 个答案:

答案 0 :(得分:1)

每个块调用DrawUserIndexedPrimitives是个问题。这种绘制调用背后的想法是让你用很少的调用绘制大量的原语,而不是进行多次调用。

您有两种选择:

  1. 使用SpriteBatch绘制四边形(这在内部为您执行批处理,因此您无需处理它)。
  2. 不是直接在每个Draw调用中调用DrawUserIndexedPrimitives,而是在临时缓冲区中累积顶点(和重新编号的索引),并在累积所有对象时提交一次(对于一组索引,每个Effect)。
  3. 希望这有帮助。