Hi again!
So I looked around and it seemed that shaders were the way to go. Since I did not know anything about shaders I searched around for a while but did not find anything that was suited for a 2D top down game. But after fighting with it for a while I managed to make something that looks alright.
[I wanted to show you the result but I cannot upload files as a new member ]
I’m not a fan of the way waves strictly follows the line of the coast and how the rhythm is regular, but for my prototype : this is good enough!
Here is the final script for those interested : (With A LOT of personal note (sorry), but I figured that they could be useful to someone who didn’t know anything like me)
There’s a version without my notes afterward.
shader_type canvas_item;
// A shader script calculates how a pixel needs to look for every frame
// You need to understand this script as a calculation happening for every pixels in view (in your camera), at every frame
// This calculation is done by your GPU (graphic card!), hence it's written in GDShader (which looks a lot like C#, it's in the same family). Not so sad you lost 2 years learning C# before trashing everything, now, right! Well no I'm still sad.
// You are applying those calculation to a texture which is a rectangle.
// Shaders consider 2 characteristics of those textures :
// ------> Their corners which defines their shape. A corner is a "vertex" ("vertices" if more than one). So vertex() makes 4 calculation per frame, one for each corner
// ------> What's inside them : pixels. This is what fragments() calculate.
// Let's do this
// SHADER PARAMETERS
//uniform is the shader equivalent of @export
// The color of the foam
uniform vec4 foam_color : source_color = vec4(1.0, 1.0, 1.0, 1.0);
// Width of your coastal zone in which waves can appear
uniform float coastal_zone_max_width = 120.0;
// Width of the shore where only foam appear but no wave
uniform float shore_foam_width = 35;
// Speed of waves
uniform float wave_speed = 2.5;
// Wave frequency
uniform float wave_frequence = 0.15;
// The noise texture for foam
uniform sampler2D noise_texture : repeat_enable;
// Scale of the noise motif in the foam
uniform float foam_pattern_scale = 0.05;
uniform float shore_foam_pattern_scale = 0.01;
// var that define the steps in smoothstep for waves
uniform float wave_step_a = 0.35;
uniform float wave_step_b = 0.50;
// var that define the steps in smoothstep for foam
uniform float foam_step_a = 0.35;
uniform float foam_step_b = 0.50;
// "varying" which corresponds to :
// - the global position of vertices in vertex()
// - the global position of our current pixel in fragment() [interpolated from what is calculated in vertex() : when called from fragment(), it essentially says : our vertices are there so this pixel is here]
varying vec2 world_position;
// The hellish thing in this shader thing is that you need to move between 4 systems of coordinates for one pixel
// - Its local coordinate on its texture rect / node : uv.
// - Its local coordinate on screen : screen_uv (which depends on the node position, the camera postion, etc.)
// - Its corresponding coordinate in the SDF (Signed Distance Field), which is used by the shader. [Conversion is made at the beginning of fragment()]
// - Its corresponding coordinate in the GPS system, which corresponds to its global coordinates in the game world in pixel. [Conversion is done in fragment() using calculations made in vertex()]
// In the vertex method, we calculate the position of the vertices (the corners of our texture node) in coordinates that can be use by the shader
// But as you can see, world_position is a "varying" variable (declared with : varying vec2 world_position;)
// A varying variable can be modified by vertex() but only read by fragments()
// What happens is that world_position get values from each corners of the texture node in vertex
// Those 4 values are used to interpolate every pixel position, automatically, by the GPU
// When called in fragment, it can be used to give the pixel position in an efficient way.
void vertex() {
// The next line requires a lot of explanation. It's essentially a translation of the local position (relative to the node carrying the shader) of the vertex to a global position in the world matrice
// VERTEX is the position vec2(x, y) from the center of the texture node of the current vertex we are looking at through the vertex() function. Their are 4 of them and we look at each of them every frame.
// Here the GPU is calculating the world shape, and it does it through MATRICES, which needs vec4 for calculations. vec4 have 4 values (x,y,z and something called "w" that I don't fully understand)
// Hence : vec4(VERTEX, 0.0, 1.0), which is the conversion of the vertex position into a matrice that can be read by the gpu. z=0 because 2D game. And 1 because???
// MODEL_MATRIX is the world matrice generated by godot : it's like a map.
// So doing (MODEL_MATRIX * vec4(VERTEX, 0.0, 1.0)) is multiplying the matrice of the world by the local position of the vertex
// This is a conversion! What you are doing is going from : this corner (vertex) is at -50,-460 from the center of the node. To : this corner (vertex) is at -48650,-33504 in the world matrice
// It's a bit like passing from local to global position.
// ".xy" is accessing the variable x and y in our vec4, which brings us back to vec2 (x , y) and ditches z and w.
// it's exactly the same as doing control_node.visible for example, when you want to access the "visible" property of a node
// In the end you get world_pos : the position of the current vertex in the world matrice in vec2 (which is sufficient for our calculations)
world_position = (MODEL_MATRIX * vec4(VERTEX, 0.0, 1.0)).xy;
// A NOTE ABOUT "w" in vec4 :
// If I understand it correctly : in a vec4 you have 4 values : x, y, z and w.
// x, y, z are simple coordinates in a 3D sapce : easy
// w is a bit like a 4th dimension and could be explained as MOVEMENT.
// If w = 0 : it does not move. If w = 1 : if follows the world matrice by applying a corresponding offset.
}
// HERE IS WHAT fragment() WILL DO TO EVERY PIXELS :
// 1 - calculate its position from the usual system (screen_uv) to the shader coordinates system (SDF)
// 2 - calculate its proximity to the nearest light_occluder (that you attach to islands)
// 3 - by default, make it transparent
// 4 - check if we are in the coastal region (defined by the user with the width of the wave zone) : IF NO, the color of the pixel stays transparent
// IF YES :
// IF IN WAVE :
// 5 - simulating a wave with a sin : we get a float value for the pixel (0, it's at the bottom of the wave; 1, it's at the top)
// 6 - sampling a part of the associated noise texture relative to the pixel position : we get another float (corresponding to the grey scale value of the noise texture). This adds choas! It avoids a smooth regular wave
// 7 - using the user defined width of the wave zone to define where the pixel is in it. We get another float comprise between 1 and 0 (0 its the farthest it can be, 1 its the closest to the beach)
// 8 - We multiply our 3 floats together and get new combined float : it gives us what could be understand as an alpha value of the pixel relative to : a - its position on the wave, b - its associated grey scale from the nosie texture, c - its distance to the shore
// 9 - We then use smoothstep to define another gradient : two values a and b. Under a, the pixel is transparent. Above b it is opaque.
// IF IN FOAM :
// ....... this was added after so check in the method directly!
// 10 - We can now give our pixel it's correct alpha value. And you get waves!
void fragment() {
// 1 - calculate its position from the usual system (screen_uv) to the shader coordinates system (SDF)
// This function is a native function in godot. Shader do not use the same coordinates as your viewport and your camera.
// This is a function that translates your viewport coordinate to the shader system (SDF : Signed Distance Fields)
// It returns the coordinate of the current pixel in the Signed Distance Field
vec2 pixel_sdf_coordinates = screen_uv_to_sdf(SCREEN_UV);
// 2 - calculate its proximity to the nearest light_occluder (that you attached to islands)
// In the background, Godot essentially draws a grey scale map of your viewscreen. If there are some light occludes, it will generates a grey scale reflecting the proximity to those light occluders.
// texture_sdf more or less accesses this map and reads it.
// by doing texture_sdf(pixel_sdf_coordinates), you are essentially asking : where is the current pixel relatively from light occluders? Is it close or far away from a light occluder?
float pixel_distance_to_shore = texture_sdf(pixel_sdf_coordinates);
// 3 - by default, make the pixel transparent
vec4 pixel_color = vec4(0, 0, 0, 0);
float wave_alpha = 0.0;
float foam_alpha = 0.0;
// 4 - check if we are in the coastal region (defined by the user with the width of the wave zone) :
if (pixel_distance_to_shore > 0.0 && pixel_distance_to_shore < coastal_zone_max_width) {
// 5 - simulating a wave with a sin :
// sin is wave function, if you feed it your distance to the shore, it will return to you floats ranging from -1 to +1
// Here we give it pixel_distance_to_shore. So the pixel position from the shore will determine its sin value. In another word, depending from it's shore distance, the sin value of our pixel will oscillate between -1 and +1.
// THIS SIMULATES A WAVE ALONG THE SHORE !
// Then we mutilply the shore distance by wave_frequence. It's because, sin is a lengthy wave and you wouldn't see a whole lot if we didn't shorten it with this factor
// Then we add TIME : this makes the waves move. This pixel will be on another position on this sin wave every frame.
// Multiplying by wave_speed increases the speed at wich this offset takes place.
// It returns a float that oscillate between -1 (the botton of the wave) and +1 (the top) depending on TIME and other user defined parameters
// You can read this as : for example, if pixel_height_on_wave = .89 then at this frame our pixel is almost at the top of the wave
float pixel_height_on_wave = sin(pixel_distance_to_shore * wave_frequence + TIME * wave_speed);
// We will use this value to define the alpha value of our pixel (the closer to the top, the more opaque)
// But alpha ranges between 0 and 1. So we need the next line to change the range of our sin result :
pixel_height_on_wave = (pixel_height_on_wave + 1.0) / 2.0;
// 6 - sampling a part of the associated noise texture relative to the pixel position :
// We now need a noise value that will add chaos : this ensures that even if you're on the top of the wave and near the shore, you can still get a pixel with a low alpha value
// Randomness is important to make this feels more natural.
// So we defined a noise texture in the editor and we want to use it to simulate foam
// To do so, we need to make a link between the position of a pixel in the world and a pixel in the noise texture
// In the next line we take the position of our pixel in the world matrice and multiply it by the scale of the texture and a vec2
// Multiplying by the scale ensure that we are only looking at a small part of the noise texture : this can be modify to achieve different looks
// vec2(TIME * 0.02, TIME * 0.01) is a position multiply by time at different scales
// This means that, each frames, our position will translate a little bit on the x and y axis (at different rates).
// This ensures that we don't sample the exact same part of the noise texture every frame for this pixel.
vec2 corresponding_noise_pixel_position = world_position * foam_pattern_scale + vec2(TIME * 0.02, TIME * 0.01);
// NOTE about using world_position here :
// So world_position is the position of our pixel in the game world. It's a global position that that does not change with player movement
// It's important here because this allow us to pick a position in our noise texture that will not move when the player moves.
// Since world_position is not relative to the player position, then the sample we take in the noise texture is not either.
// This is important to avoid the impression of the waves sliding with the player.
// And the other thing we gain from this is that, since world_position does not change, the translation made with time vec2(TIME * 0.02, TIME * 0.01) is smooth
// We are not jumping around in the texture, we move for one pixel to another close one. And since the noise texture is a smooth transition of grey scales, you don't accidently get pixel going from transparent to opaque in a single frame.
// This avoids scintillating pixels
// We now have a position (corresponding_noise_pixel_position) that can be used to sample our noise texture
// texture(noise_texture, corresponding_noise_pixel_position).r does exactly this
// It says, take our noise texture (noise_texture) and sample this position (corresponding_noise_pixel_position)
// it return the color value of our noise tecture at this position
// Since the noise texture is in grey scale we don't need every value (red, green, blue, alpha). We only need one color.
// Hence ".r", which means accessing the r variable in our vec4.
float noise_pixel_color = texture(noise_texture, corresponding_noise_pixel_position).r;
//7 - using the user defined width of the wave zone to define where the pixel is in it.
float pixel_proximity_to_shore = 1.0 - (pixel_distance_to_shore / coastal_zone_max_width);
// 8 - We multiply our 3 floats together and get new combined float : it gives us what could be understand as an alpha value of the pixel relative to : a - its position on the wave, b - its associated grey scale from the nosie texture, c - its distance to the shore
// Let's combine the three floats we calculate to get our perfect alpha value dependent on the position of the pixel on the wave and its distance to the shore (and some CHAOS via noise)
float final_alpha_value = pixel_height_on_wave * noise_pixel_color * pixel_proximity_to_shore;
// 9 - We then use smoothstep to define another gradient : two values a and b. Under a, the pixel is transparent. Above b it is opaque.
// We now have alpha value for our pixel that reflects, wave height, shore distance and chaos.
// BUT. It's a smooth gradient between 0 and 1. Visually we would get a white mist of pixels since almost every pixels would get a positive alpha value
// And almost no pixels would be completely opaque. So this would not look like foam. If you think about wind waker for example (really cartoony), you have almost no gradient : it is foam (opaque) or it is not (transparent).
// So to reproduce this effect, we use smoothstep, a built in function that uses 2 variables : a and b.
// under a : you are transparent
// above b : you are opaque
// in between : you slowly goes from transparent to opaque
// the slimer the range between a and b, the mode cartoony the look (like wind waker)
// too small, it sucks. too wide, it sucks. Find the right values!
float smooth_stepped_alpha = smoothstep(wave_step_a, wave_step_b, final_alpha_value);
wave_alpha = smooth_stepped_alpha;
// We are using the same logic as before but without the sin (no wave)
corresponding_noise_pixel_position = world_position * shore_foam_pattern_scale + vec2(TIME * 0.02, TIME * 0.01);
noise_pixel_color = texture(noise_texture, corresponding_noise_pixel_position).r;
pixel_proximity_to_shore = 1.0 - (pixel_distance_to_shore / shore_foam_width);
final_alpha_value = noise_pixel_color * pixel_proximity_to_shore;
smooth_stepped_alpha = smoothstep(foam_step_a, foam_step_b, final_alpha_value);
foam_alpha = smooth_stepped_alpha;
}
// AND WE THEN DEFINE THE COLOR WITH THE BIGGER ALPHA:
if(wave_alpha > foam_alpha){
pixel_color = vec4(foam_color.rgb, foam_color.a * wave_alpha);
}
else {
pixel_color = vec4(foam_color.rgb, foam_color.a * foam_alpha);
}
//applying the final color to our pixel
COLOR = pixel_color;
}
Without notes :
shader_type canvas_item;
// SHADER PARAMETERS
// The color of the foam
uniform vec4 foam_color : source_color = vec4(1.0, 1.0, 1.0, 1.0);
// Width of your coastal zone in which waves can appear
uniform float coastal_zone_max_width = 120.0;
// Width of the shore where only foam appear but no wave
uniform float shore_foam_width = 35;
// Speed of waves
uniform float wave_speed = 2.5;
// Wave frequency
uniform float wave_frequence = 0.15;
// The noise texture for foam
uniform sampler2D noise_texture : repeat_enable;
// Scale of the noise motif in the foam
uniform float foam_pattern_scale = 0.05;
uniform float shore_foam_pattern_scale = 0.01;
// var that define the steps in smoothstep for waves
uniform float wave_step_a = 0.35;
uniform float wave_step_b = 0.50;
// var that define the steps in smoothstep for foam
uniform float foam_step_a = 0.35;
uniform float foam_step_b = 0.50;
varying vec2 world_position;
void vertex() {
world_position = (MODEL_MATRIX * vec4(VERTEX, 0.0, 1.0)).xy;
}
void fragment() {
// 1 - calculate its position from the usual system (screen_uv) to the shader coordinates system (SDF)
vec2 pixel_sdf_coordinates = screen_uv_to_sdf(SCREEN_UV);
// 2 - calculate its proximity to the nearest light_occluder (that you attached to islands)
float pixel_distance_to_shore = texture_sdf(pixel_sdf_coordinates);
// 3 - by default, make the pixel transparent
vec4 pixel_color = vec4(0, 0, 0, 0);
float wave_alpha = 0.0;
float foam_alpha = 0.0;
// 4 - check if we are in the coastal region (defined by the user with the width of the wave zone) :
if (pixel_distance_to_shore > 0.0 && pixel_distance_to_shore < coastal_zone_max_width) {
// 5 - simulating a wave with a sin :
float pixel_height_on_wave = sin(pixel_distance_to_shore * wave_frequence + TIME * wave_speed);
pixel_height_on_wave = (pixel_height_on_wave + 1.0) / 2.0;
// 6 - sampling a part of the associated noise texture relative to the pixel position :
vec2 corresponding_noise_pixel_position = world_position * foam_pattern_scale + vec2(TIME * 0.02, TIME * 0.01);
float noise_pixel_color = texture(noise_texture, corresponding_noise_pixel_position).r;
//7 - using the user defined width of the wave zone to define where the pixel is in it.
float pixel_proximity_to_shore = 1.0 - (pixel_distance_to_shore / coastal_zone_max_width);
// 8 - We multiply our 3 floats together and get new combined float :
float final_alpha_value = pixel_height_on_wave * noise_pixel_color * pixel_proximity_to_shore;
// 9 - We then use smoothstep to define another gradient : two values a and b. Under a, the pixel is transparent. Above b it is opaque.
float smooth_stepped_alpha = smoothstep(wave_step_a, wave_step_b, final_alpha_value);
wave_alpha = smooth_stepped_alpha;
// We are using the same logic as before but without the sin (no wave)
corresponding_noise_pixel_position = world_position * shore_foam_pattern_scale + vec2(TIME * 0.02, TIME * 0.01);
noise_pixel_color = texture(noise_texture, corresponding_noise_pixel_position).r;
pixel_proximity_to_shore = 1.0 - (pixel_distance_to_shore / shore_foam_width);
final_alpha_value = noise_pixel_color * pixel_proximity_to_shore;
smooth_stepped_alpha = smoothstep(foam_step_a, foam_step_b, final_alpha_value);
foam_alpha = smooth_stepped_alpha;
}
// AND WE THEN DEFINE THE COLOR WITH THE BIGGER ALPHA:
if(wave_alpha > foam_alpha){
pixel_color = vec4(foam_color.rgb, foam_color.a * wave_alpha);
}
else {
pixel_color = vec4(foam_color.rgb, foam_color.a * foam_alpha);
}
COLOR = pixel_color;
}