在xna中使用光栅化器剪切错误

本文关键字:错误 xna | 更新日期: 2023-09-27 17:49:27

我试图使用剪刀来剪辑使用spriteBatch绘制的文本。细绳方法。

下面是我的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;

在xna中使用光栅化器剪切错误

这意味着您不能修改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;

我自己搞定了。

事实证明,剪切只在spriteBatch.End()中完成。

因此,我需要结束所有绘图到这一点,使用下面的spriteBatch.Begin()重载,spriteBatch.End()来结束剪切的绘图,并使用spriteBatch.Begin(.)

重新开始绘图的其余部分
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, rasterizerState);