我正在尝试使用Scissoring剪切使用spriteBatch.DrawString方法绘制的文本。
以下是我的Draw方法的一部分:
public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
...
spriteBatch.GraphicsDevice.RasterizerState = new RasterizerState();
spriteBatch.GraphicsDevice.RasterizerState.ScissorTestEnable = true;
Rectangle rectangle = new Rectangle();
rectangle.Height = 50;
rectangle.Width = 1; //set to 1 just so I'll immediately know if it's working
spriteBatch.GraphicsDevice.ScissorRectangle = rectangle;
for (int i = 0; i < career.news.Count; i++)
{
Vector2 newsItems = new Vector2(200 + i * 80, 550);
spriteBatch.DrawString(DefaultText, career.news[i], newsItems, Color.Black);
}
spriteBatch.GraphicsDevice.RasterizerState.ScissorTestEnable = false;
...
}
我收到以下错误:
“无法更改只读RasterizerState。状态对象在第一次绑定到GraphicsDevice时变为只读。要更改属性值,请创建一个新的RasterizerState实例。”
我真的不明白,因为我在上面的代码中创建了一个新实例。我错过了什么?
编辑:修改后的代码 - 删除了错误,但裁剪仍无效:
Rectangle rectangle = new Rectangle();
rectangle.Height = 50;
rectangle.Width = 1;
RasterizerState rasterizerState = new RasterizerState();
rasterizerState.ScissorTestEnable = true;
spriteBatch.GraphicsDevice.RasterizerState = rasterizerState;
spriteBatch.GraphicsDevice.ScissorRectangle = rectangle;
for (int i = 0; i < career.news.Count; i++)
{
Vector2 newsItems = new Vector2(200 + i * 80, 550);
spriteBatch.DrawString(DefaultText, career.news[i], newsItems, Color.Black);
}
RasterizerState rasterizerState2 = new RasterizerState();
rasterizerState2.ScissorTestEnable = false;
spriteBatch.GraphicsDevice.RasterizerState = rasterizerState2;
答案 0 :(得分:3)
这意味着您无法修改spriteBatch.GraphicsDevice.RasterizerState
属性。您需要做的是创建一个实例,设置其属性,然后将其传递给spriteBatch.GraphicsDevice.RasterizerState
。
RasterizerState r = new RasterizerState();
r.ScissorTestEnable = true;
spriteBatch.GraphicsDevice.RasterizerState = r;
....
RasterizerState r1 = new RasterizerState();
r1.ScissorTestEnable = false;
spriteBatch.GraphicsDevice.RasterizerState = r1;
答案 1 :(得分:2)
管理自己解决这个问题。
事实证明,剪刀只在spriteBatch.End()完成。
因此我需要结束所有绘图到此时,使用下面的spriteBatch.Begin()重载,spriteBatch.End()来结束剪切的绘图,并使用spriteBatch.Begin(..重新启动绘图的其余部分)。 )
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, rasterizerState);