GLSL - Moving vertex

I have no idea how to replace all the file in the SDK version of the engine. I m Currently looking at it right now. But would the problem be linked with the Alpha version? Or it as no link and it’s actually something else?

There was no “alpha”. There was alpha1, alpha2, alpha3, etc… So if you only have “alpha” without a number then it must be some odd bootleg JME that we don’t support.

OR maybe you just left that information out… which is why I asked which alpha.

Anyway, if it works on one card and not another then it’s most likely a GPU driver issue. Also, on AMD cards it is quite common that a newer driver will break OpenGL and require you to back-rev to an older version. Finding the right driver that works can be quite tricky I guess.

Alpha1 is the version, didn’t know it was that important.

But i don’t see that kind of problem in other game. I mean it’s clearly my fault i think here.
Could it be the GLSL version that would need to be changed?

LightMode MultiPass
VertexShader GLSL120:   MatDefs/Grass/MovingGrass.vert
FragmentShader GLSL120: MatDefs/Grass/MovingGrass.frag

Nah, those are fine.

Are those other games using the same code on OpenGL? Or are they using DirectX. AMD cards work fine on DirectX since they help write that ‘standard’.

alpha1 is very very old. You should upgrade but it’s unlikely to fix this issue. I mean, you never know (we have newer lwjgl versions at some point) but given that it works on nvidia and not on your new card, it seems card-specific.

Yea I ve bought the card to start working on the Vulkan version of Jmonkey as I said before in an other thread. But to end in this kind of issue is quite disapointing :expressionless:

I need to test the grass on an older AMD card if you have no idea what could be causing the issue with the AlphaDiscardThreshold.

Well, you should upgrade JME at least for a hundred other reasons. Maybe a newer version of lwjgl fixed the issue. alpha1 is pretty old.

You could also try reverting to older versions of your AMD driver. They sometimes break stuff (temporarily hopefully) with new versions.

I will give a try with the jmonkey upgrade. I m not sure how it have to do this :S

If you run the SDK and want a new SDK… then download a new SDK and install the new SDK.

If you don’t run the SDK or don’t want a new SDK… then download the new libraries and depend on them in your project.

There are betas for SDK when I recall this correctly with also beta version of the engine

There is one thing i don’t seem to get, my code is simple and yet the grass is invisible(doesn’t seem to be draw) yet i lag as if there was grass in my sight. It s only transparent. And when i remove the part about the Alpha Threshold, it seem to be draw but it erase anything in the transparency bucket behind it as it would normally do. SO I’m wondering if i did not forget to add something to my grass code?

And with the new version of jmonkey it doesn’t look like it change anything, i’ve got new driver for amd card too. But if the alpha work for everything else i really got a mistake in my GLSL, i will publish the code.

File, MovingGrass.j3md

MaterialDef Grass {
 
MaterialParameters {
Texture2D Texture
Texture2D AlphaMap
Vector4 Color
Float Time
Float WindStrength
Vector2 WindDirection
Boolean Use_VertexColor
Vector3 CamPos
// Alpha threshold for fragment discarding
Float AlphaDiscardThreshold (AlphaTestFallOff)
}
 
Technique {
LightMode MultiPass
VertexShader GLSL120:   MatDefs/Grass/MovingGrass.vert
FragmentShader GLSL120: MatDefs/Grass/MovingGrass.frag
 
WorldParameters {
WorldViewProjectionMatrix
WorldViewMatrix
Tpf
Time
}
 
Defines {

TEXTURE : Texture
VERTEX_COLOR : Use_VertexColor
ALPHAMAP : AlphaMap
DISCARD_ALPHA : AlphaDiscardThreshold

}
 
}
 
 
}

File, MovingGrass.frag

#ifdef TEXTURE
uniform sampler2D m_Texture;
varying vec2 texCoord;
#endif

#ifdef ALPHAMAP
  uniform sampler2D m_AlphaMap;
#endif
 
varying vec4 DiffuseSum;

uniform vec4 m_Color;

uniform float m_AlphaDiscardThreshold;

void main(void)
{

#ifdef TEXTURE
vec4 texVal = texture2D(m_Texture, texCoord);
gl_FragColor = texVal * m_Color;
#else
gl_FragColor = m_Color;
#endif

float alpha = DiffuseSum.a * texVal.a;
    #ifdef ALPHAMAP
       alpha = alpha * texture2D(m_AlphaMap, newTexCoord).r;
    #endif
    #ifdef DISCARD_ALPHA
        if(alpha < m_AlphaDiscardThreshold){
            discard;
        }
    #endif
}

File, MovingGrass.vert

uniform mat4 g_WorldViewProjectionMatrix;
uniform mat4 g_WorldViewMatrix;
uniform vec4 g_LightColor;
uniform float g_Time;
uniform float g_Tpf;
 
uniform float m_WindStrength;
uniform vec2 m_WindDirection;
uniform vec3 m_ObjectCenter;
uniform vec3 m_CamPos;
 
uniform sampler2D m_Texture;
uniform sampler2D m_AlphaMap;
uniform vec4 m_Color;
 
attribute vec2 inTexCoord;
 
attribute vec3 inPosition;
 
 //this is erroring
uniform float moveFactor = 0.1; // Play around with this
 
varying vec2 texCoord;
varying vec4 color;
 
#ifdef VERTEX_COLOR
attribute vec4 inColor;
#endif
 
void main()
{
vec3 displacedVertex;
displacedVertex = inPosition;
texCoord = inTexCoord;
 
float len = length( displacedVertex );


float noiseCoord = g_Time+ g_Tpf*3;
 
int totalTime = int(g_Time+ g_Tpf*3);
if (totalTime > 4096) totalTime -= 4096;
 
int pixelY = int(totalTime/64);
int pixelX = totalTime/ -pixelY;
float noiseFactor = texture2D(m_AlphaMap, vec2( pixelX*10, pixelY*10 ) ).r;
// get pixel from noise map based on time. use to create additional variation
 
vec3 wvPosition = (vec4(displacedVertex, 1.0)).xyz;
 
if(inTexCoord.y>= 0.1)//control les vertext affecter par le movement(tout les vertext en haut de 0.1f en position seront bouger
{
displacedVertex.x += moveFactor * sin(g_Time * texture2D(m_AlphaMap, wvPosition.xz*0.1).r + len) + (m_WindStrength * noiseFactor * m_WindDirection.x)/10.0;
displacedVertex.z += moveFactor * cos(g_Time * texture2D(m_AlphaMap, wvPosition.zx*0.1).r + len) + (m_WindStrength * noiseFactor * m_WindDirection.y)/10.0;
}
 

gl_Position = g_WorldViewProjectionMatrix * vec4(displacedVertex, 1.0);
 
 
#ifdef VERTEX_COLOR
color = m_Color * inColor;
color.rgb *= g_LightColor.rgb;
#else

color = m_Color;
color.rgb *= g_LightColor.rgb;
#endif
 

}

ps: If you ever know how i could change the color of the material depending on height let me know! :stuck_out_tongue: I,m trying to give a color effect but the color doesn’t seem to change at all…

i did use

 if(inPosition.y > 100){
  //anything i wrote here didn't seem to work.
 //I mean anything that should change the color seem to be completely ignored...
}

It is a bit strange that you calculate the alpha a different way than the color and then only use the alpha value to determine discard. It means that if there is something strange about the alpha value then you’d never see it… because it’s different (way different) than what is going to be in gl_FragColor.

As to your other, I don’t know where you put that block so I can’t comment.

For the first part i will try to look in the lighting shader to see if i could not understand how it was done with the rest

For the second part, right at the place where i think it does the drawing, at the end, I was trying to replace the m_Color by a value in % so it change to get a more dead and cold look and feel the higher the grass is.
But anything i change there does nothing… I think i could just remove this part of code and everything would still draw correctly on a nvidia haha

#ifdef VERTEX_COLOR
color = m_Color * inColor;
color.rgb *= g_LightColor.rgb;
#else

color = m_Color;
color.rgb *= g_LightColor.rgb;
#endif

You have now described code (incompletely) that would have taken 10 seconds to paste in so there is nothing ambiguous.

I can assure you that properly coded you can recolor a fragment based on anything you like. I do it all the time.

OK sorry, i am trying to replace the code ,

#ifdef VERTEX_COLOR
color = m_Color * inColor;
color.rgb *= g_LightColor.rgb;
#else

color = m_Color;
color.rgb *= g_LightColor.rgb;
#endif

At the end here,

uniform mat4 g_WorldViewProjectionMatrix;
uniform mat4 g_WorldViewMatrix;
uniform vec4 g_LightColor;
uniform float g_Time;
uniform float g_Tpf;
 
uniform float m_WindStrength;
uniform vec2 m_WindDirection;
uniform vec3 m_ObjectCenter;
uniform vec3 m_CamPos;
 
