WP7 Lagging when drawing lots of primitive cubes
I'm currently working on a 3D Arkanoid clone, and I'm drawing the cubes
using primitives.
I have 6 rows, each with 49 blocks, so 294 blocks total. Normally I'm not
to bothered about optomisation, but the game is ridiculously slower on
lower end phones. I know the issue is with the drawing of the blocks,
because the game runs fine with that code removed.
My code for setting up the primitives is
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;
}
My code for drawing the blocks is basic, but I have no idea how to
optomise it.
//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);
}
}
No comments:
Post a Comment