diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 89633d9..ed15a77 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -1,16 +1,5 @@ { "configurations": [ - { - "name": "macOS", - "includePath": [ - "${workspaceFolder}/include", - "${workspaceFolder}/external/engine", - "${workspaceFolder}/external/raygui", - "${workspaceFolder}/external/rini", - "/opt/homebrew/Cellar/raylib/5.5/include" - ], - "cStandard": "c99", - }, { "name": "Windows", "includePath": [ diff --git a/assets/CausticTexture.png b/assets/CausticTexture.png new file mode 100644 index 0000000..06fdc18 Binary files /dev/null and b/assets/CausticTexture.png differ diff --git a/assets/CausticTextureHighlights.png b/assets/CausticTextureHighlights.png new file mode 100644 index 0000000..067fc0e Binary files /dev/null and b/assets/CausticTextureHighlights.png differ diff --git a/assets/cloudnoise_1.png b/assets/cloudnoise_1.png new file mode 100644 index 0000000..31f75f5 Binary files /dev/null and b/assets/cloudnoise_1.png differ diff --git a/assets/cloudnoise_1.webp b/assets/cloudnoise_1.webp new file mode 100644 index 0000000..611adb7 Binary files /dev/null and b/assets/cloudnoise_1.webp differ diff --git a/assets/noiseTexture.png b/assets/noiseTexture.png new file mode 100644 index 0000000..0bde3a3 Binary files /dev/null and b/assets/noiseTexture.png differ diff --git a/assets/ocean_base.png b/assets/ocean_base.png new file mode 100644 index 0000000..c98f99b Binary files /dev/null and b/assets/ocean_base.png differ diff --git a/assets/sea.mp3 b/assets/sea.mp3 new file mode 100644 index 0000000..d86539c Binary files /dev/null and b/assets/sea.mp3 differ diff --git a/assets/start_ship.png b/assets/start_ship.png new file mode 100644 index 0000000..42f6917 Binary files /dev/null and b/assets/start_ship.png differ diff --git a/assets/voronoise_3.png b/assets/voronoise_3.png new file mode 100644 index 0000000..29b0611 Binary files /dev/null and b/assets/voronoise_3.png differ diff --git a/assets/voronoise_3.webp b/assets/voronoise_3.webp new file mode 100644 index 0000000..45e2604 Binary files /dev/null and b/assets/voronoise_3.webp differ diff --git a/config/config.ini b/config/config.ini index 3b1e504..94b4d46 100644 --- a/config/config.ini +++ b/config/config.ini @@ -1,9 +1,10 @@ -startView = 0 ; LOGO = 0, MENU = 1, GAMEPLAY = 2 +startView = 1 ; LOGO = 0, MENU = 1, GAMEPLAY = 2, SETTINGS = 3 -window_title = The Last Hive +window_title = The Last Pirate window_width = 1280 ; in Pixel window_height = 720 ; in Pixel -showFPS = 1 ; OFF = 0, ON = 1 +showFPS = 0 ; OFF = 0, ON = 1 +soundtrackOn = 1 ; OFF = 0, ON = 1 -font_path = assets/SproutLandsUI/fonts/pixelFont-7-8x14-sproutLands.fnt \ No newline at end of file +font_path = assets/SproutLandsUI/fonts/pixelFont-7-8x14-sproutLands.fnt diff --git a/external/engine/postfx.h b/external/engine/postfx.h index fa3aad7..29921fb 100644 --- a/external/engine/postfx.h +++ b/external/engine/postfx.h @@ -17,7 +17,7 @@ PostProcessing CreatePostFX(const char* shaderPath){ return postFX; } - postFX.strength = 0.3f; + postFX.strength = 0.0f; postFX.resolutionLoc = GetShaderLocation(postFX.shader, "resolution"); postFX.strengthLoc = GetShaderLocation(postFX.shader, "strength"); Vector2 resolution = { (float)GetScreenWidth(), (float)GetScreenHeight() }; diff --git a/include/floor.h b/include/floor.h index 3b6bf1a..3b3cdcc 100644 --- a/include/floor.h +++ b/include/floor.h @@ -1,37 +1,108 @@ #ifndef Floor_H #define Floor_H + +#include #include typedef struct { Sprite sprite; Rectangle bounds; + Shader shader; + float time; } Floor; -Floor CreateFloor(Texture2D texture, float textureWidth) +// ---------------------------------------------------- +// Create floor (loads shader and sets defaults) +// ---------------------------------------------------- +Floor CreateFloor() { - return (Floor){ + + + Texture2D base = LoadTexture("assets/ocean_base.png"); + Texture2D caustic = LoadTexture("assets/CausticTexture.png"); + Texture2D causticHighlight = LoadTexture("assets/CausticTextureHighlights.png"); + Texture2D noise = LoadTexture("assets/noiseTexture.png"); + Texture2D cloud = LoadTexture("assets/cloudnoise_1.png"); + Texture2D voronoise = LoadTexture("assets/voronoise_3.png"); + + float textureWidth = 64.0f; + + Floor floor = { .sprite = { - .texture = texture, + .texture = base, .frameSize = {textureWidth, textureWidth}, .sourceRec = {0.0f, 0.0f, textureWidth, textureWidth}, .destRec = {0.0f, 0.0f, textureWidth, textureWidth}, .origin = {textureWidth / 2, textureWidth / 2}, .rotation = 0.0f, .color = WHITE}, - .bounds = {0.0f, 0.0f, 0.0f, 0.0f}}; + .bounds = {0}, + .shader = LoadShader("shaders/water.vs", "shaders/water.fs"), + .time = 0.0f + }; + + float worldTileSize = 128.0f; // one tile covers 128x128 world units + float textureSize = 32.0f; // texture is 32x32 pixels + + floor.sprite.frameSize = (Vector2){worldTileSize, worldTileSize}; + floor.sprite.sourceRec = (Rectangle){0, 0, textureSize, textureSize}; + floor.sprite.destRec = (Rectangle){0, 0, worldTileSize, worldTileSize}; + floor.sprite.origin = (Vector2){0, 0}; + + + // Optional: load your extra water textures here and bind them + // Example: + + + SetShaderValueTexture(floor.shader, GetShaderLocation(floor.shader, "causticTexture"), caustic); + SetShaderValueTexture(floor.shader, GetShaderLocation(floor.shader, "causticHighlightTexture"), causticHighlight); + SetShaderValueTexture(floor.shader, GetShaderLocation(floor.shader, "causticNoiseTexture"), noise); + SetShaderValueTexture(floor.shader, GetShaderLocation(floor.shader, "causticFadeNoiseTexture"), cloud); + SetShaderValueTexture(floor.shader, GetShaderLocation(floor.shader, "specularNoiseTexture"), voronoise); + SetShaderValueTexture(floor.shader, GetShaderLocation(floor.shader, "specularMovementLeftNoiseTexture"), voronoise); + SetShaderValueTexture(floor.shader, GetShaderLocation(floor.shader, "specularMovementRightNoiseTexture"), cloud); + + + // Texture2D caustic = LoadTexture("resources/caustic.png"); + // SetShaderValueTexture(floor.shader, GetShaderLocation(floor.shader, "causticTexture"), caustic); + + // Setup aspect ratio and pixelization uniforms once + float aspect = (float)GetScreenHeight() / (float)GetScreenWidth(); + float pixelization = 2048.0f; + int aspectLoc = GetShaderLocation(floor.shader, "aspectRatio"); + int pixelLoc = GetShaderLocation(floor.shader, "pixelization"); + SetShaderValue(floor.shader, aspectLoc, &aspect, SHADER_UNIFORM_FLOAT); + SetShaderValue(floor.shader, pixelLoc, &pixelization, SHADER_UNIFORM_FLOAT); + + floor.sprite.texture = base; + + return floor; } +// ---------------------------------------------------- +// Update visible area (same logic as before) +// ---------------------------------------------------- void UpdateFloor(Floor *floor, const Camera2D *camera) { floor->bounds.x = (int)((camera->target.x - (GetScreenWidth() / 2.0f / camera->zoom)) / floor->sprite.frameSize.x) - 1; floor->bounds.width = (int)((camera->target.x + (GetScreenWidth() / 2.0f / camera->zoom)) / floor->sprite.frameSize.x) + 1; floor->bounds.y = (int)((camera->target.y - (GetScreenHeight() / 2.0f / camera->zoom)) / floor->sprite.frameSize.y) - 1; floor->bounds.height = (int)((camera->target.y + (GetScreenHeight() / 2.0f / camera->zoom)) / floor->sprite.frameSize.y) + 1; + + // Update time uniform + floor->time += GetFrameTime(); + int timeLoc = GetShaderLocation(floor->shader, "time"); + SetShaderValue(floor->shader, timeLoc, &floor->time, SHADER_UNIFORM_FLOAT); } +// ---------------------------------------------------- +// Render floor tiles using the shader +// ---------------------------------------------------- void RenderFloor(Floor *floor) { + BeginShaderMode(floor->shader); + for (int y = (int)floor->bounds.y; y <= (int)floor->bounds.height; y++) { for (int x = (int)floor->bounds.x; x <= (int)floor->bounds.width; x++) @@ -41,6 +112,16 @@ void RenderFloor(Floor *floor) RenderSprite(&floor->sprite); } } + + EndShaderMode(); +} + +// ---------------------------------------------------- +// Cleanup +// ---------------------------------------------------- +void UnloadFloor(Floor *floor) +{ + UnloadShader(floor->shader); } #endif // Floor_H diff --git a/include/game.h b/include/game.h index cac77a2..74bc3e0 100644 --- a/include/game.h +++ b/include/game.h @@ -1,34 +1,26 @@ #ifndef GAME_H #define GAME_H -#include #include #include + +#include "floor.h" +#include "player.h" + +#include "gameassets.h" #include #include #include -#include -#include - -#include "gameassets.h" -#include "floor.h" -#include "player.h" -#include "bee.h" -#include "hive.h" -#include "flowers.h" typedef struct { - Assets assets; + Assets assets; Texture2D textures[TEX_COUNT]; PostProcessing postFX[SHADER_COUNT]; Soundtrack soundtracks[SOUNDTRACK_COUNT]; RenderTexture2D target; Floor floor; - Object hive; - Array* flowers; Player player; - Bee bee1; Camera2D camera; Mouse mouse; Image image; @@ -57,22 +49,13 @@ void InitGame(GameState *g, Window *window) g->soundtracks[i] = LoadSoundtrack(g->assets.soundtrackAssets[i].path); } - // Target texture g->target = LoadRenderTexture(window->width, window->height); - - // Static Entities - g->flowers = CreateFlowers(g->textures[FLOWER], g->assets.textureAssets[FLOWER].frameWidth, g->assets.textureAssets[FLOWER].rotation, 50.0f); - g->floor = CreateFloor(g->textures[GRASS], g->assets.textureAssets[GRASS].frameWidth); - g->hive = CreateHive(g->textures[HIVE], g->assets.textureAssets[HIVE].frameWidth); - - // Animated Entities - g->player = CreatePlayer(IDLE_DOWN, HOE, g->textures[PLAYER], g->assets.textureAssets[PLAYER].frameWidth, 4, 2, g->hive.position, g->assets.textureAssets[PLAYER].rotation, 50.0f); - // g->bee1 = CreateBee(FLYING, g->textures[BEE], g->assets.textureAssets[BEE].frameWidth, 4, 10, g->hive.position, 10.0f, &g->flowers, &g->hive); - - // I/O + g->player = CreatePlayer(); + g->floor = CreateFloor(); g->camera = CreateCamera(window->width, window->height, g->player.physics.position, 10.0f); - g->mouse = CreateMouse(0.10f, 5.0f, 10.0f, &g->textures[CURSOR]); + Texture2D mouseTexture = LoadTexture("assets/SproutLandsUI/Spritesheets/Mouse sprites/TriangleMouseicon1.png"); + g->mouse = CreateMouse(0.10f, 5.0f, 10.0f, &mouseTexture); } void UpdateGame(GameState *g, State *state, Flags *flags) @@ -85,15 +68,18 @@ void UpdateGame(GameState *g, State *state, Flags *flags) { SetTargetFPS(0); } + if (IsKeyPressed(KEY_ESCAPE)) { state->currentView = MENU; } + if (WindowShouldClose() && !IsKeyPressed(KEY_ESCAPE)) { state->running = false; } - for (int i = 0; i < SHADER_COUNT; i++) + + for (int i = 0; i < SHADER_COUNT; i++) { UpdatePostFX(&g->postFX[i]); } @@ -105,18 +91,13 @@ void UpdateGame(GameState *g, State *state, Flags *flags) UpdateMouse(&g->mouse, &g->camera); UpdateCamera2D(&g->camera, &g->player.physics.position, &g->mouse); UpdateFloor(&g->floor, &g->camera); - UpdatePlayer(&g->player, g->flowers); - UpdateArray(g->flowers, UpdateFlower); - // UpdateBee(&g->bee1, &g->flowers, &g->hive); + UpdatePlayer(&g->player); } void RenderComponents(GameState *g) { RenderFloor(&g->floor); - RenderSprite(&g->hive.sprite); - RenderArray(g->flowers, RenderFlower); - RenderSprite(&g->player.sprite); - RenderSprite(&g->bee1.sprite); + RenderPlayer(&g->player); } void RenderGame(GameState *g, const State *appState, Flags *flags) @@ -128,13 +109,13 @@ void RenderGame(GameState *g, const State *appState, Flags *flags) EndMode2D(); DrawTextureRec(g->mouse.cursorTexture, (Rectangle){0, 0, g->mouse.cursorTexture.width, g->mouse.cursorTexture.height}, (Vector2){g->mouse.screenPosition.x, g->mouse.screenPosition.y}, WHITE); EndTextureMode(); + BeginDrawing(); ClearBackground(BLACK); for (int i = 0; i < SHADER_COUNT; i++) { RenderPostFX(&g->postFX[i], &g->target); } - DrawText(TextFormat("Flower Seeds: %d", g->player.inventar.flowerSeedCount), 10, 10, 50, WHITE); if (flags->showFPS) { @@ -162,7 +143,6 @@ void CleanupGame(GameState *g) UnloadMusicStream(g->soundtracks[i].music); } UnloadRenderTexture(g->target); - FreeArray(&g->flowers); } #endif // GAME_H diff --git a/include/gameassets.h b/include/gameassets.h index bf31de6..435f494 100644 --- a/include/gameassets.h +++ b/include/gameassets.h @@ -18,7 +18,7 @@ typedef enum } TextureID; const TextureAsset texAssets[TEX_COUNT] = { - {"assets/SproutLandsBasic/Characters/character.png", 48.0f, 48.0f, 0.0f}, + {"assets/ship.png", 32.0f, 32.0f, 0.0f}, {"assets/SproutLandsBasic/Characters/BasicCharakterActions.png", 48.0f, 48.0f, 0.0f}, {"assets/SproutLandsBasic/Objects/tools.png", 48.0f, 16.0f, 0.0f}, {"assets/carrot_and_tomato.png", 32.0f, 32.0f, 0.0f}, @@ -47,7 +47,7 @@ typedef enum } SoundtrackID; const SoundtrackAsset soundtrackAssets[SOUNDTRACK_COUNT] = { - {"assets/beez.mp3"}}; + {"assets/sea.mp3"}}; //////////////////////////////////////////////////////////////////////////// diff --git a/include/gameui.h b/include/gameui.h new file mode 100644 index 0000000..6e90b23 --- /dev/null +++ b/include/gameui.h @@ -0,0 +1,19 @@ +#ifndef GAMEUI +#define GAMEUI + +#include "flags.h" + +void RenderGameUI(bool showFPS){ + BeginDrawing(); + ClearBackground(BLACK); + + if (showFPS) + { + DrawFPS(10, 10); + } + + EndDrawing(); +} + +#endif //GAMEUI + diff --git a/include/player.h b/include/player.h index 7bb8912..b7c7c66 100644 --- a/include/player.h +++ b/include/player.h @@ -5,145 +5,148 @@ #include #include #include -#include +#include -#include "flowers.h" #include "inventar.h" typedef enum { - IDLE, - WALKING, - USING_TOOL -} PlayerState; + START_SHIP, + PLAYER_TEX_COUNT +} playerTextureID; -typedef enum -{ - NO_TOOL, - HOE, - AXT, - CAN -} SelectedTool; +const TextureAsset textureAssets[PLAYER_TEX_COUNT] = { + {"assets/start_ship.png", 16.0f, 16.0f, 0.0f} +}; + + + +typedef enum { + ANCHOR, + SLOW_AHEAD, + FAST_AHEAD, + SPEED_COUNT +} playerSpeedID; + +float playerSpeeds[SPEED_COUNT] = { + 0.0f, + 5.0f, + 10.0f +}; -typedef enum -{ - IDLE_DOWN, - IDLE_UP, - IDLE_LEFT, - IDLE_RIGHT, - WALKING_DOWN, - WALKING_UP, - WALKING_LEFT, - WALKING_RIGHT, - HOE_DOWN, - HOE_UP, - HOE_RIGHT, - HOE_LEFT, - AXT_DOWN, - AXT_UP, - AXT_RIGHT, - AXT_LEFT, - CAN_DOWN, - CAN_UP, - CAN_RIGHT, - CAN_LEFT, -} PlayerAnimationState; typedef struct { - SelectedTool selectedTool; Sprite sprite; Animation animation; Physics physics; - Inventar inventar; + float rotation; } Player; -Player CreatePlayer(PlayerAnimationState animState, SelectedTool selectedTool, Texture2D texture, float frameWidth, int maxFrame, int framesSpeed, Vector2 startPosition, float rotation, float speed) +Player CreatePlayer(void) { - return (Player){ - .selectedTool = selectedTool, - .sprite = { + + TextureAsset startShipAsset = textureAssets[START_SHIP]; + + Texture2D texture = LoadTexture(startShipAsset.path); + float frameWidth = startShipAsset.frameWidth; + float rotation = startShipAsset.rotation; + + + Sprite playerSprite = { .texture = texture, .frameSize = {frameWidth, frameWidth}, .sourceRec = {0.0f, 0.0f, frameWidth, frameWidth}, .destRec = {0.0f, 0.0f, frameWidth, frameWidth}, .origin = {frameWidth / 2, frameWidth / 2}, .rotation = rotation, - .color = WHITE}, - .animation = {.state = animState, .currentFrame = 0, .maxFrame = maxFrame, .framesCounter = 0, .framesSpeed = framesSpeed, .animTimer = 0.0f}, - .physics = {.position = startPosition, .direction = (Vector2){0.0f, 0.0f}, .speed = speed}, - .inventar = {.flowerSeedCount = 6}}; + .color = WHITE + }; + + Animation playerAnimation = { + .state = 1, + .currentFrame = 0, + .maxFrame = 3, + .framesCounter = 0, + .framesSpeed = 0.0f, + .animTimer = 0.0f + }; + + Physics playerPhysics = { + .position = (Vector2){0.0f,0.0f}, + .direction = (Vector2){0.0f, 0.0f}, + .speed = playerSpeeds[ANCHOR] + }; + + + return (Player){ + .sprite = playerSprite, + .animation = playerAnimation, + .physics = playerPhysics, + .rotation = rotation + }; } -void UpdatePlayerAnimationState(int *state, SelectedTool *selectedTool, Vector2 *dir) +void UpdatePlayer(Player *player) { - if (IsKeyPressed(KEY_ZERO)) - { - *selectedTool = NO_TOOL; - } - if (IsKeyPressed(KEY_ONE)) - { - *selectedTool = HOE; - } - if (IsKeyPressed(KEY_TWO)) - { - *selectedTool = AXT; - } + if(IsKeyPressed(KEY_W) || IsKeyPressed(KEY_UP)){ + if(player->physics.speed < playerSpeeds[FAST_AHEAD]){ + if(player->physics.speed < playerSpeeds[SLOW_AHEAD]){ + player->physics.speed = playerSpeeds[SLOW_AHEAD]; + player->sprite.sourceRec.x = 16.0f * 1; + } + else { + player->physics.speed = playerSpeeds[FAST_AHEAD]; + player->sprite.sourceRec.x = 16.0f * 2; + } + } + } - if (IsKeyPressed(KEY_THREE)) - { - *selectedTool = CAN; - } + if(IsKeyPressed(KEY_S) || IsKeyPressed(KEY_DOWN)){ + if(player->physics.speed > playerSpeeds[ANCHOR]){ + if(player->physics.speed > playerSpeeds[SLOW_AHEAD]){ + player->physics.speed = playerSpeeds[SLOW_AHEAD]; + player->sprite.sourceRec.x = 16.0f * 1; + } + else { + player->physics.speed = playerSpeeds[ANCHOR]; + player->sprite.sourceRec.x = 16.0f * 0; + + } + } + } + + + float rotationSpeed = 100.0f; + + if (player->physics.speed > playerSpeeds[ANCHOR]){ + + if (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT)) { + player->rotation -= rotationSpeed * GetFrameTime(); + } + + if (IsKeyDown(KEY_D) || IsKeyDown(KEY_RIGHT)) { + player->rotation += rotationSpeed * GetFrameTime(); + } + } + + player->sprite.rotation = player->rotation; + + float rad = (player->rotation - 90.0f) * (PI / 180.0f); + Vector2 dir = { cosf(rad), sinf(rad) }; - if (dir->x == 0 && dir->y == 0) - { - switch (*state) - { - case WALKING_DOWN: - *state = IDLE_DOWN; - break; - case WALKING_UP: - *state = IDLE_UP; - break; - case WALKING_LEFT: - *state = IDLE_LEFT; - break; - case WALKING_RIGHT: - *state = IDLE_RIGHT; - break; - default: - break; - } - } - else - { - if (dir->y == 1) - *state = WALKING_DOWN; - else if (dir->y == -1) - *state = WALKING_UP; - else if (dir->x == -1) - *state = WALKING_LEFT; - else if (dir->x == 1) - *state = WALKING_RIGHT; - } -} -void UpdatePlayer(Player *player, Array* flowers) -{ - if (IsKeyPressed(KEY_SPACE) && player->inventar.flowerSeedCount > 0) - { - AddFlower(flowers, player->sprite.destRec); - player->inventar.flowerSeedCount--; - } - Vector2 dir = GetDirectionVector(); - UpdatePlayerAnimationState(&player->animation.state, &player->selectedTool, &dir); UpdatePhysics(&player->physics, dir); UpdateSpriteDestRec(&player->sprite, &player->physics.position); - UpdateAnimation(&player->animation, GetFrameTime()); - UpdateSpriteSourceRec(&player->sprite, &(Vector2){player->sprite.frameSize.x * player->animation.currentFrame, player->sprite.frameSize.y * player->animation.state}); + //UpdateAnimation(&player->animation, GetFrameTime()); + //UpdateSpriteSourceRec(&player->sprite, &(Vector2){player->sprite.frameSize.x * player->animation.currentFrame, player->sprite.frameSize.y * player->animation.state}); +} + +void RenderPlayer(Player *player){ + RenderSprite(&player->sprite); } #endif // PLAYER_H diff --git a/include/ship.h b/include/ship.h new file mode 100644 index 0000000..e69de29 diff --git a/shaders/notes.md b/shaders/notes.md new file mode 100644 index 0000000..f7df0a9 --- /dev/null +++ b/shaders/notes.md @@ -0,0 +1,14 @@ +✅ Notes for using in Raylib + +Vertex shader: You can use the default Raylib one (default.vs). + +Time uniform: Update each frame in C: + +float t = GetTime(); +SetShaderValue(shader, GetShaderLocation(shader, "time"), &t, SHADER_UNIFORM_FLOAT); + +Other textures: Bind them using SetShaderTexture(shader, loc, texture). + +If you want, I can also write a full Raylib C snippet showing exactly how to load this shader and set all the textures and uniforms. + +Do you want me to do that? diff --git a/shaders/water.fs b/shaders/water.fs new file mode 100644 index 0000000..6cb7fce --- /dev/null +++ b/shaders/water.fs @@ -0,0 +1,137 @@ +#version 330 + +// ----------------------------- +// Inputs +in vec2 fragTexCoord; +in vec4 fragColor; + +// ----------------------------- +// Outputs +out vec4 finalColor; + +// ----------------------------- +// Uniforms +uniform sampler2D texture0; // main texture +uniform sampler2D waterDepthGradient; +uniform sampler2D causticTexture; +uniform sampler2D causticHighlightTexture; +uniform sampler2D causticNoiseTexture; +uniform sampler2D causticFadeNoiseTexture; +uniform sampler2D specularNoiseTexture; +uniform sampler2D specularMovementLeftNoiseTexture; +uniform sampler2D specularMovementRightNoiseTexture; +uniform sampler2D foamTexture; + +uniform float aspectRatio = 1.0; +uniform float pixelization = 2048.0; + +uniform vec4 causticColor = vec4(0.455, 0.773, 0.765, 1.0); +uniform vec4 causticHighlightColor = vec4(0.741, 0.894, 0.898, 1.0); +uniform float causticScale = 12.0; +uniform float causticSpeed = 0.005; +uniform float causticMovementAmount = 0.15; +uniform float causticFaderMultiplier = 1.45; + +uniform vec4 specularColor = vec4(1.0, 1.0, 1.0, 1.0); +uniform float specularThreshold = 0.35; +uniform float specularSpeed = 0.025; +uniform float specularScale = 15.0; + +uniform vec4 foamColor = vec4(1.0, 1.0, 1.0, 1.0); +uniform float foamIntensity = 0.2; +uniform float foamScale = 15.0; + +uniform vec4 outlineColor = vec4(0.675, 0.86, 1.0, 1.0); +uniform float generalTransparency = 1.0; + +uniform float time; + +// ----------------------------- +// Helper functions + +vec2 blendSubtract_vec2(vec2 base, vec2 blend, float opacity) +{ + vec2 result = base - blend; + return mix(base, result, opacity); +} + +float blendSubtract_float(float base, float blend, float opacity) +{ + float result = base - blend; + return mix(base, result, opacity); +} + +float blendOverlay_float(float base, float blend, float opacity) +{ + float result1 = 1.0 - 2.0 * (1.0 - base) * (1.0 - blend); + float result2 = 2.0 * base * blend; + float zeroOrOne = step(0.5, base); + float res = result2 * zeroOrOne + (1.0 - zeroOrOne) * result1; + return mix(base, res, opacity); +} + +vec2 pixelizeCoordinates(vec2 coordinates) +{ + return floor(coordinates * pixelization) / pixelization; +} + +vec2 applyAspectRatio(vec2 coordinates) +{ + return vec2(coordinates.x, coordinates.y * aspectRatio); +} + +// ----------------------------- +// Shader layers + +vec4 caustics(vec2 pixelizedCoordinates) +{ + vec4 causticNoise = texture(causticNoiseTexture, time * causticSpeed + pixelizedCoordinates); + vec2 noiseCoordinates = blendSubtract_vec2(pixelizedCoordinates * causticScale, causticNoise.rg, causticMovementAmount); + vec4 causticHighlight = texture(causticHighlightTexture, noiseCoordinates) * causticHighlightColor; + vec4 caustic = texture(causticTexture, noiseCoordinates) * causticColor; + vec4 interpolatedCaustics = mix(caustic, causticHighlight, causticHighlight.a); + float fadeNoise = texture(causticFadeNoiseTexture, noiseCoordinates).r * causticFaderMultiplier; + return vec4(interpolatedCaustics.rgb, clamp(interpolatedCaustics.a - fadeNoise, 0.0, 1.0)); +} + +vec4 specular(vec2 pixelizedCoordinates) +{ + vec2 scaledCoordinates = pixelizedCoordinates * specularScale; + float specularNoise = texture(specularNoiseTexture, scaledCoordinates).r; + float leftScrollingNoise = texture(specularMovementLeftNoiseTexture, scaledCoordinates + vec2(time * specularSpeed, 0.0)).r; + float rightScrollingNoise = texture(specularMovementRightNoiseTexture, scaledCoordinates + vec2(time * specularSpeed * -1.0, 0.0)).r; + float combined = blendSubtract_float(blendOverlay_float(leftScrollingNoise, rightScrollingNoise, 1.0), specularNoise, 1.0); + return step(specularThreshold, combined) * specularColor; +} + +vec4 foam(vec2 pixelizedCoordinates, vec4 mainTexColor) +{ + vec4 colorizedFoam = texture(foamTexture, pixelizedCoordinates * foamScale) * foamColor; + float intensity = clamp(mainTexColor.g * mainTexColor.a - foamIntensity, 0.0, 1.0); + return vec4(colorizedFoam.rgb, colorizedFoam.a * intensity); +} + +// ----------------------------- +// Main fragment + +void main() +{ + vec2 pixelizedCoordinates = pixelizeCoordinates(applyAspectRatio(fragTexCoord)); + vec4 mainTex = texture(texture0, fragTexCoord); + vec4 depthBasedWaterColor = texture(waterDepthGradient, vec2(1.0 - mainTex.b, 1.0)); + + vec4 finalCaustics = caustics(pixelizedCoordinates); + vec4 finalSpecular = specular(pixelizedCoordinates); + vec4 finalFoam = foam(pixelizedCoordinates, mainTex); + + vec4 waterWithCausticLayer = mix(depthBasedWaterColor, finalCaustics, finalCaustics.a); + vec4 waterWithCausticAndSpecularLayer = mix(waterWithCausticLayer, finalSpecular, ceil(finalCaustics.a) * finalSpecular.a); + vec4 waterWithCausticAndSpecularAndFoamLayer = mix(waterWithCausticAndSpecularLayer, finalFoam, finalFoam.a); + + float outline = mainTex.a * mainTex.r; + vec4 finalOutlineColor = outline * outlineColor; + + vec4 finalRGBColor = mix(waterWithCausticAndSpecularAndFoamLayer, finalOutlineColor, outline); + finalColor = vec4(finalRGBColor.rgb, mainTex.b * generalTransparency); +} + diff --git a/shaders/water.vs b/shaders/water.vs new file mode 100644 index 0000000..a8ba9ce --- /dev/null +++ b/shaders/water.vs @@ -0,0 +1,17 @@ +#version 330 +in vec3 vertexPosition; +in vec2 vertexTexCoord; +in vec4 vertexColor; + +out vec2 fragTexCoord; +out vec4 fragColor; + +uniform mat4 mvp; + +void main() +{ + fragTexCoord = vertexTexCoord; + fragColor = vertexColor; + gl_Position = mvp * vec4(vertexPosition, 1.0); +} +