uniform sampler2D m_Texture;
uniform sampler2D m_AlphaMap;
uniform vec4 m_Color;
 
attribute vec2 inTexCoord;
 
attribute vec3 inPosition;
 
 //this is erroring
uniform float moveFactor = 0.1; // Play around with this
 
varying vec2 texCoord;
varying vec4 color;
 
#ifdef VERTEX_COLOR
attribute vec4 inColor;
#endif
 
void main()
{
vec3 displacedVertex;
displacedVertex = inPosition;
texCoord = inTexCoord;
 
float len = length( displacedVertex );


float noiseCoord = g_Time+ g_Tpf*3;
 
int totalTime = int(g_Time+ g_Tpf*3);
if (totalTime > 4096) totalTime -= 4096;
 
int pixelY = int(totalTime/64);
int pixelX = totalTime/ -pixelY;
float noiseFactor = texture2D(m_AlphaMap, vec2( pixelX*10, pixelY*10 ) ).r;
// get pixel from noise map based on time. use to create additional variation
 
vec3 wvPosition = (vec4(displacedVertex, 1.0)).xyz;
 
if(inTexCoord.y>= 0.1)//control les vertext affecter par le movement(tout les vertext en haut de 0.1f en position seront bouger
{
displacedVertex.x += moveFactor * sin(g_Time * texture2D(m_AlphaMap, wvPosition.xz*0.1).r + len) + (m_WindStrength * noiseFactor * m_WindDirection.x)/10.0;
displacedVertex.z += moveFactor * cos(g_Time * texture2D(m_AlphaMap, wvPosition.zx*0.1).r + len) + (m_WindStrength * noiseFactor * m_WindDirection.y)/10.0;
}
 

gl_Position = g_WorldViewProjectionMatrix * vec4(displacedVertex, 1.0);
 
 
#ifdef VERTEX_COLOR
color = m_Color * inColor;
color.rgb *= g_LightColor.rgb;
#else

color = m_Color;
color.rgb *= g_LightColor.rgb;
#endif
 

}

With something like this,

if(inPosition.y > 1){
    #ifdef VERTEX_COLOR
        color = ((m_Color)*(1,1,1,1)) * inColor;
    #else
        color = m_Color*(100,100,100,100);
    #endif

}else{
    #ifdef VERTEX_COLOR
        color = ((m_Color)*(inPosition.y/140, inPosition.y/140, inPosition.y/140, 1)) * inColor;
    #else
        color = ((m_Color)*(inPosition.y/140, inPosition.y/140, inPosition.y/140, 1));
    #endif
}

Edit: But nothing seem to happen when I do this. Not even a crash or anything. The grass keep it base color

Edit2: I even tryed to move the color.rgb*= g_LightColor.rgb inside the if,else

This comes down to shader debugging to knock off assumptions one by one. First, is this color parameter even showing up in your frag shader?

Test: set the color to red or something obvious.

Next assumption, is the if() doing what you want (branching is bad in shaders but nevermind that for the moment)…

Test: if inPosition.y > 1 set the color red, else set the color to blue.

Then work from there. At that point if you still aren’t seeing something for y > 1 (or the other) then verify that your model data actually has values above 1… and so on.

btw i got the grass to show up, seem like AMD card assume a different value for an unset variable, I was not setting no where DiffuseSum, so it was returning or >0 or <=0.

Now i ve added the correction so i could finaly try yo work on the color problem.
I think the in position work, I did remove the branching issue already. I kind of feel like it was wrong to do this that way.

For some reason i gained 200% fps after i’ve added the Lighting support.
Edit: It seem just correcting the issue gave fps.

When we started this conversation, I incorrectly assumed you were running stock JME shaders (or at least just modified ones). Else it might have occurred to me sooner what the issue was.

Default values are definitely different (and I believe unspecified) from GPU to GPU vendor.

Ah no problem, anyway i got it running, i knew it had something to do with the Alpha. Only took 2 days reading on the subject, at least I’ve learn a little more about shaders, i wish i knew how they work better.It’s such a powerful tool.

I will post a video tomorrow on my progress in the Month Thread. (My game run better on my laptop for some obscure reason, i really don’t get this) (how come a 3.07ghz cpu run slower then a 2.4ghz cpu) both i7 4cores/8threads, a real mystery. and used gpu → ( gtx460/AMD rx480 vs gtx 750m ). I really have a some kind of leak somewhere i think…