Added shader examples

This commit is contained in:
codeanticode
2012-02-13 06:09:00 +00:00
parent 82f53e87c2
commit 2307724a5e
6 changed files with 161 additions and 1 deletions
@@ -0,0 +1,34 @@
// This example shows how to change the default fragment shader used
// in P3D to render textures, by a custom one that applies a simple
// edge detection filter.
//
// Press any key to switch between the custom and the default shader.
PImage img;
PShader shader;
PGraphicsOpenGL pg;
boolean usingShader;
void setup() {
size(400, 400, P3D);
img = loadImage("berlin-1.jpg");
pg = (PGraphicsOpenGL)g;
shader = pg.loadShader("edges.glsl", FILL_SHADER_TEX);
pg.setShader(shader, FILL_SHADER_TEX);
usingShader = true;
}
public void draw() {
image(img, 0, 0, width, height);
}
public void keyPressed() {
if (usingShader) {
pg.resetShader(FILL_SHADER_TEX);
usingShader = false;
} else {
pg.setShader(shader, FILL_SHADER_TEX);
usingShader = true;
}
}
@@ -0,0 +1,38 @@
// Edge detection shader
uniform sampler2D textureSampler;
// The inverse of the texture dimensions along X and Y
uniform vec2 texcoordOffset;
varying vec4 vertColor;
varying vec4 vertTexcoord;
void main() {
vec4 sum = vec4(0);
float kernel[9];
kernel[0] = -1.0; kernel[1] = -1.0; kernel[2] = -1.0;
kernel[3] = -1.0; kernel[4] = +8.0; kernel[5] = -1.0;
kernel[6] = -1.0; kernel[7] = -1.0; kernel[8] = -1.0;
vec2 offset[9];
offset[0] = vec2(-texcoordOffset.s, -texcoordOffset.t);
offset[1] = vec2( 0.0, -texcoordOffset.t);
offset[2] = vec2(+texcoordOffset.s, -texcoordOffset.t);
offset[3] = vec2(-texcoordOffset.s, 0.0);
offset[4] = vec2( 0.0, 0.0);
offset[5] = vec2(+texcoordOffset.s, 0.0);
offset[6] = vec2(-texcoordOffset.s, +texcoordOffset.t);
offset[7] = vec2( 0.0, +texcoordOffset.t);
offset[8] = vec2(+texcoordOffset.s, +texcoordOffset.t);
for (int i = 0; i < 9; i++) {
vec4 tmp = texture2D(textureSampler, vertTexcoord.st + offset[i]);
sum += tmp * kernel[i];
}
gl_FragColor = vec4(sum.rgb, 1.0);
}
@@ -0,0 +1,39 @@
// Example showing the use of a custom lighting shader in order
// to apply a toon effect on the scene.
PShader shader;
PGraphicsOpenGL pg;
boolean usingShader;
public void setup() {
size(400, 400, P3D);
noStroke();
fill(204);
pg = (PGraphicsOpenGL)g;
shader = pg.loadShader("ToonVert.glsl", "ToonFrag.glsl", FILL_SHADER_LIT);
pg.setShader(shader, FILL_SHADER_LIT);
usingShader = true;
}
public void draw() {
noStroke();
background(0);
float dirY = (mouseY / float(height) - 0.5) * 2;
float dirX = (mouseX / float(width) - 0.5) * 2;
directionalLight(204, 204, 204, -dirX, -dirY, -1);
translate(width/2, height/2);
sphere(80);
}
public void keyPressed() {
if (usingShader) {
pg.resetShader(FILL_SHADER_LIT);
usingShader = false;
}
else {
pg.setShader(shader, FILL_SHADER_LIT);
usingShader = true;
}
}
@@ -0,0 +1,20 @@
varying vec3 vertNormal;
varying vec3 vertLightDir;
void main() {
float intensity;
vec4 color;
intensity = max(0.0, dot(vertLightDir, vertNormal));
if (intensity > 0.95) {
color = vec4(1.0, 0.5, 0.5, 1.0);
} else if (intensity > 0.5) {
color = vec4(0.6, 0.3, 0.3, 1.0);
} else if (intensity > 0.25) {
color = vec4(0.4, 0.2, 0.2, 1.0);
} else {
color = vec4(0.2, 0.1, 0.1, 1.0);
}
gl_FragColor = color;
}
@@ -0,0 +1,29 @@
// Toon shader using per-pixel lighting. Based on the glsl
// tutorial from lighthouse 3D:
// http://www.lighthouse3d.com/tutorials/glsl-tutorial/toon-shader-version-ii/
uniform mat4 modelviewMatrix;
uniform mat4 projmodelviewMatrix;
uniform mat3 normalMatrix;
uniform vec3 lightNormal[8];
attribute vec4 inVertex;
attribute vec3 inNormal;
varying vec3 vertNormal;
varying vec3 vertLightDir;
void main() {
// Vertex in clip coordinates
gl_Position = projmodelviewMatrix * inVertex;
// Normal vector in eye coordinates is passed
// to the fragment shader
vertNormal = normalize(normalMatrix * inNormal);
// Assuming that there is only one directional light.
// Its normal vector is passed to the fragment shader
// in order to perform per-pixel lighting calculation.
vertLightDir = -lightNormal[0];
}
@@ -11,7 +11,7 @@ float px = 0, py = 0, pz = 0;
float flapSpeed = 0.2;
void setup(){
size(640, 360, P3D);
size(640, 360, P3D);
noStroke();
}