sco*_*kel 15
在XNA 4中,更改SpriteBatch.Begin()以将采样器状态设置为SamplerState.PointClamp
如果您使用着色器绘制图像,则可以修改采样器状态:
sampler2D mySampler = sampler_state
{
Texture=<SomeTexture>;
Filter=POINT;
};
Run Code Online (Sandbox Code Playgroud)
点采样应防止GPU在对图像进行采样时进行插值,这可能是导致您的抗锯齿/模糊行为的原因.
如果您只是SpriteBatch用来绘制图像,可以使用以下方法设置过滤器:
Device.SamplerStates[0] = SamplerState.PointClamp;
Run Code Online (Sandbox Code Playgroud)
此外,您似乎可能必须将SpriteBatch设置为使用立即模式.有关详细信息,请参阅MSDN上的这篇文章,或在App Hub论坛上查看此主题.
这是一个早期的SO线程,可能会有所帮助:
使用标准化坐标(0..1,0..1)而不是纹素坐标对纹理进行采样.GPU将找到给定纹理坐标的四个最接近的纹素,并基于该正方形内的样本点的位置在它们之间进行插值.
所以,如果我的纹理是10 x 10像素,并且我尝试从(0.15,0.15)读取,则GPU将在(1,1),(2,1)处的纹素之间进行插值,( 1,2)和(2,2).在这种情况下,0.05应该使得屏幕上的结果像素简单地是四个周围像素的平均值.但是,如果纹理在(0.19,0.19)处采样,则所得到的颜色将在(2,2)处严重偏向纹素.
点采样将使GPU始终从底层纹理读取精确颜色,而不是加权样本区域周围的坐标.
这是一个有效的.Draw()方法,它说明了所有这些:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
var largeRect = new Rectangle(50, 50, sprite.Width * 3, sprite.Height * 3);
/// start the batch, but in immediate mode to avoid antialiasing
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.Opaque);
/// set the filter to Point
GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
/// draw the sprite
spriteBatch.Draw(sprite, largeRect, Color.White);
/// done!
spriteBatch.End();
// TODO: Add your drawing code here
base.Draw(gameTime);
}
Run Code Online (Sandbox Code Playgroud)