Skip to content

Instantly share code, notes, and snippets.

@josephwilk
Last active August 29, 2015 14:07
Show Gist options
  • Save josephwilk/77c07f45c588e3e0ba68 to your computer and use it in GitHub Desktop.
Save josephwilk/77c07f45c588e3e0ba68 to your computer and use it in GitHub Desktop.
require 'rubygems'
$LOAD_PATH.unshift '/Users/josephwilk/Workspace/ruby/ashton/lib/'
require "ashton"
class TestWindow < Gosu::Window
NOISE_FRAGMENT =<<-END
#version 110
// Use 3D Simplex noise, even though the shader operates on a 2D
// texture, since then we can make the Z-coordinate act as time.
#include <noise3d>
uniform sampler2D in_Texture;
uniform float in_T;
uniform vec4 in_BlobColor;
uniform float in_Pants;
varying vec2 var_TexCoord;
void main()
{
gl_FragColor = texture2D(in_Texture, var_TexCoord);
// First layer. faster, low intensity, small scale blobbing.
// Use [x, y, t] to create a 2D noise that varies over time.
vec3 position1 = vec3(var_TexCoord * 25.0, in_T * 1.6);
// Gives range 0.75..1.25
float brightness1 = snoise(position1) / 4.0 + 1.0;
// Second layer - slow, high intensity, large-scale blobbing
// This decides where the first layer will be "seen"
// Use [x, y, t] to create a 2D noise that varies over time.
vec3 position2 = vec3(var_TexCoord * 3.0, in_T);
// Gives range 0.3..1.3
float brightness2 = snoise(position2) / 1.0 + 0.1;
if(brightness2 > 0.8)
{
gl_FragColor.rgb += in_BlobColor.rgb * in_Pants;
gl_FragColor.rgb *= brightness1 * brightness2;
}
}
END
def initialize
super 800, 600, false
@pants = 0.0
@noise = Ashton::Shader.new fragment: NOISE_FRAGMENT, uniforms: {
blob_color: Gosu::Color.rgb(100, 100, 100),
}
@background = Gosu::Image.new(self, "/Users/josephwilk/Workspace/repl-electric/shadertone/textures/buddha_3.jpg", true)
@start_time = Time.now
update # Ensure the values are initially set.
end
def update
$gosu_blocks.clear if defined? $gosu_blocks # Workaround for Gosu bug (0.7.45)
@noise.t = Time.now - @start_time
@noise.pants = @pants
end
def pants=(v)
@pants = v
end
def draw
post_process @noise do
@background.draw 0, 0, 0, width.fdiv(@background.width), height.fdiv(@background.height)
end
end
def button_down(id)
if id == Gosu::KbEscape
close
end
end
end
#if $window
Thread.new do
$window.close if $window
$window ||= TestWindow.new
$window.show
end
#end
live_loop(:graphics) do
sync :choir
sample :drum_heavy_kick
$window.pants = 2.0
sleep 1
$window.pants = 1.0
sleep 1
$window.pants = 0.0
end
live_loop(:choir) do
sample :ambi_choir, rate: 0.4
sleep 4
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment