MonoGame - ScissorRectangle 不工作
MonoGame - ScissorRectangle not working
我正在尝试使用 RenderTarget2D 和 ScissorRectangle 创建一个可滚动的日志 window。然而,ScissorRectangle 似乎不起作用。这是代码:
spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, new RasterizerState() { ScissorTestEnable = true });
Rectangle scissor = new Rectangle(0, Constants.GAME_AREA_HEIGHT, Constants.GAME_AREA_WIDTH, Constants.LOG_HEIGHT);
Rectangle scissorBackup = this.graphicsDevice.ScissorRectangle;
this.graphicsDevice.ScissorRectangle = scissor;
spriteBatch.Draw(logRenderTarget,
new Rectangle(0, Constants.GAME_AREA_HEIGHT, Constants.GAME_AREA_WIDTH, Constants.LOG_HEIGHT),
Color.White
);
this.graphicsDevice.ScissorRectangle = scissorBackup;
spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null);
我决定改用视口。然而,似乎 MonoGame 在调用 spriteBatch.End()
之前使用了最后一个 set-up 视口。它似乎也与 ScissorRectangle 相同,所以我本可以让它也起作用。我在评论中添加了 ScissorRectangle-specific 代码。这是我的新 class:
public class LogWriter
{
private GraphicsDevice graphicsDevice;
private Viewport defaultViewport, logViewport;
//private Rectangle scissor, scissorBackup;
public void Init(GraphicsDevice graphicsDevice)
{
this.graphicsDevice = graphicsDevice;
defaultViewport = this.graphicsDevice.Viewport;
logViewport = new Viewport(0, Constants.GAME_AREA_HEIGHT, Constants.GAME_AREA_WIDTH, Constants.LOG_HEIGHT);
//scissor = new Rectangle(0, Constants.GAME_AREA_HEIGHT, Constants.GAME_AREA_WIDTH, Constants.LOG_HEIGHT);
//scissorBackup = this.graphicsDevice.ScissorRectangle;
}
public void WriteLog(SpriteBatch spriteBatch)
{
this.graphicsDevice.Viewport = logViewport;
//this.graphicsDevice.ScissorRectangle = scissor;
spriteBatch.Begin();
//spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, new RasterizerState() { ScissorTestEnable = true });
for (int i = 0; i < Log.Count; i++)
{
spriteBatch.DrawString(Scavenger.AssetManager.Font12,
Log.Entries[i],
new Vector2(
Constants.LOG_MARGIN,
Constants.LOG_MARGIN + i * Scavenger.AssetManager.Font12.LineSpacing),
Color.GreenYellow
);
}
spriteBatch.End();
this.graphicsDevice.Viewport = defaultViewport;
//this.graphicsDevice.ScissorRectangle = scissorBackup;
}
}
Init()
在LoadContent()
函数中被调用。 WriteLog()
在 Draw()
函数中被调用,在 End()
在绘制所有其他东西之后在 spriteBatch 上被调用之后。
我正在尝试使用 RenderTarget2D 和 ScissorRectangle 创建一个可滚动的日志 window。然而,ScissorRectangle 似乎不起作用。这是代码:
spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, new RasterizerState() { ScissorTestEnable = true });
Rectangle scissor = new Rectangle(0, Constants.GAME_AREA_HEIGHT, Constants.GAME_AREA_WIDTH, Constants.LOG_HEIGHT);
Rectangle scissorBackup = this.graphicsDevice.ScissorRectangle;
this.graphicsDevice.ScissorRectangle = scissor;
spriteBatch.Draw(logRenderTarget,
new Rectangle(0, Constants.GAME_AREA_HEIGHT, Constants.GAME_AREA_WIDTH, Constants.LOG_HEIGHT),
Color.White
);
this.graphicsDevice.ScissorRectangle = scissorBackup;
spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null);
我决定改用视口。然而,似乎 MonoGame 在调用 spriteBatch.End()
之前使用了最后一个 set-up 视口。它似乎也与 ScissorRectangle 相同,所以我本可以让它也起作用。我在评论中添加了 ScissorRectangle-specific 代码。这是我的新 class:
public class LogWriter
{
private GraphicsDevice graphicsDevice;
private Viewport defaultViewport, logViewport;
//private Rectangle scissor, scissorBackup;
public void Init(GraphicsDevice graphicsDevice)
{
this.graphicsDevice = graphicsDevice;
defaultViewport = this.graphicsDevice.Viewport;
logViewport = new Viewport(0, Constants.GAME_AREA_HEIGHT, Constants.GAME_AREA_WIDTH, Constants.LOG_HEIGHT);
//scissor = new Rectangle(0, Constants.GAME_AREA_HEIGHT, Constants.GAME_AREA_WIDTH, Constants.LOG_HEIGHT);
//scissorBackup = this.graphicsDevice.ScissorRectangle;
}
public void WriteLog(SpriteBatch spriteBatch)
{
this.graphicsDevice.Viewport = logViewport;
//this.graphicsDevice.ScissorRectangle = scissor;
spriteBatch.Begin();
//spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, new RasterizerState() { ScissorTestEnable = true });
for (int i = 0; i < Log.Count; i++)
{
spriteBatch.DrawString(Scavenger.AssetManager.Font12,
Log.Entries[i],
new Vector2(
Constants.LOG_MARGIN,
Constants.LOG_MARGIN + i * Scavenger.AssetManager.Font12.LineSpacing),
Color.GreenYellow
);
}
spriteBatch.End();
this.graphicsDevice.Viewport = defaultViewport;
//this.graphicsDevice.ScissorRectangle = scissorBackup;
}
}
Init()
在LoadContent()
函数中被调用。 WriteLog()
在 Draw()
函数中被调用,在 End()
在绘制所有其他东西之后在 spriteBatch 上被调用之后。