diff --git a/shaders/texture-damage-fragment.glsl b/shaders/texture-damage-fragment.glsl new file mode 100644 index 0000000..9b45785 --- /dev/null +++ b/shaders/texture-damage-fragment.glsl @@ -0,0 +1,44 @@ +precision mediump float; + +uniform sampler2D tex0; +uniform sampler2D tex1; + +varying vec2 v_texture; +varying float v_width; +varying float v_height; + +float get_pixel_damage(vec2 coord) +{ + vec3 diff = texture2D(tex0, coord).rgb - texture2D(tex1, coord).rgb; + + vec3 absdiff = ceil(abs(diff)); + + return clamp(absdiff.r + absdiff.g + absdiff.b, 0.0, 1.0); +} + +float get_damage(vec2 coord) +{ + float x_off = 2.0 / v_width; + float y_off = 2.0 / v_height; + + float ix = v_width * (1.0 + coord.x) * 2.0; + float iy = v_height * (1.0 + coord.y) * 2.0; + + float x_start = mod(ix, 32.0) / v_width / 2.0; + float y_start = mod(iy, 32.0) / v_height / 2.0; + + float color = 0.0; + for (float y = 0.0; y < 32.0; y += 1.0) + for (float x = 0.0; x < 32.0; x += 1.0) { + color += get_pixel_damage(vec2(coord.x - x_start + x * x_off, + coord.y - y_start + y * y_off)); + } + + return clamp(color, 0.0, 1.0); +} + +void main() +{ + float color = get_damage(v_texture); + gl_FragColor = vec4(color, color, color, 1.0); +} diff --git a/shaders/texture-damage-vertex.glsl b/shaders/texture-damage-vertex.glsl new file mode 100644 index 0000000..6f4b2e4 --- /dev/null +++ b/shaders/texture-damage-vertex.glsl @@ -0,0 +1,15 @@ +attribute vec2 pos; +attribute vec2 texture; +attribute float width; +attribute float height; + +varying vec2 v_texture; +varying float v_width; +varying float v_height; + +void main() { + v_texture = texture; + v_width = width; + v_height = height; + gl_Position = vec4(pos, 0, 1); +}