Prevent Mipmapping Artefacts with masked Textures

Blitz3D Forums/Blitz3D Tutorials/Prevent Mipmapping Artefacts with masked Textures

jfk EO-11110(Posted 2006) [#1]
There's a certain sort of masked texture that frequently makes troubles when displayed with full mipmapping/high speed settings, especially on Radeon chipsets. These are fences, gatters etc., finely structured masked things with a pattern character.

Let us assume we have a fence, textured with a masked wire fence texture. Let us assume the fence is rotated about 60°, relative to the camera, so it's a steep angle to the camera. It may happen that the more distant parts of the fence will become invisible. In the worst case this is a transparent fence that will pop up when the player gets closer, due to mipmaping artefacts.

There's a trick to prevent this:

The optimal number of the non-transparent pixels is 51 percent of the whole texture. It may be close to 51 percent, but it should under no circumstances be less than 50%.

You may use a simple blitz program to count the non-transparent texels:
graphics3d 640,480,32,2
tex=loadtexture("test.bmp",4)
setbuffer texturebuffer(tex)
lockbuffer()
w=texturewidth(tex)
h=textureheight(tex)
c#=0
for y=0 to h-1
 for x=0 to w-1
  rgb=readpixelfast(x,y) and $ffffff
  if rgb<>0 then
   c=c+1
  endif
 next
next
unlockbuffer()
setbuffer backbuffer()
print "non-transparent texels:"+((c/(w*h))*100)+" percent"
waitkey()


You may use the selection expand/contract functions of your graphics app to rise/lower the number of non-transparent texels on such pattern-like masked textures.

Thought I should let you know. Took me quiet a while until I figured out the 51% thing.

BTW: you may also reduce or fix this problem by loading the texture without mipmaping. But I think this has a lot of cons, so I prefere the 51% way.


jfk EO-11110(Posted 2006) [#2]
Additional Note:

Since Blitz3D Version 1.98 we can use DDS. I would STRONGLY reccommend to save your masked textures as DDS in DXT3 or DXT5 mode, with alpha information. The Alpha should be defined by nonpainted pixels , not in a fourth channel.
Unlike normally loaded masked BMP etc., with the DDS textures the transparent pixels don't have to be black, but transparent (not painted) instead. This will also prevent the dark border artefact as seen with masked textures other than DDS.

Where masked BMP etc. textures may have blocky edges , strictly following the texels zigzag contoures, DDS will use some kind of vector drawing , so the outlines will me smooth and rounded, no matter how close the camera is.

There's a plugin for Photoshop:
http://developer.nvidia.com/object/nv_texture_tools.html
And for The Gimp:
http://nifelheim.dyndns.org/%7Ecocidius/dds/
There's even a standalone tool written in Bitz:
http://www.blitzbasic.com/Community/posts.php?topic=63126

I'd suggest to select the transparent parts, then make a border selection of about 4 pixels, then blur this border (more). This will also blur the alpha, so Directx will be able to interpolate the outline much smoother (IMHO).

Although the pros of masked DXT3 or 5 textures are immense, they still don't fix the problem of vanishing texels as a result of mipmapping (as discussed in the first post). They are however the best choice for masked textures. In fact they're giving Blitz3D yet another afterburner boost.