使用GraphicsDevice.DrawUserPrimitives()后返回SpriteBatch.Draw()

问题描述 投票:0回答:1

在我的2D XNA游戏中,我一直在尝试绘制三角形。我一直在关注教程here

// Game1.FX is a static instance of Effect
Game1.FX.CurrentTechnique = Game1.FX.Techniques["Pretransformed"];

foreach (EffectPass pass in Game1.FX.CurrentTechnique.Passes)
{
    pass.Apply();

    Game1.GameInstance.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, vertices, 0, 1, VertexPositionColor.VertexDeclaration);
}

// Exception is thrown when we go back to spriteBatch.Draw()
spriteBatch.Draw(Textures.Get("Projectiles\\laser01"), new Rectangle((int)compartment.Position.X, (int)compartment.Position.Y,
    compartment.CompartmentWeapon.MaxRange, 2), null, laserColour, gunRotation - compartment.CompartmentWeapon.ArcLOS, Vector2.Zero, SpriteEffects.None, 1);

从GraphicsDevice.DrawUserPrimitives()返回spriteBatch.Draw()时收到System.InvalidOperationException。

“在执行任何绘制操作之前,必须在设备上设置有效的顶点缓冲区(如果使用索引基元,则使用有效的索引缓冲区)。”

如果有人能指出为什么会这样,我将不胜感激。

c# xna spritebatch
1个回答
2
投票

你必须在你的spriteBatch.End()之前打电话给DrawUserPrimitives()。然后在你尝试发行另一个spriteBatch.Draw()之前,你必须发出另一个spriteBatch.Begin()

SpriteBatch.Begin()为您设置所有缓冲区和暂存,以便提供简单的渲染功能。一旦触摸GraphicsDevice,您就有可能对SpriteBatch可能不知道的事情进行更改,并且重新启动SpriteBatch有助于将管道重置为已知状态。

在GraphicsDevice上做一些事情就像更改当前的RenderTarget或清除目标一样。

© www.soinside.com 2019 - 2024. All rights reserved.