tessarakkt / godot4-oceanfft

Tessendorf FFT based ocean waves and buoyancy in Godot 4 using compute shaders
MIT License
372 stars 18 forks source link

wave direction error #1

Closed toudart closed 1 year ago

toudart commented 1 year ago

Hi, Thank you very much for this work. I noticed that the wind_uv_offset and wind_angle transfered from Ocean3d to the shader contains both the rotation. If we remove the wind_angle, the choosen rotation with the widget is applyed.

Thank you

tessarakkt commented 1 year ago

Hey! Thanks for reaching out!

The wind_uv_offset and wind_angle variables do both include rotation information, but for different purposes.

The wave speed slider in the widget is used to adjust a scrolling speed for the displacement map in the direction of the wind. If you then changed the wind angle after the scrolling has been happening for a bit, it was causing the textures (and thus the waves) to jump around. To avoid this, I total how much the textures have scrolled each frame in Ocean3D, which adds a vector containing the distance the wave scrolled each frame, as well as the wind angle for that frame.

This results in wind_uv_offset effectively being an accumulated history of the wind, which can be applied in a single pass. This offsets the displacement textures to avoid jumping when a wind_angle is applied, but does not rotate them, so they are always square with the world axes.

The calculations with wind_angle within the shader are used to rotate the displacement map itself (via rotated_uv), and then also rotate each individual pixel (via get_rotated_displacement()).

The rotation of the displacement map rotates the waves to align with the wind, rather than with the world axes.

The additional rotation of each pixel is due to the fact that the displacement map only encodes 2 displacements per pixel; a horizontal displacement in the red channel, and a vertical (height) displacement in the green channel. If the individual pixels are not rotated, then the wave will not properly deform when a non-default wind angle is applied, and this also causes the surface normals to not correctly be calculated, resulting in very visible lighting issues, especially when the wind angle provided is perpendicular to the default wind.

This should be working as intended, and was for me when I tested on the latest commit (cee5f2c), and latest Godot 4.0-beta8. Do the waves not rotate for you when you adjust the wind angle? Are you able to provide screenshots (before and after making a change to the wind angle) or video of what happens?

toudart commented 1 year ago

When i used your code without modification, the swell always come from east and go west (from original camera POV), when i comment ##_material.set_shader_parameter("wind_angle", _wind_rad) to not update the angle in the shader, the swell is aligned with wind direction. With this correction i obtained this video : https://www.transfernow.net/dl/202212191Rdrpm9H

tessarakkt commented 1 year ago

Thanks for getting back to me and including the video.

It seems I wasn't paying enough attention when I was testing the wave motion when I said this was working. Although the wave textures get rotated, the actual motion of them is in a constant direction.

This rotation method worked when I was working with a single FFT, but it looks like with multiple cascaded FFTs it is not good. Similarly the method you suggest didn't look good with a single FFT, but looks great with multiple (and in theory should be better for performance, due to fewer rotation calculations).

I'll need to remove the code in the shader that actually uses that angle. The buoyancy system will also need to be adjusted to the new wave pattern. I'll aim to have that done by the weekend.

tessarakkt commented 1 year ago

This latest commit 310de63 makes your suggested change to the wave direction, as well as the supporting changes I mentioned in my last comment.

Thank you for taking the time to help me improve this, and don't hesitate to reopen the issue - or open a new one - if there is anything else that you notice.