Hi again guys!
So for future reference and to WHOEVER searched and found nothing concerning the VERTEX SHADER BILLBOARDING WITH SCALING, here is what I did to accomplish my GeometryBatchFactory’ed thousands of textured quads of different size billboarding via the GPU using solely a vertex shader and a little VBO hacking:
(Please note that my texture DAISY.PNG is a SQUARE texture, 512x512, this won’t work with non-square textures, but it could work with little adaptation tough and also note that I stripped the randomization process to lighten the code)
In JME3 Java code, I created my thousands of quads like this:
[java]
// Setup
Node flowerLayer = new Node(“Flower Layer”);
// Create a layer of 2000 flowers
for(int i = 0; i < 2000; i++){
// Random size and position
float s = 2.5f // Imagine a random size here
Vector3f location = new Vector3f(1,2,3); // Imagine a random position here
Node flowerPatch = new Node();
Quad faceShape = new Quad(s, s);
// Billboarding flat vegetation
faceShape.setBuffer(Type.Position, 3, new float[]{0, 0, 0,
0, 0, 0,
0, s, 0,
0, s, 0});
faceShape.setBuffer(Type.TexCoord2, 2, new float[]{s, 0, // ANYBODY SEES A BETTER WAY TO COMPRESS THIS?
s, 0,
s, 0,
s, 0});
Geometry face = new Geometry(“face”,faceShape);
flowerPatch.attachChild(face);
flowerPatch.setLocalTranslation(location);
}
// Batch optimization
GeometryBatchFactory.optimize(flowerLayer);
rootNode.attachChild(flowerLayer);
// Materialize those flowers
Texture tex = this.getApplication().getAssetManager().loadTexture(“Textures/daisy.png”);
Material mat = new Material(this.getApplication().getAssetManager(), “Shaders/Grass/grassBase.j3md”);
mat.getAdditionalRenderState().setFaceCullMode(RenderState.FaceCullMode.Off);
mat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
mat.setTextureParam(“ColorMap”,VarType.Texture2D,tex);
mat.setBoolean(“VertexLighting”,true);
mat.setVector2(“Wind”, new Vector2f(1,0));
mat.setFloat(“AlphaThreshold”, 0.5f);
// Attach my flower layer (the thousands of quads)
rootNode.getChild(“Flower Layer”).setMaterial(mat);
[/java]
<br />
EDIT: BTW, see on line 19 above, does anybody see an optimization instead of pushing 8 floats (for a quad) only for ONE float (my desired quad width) is there any other buffer I could use that would only use 4 floats or even better: is there a way to compress it inside the position buffer or some other buffer that is constantly pushed in the VBO?
<br /> <br /> <br />
Now I used this in my vertex shader:
(You’ll notice I do this: if(inTexCoord2.x > 0) to isolate the billboarding code because this vertex shader is also used on non-billboarded quads that are crossed in X shape to give a fake depth, like you can see in @vvishmaster’s original and very good grassBase.j3md shader)
[java]
// ---------- Billboarding effect ----------
if(inTexCoord2.x > 0){
// Convert vertex location to world-view space, ie: camera-relative space
pos = g_WorldViewMatrix * pos;
// Figure out how to offset it in camera space
float offset = texCoord.x - 0.5; // (make it -0.5 to 0.5)
pos.x += offset * inTexCoord2.x;
}
// -----------------------------------------
#ifdef SWAYING
float angle = (g_Time + pos.x*m_SwayData.y) * m_SwayData.x;
pos.xz += 0.1*m_Wind*inTexCoord.y*sin(angle);
#endif
if(inTexCoord2.x > 0){
// Billboarding effect projection matrix
gl_Position = g_ProjectionMatrix * pos;
}
else{
// NOT billboarding effect projection matrix
gl_Position = g_WorldViewProjectionMatrix * pos;
}
[/java]
I must say that I did not invent anything here, it’s all other people’s work combined and even after staring at the screen for more than 1 hour, I still couldn’t QUITE understand why pos.x += offset does the trick, but I guess it’s all a matter of PROJECTION. It’s just maths I’m not used to AT ALL, but in the end I must say I feel SO GOOD to finally see it working! It’s flawless! LOOK AT THOSE DAISIES. LOOK AT THEM! ISN’T THAT WONDERFUL?! I have to go to bed now, but I swear tomorrow morning I shalln’t resist and I’ll be downloading dozens of transparent PNG’s and make all kinds of flowers pop everywhere on the infinite terrain like crazy. Different colors, different sizes, it’s just… it’s a wonderful feeling! ok enough.
Can’t wait to get crazy with the flowers popping Off to bed now! XD
Thx again @pspeed and a little @zzuegg too for geometric shader even if I did not use it, it’s always good to read and try to learn stuff from others and get all those points of view. I like being led by example, it’s much easier.