Playing around with Lighting.vert

Inspired by http://hub.jmonkeyengine.org/groups/user-code-projects/forum/topic/wip-hex-engine-–-ai-update-video/ I decided to try out modding of the lighting vertex shader using @thetoucher 's recipe (thanks!). It’s really only the part where the y-position is altered that’s changed so I’ve left out the rest of the method.

[java]

void main(){

vec4 pos = vec4(inPosition, 1.0);





float totalTime = float(m_Time);



if (totalTime > pi2){

totalTime = totalTime-pi2;

}



pos.y = sin(totalTime - inPosition.x0.2f)1.0f + sin(totalTime2 + inPosition.z0.5f)*0.5f;



gl_Position = g_WorldViewProjectionMatrix * pos;



[/java]

It gives a nice wavy movement to a gridded mesh I’m using as a water surface.



My only problem now is that I’m not quite sure what happens to the normals and other variables in the shader related to the position of the vertices. Are e.g. the normals updated automatically?



Obviously I’m a bit lost at the moment, so if anyone could push me in the right direction I would appreciate it :slight_smile:

@makeshift said:
My only problem now is that I'm not quite sure what happens to the normals and other variables in the shader related to the position of the vertices. Are e.g. the normals updated automatically?


No. No magic happens unless you code it, really. You supplied the normals in the mesh. The normals were not updated for the new position... and in fact there is no way they could have been since it is not known exactly which way a normal should point once its vertex has moved. That would be based on the rest of the mesh.

For water, this almost never matters... or you can pick some arbitrary deformation. But this is not something your GPU could know without you providing that in the shader.
1 Like

Thanx for clearing that up:)

@pspeed said:
and in fact there is no way they could have been since it is not known exactly which way a normal should point once its vertex has moved. That would be based on the rest of the mesh.

Good point. In my case it will always be the one with a positive y value, I don't need a general solution. I'm only using colors in the surface's material, so when initially the normals are computed for a flat surface there is no change to how the surface looks except for the y position of the vertices.

Is recomputing the normal's for each frame something you wouldn't recommend? It sounds a bit resource demanding.

Depends on where and how you recompute them. If you have to recompute them in the Mesh every frame then that is kind of expensive just to get a wavy effect… especially considering that the normals shouldn’t matter much in that case.



From your description, it sounds like you don’t need to recompute the normals. If the vertex normals don’t change when they are moved then there is nothing to do.

1 Like

Thanks for bearing with me. I think I might have misunderstood how normal’s are used. In the diagram below you see the triangle from the side and the arrow is the normal. Now what I have assumed is that the result if I do nothing with the normals is scenario 1, but hopefully it’s 2, and I need to do nothing as you say.



http://i.imgur.com/CGRlj.png

Normals are attached to vertexes. Change your diagram to have normals at the end points and then tell me what you expect the normals to do.



Diagram 1 is closest to what actually happens.



Diagram 2 is hard to calculate correctly though you might be able to fake it given some assumptions.



Note: if your vertexes are always the peaks and the troughs of a sine wave then their normals wouldn’t change anyway. If the sine wave “moves” then you can calculate the normal pretty easily as the tangent of the sine wave.

1 Like

Thanks for your time, I feel a little better armored for my vertex battles now. I’ll be back with the results if they are in my favor :slight_smile:

Edit:

@pspeed said:
If the sine wave "moves" then you can calculate the normal pretty easily as the tangent of the sine wave.

Yes, it moves. Thanks for the tip, didn't think of that.
EDIT2: And that was your 500th point:)

Yeah, I didn’t mean the tangent… but hopefully you know what I mean. You know the slope at that point on the sine wave so you can calculate the normal. It will be perpendicular to the tangent of course. :slight_smile:

@makeshift said:
EDIT2: And that was your 500th point:)


Heheh. Thanks.

Edit: you can bet that whatever my point total is that @normen's is 2.01 times that or so. ;)
1 Like
@pspeed said:
Heheh. Thanks.

Edit: you can bet that whatever my point total is that @normen's is 2.01 times that or so. ;)

Hehe, yeah, gratz :) You're right, this correlation becomes frighteningly consistent ^^ Tangible data for strange phenomena ;)
1 Like
@pspeed said:
Yeah, I didn't mean the tangent... but hopefully you know what I mean. You know the slope at that point on the sine wave so you can calculate the normal. It will be perpendicular to the tangent of course. :)

Yeah, got that part:) Googled up: http://www.gamedev.net/topic/551569-finding-the-normal-of-a-point-on-a-sine-wave/ just to be sure.
@pspeed and @normen:
Maybe you guys should post a live graph with your point score over time :p