Sign in to follow this  

How is foliage done in modern games?

This topic is 2845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

All modern games seem to have foliage that is a texture with transparency, except that you can't see the pixels, and the edge between the transparent stuff and the leaves are really smooth. How is that done?

Share this post


Link to post
Share on other sites
Even with high-res textures and filtering, you still get a (rounded) staircase pattern if you zoom in close enough.

One way to get really smooth edges is to store a distance-field in the alpha channel instead of an alpha-mask. This gets high-res looking results with only low res textures, plus it works on old hardware (like the Wii ;P).

Another technique to look into is alpha to coverage which lets alpha masks cooperate with MSAA.

Share this post


Link to post
Share on other sites
Quote:
Original post by Hodgman
Even with high-res textures and filtering, you still get a (rounded) staircase pattern if you zoom in close enough.
interpolating the alphachannel and applying alpha-test is how distancefields work.

Share this post


Link to post
Share on other sites
Quote:
Original post by Krypt0n
Quote:
Original post by Hodgman
Even with high-res textures and filtering, you still get a (rounded) staircase pattern if you zoom in close enough.
interpolating the alphachannel and applying alpha-test is how distancefields work.
The alpha-channel doesn't contain a simple mask though, each pixel contains the shortest distance to a pixel of the opposite type (type being "inside" or "outside" the mask). Inside pixels store positive distances, outside pixels store negative distances. All distances are biased by 127 to fit them in a unsigned byte, hence 50% alpha == 0 distance to the nearest edge.
Because a distance-field is a very smooth gradient, it interpolates much better than a traditional alpha mask, which means you don't get (rounded) staircase patterns - if you zoom in really close it actually looks like it's made out of polygons (in the pic below, look at the top of the 2 or 3).
e.g. in this picture the actual texture for each character is about 36 pixels, but the texture is stretched about 10x bigger than that (click for full size).
The distance-field data also makes the bevel, outline and drop-shadow effects ridiculously efficient to implement in a pixel shader ;)

Share this post


Link to post
Share on other sites
Quote:
Original post by Hodgman
Quote:
Original post by Krypt0n
Quote:
Original post by Hodgman
Even with high-res textures and filtering, you still get a (rounded) staircase pattern if you zoom in close enough.
interpolating the alphachannel and applying alpha-test is how distancefields work.
The alpha-channel doesn't contain a simple mask though, each pixel contains the shortest distance to a pixel of the opposite type (type being "inside" or "outside" the mask).
for alpha-blend-textures it means above and below the treshold.

Quote:

Because a distance-field is a very smooth gradient, it interpolates much better than a traditional alpha mask, which means you don't get (rounded) staircase patterns

usually for alphablended geometry you already have smooth alpha values, you can easily switch to alpha test (if the diffuse is created properly) and get away with a nice smooth shape.

Quote:
- if you zoom in really close it actually looks like it's made out of polygons (in the pic below, look at the top of the 2 or 3).
e.g. in this picture the actual texture for each character is about 36 pixels, but the texture is stretched about 10x bigger than that (click for full size).
The distance-field data also makes the bevel, outline and drop-shadow effects ridiculously efficient to implement in a pixel shader ;)

efficiency seems to be relative, cause a game like FarCry(1) had similar smooth tree-sillouettes and did not use any shader instruction to accomplish that, just by passing the alpha to the alpha-test-stage.

Share this post


Link to post
Share on other sites
Quote:
Original post by Krypt0n
efficiency seems to be relative, cause a game like FarCry(1) had similar smooth tree-sillouettes and did not use any shader instruction to accomplish that, just by passing the alpha to the alpha-test-stage.

I suggest you read the distance field paper linked earlier. Distance fields can be rendered with just an alpha test and produce the same results, the pixel shader is just to add the gradients/outline/shadow/etc.

Quote:
Original post by Krypt0n
usually for alphablended geometry you already have smooth alpha values, you can easily switch to alpha test (if the diffuse is created properly) and get away with a nice smooth shape.

No, a regular alpha channel (done as a mask for blending) is not the same as a distance field. It defines a (possibly antialiased) boundary, not a continuous field of values. As such, when alpha test is applied you'll get stair-stepping with a regular alpha mask, but a distance field ends up much smoother.

I wouldn't be surprised if FarCry is using distance fields for it's foliage.

Share this post


Link to post
Share on other sites
Quote:
Original post by Krypt0n
usually for alphablended geometry you already have smooth alpha values, you can easily switch to alpha test (if the diffuse is created properly) and get away with a nice smooth shape.
Yes, this is not a distance field though. It doesn't produce the same results. Same code, different data.
Quote:
efficiency seems to be relative, cause a game like FarCry(1) had similar smooth tree-sillouettes and did not use any shader instruction to accomplish that, just by passing the alpha to the alpha-test-stage.
Far cry also didn't perform outlined/shadowed/bevelled-font rendering on it's foliage! That was just an aside on other things that distance-field data is useful for (besides foliage). I mentioned to begin with that it works on old hardware (by simply performing an alpha-test on the data)! I'm confused as to what you're saying...

Share this post


Link to post
Share on other sites
Quote:
Original post by Hodgman
Quote:
Original post by Krypt0n
usually for alphablended geometry you already have smooth alpha values, you can easily switch to alpha test (if the diffuse is created properly) and get away with a nice smooth shape.
Yes, this is not a distance field though. It doesn't produce the same results. Same code, different data.
Quote:
efficiency seems to be relative, cause a game like FarCry(1) had similar smooth tree-sillouettes and did not use any shader instruction to accomplish that, just by passing the alpha to the alpha-test-stage.
Far cry also didn't perform outlined/shadowed/bevelled-font rendering on it's foliage! That was just an aside on other things that distance-field data is useful for (besides foliage). I mentioned to begin with that it works on old hardware (by simply performing an alpha-test on the data)! I'm confused as to what you're saying...
I'm just saying how it's done. that was the question from peon1234.
The data is created by artist who controll themself how it should look like, the result is the same and created without any distance-field paper in mind, since the first days of alpha test
(tho, you have better quality now than we got back in the days where trees had 64x32 pixel, just because of resolution).


Share this post


Link to post
Share on other sites
Okay, I don't believe my eyes, so let me get this straight:
If you have the distance field in the alpha channel of an image, and if you enable alpha test with grater than 0.5, you get that smooth result presented in the paper with fixed pipeline (for example in openGL)?
I'm stunned.

Does Photoshop have a plugin for producing the distance field?
But it seems simple to write your own program for that too.

Share this post


Link to post
Share on other sites
Quote:
Original post by Krypt0n
The data is created by artist who controll themself how it should look like, the result is the same and created without any distance-field paper in mind, since the first days of alpha test

No, the result won't be the same. A hand-painted alpha mask will not create the same result as a properly generated distance field. It'd be like trying to paint a cube map - a skilled artist could approximate the distortion needed, but it's never going to be as accurate as generating one properly.

Share this post


Link to post
Share on other sites
Quote:
Original post by szecs
If you have the distance field in the alpha channel of an image, and if you enable alpha test with grater than 0.5, you get that smooth result presented in the paper with fixed pipeline (for example in openGL)?
I'm stunned.

Yup, it's rather impressive. :) I had some experimental pictures and code on my journal a while ago, with some more links if you're interested.

And by a strange coincidence I'm currently writing a font renderer using the same technique.

Share this post


Link to post
Share on other sites
Quote:
Original post by OrangyTang
Quote:
Original post by Krypt0n
The data is created by artist who controll themself how it should look like, the result is the same and created without any distance-field paper in mind, since the first days of alpha test

No, the result won't be the same. A hand-painted alpha mask will not create the same result as a properly generated distance field. It'd be like trying to paint a cube map - a skilled artist could approximate the distortion needed, but it's never going to be as accurate as generating one properly.

I'm talking about the visual result, that's what the topic is about, which is the same.
Simple prove is: You seem to think the games might use distance field, although vegetation is artist created.
q.e.d.

Share this post


Link to post
Share on other sites
Quote:
Original post by szecs
Okay, I don't believe my eyes, so let me get this straight:
If you have the distance field in the alpha channel of an image, and if you enable alpha test with grater than 0.5, you get that smooth result presented in the paper with fixed pipeline (for example in openGL)?
I'm stunned.
yep, that's how it's always done ;)

Quote:
Does Photoshop have a plugin for producing the distance field?
But it seems simple to write your own program for that too.
if you check textures for example from crysis (texture.pak which is just a zip), you'll see that everything is just painted (in photoshop). There are more things that are more important for the quality than highly accurate alpha values, artist know those.

Distance fields are like all those tree-generators, they do it mathematically correct, but miss the visual appearance.
Using and automated tool will result in less quality than what artist would achive, that's why distance fields are just used in special cases, trees are made by hand.

Share this post


Link to post
Share on other sites
Quote:
Original post by Krypt0n
Simple prove is: You seem to think the games might use distance field, although vegetation is artist created.
q.e.d.
How on earth do you figure that distance fields can't coexist with artists? Whether or not you use distance fields, an artist draws the bases textures to be fed into the distance field generator..

Share this post


Link to post
Share on other sites
Quote:
Original post by swiftcoder
Quote:
Original post by Krypt0n
Simple prove is: You seem to think the games might use distance field, although vegetation is artist created.
q.e.d.
How on earth do you figure that distance fields can't coexist with artists?
Some artist that worked for epic and also on Crysis told me that. I somehow belive him.


Quote:
Whether or not you use distance fields, an artist draws the bases textures to be fed into the distance field generator..

that's the naiv view I also had before, then he told me his experience with that. Just like with minification tools, reducing the data resolution had random results. depending on where you place your texture, the result was different. some leaves were combined while some were moved apart(similar to some kind of moire effect). Texture artist started to move parts of the textures in the source image by some random amount of pixel to compensate this arbitrary behaviour (some started rumors that you should try to keep everything 45degree aligned for best quality).
But therefor models needed to be adjusted as the UVs didn't fit anymore.

That's why those artist abandoned this weak tech and do as they always did, drawing those blured alpha channels.
tho, coders love it. Beyond font you'll probably see roads(marks) and shadows with this tech, if I understand them right, but nothing artistic and some other rather procedural stuff didn't work either.


Share this post


Link to post
Share on other sites
Quote:
Original post by szecs
Does Photoshop have a plugin for producing the distance field?
But it seems simple to write your own program for that too.
Yeah it's fairly easy to write your own program (though it's a fun case in optimisation to make it fast) - usually you run it at a very high resolution and then down-size the results.
Here's an article on how to make them in photoshop - changing the "size" parameters of the glow actually changes the scale of your distance units. Ideally you want the gradient to be as smooth as possible (large size == less aliased outline), but you still want to retain enough contrast around small features for them to be preserved when you down-size the image (to avoid the "random results" Krypton is talking about). If your engine can refresh textures while it's running, you can iterate on the alpha channel in photoshop very quickly to get the results you want.
Quote:
Original post by Krypt0n
I'm talking about the visual result, that's what the topic is about, which is the same.
Simple prove is: You seem to think the games might use distance field, although vegetation is artist created.
q.e.d.
You say that hand-painted smooth masks and distance fields have the same result, but you say that distance-fields aren't used because they have problematic visual results.
Therefore the visual results are different.
q.e.d. :P

Also your proof is wrong - artists are capable of making these themselves :/
FWIW, I've seen them used for foliage on the Wii and the artists learnt to control them fine... The reason we started using them was because hand-painted masks were having random results like the aliasing on really thin stems turning them into dotted-lines!

Share this post


Link to post
Share on other sites

This topic is 2845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this