Masking or Alpha Channel?

Blitz3D Forums/Blitz3D Programming/Masking or Alpha Channel?

ClayPigeon(Posted 2010) [#1]
I've created a descent grass system that can cover a 256x256 terrain with dense grass without dropping below 60FPS, and it's completely up to my standards except for one thing. Since the grass texture on the quads is masked, so when a blade of grass in the grass texture is smaller than one pixel, it isn't rendered. So, this effectively limits the distance grass is rendered depending on the user's screen resolution. I didn't notice it at first, because my screen is 1440x900. But when I drop the resolution to 800x600, it becomes evident that the grass stops a few feet it front of you:

So, I tried using alpha channel, but even though it ended the first problem, it made all grass look blackish, because it was interpolating the grass color to black (color behind 0 alpha). I was happy with how it looked with masking, but I want it to be visible further. Anyone know what to do about this?

Last edited 2010

Last edited 2010

Last edited 2010


Rroff(Posted 2010) [#2]
Using alpha will result in all sorts of sorting and lighting (colored lighting not having effect, etc.) issues even if you batch it. It might be possible with careful use of additive blending to fade it out nicer at a distance.

If you use FastExt some of the custom alpha blends do work differently i.e. 1bit alpha which works like mask but handle distance better, tho again issues with light and sorting if your not really careful.

Last edited 2010


jfk EO-11110(Posted 2011) [#3]
Rroff is correct about the Z-Sorting Dilemma with alpha blending. Something we are suffering of since the begining. It has always been a bit of a mystery to work around this problem. I used to release a Grass Demo some years ago, and somebody else made it a singlesurface thing and added a Vertex structure based alpha sorting.

Later I made a grass system for my engine, see demo 8 on my fps project page. There I used a further method to alphasort by code. The trick is to reposition all entities starting with the one that is closest to the camera, ending with the one that is farthest away from it. It depends on if it is a single surface system, or if the grass quads or meshes are seperate entities. Using Copyentity allows individual entities using the same brush (alafair), but a singlesurface system is still faster.

Mask will most likely always look bad. Especially simple BMP or so. You have to use DDS, get an export pugin for gimp or photoshop, see also code archives for an exporter in Blitz (tho not sure how it will handle alpha etc.).

The DXT3 and 5 of DDS formats will allow you to save alpha and masks in a much more sophisticated way. For masks you can force directX to interpolate your outline, like in vector graphics. Here's a sample:

See the rounded shapes (it's 512 pixels)? A masked BMP would not only be much blockier, with checkerboardlike seams, it would also bleed to black at the seams.

Here's an important trick on how to make the image use a good matte color:
in your Graphics app make an additional layer to the texture, behind the original. Set the layer to about 2% opaque (or 98% transparent), almost invisible. Then blur it. This way the "matte color" is used as desired, but the masking remains as in the original.
Save this as DDS with alpha of course.

In the DDS options you can also define how it should handle the various stages of mipmapping, and therefor you can allow exceptional rendering depending on distance. For example a wirefence that fades to a semitransparent grey when the structure becomes so small that it normally would fade away (as in your description).
Now that I have revealed the top secrets of alpha grass, let me show off my grass :)


Last edited 2011


ClayPigeon(Posted 2011) [#4]
Okay, I got the exporter for DDS textures in GIMP. But, when I try to load it in Blitz, it tells me the texture doesn't exist. I don't know if I'm exporting it wrong or what, but how do I set the mask and get the sharp-edge effect you were were referring to? BTW, I think I can solve my distance problem by giving the grass billboard image custom mipmaps in which the further away mipmaps have thicker grass. Also I don't have each grass quad as a separate entity. I have the land divided into squares. Each square is a separate mesh, filled with grass quads (AddMesh). I can't EntityAutoFade every single grass quad as an individual entity since it's kind of slow, so I devised this square-mesh plan and just EntityAutoFade'd those. And also, do DDS textures automatically set the texture flags, or do I still have to set them myself?

EDIT: Okay, never mind. I figured it out. I found one of your older posts which helped tremendously. Thanks for your help!

Last edited 2011


D4NM4N(Posted 2011) [#5]
Alpha all the way i say. Masks look poo.


ClayPigeon(Posted 2011) [#6]
Alpha more than doubles the processing power required where it is not really required. Plus, it makes the edges look blurry. It depends on what you are going for.


Kryzon(Posted 2011) [#7]
Did Gabriel just post something here and it vanished? The forum shows he made a post 1 hour ago from this message of mine, but it isn't showing up.


jfk EO-11110(Posted 2011) [#8]
He should know it. Maybe there's a redundant topic somewhere?

BTW I think Alpha is not always double as slow as simple diffuse mode: when A texel is full transparent then it should be even faster, at least in theory. When it is fully opaque the there also needs to be no mixing. Furthermore the blurness of the alpha mode is caused mostly by mipmapping, something you can control or even turn off in DDS, if you want. Where Alpha has this tendency for blurry edges, masked ones will always be bound to the screen pixel width. It may look ok in a high resolution when you don't watch it closely, but the edges are too sharp to look realistic. In my opinion the only problem with alpha is the sorting issue. Masks are ok for certain things, shure. Especially things that really need sharp edges. At the other hand you should try to use Alpha for a few things only, so you only have to sort a few things.

Using Masks for grass because it is technically easier, or faster, wouldn't be that much of a wise decision IMHO. At the end of the day, a screenshot must look convincing. And later on, it must look good when you walk trough it.

For all kinds of Vegetation you have to use some LOD tricks to make it rea fast. There are methods that are using Billboard quads or sprites for distant patches, speeding things up by enormous amounts. Instead of rendering the same grass patch hundreds of times, render it one time, copy the texture to a billboard quads texture and use this instead, the player most likely won't see any diffrence.

This way you can display highly dense content. If you try to render everything in a NON-LOD way, then that's not really efficient or professional. (Have to say, as long as an engine is not really designed for outdoor scenes, LOD implementation is not always given. For indoor stuff people may prefere some sort of VIS Occlusion instead, rather than in combination with LOD, except maybe for NPCs)

It is nice if you offer the ability to load aditional LOD meshes to whatever your leveleditor allows to load, but for something like an embeded Grass system for huge outdoor worlds I'd say LOD is mandatory.

Last edited 2011


ClayPigeon(Posted 2011) [#9]
DDS Saves the day!!!:



If you look closely, you can see the grass continues to the distance I originally intended it to reach. Also, it took me forever to figure out how to get the transition from real grass to flat grass texture to be unnoticeable. I was wondering, is there a way to make custom mipmaps with DDS textures?


jfk EO-11110(Posted 2011) [#10]
Looks much better! My experience with the settings for the Mipmaps is not really substancial, I tried many combinations and found it rather hard to find good settings, also because the phtoshop plugin by Nvidia often crashed my machine after a dozen or two savings. I would guess the Gimp Plugin gives access to all options too, so you may have to spend some time in trial and error, then note down the useful settings. Custom mipmaps? Do you mean from custom images? I don't think so. You can define the number of levels, the distances they are fadeing, and eg. if they should fade to a color or to transparency. Well maybe I'm wrong about that.


ClayPigeon(Posted 2011) [#11]
Furthermore the blurness of the alpha mode is caused mostly by mipmapping, something you can control or even turn off in DDS


No, I'm referring to the effect of linear interpolation. The alpha values are interpolated through the image as well and I don't think there's a way to turn off interpolation in B3D except for (like your image above) it uses a threshold-type thing that quantizes alpha values to 1 or 0.

I'd say LOD is mandatory.


Well, dur! :) How do ya think I was getting 60FPS on a (now) 512x512 terrain? BTW, I think I'm going to start a thread as a database for people to share methods of efficiently yet realistically rendering different types of vegetation. It would be really helpful to those who want to make good outdoor scenes.


D4NM4N(Posted 2011) [#12]
Alpha more than doubles the processing power required where it is not really required.
Does it? and anyway, unless you are coding for a 10+ year old pc i dont see where this would be a problem.
Plus, it makes the edges look blurry.
not if you draw them properly.
It depends on what you are going for.
Exactly, although mask looks terrible i can think of very few cases where it would be useful anymore in 2d or 3d tbh... except where there extreme size or processing issues (eg. programming games for a non-smart phone or where you are -really- going for the retro look)

Last edited 2011


Ross C(Posted 2011) [#13]
Alpha in blitz3D i've found, really kills the frame rate, if your using it for particle, or anything that is repeated/overlapped on the screen many times, like grass. The DDS masking is a really nice way to do it :)