I'm working in Java, using LWJGL3. I'm creating a 3D world, and would like a quad (that represents water) to have its vertices "fluctuate" away from the center of the terrain, in all directions. I was thinking of having some sort of function in the Vertex Shader, that calculates the y offset based on the Sin graph. It would take in a uniform that would be the center of the terrain, and thus would be able to correctly calculate the new position for each vertex.

Any suggestions on the maths I'd need to calculate this? I've included a few photos to try and exaggerate what I mean.

Green - Terrain

Blue - Water

Top down

Side on

EDIT: I added this into the Veretx shader, but the quad now vibrates on the spot. However it does look like the sin graph (see photo).

vec4 worldPosition = transformationMatrix * vec4(position, 1.0);
float disX = abs(center.x - worldPosition.x);
float disZ = abs(center.z - worldPosition.z);
float actualTime = time;
float distance1 = sqrt((disX * disX) + (disZ * disZ));
float newY = 20.0 * sin((distance1 - time)/20.0);
worldPosition.y = newY;

enter image description here


1 Answer 1


This question should perhaps be posted in Game Development SE site. However, I shall try to provide a solution for its mathematical part.

The points of water surface, $P(x,y,z)$ can be modeled to belong to the surface:
$\frac{\sin(x^2 + y^2 - t)}{(x^2 + y^2 + 1)}$ where

  • $(x,y) \in [-L, L] \times [-L, L]$ in which $L$ specifies the extent of the rendered water surface
  • $t$ parameter varies with time or in each iteration/render cycle

A visualization of this can be found here

  • $\begingroup$ Thanks, i just realised this myself. Did not realise it would be so simple. I posted on Stack Overflow, question got removed and suggested to post here instead. Thanks, answer accepted! $\endgroup$
    – Kris Rice
    Feb 8, 2019 at 20:29
  • $\begingroup$ @KrisRice Glad could be of help and Thanks ! You can change the constant term in denominator as well add a scalar multiplier to \sin argument to vary height and frequency of wave. Also, can replace \sin with other periodic functions to achieve other waveforms $\endgroup$
    – programmer
    Feb 8, 2019 at 20:42
  • $\begingroup$ I tried implementing this, and the quad changes to the shape of the graph, but it doesn't move. instead it seems to vibrate on the spot? See edit $\endgroup$
    – Kris Rice
    Feb 8, 2019 at 20:49
  • $\begingroup$ @KrisRice are you changing the time in your code ? if not, the wave will not move. Also, did you unaccept the answer $\endgroup$
    – programmer
    Feb 9, 2019 at 5:22
  • $\begingroup$ sorry I didn't mean to unaccept. Yes time is changing, it is uploaded to the shader every render. The quad moves, it just vibrates on the spot rather than "flows" along the wave $\endgroup$
    – Kris Rice
    Feb 12, 2019 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.