2

I seem to be getting really blurry textures when I look at my textures up close. I am creating a minecraft-like terrain thing and I would like the textures to be pixelated - like it is made rather than XNA try to smooth it out for me.

Here is how it appears: http://s1100.photobucket.com/albums/g420/darestium/?action=view&current=bluryminecraftterrain.png

Any suggestions would be much appeciated.

Michael J. Barber
  • 24,518
  • 9
  • 68
  • 88
Darestium
  • 643
  • 2
  • 11
  • 29

1 Answers1

11

It's not related to anti-aliasing... it's related to how the hardware samples the texels in the texture. The default filter in XNA is usually Linear, but to get those "blocky" looking textures you must use Point.

In C# you can set any of your SamplerStates to use PointWrap. This is a combination of point filtering with UV wrapping.

// any state index from 0 to 15, textures usually take 0 first    
GraphicsDevice.SamplerStates[0] = SamplerState.PointWrap; 

However it must the one assigned to the same register as the SamplerState. eg. register s0 will usually be SamplerStates[0]. Alternatively you can enforce sampler states on the shader, and set your registers there:

sampler2D textureSampler : register(s0) = sampler_state
{
    Texture = <Texture>;
    MipFilter = Point;
    MagFilter = Point;
    MinFilter = Point;
    AddressU = Wrap;
    AddressV = Wrap;
};

You can also force mipmapping off with MipFilter set to None.

Chris C
  • 2,003
  • 15
  • 18
  • Should the filters not be Nearest instead of Linear? – Felix K. Dec 01 '11 at 09:09
  • @FelixK. You're right, I made that correction. The filter is actually called Point, which is the same as a nearest-neighbor filter. – Chris C Dec 01 '11 at 09:15
  • Well, that first line doesn't seem to do anything at all, and I have no idea how to write effect files yet. so the latter opinion is closed for me :). Any ideas? (note I do have mipmapping enabled with that texture that I am rendering) – Darestium Dec 01 '11 at 09:32
  • You are using BasicEffect, right? Are you setting GraphicsDevice.SamplerStates[0] as soon as before you apply the effect pass? One gotcha I should probably mention is that using SpriteBatch grabs SamplerState 0 and [resets some states to it](http://blogs.msdn.com/b/shawnhar/archive/2010/06/18/spritebatch-and-renderstates-in-xna-game-studio-4-0.aspx). You should switch sampler states again before rendering the mesh. – Chris C Dec 01 '11 at 09:45
  • Nope, I am using riemers effect file, and yes it performs the assignment directly before I start rendering my vertices. – Darestium Dec 01 '11 at 19:55
  • Rimers has a lot of tutorials so I don't know which one that is. You should probably clarify the problem and use some code in your question. – Chris C Dec 02 '11 at 01:48
  • Well, I use the effect file which is used the flight simulator tutorial. And my problem is that the textures appear the same as they did without that line in place - Blurry and not pixelated. – Darestium Dec 02 '11 at 05:34
  • I tryed the line in a different location! Now it works like a 'charm' ;) thanks – Darestium Dec 03 '11 at 04:28
  • +1 for explaining how to do it via the GraphicsDevice (and thus 3D) level, and not just on the SpriteBatch – Kyle Baran Mar 18 '14 at 00:58