Initial Commit
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Toon Shading.
|
||||
*
|
||||
* Example showing the use of a custom lighting shader in order
|
||||
* to apply a "toon" effect on the scene. Based on the glsl tutorial
|
||||
* from lighthouse 3D:
|
||||
* http://www.lighthouse3d.com/tutorials/glsl-tutorial/toon-shader-version-ii/
|
||||
*/
|
||||
|
||||
PShader toon;
|
||||
boolean shaderEnabled = true;
|
||||
|
||||
void setup() {
|
||||
size(640, 360, P3D);
|
||||
noStroke();
|
||||
fill(204);
|
||||
toon = loadShader("ToonFrag.glsl", "ToonVert.glsl");
|
||||
}
|
||||
|
||||
void draw() {
|
||||
if (shaderEnabled == true) {
|
||||
shader(toon);
|
||||
}
|
||||
|
||||
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(120);
|
||||
}
|
||||
|
||||
void mousePressed() {
|
||||
if (shaderEnabled) {
|
||||
shaderEnabled = false;
|
||||
resetShader();
|
||||
}
|
||||
else {
|
||||
shaderEnabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifdef GL_ES
|
||||
precision mediump float;
|
||||
precision mediump int;
|
||||
#endif
|
||||
|
||||
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,31 @@
|
||||
// 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/
|
||||
|
||||
#define PROCESSING_LIGHT_SHADER
|
||||
|
||||
uniform mat4 modelview;
|
||||
uniform mat4 transform;
|
||||
uniform mat3 normalMatrix;
|
||||
|
||||
uniform vec3 lightNormal[8];
|
||||
|
||||
attribute vec4 vertex;
|
||||
attribute vec3 normal;
|
||||
|
||||
varying vec3 vertNormal;
|
||||
varying vec3 vertLightDir;
|
||||
|
||||
void main() {
|
||||
// Vertex in clip coordinates
|
||||
gl_Position = transform * vertex;
|
||||
|
||||
// Normal vector in eye coordinates is passed
|
||||
// to the fragment shader
|
||||
vertNormal = normalize(normalMatrix * normal);
|
||||
|
||||
// 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];
|
||||
}
|
||||
Reference in New Issue
Block a user