Skip to content

Instantly share code, notes, and snippets.

@nikki93
Created November 6, 2020 03:17
Show Gist options
  • Save nikki93/036823a658d20c7f0b750835cffbc05a to your computer and use it in GitHub Desktop.
Save nikki93/036823a658d20c7f0b750835cffbc05a to your computer and use it in GitHub Desktop.
proc loadEffect*(gfx: var Graphics, path: static string): Effect =
## Load an `Effect` based on the shader source file at the given path.
## The path must be statically known. The file should be present
## alongside the source code files of the application and is read at
## compile time.
# Constant code string with preamble based on platform
proc prepareCode(path: static string): string =
when defined(emscripten):
result.add("""#version 100
precision highp float;
precision mediump int;
#define in varying
#define texture texture2D
#define fragColor gl_FragColor
""")
else:
result.add("""#version 150
out vec4 fragColor;
""")
result.add(staticRead(path))
const code = prepareCode(path)
gfx.loadEffect(path, code) # Call to version without `static` param
in vec4 color;
in vec2 texCoord;
uniform sampler2D tex;
uniform float u_time;
float rand(vec2 co){
return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453) * 2.0 - 1.0;
}
float offset(float blocks, vec2 uv) {
return rand(vec2(u_time, floor(uv.y * blocks)));
}
void main(void)
{
vec2 uv = texCoord;
vec4 glitched = texture(tex, uv);
glitched.r = texture(tex, uv + vec2(offset(16.0, uv) * 0.1, 0.0)).r;
glitched.g = texture(tex, uv + vec2(offset(8.0, uv) * 0.1 * 0.16666666, 0.0)).g;
glitched.b = texture(tex, uv + vec2(offset(8.0, uv) * 0.1, 0.0)).b;
fragColor = glitched * color;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment