rewritten watershader from barotrauma

This commit is contained in:
Kaehvaman 2024-12-27 10:57:48 +04:00
parent 39f6760d05
commit 7ca87e52a9
11 changed files with 327 additions and 5284 deletions

File diff suppressed because it is too large Load Diff

View File

@ -140,6 +140,7 @@
<ClInclude Include="..\include\raylib.h" />
<ClInclude Include="..\include\raymath.h" />
<ClInclude Include="..\include\resource_dir.h" />
<ClInclude Include="..\include\rlgl.h" />
<ClInclude Include="..\include\tinyfiledialogs.h" />
<ClInclude Include="..\include\windows_functions.h" />
<ClInclude Include="resource.h" />

View File

@ -42,6 +42,9 @@
<ClInclude Include="..\include\tinyfiledialogs.h">
<Filter>Файлы заголовков</Filter>
</ClInclude>
<ClInclude Include="..\include\rlgl.h">
<Filter>Файлы заголовков</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\src\main.c">

View File

@ -14,7 +14,7 @@ out vec4 finalColor;
// NOTE: Add here your custom variables
// NOTE: values must be passed from code
uniform float renderWidth;
uniform float renderWidth = 750;
uniform float renderHeight;
uniform float seconds;
@ -28,17 +28,17 @@ void main()
for (int i = 1; i < 3; i++)
{
texelColor += texture(texture0, fragTexCoord + vec2(offset[i], offset[i]) / renderWidth).rgba * weight[i] / 2;
texelColor += texture(texture0, fragTexCoord - vec2(offset[i], offset[i]) / renderWidth).rgba * weight[i] / 2;
texelColor += texture(texture0, fragTexCoord + vec2(-offset[i], offset[i]) / renderWidth).rgba * weight[i] / 2;
texelColor += texture(texture0, fragTexCoord - vec2(-offset[i], offset[i]) / renderWidth).rgba * weight[i] / 2;
texelColor += texture(texture0, fragTexCoord + vec2(offset[i], 0.0) / renderWidth).rgba * weight[i] / 2;
texelColor += texture(texture0, fragTexCoord + vec2(-offset[i], 0.0) / renderWidth).rgba * weight[i] / 2;
texelColor += texture(texture0, fragTexCoord + vec2(0.0, offset[i]) / renderWidth).rgba * weight[i] / 2;
texelColor += texture(texture0, fragTexCoord + vec2(0.0, -offset[i]) / renderWidth).rgba * weight[i] / 2;
}
vec4 red = vec4(1.0, 0.5, 0.5, 1.0);
vec4 blue = vec4(0.0, 0.5, 0.5, 1.0);
finalColor = mix(red, blue, (sin(fragTexCoord.x * 24 + seconds * 4.0) + 1.0) / 2.0);
//finalColor = mix(red, blue, (sin(fragTexCoord.x * 24 + seconds * 4.0) + 1.0) / 2.0);
//finalColor = texelColor;
finalColor = texelColor;
//finalColor = vec4((sin(fragTexCoord.x * 24 + seconds * 4.0) + 1.0) / 2.0, 0.5, 0.5, 1.0);
}

View File

@ -0,0 +1,27 @@
#version 330
// Input vertex attributes (from vertex shader)
in vec3 vertexPos;
in vec2 fragTexCoord;
in vec4 fragColor;
// Input uniform values
uniform sampler2D texture0;
uniform sampler2D texture1;
uniform vec4 colDiffuse;
uniform float divider = 0.5;
out vec4 finalColor;
void main()
{
// Texel color fetching from texture sampler
vec4 texelColor0 = texture(texture0, fragTexCoord);
vec4 texelColor1 = texture(texture1, fragTexCoord);
float x = fract(fragTexCoord.s);
float final = smoothstep(divider - 0.1, divider + 0.1, x);
finalColor = mix(texelColor0, texelColor1, final);
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 394 KiB

View File

@ -0,0 +1,32 @@
#version 330
// Input vertex attributes (from vertex shader)
in vec2 fragTexCoord;
in vec4 fragColor;
// Input uniform values
uniform sampler2D texture0;
uniform vec4 colDiffuse;
// Output fragment color
out vec4 finalColor;
// Custom variables
uniform float renderWidth;
uniform float renderHeight;
uniform float seconds;
uniform float intensity;
uniform float waves;
uniform float speedV;
uniform float speedH;
void main()
{
vec2 texelCoord = fragTexCoord;
vec2 pixelSize = vec2(1.0 / renderWidth, 1.0 / renderHeight);
texelCoord.x += sin(fragTexCoord.y * waves + seconds * speedV * 2.0 * 3.14) * pixelSize.x * intensity;
texelCoord.y += sin(fragTexCoord.x * waves + seconds * -speedH * 2.0 * 3.14) * pixelSize.y * intensity;
finalColor = texture(texture0, texelCoord).rgba*colDiffuse*fragColor;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 442 KiB

View File

@ -0,0 +1,48 @@
#version 330
in vec2 fragTexCoord; // Fragment input attribute: texture coordinate
in vec4 fragColor; // Fragment input attribute: color
out vec4 finalColor; // Fragment output: color
uniform sampler2D texture0; // Fragment input texture (always required, could be a white pixel)
uniform vec4 colDiffuse; // Fragment input color diffuse (multiplied by texture color)
// 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 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);
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;
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));
result = result * 0.25;
//vec4 result = texture(texture0, samplePos);
result.a = fragColor.a;
result = mix(result, result * waterColor, fragColor.b);
finalColor = result;
}

View File

@ -0,0 +1,143 @@
float xBlurDistance;
Texture2D xWaterBumpMap;
sampler WaterBumpSampler =
sampler_state
{
Texture = <xWaterBumpMap>;
MagFilter = LINEAR;
MinFilter = LINEAR;
MipFilter = LINEAR;
AddressU = WRAP;
AddressV = WRAP;
};
Texture2D xTexture;
sampler TextureSampler = sampler_state { Texture = <xTexture>; };
float4x4 xTransform;
float4x4 xUvTransform;
struct VertexShaderInput
{
float4 Position : POSITION0;
float4 Color : COLOR0;
float2 TexCoords: TEXCOORD0; // added
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float4 Color : COLOR0;
float2 TexCoords: TEXCOORD0; // added
};
float xWaveWidth;
float xWaveHeight;
float2 xBumpPos;
float2 xBumpScale;
float2 xUvOffset;
float4 waterColor;
VertexShaderOutput mainVS(in VertexShaderInput input)
{
VertexShaderOutput output = (VertexShaderOutput)0;
output.Position = mul(input.Position, xTransform);
output.Color = input.Color;
output.TexCoords = mul(input.Position, xUvTransform).xy;
return output;
}
float4 mainPS(VertexShaderOutput input) : COLOR
{
float4 bumpColor = xWaterBumpMap.Sample(WaterBumpSampler, xUvOffset + (input.TexCoords+xBumpPos) * xBumpScale);
bumpColor = (bumpColor + xWaterBumpMap.Sample(WaterBumpSampler, xUvOffset + (input.TexCoords-xBumpPos*2) * xBumpScale)) * 0.5f;
float2 samplePos = input.TexCoords;
samplePos.x += (bumpColor.r - 0.5f) * xWaveWidth * input.Color.r;
samplePos.y += (bumpColor.g - 0.5f) * xWaveHeight * input.Color.g;
float4 sample = xTexture.Sample(TextureSampler, samplePos);
sample.a = input.Color.a;
sample = lerp(sample, sample * waterColor, input.Color.b);
return sample;
}
float4 mainPSBlurred(VertexShaderOutput input) : COLOR
{
float4 bumpColor = xWaterBumpMap.Sample(WaterBumpSampler, xUvOffset + (input.TexCoords + xBumpPos) * xBumpScale);
bumpColor = (bumpColor + xWaterBumpMap.Sample(WaterBumpSampler, xUvOffset + (input.TexCoords - xBumpPos * 2) * xBumpScale)) * 0.5f;
float2 samplePos = input.TexCoords;
samplePos.x += (bumpColor.r - 0.5f) * xWaveWidth * input.Color.r;
samplePos.y += (bumpColor.g - 0.5f) * xWaveHeight * input.Color.g;
float4 sample;
sample = xTexture.Sample(TextureSampler, float2(samplePos.x + xBlurDistance, samplePos.y + xBlurDistance));
sample += xTexture.Sample(TextureSampler, float2(samplePos.x - xBlurDistance, samplePos.y - xBlurDistance));
sample += xTexture.Sample(TextureSampler, float2(samplePos.x + xBlurDistance, samplePos.y - xBlurDistance));
sample += xTexture.Sample(TextureSampler, float2(samplePos.x - xBlurDistance, samplePos.y + xBlurDistance));
sample = sample * 0.25;
sample.a = input.Color.a;
sample = lerp(sample, sample * waterColor, input.Color.b);
return sample;
}
float4 mainPostProcess(float4 position : POSITION0, float4 color : COLOR0, float2 texCoord : TEXCOORD0) : COLOR0
{
float4 bumpColor = tex2D(WaterBumpSampler, texCoord + xUvOffset + xBumpPos);
bumpColor = (bumpColor + tex2D(WaterBumpSampler, texCoord - xUvOffset * 2.0f + xBumpPos)) * 0.5f;
float2 samplePos = texCoord;
samplePos.x += (bumpColor.r - 0.5f) * xWaveWidth;
samplePos.y += (bumpColor.g - 0.5f) * xWaveHeight;
float4 sample;
sample = tex2D(TextureSampler, float2(samplePos.x + xBlurDistance, samplePos.y + xBlurDistance));
sample += tex2D(TextureSampler, float2(samplePos.x - xBlurDistance, samplePos.y - xBlurDistance));
sample += tex2D(TextureSampler, float2(samplePos.x + xBlurDistance, samplePos.y - xBlurDistance));
sample += tex2D(TextureSampler, float2(samplePos.x - xBlurDistance, samplePos.y + xBlurDistance));
sample = sample * 0.25;
return sample;
}
technique WaterShader
{
pass Pass1
{
VertexShader = compile vs_4_0_level_9_1 mainVS();
PixelShader = compile ps_4_0_level_9_1 mainPS();
}
}
technique WaterShaderBlurred
{
pass Pass1
{
VertexShader = compile vs_4_0_level_9_1 mainVS();
PixelShader = compile ps_4_0_level_9_1 mainPSBlurred();
}
}
technique WaterShaderPostProcess
{
pass Pass1
{
PixelShader = compile ps_4_0_level_9_1 mainPostProcess();
}
}

View File

@ -24,7 +24,7 @@
#define N 15
#define HEIGHT 50
#define WIDTH 50
#define VOFFSET 50
#define VOFFSET 52
#define FWIDTH (float)WIDTH
#define FHEIGHT (float)HEIGHT
@ -528,13 +528,39 @@ int main()
//SetTextureFilter(Arial.texture, TEXTURE_FILTER_BILINEAR);
RenderTexture2D canvas = LoadRenderTexture(screenWidth, screenHeight);
SetTextureFilter(canvas.texture, TEXTURE_FILTER_BILINEAR);
SetTextureWrap(canvas.texture, TEXTURE_WRAP_CLAMP);
Shader blur = LoadShader(0, "blur.frag");
int renderWidthLoc = GetShaderLocation(blur, "renderWidth");
int renderHeightLoc = GetShaderLocation(blur, "renderHeight");
int secondsLoc = GetShaderLocation(blur, "seconds");
SetShaderValue(blur, renderWidthLoc, &screenWidthF, SHADER_UNIFORM_FLOAT);
SetShaderValue(blur, renderHeightLoc, &screenHeightF, SHADER_UNIFORM_FLOAT);
int blurRenderWidthLoc = GetShaderLocation(blur, "renderWidth");
int blurRenderHeightLoc = GetShaderLocation(blur, "renderHeight");
int blurSecondsLoc = GetShaderLocation(blur, "seconds");
SetShaderValue(blur, blurRenderWidthLoc, &screenWidthF, SHADER_UNIFORM_FLOAT);
SetShaderValue(blur, blurRenderHeightLoc, &screenHeightF, SHADER_UNIFORM_FLOAT);
Shader water = LoadShader(0, "water.frag");
int waterRenderWidthLoc = GetShaderLocation(water, "renderWidth");
int waterRenderHeightLoc = GetShaderLocation(water, "renderHeight");
int intensityLoc = GetShaderLocation(water, "intensity");
int wavesLoc = GetShaderLocation(water, "waves");
int speedVLoc = GetShaderLocation(water, "speedV");
int speedHLoc = GetShaderLocation(water, "speedH");
int waterSecondsLoc = GetShaderLocation(water, "seconds");
float waves = 60.0f;
float intensity = 1.0f;
float speedV = 2.0f;
float speedH = 2.0f;
SetShaderValue(water, wavesLoc, &waves, SHADER_UNIFORM_FLOAT);
SetShaderValue(water, intensityLoc, &intensity, SHADER_UNIFORM_FLOAT);
SetShaderValue(water, speedVLoc, &speedV, SHADER_UNIFORM_FLOAT);
SetShaderValue(water, speedHLoc, &speedH, SHADER_UNIFORM_FLOAT);
SetShaderValue(water, waterRenderWidthLoc, &screenWidthF, SHADER_UNIFORM_FLOAT);
SetShaderValue(water, waterRenderHeightLoc, &screenHeightF, SHADER_UNIFORM_FLOAT);
Texture waterbump = LoadTexture("waterbump.png");
Shader watershader = LoadShader(0, "watershader.frag");
int xWaterBumpMapLoc = GetShaderLocation(watershader, "texture1");
int watershaderSecondsLoc = GetShaderLocation(watershader, "seconds");
GuiSetFont(InconsolataBold);
GuiSetStyle(DEFAULT, TEXT_SIZE, 24);
@ -556,6 +582,24 @@ int main()
// Update game logic
//------------------------------------------------------------------
float frametime = GetFrameTime();
if (IsKeyPressed(KEY_I)) {
speedV -= 1.0f;
}
if (IsKeyPressed(KEY_K)) {
speedV += 1.0f;
}
if (IsKeyPressed(KEY_J)) {
speedH -= 1.0f;
}
if (IsKeyPressed(KEY_L)) {
speedH += 1.0f;
}
SetShaderValue(water, speedVLoc, &speedV, SHADER_UNIFORM_FLOAT);
SetShaderValue(water, speedHLoc, &speedH, SHADER_UNIFORM_FLOAT);
if (errorCode == OK)
{
handleKeys();
@ -590,9 +634,9 @@ int main()
//------------------------------------------------------------------
BeginDrawing();
// Setup the back buffer for drawing (clear color and depth buffers)
//ClearBackground(WHITE);
//ClearBackground(BLACK);
//BeginTextureMode(canvas);
BeginTextureMode(canvas);
drawMap();
drawPlayer();
@ -627,9 +671,9 @@ int main()
//drawRayguiErrorBoxes();
}
BeginTextureMode(canvas);
//BeginTextureMode(canvas);
{
ClearBackground((Color) { 255, 255, 255, 0 });
//ClearBackground((Color) { 255, 255, 255, 0 });
//ClearBackground(BLANK);
Rectangle roundRect = { 100, 260, 185, 36 };
DrawRectangleRounded(roundRect, 0.5f, 6, BLACK);
@ -638,18 +682,21 @@ int main()
EndTextureMode();
float timeF = (float)GetTime();
SetShaderValue(blur, secondsLoc, &timeF, SHADER_UNIFORM_FLOAT);
SetShaderValue(blur, blurSecondsLoc, &timeF, SHADER_UNIFORM_FLOAT);
SetShaderValue(water, waterSecondsLoc, &timeF, SHADER_UNIFORM_FLOAT);
SetShaderValue(watershader, watershaderSecondsLoc, &timeF, SHADER_UNIFORM_FLOAT);
BeginShaderMode(blur);
Rectangle rec = { 0, 0, (float)canvas.texture.width, (float)(-canvas.texture.height) };
BeginShaderMode(watershader);
{
Rectangle rec = { 0, 0, canvas.texture.width, -canvas.texture.height };
SetShaderValueTexture(watershader, xWaterBumpMapLoc, waterbump);
DrawTextureRec(canvas.texture, rec, (Vector2) { 0.0f, 0.0f }, WHITE);
}
EndShaderMode();
//drawBottomBar(InconsolataBold, 24);
//DrawTextureEx(canvas.texture, (Vector2) { 0.0f, 0.0f }, 0.0f, 1.0f, WHITE);
//DrawTextureRec(canvas.texture, rec, (Vector2) { 0.0f, 0.0f }, SKYBLUE);
//DrawTextEx(Consolas, u8"Файл не найден\nПопробуйте сначала сохранить игру", (Vector2) { 100, 100 }, 24, 0, BLACK);
@ -670,9 +717,13 @@ int main()
//UnloadFont(Arial);
//UnloadFont(InconsolataBold);
UnloadShader(blur);
UnloadRenderTexture(canvas);
UnloadShader(blur);
UnloadShader(water);
UnloadShader(watershader);
UnloadTexture(waterbump);
// De-initialize the Nuklear GUI
UnloadNuklear(ctx);