improve watershader.frag

This commit is contained in:
Kaehvaman 2024-12-27 14:05:23 +04:00
parent 7ca87e52a9
commit 6fefff9813
3 changed files with 50 additions and 21 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 256 KiB

View File

@ -8,41 +8,64 @@ uniform vec4 colDiffuse; // Fragment input color diffuse (multiplied by textu
// Custom variables
uniform sampler2D texture1; // water bump map
//uniform vec2 xBumpPos;
//uniform vec2 xBumpScale;
//uniform vec2 xUvOffset;
uniform float xBlurDistance = 0.6 / 750;
uniform float xBlurDistance = 0.5 / 750;
uniform float xWaveWidth = 0.1;
uniform float xWaveHeight = 0.1;
uniform float seconds;
uniform vec4 waterColor = vec4(180.0 / 255.0, 230.0 / 255.0, 255.0 / 255.0, 255.0 / 255.0);
uniform vec2 iResolution;
//uniform vec4 waterColor = vec4(0.8, 0.95, 1.0, 1.0);
vec4 getAverageColor(sampler2D iTexture, vec2 uv, float power)
{
vec4 color = vec4(0);
for (int x = -1; x < 2; x++)
{
for (int y = -1; y < 2; y++)
{
vec2 offset = vec2(x,y) / iResolution.xy * power;
color += texture(iTexture, uv + offset);
}
}
color /= 9.0;
return color;
}
void main()
{
//vec4 bumpColor = texture(xWaterBumpMap, xUvOffset + xBumpPos * xBumpScale);
//bumpColor = (bumpColor + texture(xWaterBumpMap, xUvOffset + xBumpPos*2 * xBumpScale)) * 0.5;
vec4 bumpColor = texture(texture1, fragTexCoord + sin(seconds / 2.0) / 20.0);
bumpColor = (bumpColor + texture(texture1, fragTexCoord*2 + cos(seconds / 2.0) / 20.0)) * 0.5;
bumpColor = (bumpColor + texture(texture1, fragTexCoord*1.5 + cos(seconds / 2.0) / 20.0)) * 0.5;
vec2 samplePos = fragTexCoord;
samplePos.x += (bumpColor.r - 0.5) * xWaveWidth * fragColor.r;
samplePos.y += (bumpColor.g - 0.5) * xWaveHeight * fragColor.g;
vec4 result;
result = texture(texture0, vec2(samplePos.x + xBlurDistance, samplePos.y + xBlurDistance));
result += texture(texture0, vec2(samplePos.x - xBlurDistance, samplePos.y - xBlurDistance));
result += texture(texture0, vec2(samplePos.x + xBlurDistance, samplePos.y - xBlurDistance));
result += texture(texture0, vec2(samplePos.x - xBlurDistance, samplePos.y + xBlurDistance));
// vec4 result = texture(texture0, samplePos);
// result += texture(texture0, vec2(samplePos.x + xBlurDistance, samplePos.y));
// result += texture(texture0, vec2(samplePos.x - xBlurDistance, samplePos.y));
// result += texture(texture0, vec2(samplePos.x, samplePos.y - xBlurDistance));
// result += texture(texture0, vec2(samplePos.x, samplePos.y + xBlurDistance));
// result += texture(texture0, vec2(samplePos.x + xBlurDistance, samplePos.y + xBlurDistance));
// result += texture(texture0, vec2(samplePos.x - xBlurDistance, samplePos.y - xBlurDistance));
// result += texture(texture0, vec2(samplePos.x + xBlurDistance, samplePos.y - xBlurDistance));
// result += texture(texture0, vec2(samplePos.x - xBlurDistance, samplePos.y + xBlurDistance));
//
// result = result / 9;
result = result * 0.25;
vec4 result = getAverageColor(texture0, samplePos, 0.7);
//vec4 result = texture(texture0, samplePos);
result.a = fragColor.a;
result = mix(result, result * waterColor, fragColor.b);
//result.a = texture(texture0, samplePos).a;
//result = mix(result, result * waterColor, fragColor.b);
result = result*colDiffuse*fragColor;
finalColor = result;
}

View File

@ -32,6 +32,7 @@
#define PUREBLUE (Color) { 0, 0, 255, 255 }
#define BLACKGRAY (Color) {30, 30, 30, 255}
#define VSGREEN (Color) {78, 201, 176, 255}
#define WATERBLUE CLITERAL(Color){200, 240, 255, 255}
// Коды ячеек:
// 0 - свободна
@ -500,7 +501,7 @@ void callNKErrorBoxes(struct nk_context* ctx) {
#define CPSIZE 213
int main()
{
//SetConfigFlags(FLAG_WINDOW_HIGHDPI);
SetConfigFlags(FLAG_WINDOW_HIGHDPI);
//SetConfigFlags(FLAG_WINDOW_RESIZABLE);
const int screenWidth = N * WIDTH;
@ -557,10 +558,15 @@ int main()
SetShaderValue(water, waterRenderWidthLoc, &screenWidthF, SHADER_UNIFORM_FLOAT);
SetShaderValue(water, waterRenderHeightLoc, &screenHeightF, SHADER_UNIFORM_FLOAT);
Texture waterbump = LoadTexture("waterbump.png");
Texture waterbump = LoadTexture("waterbump_blur.png");
SetTextureFilter(waterbump, TEXTURE_FILTER_BILINEAR);
Shader watershader = LoadShader(0, "watershader.frag");
int xWaterBumpMapLoc = GetShaderLocation(watershader, "texture1");
int watershaderSecondsLoc = GetShaderLocation(watershader, "seconds");
int iResolutionLoc = GetShaderLocation(watershader, "iResolution");
Vector2 iResolution = { screenWidthF, screenHeightF };
SetShaderValue(watershader, iResolutionLoc, &iResolution, SHADER_UNIFORM_VEC2);
SetShaderValueTexture(watershader, xWaterBumpMapLoc, waterbump);
GuiSetFont(InconsolataBold);
GuiSetStyle(DEFAULT, TEXT_SIZE, 24);
@ -634,7 +640,7 @@ int main()
//------------------------------------------------------------------
BeginDrawing();
// Setup the back buffer for drawing (clear color and depth buffers)
//ClearBackground(BLACK);
ClearBackground(WHITE);
BeginTextureMode(canvas);
@ -690,7 +696,7 @@ int main()
BeginShaderMode(watershader);
{
SetShaderValueTexture(watershader, xWaterBumpMapLoc, waterbump);
DrawTextureRec(canvas.texture, rec, (Vector2) { 0.0f, 0.0f }, WHITE);
DrawTextureRec(canvas.texture, rec, (Vector2) { 0.0f, 0.0f }, WATERBLUE);
}
EndShaderMode();