I try to create dynamic grass, by ray casting a grid on my game level
I tried to do it dynamicaly but it is too slow once the grid gets big
so I tried creating the grass geo (2 triangles making a quad) at startup
I get something like 150000 geometries
so the game window disapears while the app seems to keep running in the IDE
if I reduce the grass count, the game is running ok, but it is really slow
int count=0;
for(int x=(int)(min.x/GRID_CELL_SIZE);x<=(int)(max.x/GRID_CELL_SIZE);x++)
{
for(int z=(int)(min.z/GRID_CELL_SIZE);z<=(int)(max.z/GRID_CELL_SIZE);z++)
{
Ray ray = new Ray(new Vector3f(x*GRID_CELL_SIZE,max.y,z*GRID_CELL_SIZE),Vector3f.UNIT_Y.mult(-1));
CollisionResults results= new CollisionResults();
ground.collideWith(ray, results);
if(results.size()>0)
{
CollisionResult collision = results.getClosestCollision();
Geometry geo = Tools.CreateSquareGeo("grassGeo",0.4f, Vector3f.UNIT_Z,collision.getContactPoint(), MaterialsFactory.GetGrassMaterial());
rootNode.attachChild(geo);
count++;
}
}
}
System.out.println("count "+count);
the goal is to use a shader to only display the grass around the player
but most of papers I found use procedural shaders, while my terrain is hand created
so I wanted some advice on how could I achieve this, If anyone already done that
is it possible to give the shader my terrain data, or use geometry shader�
Search the forum, theres been a few âgrassâ libraries/shaders being posted. Also make sure to check out the wiki and read the best practices docs etc. Adding lots of geometries is not a good idea in any way (as described in the mentioned docs).
Yes, the latest 3.1 has geometry shader support, I use this for generating grass vertices on the graphics card rather than creating hundreds of grass entities in the scene graph
Or you can batch entire sections of grass as geometry.
Creating an entity per blade of grass is going to be pretty ridiculous⌠thatâs why no one does it that way.
Note: to OP, even if many of the existing examples are procedural it doesnât negate the actual setting up of a mesh⌠which should be reusable no matter how the data got there. Itâs all about batching. (if not doing geometry shaders.)
I use quads for grass, you know, like setting 4 vertex at the same position but giving uvs that tells the vertex shader how to displace and orient them towards camera (there is an article somewhere on the web, cant find the link)
noob question, but what do you call batching ? sending all the hand-modeled landscape mesh to the vertex shader ?
is there an example of geometry shader with jme somewhere ?
You really donât need a geometry shader to do this. For one it wonât run on many graphics cards and furthermore just reducing the grass to one geometry (or like 4 or so) will work fine. Again, search the forum (grass shader) and read the wiki. Batching is explained and theres several non-geometry shader examples on the forum.
ok, but batching is just rude mix everything together, dont like this approach
I thought of something else,
what if I created some data about plant positions only, and I would populate the ground with plants around the player with such data ? or add all plants to the scenegraph and only display the ones around the player⌠Iâll make some test
In my IsoSurface demos, I sort the grass in the batch. A batch in this case is a custom mesh with N âquadsâ in it exactly as you describe (though I use triangles instead of quads).
For farther zones of grass, I sort them once based on the zone that the player is in⌠then I only per-frame re-sort the zone that they are in currently.
Iâm not sure a geometry shader is going to resolve your sorting issue, anyway⌠but I guess it depends on what data itâs been provided and whether it is already sorted. But then it might not be much different than just sorting the data in the batch and using a regular mesh.
This is a very early and not optimized geometry shader for emmiting billboarded quads with wind support out of single vertices.
Note, depth sorting is NOT done by the geometry shader, if you need it you need to do it yourself.
Also, batching is likely faster, depending on how often you need to update your buffers, and how you implement the billboarding. If you do optimisations like this you are always trading performance, in this case you save cpu and bus perfrormance and spend gpu texture bandwith and gpu computing power.
sort in the batch?? but batching is a jme tool. I mean I cant do anything about the way it works
or you mean batching like create a custom mesh I guess
I tried to use TestBatchLod.java but it gives some error âLOD levels are not set on this meshâ
yeah, but geometry shader might not be compatible with all card and billboards is what I already do and is really slow
Iâll try to put everything in one mesh and do z sorting