Skip to content

Instantly share code, notes, and snippets.

@Mon-Ouie
Forked from bil-bas/pixelation.rb
Created July 15, 2011 11:37
Show Gist options
  • Save Mon-Ouie/1084533 to your computer and use it in GitHub Desktop.
Save Mon-Ouie/1084533 to your computer and use it in GitHub Desktop.
Pixelation shader in Ray
# Pixelation post-processing effect
# http://www.geeks3d.com/20101029/shader-library-pixelation-post-processing-effect-glsl/
# Shaders -------------------
PIXEL_SHADER =<<END
uniform sampler2D sceneTex; // 0
uniform float vx_offset;
uniform float rt_w; // GeeXLab built-in
uniform float rt_h; // GeeXLab built-in
uniform float pixel_w; // 15.0
uniform float pixel_h; // 10.0
void main()
{
vec2 uv = gl_TexCoord[0].xy;
vec3 tc = vec3(1.0, 0.0, 0.0);
if (uv.x < (vx_offset-0.005))
{
float dx = pixel_w*(1./rt_w);
float dy = pixel_h*(1./rt_h);
vec2 coord = vec2(dx*floor(uv.x/dx),
dy*floor(uv.y/dy));
tc = texture2D(sceneTex, coord).rgb;
}
else if (uv.x>=(vx_offset+0.005))
{
tc = texture2D(sceneTex, uv).rgb;
}
gl_FragColor = vec4(tc, 1.0);
}
END
# Code ----------------------------
$:.unshift File.expand_path(File.dirname(__FILE__) + "/../../lib")
$:.unshift File.expand_path(File.dirname(__FILE__) + "/../../ext")
require 'ray'
def path_of(res)
File.expand_path File.join(File.dirname(__FILE__), '../../test/res', res)
end
Ray.game "Pixelization shader" do
register { add_hook :quit, method(:exit!) }
scene :pixelization do
@sprites = []
@sprites << sprite(path_of("sprite.png"))
@sprites << sprite(path_of("sprite.png"), at: [400, 200])
window.shader.compile(:frag => StringIO.new(PIXEL_SHADER))
window.shader[:sceneTex] = 0
window.shader[:rt_w] = 640
window.shader[:rt_h] = 480
window.shader[:vx_offset] = 0.45 # How much of screen to pixelize (0: none, 0.5: left side, 1: all )
window.shader[:pixel_w] = 1.0 # Width of meta-pixel in pixels.
window.shader[:pixel_h] = 1.0 # Height of meta-pixel in pixels.
# Animate so that processed pixels grow from 1 to 100 normal pixels.
@animation = block_animation :duration => 5, :block => proc { |target, progression|
target[:pixel_w] = progression
target[:pixel_h] = progression
}
@animation.start window.shader
always do
@animation.update
end
render do |win|
@sprites.each {|s| win.draw s }
end
end
scenes << :pixelization
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment