Grass, Bush, Tree system..

Blitz3D Forums/Blitz3D Programming/Grass, Bush, Tree system..

Rick Nasher(Posted 2014) [#1]
Hi guys,

On my quest of creating a foliage system that is free and flexible I've found Grass on a landscape (lite version) by Matt Merkulov which was looking good to me:



However I of course wanted enhance it a bit so I modified it so that it can now load (Blitz)terrains and multiple simple 3d models as well as the original quad billboards.



(Don't mind the models ugliness, they're only for experimentation, no finals)


Now of course I'm running into some transparency/alpha issue's here and there as I'm using multitextured terrain also:




Code with media click here.
- Does anybody have a suggestion how to reduce/resolve that?
- Will MikhailV's FastImage resolve this issue? (I have his FastExtension already)


RemiD(Posted 2014) [#2]
Try to position your different meshes using alpha at a distance high enough so that each bounding box does not cross the others bounding box.

If you turn each grass mesh in front of the camera each frame, try to position each mesh using alpha at a distance high enough so that each radius (the highest of width or depth) does not cross the others radius.

If this does not work, you can try to use the flag 4 (hide the black pixels) instead of the flag 2 (alpha).

If this does not work you want to search in the forums/code archives to find a way to set the order of render of each mesh manually.
https://www.google.com/#q=alpha+z+ordering+site:blitzbasic.com


Rick Nasher(Posted 2014) [#3]
Hmm didn't think about that flag 4 thinghy, thanks RemiD. That just might come in handy somewhere.
I once traveled the z-ordering road for something else, but I vaguely recall it was a pain(for some reason I can't remember, years back).

Perhaps I need to give it another look.


Mikorians(Posted 2014) [#4]
Looking good, Rick!
I swear by flag 4.
I have found that trying to do the gfx card's work for it is a dead end because WE can't occlude by polygon. (My opinion)


Rick Nasher(Posted 2014) [#5]
Did a quick(ugly) test and flag 4 indeed does trick! Now all I need to do is replace the png textures transparency with black, save them as a jpg and adjust the models.

Thanks guys.


Rick Nasher(Posted 2014) [#6]
OK no more z-ordering mess due to alpha textures, but now I of course need to convert some pictures from transparency to black pixels, which needs to be done pretty tight, otherwise gonna look messy:


I tried MS Paint and Paint.NET, but both do not give me satisfying results.

Any advice on best tools/method for the job?
(I do also have Photoshop, which I didn't install yet cos low on diskspace on this laptop, but if necessary I could use it.)


RemiD(Posted 2014) [#7]
I use photofiltre, all transparent pixels must be colored with 000R,000G,000B, then i save in png.


Kryzon(Posted 2014) [#8]
You can also use the GIMP, with the tool Layers -> Transparency -> Threshold Alpha.
It makes semi-transparent pixels either fully transparent or fully opaque based on a threshold alpha value that you specify.

Then you can merge that texture layer with a fully black background layer to cover the empty parts.


Mikorians(Posted 2014) [#9]
I get excellent control with DxtBmp !
It's very simple.
(Any trouble, save as .TGA)


Rick Nasher(Posted 2014) [#10]
Okies, this is not a 'done' deal, needs lots of work still, but posting for if anyone is interested.

Managed to get it to swallow a vegetation map(different colors for different plants), my own 3d plant models(quads and combined quads using masked textures rather than alpha) and trees, plus I added a bit of simple wind.





I found on my system(NVIDIA GeForce 8400M GS) the code with trees added to it does about ~30FPS with debug disabled, which leaves too few FPS to be usable in a dynamic game, so kicked it to the curve and put it on hold(for now). (the version without trees, which wasn't updated with other features is doing about 60FPS).

I'm sure better skilled coders could do a much better job. Probably can be optimized and trees should be handled by separate piece of code for speed gain. Dunno how much can be gained though(I'll see when I have the time).

Foliage V0.031.bb (without trees, more bushes and higher FPS)
Foliage V0.12g.bb (with trees and other features, lower FPS)



Of course some media needed, download here(code included) https://www.mediafire.com/?yi7xkwql56ukd9j

@stayne: as we speak.


stayne(Posted 2014) [#11]
Gad zooks! Might wanna edit that post.


RemiD(Posted 2014) [#12]
One way you could decrease the render time and also increase grass density would be to use one pivot and only one quad (4vertices, 2 triangles) for each grass.
Then each frame you would be able to rotate the pivot so that it faces the camera and then reposition the vertices at the appropriate position so that the grass is visible by the camera, whatever the camera position orientation. And it would still be a single surface system.


By curiosity, what are you planning to do with this ?


RemiD(Posted 2014) [#13]
The nenuphars add a nice touch, but i think the flower you have put on the plant is an artichoke. ;)


Rick Nasher(Posted 2014) [#14]
@RemiD: Decoration in a free roaming environment. I considered that other approach indeed, but I never seen one that gave me a convincing look from all points of view. Also a bit of a shame there are such issues with alpha cos I think was looking way better.

Just wanted a bush here and there and also to see how far I could push it, but perhaps I can try and see what would look like indeed, won't hurt to try, but not currently working on it due to time restrained. Just put it up cos Hotshot2005 was looking for similar stuff in another thread.

The 'artisjokes' indeed look very edible. :-) (Gfx where by no means final, just a quick see-what-it-could-look-like)


RemiD(Posted 2014) [#15]
Here are some simple tweaks made to the code by Matt Merkulov



The idea is this :
If you look at the appearance of the grass when it is rendered with textured quads near the camera, you can see that there are different shades of green.
So instead of using only one green color to texture the terrain, you use a base green color and others shades of green depending on if there is a grass at this position or not.
Then even if there is no textured mesh displayed on the terrain because it is too far from the camera, you can see different shades of green.

In this code example the render is not ideal because it seems that the normal of the triangles of some quads are not calculated and set properly. But you get the idea...

To go further if you want to improve the lighting :
If you create a light for the sun light, let's say with a color of 250,250,250
And then you set the ambientlight to 050,050,050
050 corresponds to 1/5 or 0.2 of 250
This means that a triangle which "faces" the light will be colored with 250,250,250
and a triangle which "oppose" the light will be colored with 050,050,050
So when you want to color your grass, you want to make the top of the grass (the part which will be illuminated) with the base color (000,100,000) and the bottom of the grass (the part which will not be illuminated or only slightly illuminated) with 0.2 of the base color (000,020,000)
And to color the terrain, we could use the base color (000,100,000) and the "median" color (000,050,000).
Then you need to set the grass meshes to fullbright so that they will not be affected by the light.


RemiD(Posted 2014) [#16]
Here are some simple tweaks made to the code by Matt Merkulov



The idea is this :
If you look at the appearance of the grass when it is rendered with textured quads near the camera, you can see that there are different shades of green.
So instead of using only one green color to texture the terrain, you use a base green color and others shades of green depending on if there is a grass at this position or not.
Then even if there is no textured mesh displayed on the terrain because it is too far from the camera, you can see different shades of green.

In this code example the render is not ideal because it seems that the normal of the triangles of some quads are not calculated and set properly. But you get the idea...

To go further if you want to improve the lighting :
If you create a light for the sun light, let's say with a color of 250,250,250
And then you set the ambientlight to 050,050,050
050 corresponds to 1/5 or 0.2 of 250
This means that a triangle which "faces" the light will be colored with 250,250,250
and a triangle which "oppose" the light will be colored with 050,050,050
So when you want to color your grass, you want to make the top of the grass (the part which will be illuminated) with the base color (000,100,000) and the bottom of the grass (the part which will not be illuminated or only slightly illuminated) with 0.2 of the base color (000,020,000)
And to color the terrain, we could use the base color (000,100,000) and the "median" color (000,050,000).
Then you need to set the grass meshes to fullbright so that they will not be affected by the light.

What would look even better would be to color each grass of a slightly different green color and have the same color for the pixel of terrain color


Rick Nasher(Posted 2014) [#17]
Nice ideas indeed.


RemiD(Posted 2014) [#18]
I have reduced the texel size on the terrain color so that one texel corresponds to 0.5*0.5 unit for the terrain
This means that it appears that there are 4 grasses for each 1*1unit
I have also augmented the square size (containing the grasses) to 10*10 unit so that there are less squares to manage.
Now, see how you almost cannot see the grass appear/disappear :


As i have said previously an even better approach would be to slightly modify the color of each grass so that it corresponds to the green color of the texel on the terrain color.

And to make it faster, only have one quad for each grass and each frame oriente the visible grasses to face the camera.


Rick Nasher(Posted 2014) [#19]
Thanks. Indeed much harder to see where the grass ends. Nice effect that really helps.

One thought was to have a mixed system;
For low grass just use single sided billboard quads using pointentity to face the camera and something like the current system for larger bushes and perhaps trees and other stuff.

That way would speed up things (I think)and still look much better than just using billboards for everything, especially when the cam moves above the terrain while jumping/flying etc.
Also would allow for different wind effects(trees shouldn't move as much as grass, waterplants moving along with water).

However I currently have no 100% clear idea at what point(s) in code and how I can addres the individual bushes to manage the cam facing bits.

For the wind I kinda cheated using:




RemiD(Posted 2014) [#20]
There are several limits with what you can do with the code by Matt Merkulov.
The first limit i have seen is that it uses AddMesh instead of a "AddSurface" and i don't think you can keep all the infos about the surface added to the other surface with AddMesh.
Not sure about that, i have to do some tests.

What would be a great improvement for the render would be to add different kinds of grass meshes and grasses textures and that they would be automatically "merged" into one surface with one texture for each 10x10square

Another improvement would be to first color the terrain with one of the main color used by one of the different grass mesh and then position each corresponding grass mesh at this position with a slight random offset on the x axis and on the z axis

About replacing the grass detailed meshes by quad meshes, this is not really needed, if you plan to add low tris meshes this should be ok.
Keep in mind that one surface can contain up to around 32000 vertices or triangles, but you can also have different surfaces in a mesh so there is no limit. The only limit you have is the target hardware you want to run the game on. I was suggesting to use only a quad for each grass if you want to display many many many grasses on a big terrain.

For the trees, i would use detailed meshes to display the near enough trees and textured quad meshes to display the far enough trees.

For the wind, i don't know, i don't think this is a priority. Most players would probably not notice if there is wind or not.