Transparency, Alpha-Blending: need help: ?

Blitz3D Forums/Blitz3D Beginners Area/Transparency, Alpha-Blending: need help: ?

etxtra(Posted 2011) [#1]
Hi everyone ! Please, I would like to make some trees in my blitz3D game, so how can I have only the black pixels of the trees textures invisible ? What commands should I use to achieve transparency?
how can I make some color transparent and the others not ?


Yasha(Posted 2011) [#2]
You have four options for transparency when loading a texture:

1) None (obviously)
2) Masked (black is transparent)
3) By brightness (the brightness of the combined colour value of the pixel sets its gradual transparency)
4) Alpha channel (the transparency is part of the image and just loaded from it)

One thing it may help to understand is that the latter three are all the same internally. When loading the image according to the flags you specify, if you go for alpha (on an image without an alpha channel) or masking, the alpha channel will be created on the spot and modified to hold the values Blitz3D thinks it should. So you can't, for example, draw black spots on a texture later and expect bits to disappear, the way they would with a masked 2D image.

You can control whether masking is used by applying flag 4 to the optional flags when loading, and proper alpha by applying flag 4. Blitz decides whether an image has an alpha channel based on its file format (I think... I forget), so BMP and JPG will be created with new alpha channels based on their brightness, while PNG will load the file's existing alpha channel. (PNG is pretty much the best format for everything.)

The advantages and disadvantages are simple enough. A built-in alpha channel is always the way to get the best results, but you have to have edited it yourself in an image editor that allows that (something better than MS Paint).

Loading an alpha channel on a texture that doesn't have it is a bad move for pretty much everything because it uses the colour map to create it: this means very little detail will actually be visible in your resulting texture and almost all of it will be transparent to varying degrees unless you used a lot of hard black and white. The main exception is things like flare particles, that have a single solid colour and fuzzy edges, so it saves you a little work (although even then, I think it's a bit sloppy).

Masking is the simplest to understand, as long as you know it's just a "cheat" at load-time to create an alpha channel and can't be easily modified at runtime by drawing on it. Black pixels are set to transparent, and everything else is drawn. While this is very simple to do (LoadTexture with flags 1+4 - it's normal to have explicit plusses so you can quickly see which ones you meant to include), it has two big visual problems.

1) It's blocky. Because the alpha is there-or-not rather than the smooth gradients of a properly drawn channel, any diagonal or curved edges will have a jagged appearance.

2) You tend to get ugly black outlines. The bilinear filtering on textures will blur together pixels so that textures appear smooth, which is normally fine but it will also sometimes blur the edge pixels with the black colour of the masked pixels, causing a faint black outline to appear on your texture with no obvious way to remove it.

Anyway, the answer to your question is to use LoadTexture and add four to the flags you're using (or 1+4 if you weren't supplying flags). If the above issues with masking become a problem for you, the other option is to supply an explicit alpha channel, or to take a look in the code archives for any functions that fix the masked image loader (both are relatively easy to correct, in some cases).

I hope this helps.

Last edited 2011


jfk EO-11110(Posted 2011) [#3]
As an additional suggestion I'd say: If you are using Photoshop, The Gimp or a similar Painting Program, get yourself a DDS exporter. Blitz can make use of DXT1, DXT3 and DXT5 of the DDS texture format. In your graphics app you will simply leave the background unpainted, then export it as DXT3 or 5. In order to get a perfect Matte Line (the outline of your masked object) you should use a slightly blured version of the object as an underlying additional layer (in the graphics app), with transparency set to about 2% (2% opaque). This sounds like some irrelevant tech bla, but in fact this is top pro knowledge that seperates some hobby works from retail title material.
(I think it was Gabriel (no?) who gave me this hint some time ago, thanks again!)