GLSL - Moving vertex

Alright, some of you may already know, i m working on a shader base grass.
I ve got a huge problem right now and i need to know how do i only move designated vertex?

My example is easy, if i create a model with this

Geometry grassGeom = new Geometry("grass", new Quad(2, 2));

i apply a Material i made.

And in my update loop i update the material with this code

shaderGrass.setFloat("Time", tpf*3);

my current problem is i was using the world position of the vertex and not the local one, so this code does not work anymore if i place my grass over 0 or under 0


vec3 wvPosition = (g_WorldViewProjectionMatrix * vec4(displacedVertex, 1.0)).xyz;
if(inPosition.y > 0.1)
displacedVertex.x += moveFactor * sin(m_Time * texture2D(m_AlphaMap, wvPosition.xz*0.001).r + len) + (m_WindStrength * noiseFactor * m_WindDirection.x)/10.0;
displacedVertex.z += moveFactor * cos(m_Time * texture2D(m_AlphaMap, wvPosition.zx*0.001).r + len) + (m_WindStrength * noiseFactor * m_WindDirection.y)/10.0;
gl_Position = g_WorldViewProjectionMatrix * vec4(displacedVertex, 1.0);

So your question is [quote=“n3cr0, post:1, topic:37341”]
how do i only move designated vertex?

What is your designated vertex. Since we are talking about grass and a quad, I am assuming, you’d like to move the top two vertices of the quad right?

In that, case you can simply check the inPosition.y coordinate.

Like i m already doing? I mean this is the actual problem, the y is world relative and not Local Relative so it doesn’t work (And i forgot to add the .y to the inPosition while copy pasting) My bad

I’ve done something wrong since i did test the exact same code and it does work in simplified scene…

One thing that looks strange in your code is that you are multiplying by g_WorldViewProjectionMatrix twice. Is that intended?

On closer look, nvm you only use that for the texture
Anyways, so what is currently the problem, are all the vertices moving?

It should since it seem to move correctly normally. I’m not really good in these thing so i copy people stuff :sweat: And thus i don’t understand most of the equation. (I get the cos and the sin part with the inPosition > 0.1)
I m not sure how does the gpu know what i ask and how people understand these thing

Okai i got what s happening, since they all share the same material, the GLSL calculate the lowest one. I would need to split the material and update all of them 1 by one.

Omg you were right, the dual multiply was wrong haha it made the weird thing happen wow thx a lot.

I mean the other problem is still there but thank you!

glad that helped. By other problem, do you mean that all vertices are moving?
btw what is displacedVertex initialized with?

yea look at this and you will understand.
All the node share the same Material.

The one to the bottom are fine, but anything over them by .1 will be move so all the vertex are moving instead of the top part only.

So basically, you are either no longer using quad, or using quad and modifying its position buffer, or using batching right?

Well, in that case you can always use eg TexCoords Buffer or any other buffer to specify which vertices should move and which not.

Yea i m Using batch node and then reapply the material to the batched node.

Hmm ok, so i add a new texture parameter to the frag? and then i tell the gpu to only move the part highlighted on the texture? Same as an Alpha map?
Edit: Like a Mask? and using a gradiant black to white, Whit receive 100% moving and black 0%?

It is not a texture, but a attribute, like inPosition, inNormal, etc.

Like now, you were doing it by relying on inPosition.y coordinate. Since you are batching you cannot do that any longer.

What you can use instead is inTexCoord.y coordinate. Assuming you are using jME quad with TexCoord buffer already in it, try if(inTexCoord.y > 0.1)

1 Like

IT WORKED!!! From now own i should call you a savior xD

First you fix an anoying bug, then you save the whole thing haha xD

Now i just need to add a lighting support

glad I could have helped, make sure you do understand what your shader is doing thou.
gl with the rest of the shader and feel free to ask if you’ll run into difficulties

g_Time and g_Tpf is already passed in by the engine. You just need to declare them in your variables.

And in your material’s WorldParameters.

(Jmonkey 3.1-alpha)Ok, my grass was perfect… until i got my New AMD, the grass doesn’t exist anymore. I was on a Nvidia GTX 460 before and today i just got my New AMD rx 480 and the grass doesn’t seem to draw even on the test project. Anyone know what could’ve done this?

float alpha = DiffuseSum.a * texVal.a;
    #ifdef ALPHAMAP
       alpha = alpha * texture2D(m_AlphaMap, newTexCoord).r;
    #ifdef DISCARD_ALPHA // IF I REMOVE THIS it seem to work, but with some visuel glitch.
        if(alpha < m_AlphaDiscardThreshold){

So when i remove the

grassShader.setFloat("AlphaDiscardThreshold", 0.1f);

Everything work, but with glitched trandsparency, we can see through the stuff sadly

Btw: i did the correction @pspeed and @asser_fahrenholz said before.

…which alpha?

Is there a reason you aren’t on beta2 by now? (Or even beta1 which was at least more publicized.)