Skip to content

Instantly share code, notes, and snippets.

@MGDSStudio
Created June 25, 2024 07:27
Show Gist options
  • Save MGDSStudio/fbc0be84a851d4947af1f4db3532623b to your computer and use it in GitHub Desktop.
Save MGDSStudio/fbc0be84a851d4947af1f4db3532623b to your computer and use it in GitHub Desktop.
This shader is enabled when the player in my game is dead
// Copyright © 2024 MGDS Studio. All rights reserved.
/** Used to create a visual effect when the player in my game is dead.
* Values must be passed:
shaderProgram.setUniformf( "time", deltaTime );
shaderProgram.setUniformf( "u_imageSize", new Vector2(textureRegion.getWidth(), textureRegion.getHeight()) );
*/
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif
/* For grayscale */
const float START_RELATIVE_SHIFTING = 1.0;
const float END_RELATIVE_SHIFTING = 0.20;
const float TIME_TO_CHANGE_COLOR = 3.0; //sec
/** For blur */
varying vec4 v_color;
varying vec2 v_texCoords;
uniform sampler2D u_texture;
uniform vec2 u_imageSize;
//uniform int u_blurRadius;
const float TIME_TO_CHANGE_BLUR = 6.0; //sec
const float MIN_BLUR_RADIUS = 1.0;
const float MAX_BLUR_RADIUS = 8.0;
/** For both */
uniform float time;
void applyBlurFilter(){
vec4 color = texture2D(u_texture, v_texCoords);
vec2 pixelToTextureCoords = 1.0 / u_imageSize;
vec4 averageColor = vec4(0.0, 0.0, 0.0, 0.0);
float relativeEllapsedTime = time/TIME_TO_CHANGE_BLUR;
if (relativeEllapsedTime > 1.0) relativeEllapsedTime = 1.0;
int u_blurRadius = int(MIN_BLUR_RADIUS) + int(relativeEllapsedTime*(MAX_BLUR_RADIUS-MIN_BLUR_RADIUS));
for (int dx = -u_blurRadius; dx <= u_blurRadius; dx++)
{
for (int dy = -u_blurRadius; dy <= u_blurRadius; dy++)
{
vec2 point = v_texCoords + vec2(dx,dy) * pixelToTextureCoords;
averageColor += texture2D(u_texture, point);
}
}
averageColor /= pow(2.0 * u_blurRadius + 1.0, 2.0);
gl_FragColor = v_color * averageColor;
}
void applyGrayscaleFilter(){
float relativeColorShifting = END_RELATIVE_SHIFTING+(1.0-time/TIME_TO_CHANGE_COLOR)*(START_RELATIVE_SHIFTING-END_RELATIVE_SHIFTING);
if (relativeColorShifting<END_RELATIVE_SHIFTING) relativeColorShifting = END_RELATIVE_SHIFTING;
float average = (gl_FragColor.r + gl_FragColor.g + gl_FragColor.b) / 3.0;
float red = average + (gl_FragColor.r-average)*relativeColorShifting;
float green = average + (gl_FragColor.g-average)*relativeColorShifting;
float blue = average + (gl_FragColor.b-average)*relativeColorShifting;
gl_FragColor.rgb = vec3(red,green, blue);
}
void main()
{
applyBlurFilter();
applyGrayscaleFilter();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment