Skip to content

Instantly share code, notes, and snippets.

@ishikawash
Created July 14, 2014 15:37
Show Gist options
  • Save ishikawash/4ca582baede2cf76a144 to your computer and use it in GitHub Desktop.
Save ishikawash/4ca582baede2cf76a144 to your computer and use it in GitHub Desktop.
Basic shader demo with Processing
PShader sh;
PShader f;
PShape square;
PGraphics canvas;
void setup() {
size(640, 480, P3D);
canvas = createGraphics(width, height, P3D);
canvas.noStroke();
canvas.noLights();
square = createShape(RECT, 0, 0, width, height);
square.setFill(color(0, 0, 0));
square.setStroke(false);
ortho(0, width, 0, height);
sh = loadShader("scene.fs", "scene.vs");
sh.set("resolution", float(width), float(height));
sh.set("origin", 0.0, 0.0);
f = loadShader("filter.fs");
}
void draw() {
sh.set("radius", map(cos(millis()/1000.0), -1.0, 1.0, 0.001, 0.1));
shader(sh);
// Rendering scene to offscreen buffer
canvas.beginDraw();
canvas.shape(square, 0, 0);
canvas.endDraw();
// Applying post process
image(canvas, 0, 0, width, height);
filter(f);
}
#version 120
uniform sampler2D texture;
uniform vec2 texOffset;
varying vec4 vertColor;
varying vec4 vertTexCoord;
void main(void)
{
vec4 pixel = texture2D(texture, vertTexCoord.xy);
gl_FragColor = vec4(vec3(1.0) - pixel.rgb, 1.0);
}
#version 120
uniform vec2 resolution;
uniform vec2 origin;
uniform float radius;
void main(void)
{
float aspect = resolution.y/resolution.x;
vec2 p = 2.0*gl_FragCoord.xy/resolution - 1.0;
p.y *= aspect;
vec3 c = radius/(length(p - origin))*vec3(1.0);
gl_FragColor = vec4(c, 1.0);
}
#version 120
uniform mat4 transform;
attribute vec4 vertex;
void main(void)
{
gl_Position = transform * vertex;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment