diff --git a/assets/shaders/pixel.fs b/assets/shaders/pixel.fs new file mode 100644 index 0000000..2148371 --- /dev/null +++ b/assets/shaders/pixel.fs @@ -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; + +// Viewport dimensions +const float renderWidth = 1080; +const float renderHeight = 720; + +// Pixel scaling +uniform float pixelWidth = 2.0; +uniform float pixelHeight = 2.0; + +void main() +{ + float dx = pixelWidth * (1.0 / renderWidth); + float dy = pixelHeight * (1.0 / renderHeight); + + vec2 coord = vec2(dx * floor(fragTexCoord.x / dx), dy * floor(fragTexCoord.y / dy)); + + vec3 tc = texture(texture0, coord).rgb; + + finalColor = vec4(tc, 1.0); +} diff --git a/assets/shaders/ripple.fs b/assets/shaders/ripple.fs deleted file mode 100644 index 7d0d268..0000000 --- a/assets/shaders/ripple.fs +++ /dev/null @@ -1,98 +0,0 @@ -#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; - -// NOTE: Add here your custom variables - -const vec2 size = vec2(1080, 720); // render size -const float samples = 5.0; // pixels per axis; higher = bigger glow, worse performance -const float quality = 1.5; // lower = smaller glow, better quality - -void main() -{ - vec4 sum = vec4(0); - vec2 sizeFactor = vec2(1)/size*quality; - - // Texel color fetching from texture sampler - vec4 source = texture(texture0, fragTexCoord); - - const int range = 2; // should be = (samples - 1)/2; - - for (int x = -range; x <= range; x++) - { - for (int y = -range; y <= range; y++) - { - sum += texture(texture0, fragTexCoord + vec2(x, y)*sizeFactor); - } - } - - // Calculate final fragment color - finalColor = ((sum/(samples*samples)) + source)*colDiffuse; -} - -// #version 330 - -// // Input vertex attributes (from vertex shader) -// in vec2 fragCoord; -// out vec4 fragColor; - -// vec2 resolution = vec2(1080.0, 720.0); // viewport resolution (in pixels) -// uniform float time = 0.0; // shader playback time (in seconds) -// // uniform float iTimeDelta; // render time (in seconds) -// // uniform int iFrame; // shader playback frame -// // uniform float iChannelTime[4]; // channel playback time (in seconds) -// // uniform vec3 iChannelResolution[4]; // channel resolution (in pixels) -// // uniform vec4 iMouse; // mouse pixel coords. xy: current (if MLB down), zw: click -// uniform sampler2D texture0; // input channel. XX = 2D/Cube -// // uniform vec4 iDate; // (year, month, day, time in seconds) -// // uniform float iSampleRate; // sound sample rate (i.e., 44100) - - - -// void main() -// { - -// vec2 uv = fragCoord.xy / vec2(1080.0, 720.0); - -// float X = uv.x*25.+time; -// float Y = uv.y*25.+time; -// uv.y += cos(X+Y)*0.01*cos(Y); -// uv.x += sin(X-Y)*0.01*sin(Y); - -// fragColor = texture(texture0,uv); -// } -// // #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; - -// // // NOTE: Add here your custom variables - -// // void main() -// // { -// // // Texel color fetching from texture sampler -// // vec4 texelColor = texture(texture0, fragTexCoord)*colDiffuse*fragColor; - -// // // Convert texel color to grayscale using NTSC conversion weights -// // float gray = dot(texelColor.rgb, vec3(0.299, 0.587, 0.114)); - -// // // Calculate final fragment color -// // finalColor = vec4(gray, gray, gray, texelColor.a); -// // } \ No newline at end of file diff --git a/src/logic/ingame/mod.rs b/src/logic/ingame/mod.rs index 74a01d0..e45a6e6 100644 --- a/src/logic/ingame/mod.rs +++ b/src/logic/ingame/mod.rs @@ -174,7 +174,7 @@ impl Screen for InGameScreen { // Open a 2D context { unsafe { - raylib::ffi::BeginTextureMode(*game_core.resources.ripple_texture); + raylib::ffi::BeginTextureMode(*game_core.resources.shader_texture); } { let mut context_2d = draw_handle.begin_mode2D(game_core.master_camera); @@ -221,16 +221,16 @@ impl Screen for InGameScreen { // Render the 2D context via the ripple shader { let mut shader_context = - draw_handle.begin_shader_mode(&game_core.resources.ripple_shader); + draw_handle.begin_shader_mode(&game_core.resources.pixel_shader); // Blit the texture shader_context.draw_texture_rec( - &game_core.resources.ripple_texture, + &game_core.resources.shader_texture, Rectangle { x: 0.0, y: 0.0, - width: game_core.resources.ripple_texture.width() as f32, - height: (game_core.resources.ripple_texture.height() as f32) * -1.0, + width: game_core.resources.shader_texture.width() as f32, + height: (game_core.resources.shader_texture.height() as f32) * -1.0, }, Vector2::zero(), Color::WHITE, diff --git a/src/resources.rs b/src/resources.rs index 55e0a3a..b814c9d 100644 --- a/src/resources.rs +++ b/src/resources.rs @@ -20,8 +20,8 @@ pub struct GlobalResources { // Cave pub cave_mid_layer: Texture2D, - pub ripple_shader: Shader, - pub ripple_texture: RenderTexture2D, + pub pixel_shader: Shader, + pub shader_texture: RenderTexture2D, // Enemies pub jellyfish_animation_regular: FrameAnimationWrapper, @@ -104,8 +104,8 @@ impl GlobalResources { &thread, &Image::load_image("./assets/img/map/cave.png")?, )?, - ripple_shader: raylib.load_shader(&thread, None, Some("./assets/shaders/ripple.fs"))?, - ripple_texture: raylib.load_render_texture(&thread, raylib.get_screen_width() as u32, raylib.get_screen_height() as u32)?, + pixel_shader: raylib.load_shader(&thread, None, Some("./assets/shaders/pixel.fs"))?, + shader_texture: raylib.load_render_texture(&thread, raylib.get_screen_width() as u32, raylib.get_screen_height() as u32)?, jellyfish_animation_regular: FrameAnimationWrapper::new( raylib.load_texture_from_image( &thread,