私は現在、3D アルカノイド クローンに取り組んでおり、プリミティブを使用して立方体を描いています。
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 ドローコード
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);
}
}