Geometry Shader?


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);
                    CollisionResult collision = results.getClosestCollision();
                    Geometry geo = Tools.CreateSquareGeo("grassGeo",0.4f, Vector3f.UNIT_Z,collision.getContactPoint(), MaterialsFactory.GetGrassMaterial());
        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).

well I wanted to add them on the fly, but it is too slow
I’ll check these out
[edit: they all seem to be procedural… :frowning: ]

so, does jme supports geometry shaders ?

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

Note: this is not an either or.

You can create grass in a geometry shader.

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.)

3.1 …

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.

but reducing the grass to one geometry will void the z sorting algorithm, right ?
I already have that problem with other big transparent objects

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

does JME supports data paging somehow ?
[edit : found this
but does not seem complete, I’ll do it myself then,


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.

yes, 3.1 being the latest source code from github

Absolutely, I use both geometry shader for close up grass and batched quads as the distance from the camera increases…


These three posts pretty much cover everything.

First one with a geometry shader, second one with billboards and third demonstrating a mix of the two.

Note that “3.1” isn’t 3.1, its just the latest development version and is prone to break and change every day.

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.


hence my “3.1…”

thx, I’ll check it out
I thought z sorting was done by the gpu or the opengl driver somehow

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 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