steverichey / HaxeFlixelGlitchRender

A simple example of JPEG glitching a HaxeFlixel game.
MIT License
29 stars 2 forks source link

Separate glitch effect and logic #1

Closed anissen closed 3 years ago

anissen commented 10 years ago

Right now, the glitch effect is tied into some logic that handles the chance of glitches. In my use case, I need to be able to trigger the glitches myself. So I propose to separate the glitch effect and the "glitch chance"-logic into separate classes.

steverichey commented 10 years ago

So you'd like to be able to trigger the glitch by, for example, calling RenderLayer.triggerGlitch()?

anissen commented 10 years ago

Yeah, that was the idea. E.g. having a RenderLayer.randomGlitch() and RenderLayer.glitch(x, y, strength, duration) sort of like FlxG.camera.shake(). You could then have a new GlitchLayer(glitchChance, ...) that uses RenderLayer.

For my purposes, I went the shader way (possible with the new postprocessing support in HaxeFlixel). The following fragment shader make a nice glitchy effect:

float rand(vec2 co) {
    return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
}

void main() {
    // (...)
    if (rand(vec2(1.0 - uTime, sin(uTime))) > 0.98) { 
       shift = 0.1 * rand(vec2(uTime, uTime)); 
    }
    col.r = texture2D(uImage0, vec2(uv.x + shift, uv.y)).x;
    col.g = texture2D(uImage0, vec2(uv.x, uv.y)).y;
    col.b = texture2D(uImage0, vec2(uv.x - shift, uv.y)).z;
    // (...)
}