Skip to content
This repository was archived by the owner on Jun 24, 2024. It is now read-only.

Commit f9fb263

Browse files
committed
uploaded first version of intro examples
0 parents  commit f9fb263

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+1657
-0
lines changed

README

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Code examples that accompany the PShader tutorials for the Processing website. They might move into the built-in examples later.
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
PImage label;
2+
PShape can;
3+
float angle;
4+
PShader colorShader;
5+
6+
void setup() {
7+
size(640, 360, P3D);
8+
can = createCan(100, 200, 32);
9+
}
10+
11+
void draw() {
12+
background(0);
13+
translate(width/2, height/2);
14+
rotateY(angle);
15+
shape(can);
16+
angle += 0.01;
17+
}
18+
19+
PShape createCan(float r, float h, int detail) {
20+
textureMode(NORMAL);
21+
PShape sh = createShape();
22+
sh.beginShape(QUAD_STRIP);
23+
sh.noStroke();
24+
for (int i = 0; i <= detail; i++) {
25+
float angle = TWO_PI / detail;
26+
float x = sin(i * angle);
27+
float z = cos(i * angle);
28+
float u = float(i) / detail;
29+
sh.normal(x, 0, z);
30+
sh.vertex(x * r, -h/2, z * r, u, 0);
31+
sh.vertex(x * r, +h/2, z * r, u, 1);
32+
}
33+
sh.endShape();
34+
return sh;
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
PShape can;
2+
float angle;
3+
4+
PShader colorShader;
5+
6+
void setup() {
7+
size(640, 360, P3D);
8+
can = createCan(100, 200, 32);
9+
colorShader = loadShader("colorfrag.glsl", "colorvert.glsl");
10+
}
11+
12+
void draw() {
13+
background(0);
14+
15+
shader(colorShader);
16+
17+
translate(width/2, height/2);
18+
rotateY(angle);
19+
shape(can);
20+
angle += 0.01;
21+
}
22+
23+
PShape createCan(float r, float h, int detail) {
24+
textureMode(NORMAL);
25+
PShape sh = createShape();
26+
sh.beginShape(QUAD_STRIP);
27+
sh.noStroke();
28+
for (int i = 0; i <= detail; i++) {
29+
float angle = TWO_PI / detail;
30+
float x = sin(i * angle);
31+
float z = cos(i * angle);
32+
float u = float(i) / detail;
33+
sh.normal(x, 0, z);
34+
sh.vertex(x * r, -h/2, z * r, u, 0);
35+
sh.vertex(x * r, +h/2, z * r, u, 1);
36+
}
37+
sh.endShape();
38+
return sh;
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifdef GL_ES
2+
precision mediump float;
3+
precision mediump int;
4+
#endif
5+
6+
varying vec4 vertColor;
7+
8+
void main() {
9+
gl_FragColor = vertColor;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#define PROCESSING_COLOR_SHADER
2+
3+
uniform mat4 transform;
4+
5+
attribute vec4 vertex;
6+
attribute vec4 color;
7+
8+
varying vec4 vertColor;
9+
10+
void main() {
11+
gl_Position = transform * vertex;
12+
vertColor = color;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Texture from Jason Liebig's FLICKR collection of vintage labels and wrappers:
2+
// http://www.flickr.com/photos/jasonliebigstuff/3739263136/in/photostream/
3+
4+
PImage label;
5+
PShape can;
6+
float angle;
7+
8+
PShader texShader;
9+
10+
void setup() {
11+
size(640, 360, P3D);
12+
label = loadImage("lachoy.jpg");
13+
can = createCan(100, 200, 32, label);
14+
texShader = loadShader("texfrag.glsl", "texvert.glsl");
15+
}
16+
17+
void draw() {
18+
background(0);
19+
20+
shader(texShader);
21+
22+
translate(width/2, height/2);
23+
rotateY(angle);
24+
shape(can);
25+
angle += 0.01;
26+
}
27+
28+
PShape createCan(float r, float h, int detail, PImage tex) {
29+
textureMode(NORMAL);
30+
PShape sh = createShape();
31+
sh.beginShape(QUAD_STRIP);
32+
sh.noStroke();
33+
sh.texture(tex);
34+
for (int i = 0; i <= detail; i++) {
35+
float angle = TWO_PI / detail;
36+
float x = sin(i * angle);
37+
float z = cos(i * angle);
38+
float u = float(i) / detail;
39+
sh.normal(x, 0, z);
40+
sh.vertex(x * r, -h/2, z * r, u, 0);
41+
sh.vertex(x * r, +h/2, z * r, u, 1);
42+
}
43+
sh.endShape();
44+
return sh;
45+
}
193 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifdef GL_ES
2+
precision mediump float;
3+
precision mediump int;
4+
#endif
5+
6+
uniform sampler2D texture;
7+
8+
varying vec4 vertColor;
9+
varying vec4 vertTexCoord;
10+
11+
void main() {
12+
gl_FragColor = texture2D(texture, vertTexCoord.st) * vertColor;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#define PROCESSING_TEXTURE_SHADER
2+
3+
uniform mat4 transform;
4+
uniform mat4 texMatrix;
5+
6+
attribute vec4 vertex;
7+
attribute vec4 color;
8+
attribute vec2 texCoord;
9+
10+
varying vec4 vertColor;
11+
varying vec4 vertTexCoord;
12+
13+
void main() {
14+
gl_Position = transform * vertex;
15+
16+
vertColor = color;
17+
vertTexCoord = texMatrix * vec4(texCoord, 1.0, 1.0);
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
PShape can;
2+
float angle;
3+
4+
PShader lightShader;
5+
6+
void setup() {
7+
size(640, 360, P3D);
8+
can = createCan(100, 200, 32);
9+
lightShader = loadShader("lightfrag.glsl", "lightvert.glsl");
10+
}
11+
12+
void draw() {
13+
background(0);
14+
15+
shader(lightShader);
16+
17+
pointLight(255, 255, 255, width/2, height, 200);
18+
19+
translate(width/2, height/2);
20+
rotateY(angle);
21+
shape(can);
22+
angle += 0.01;
23+
}
24+
25+
PShape createCan(float r, float h, int detail) {
26+
textureMode(NORMAL);
27+
PShape sh = createShape();
28+
sh.beginShape(QUAD_STRIP);
29+
sh.noStroke();
30+
for (int i = 0; i <= detail; i++) {
31+
float angle = TWO_PI / detail;
32+
float x = sin(i * angle);
33+
float z = cos(i * angle);
34+
float u = float(i) / detail;
35+
sh.normal(x, 0, z);
36+
sh.vertex(x * r, -h/2, z * r, u, 0);
37+
sh.vertex(x * r, +h/2, z * r, u, 1);
38+
}
39+
sh.endShape();
40+
return sh;
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifdef GL_ES
2+
precision mediump float;
3+
precision mediump int;
4+
#endif
5+
6+
varying vec4 vertColor;
7+
8+
void main() {
9+
gl_FragColor = vertColor;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#define PROCESSING_LIGHT_SHADER
2+
3+
uniform mat4 modelview;
4+
uniform mat4 transform;
5+
uniform mat3 normalMatrix;
6+
7+
uniform vec4 lightPosition;
8+
9+
attribute vec4 vertex;
10+
attribute vec4 color;
11+
attribute vec3 normal;
12+
13+
varying vec4 vertColor;
14+
15+
void main() {
16+
gl_Position = transform * vertex;
17+
vec3 ecVertex = vec3(modelview * vertex);
18+
vec3 ecNormal = normalize(normalMatrix * normal);
19+
20+
vec3 direction = normalize(lightPosition.xyz - ecVertex);
21+
float intensity = max(0.0, dot(direction, ecNormal));
22+
vertColor = vec4(intensity, intensity, intensity, 1) * color;
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
PShape can;
2+
float angle;
3+
4+
PShader pixlightShader;
5+
6+
void setup() {
7+
size(640, 360, P3D);
8+
can = createCan(100, 200, 32);
9+
pixlightShader = loadShader("pixlightfrag.glsl", "pixlightvert.glsl");
10+
}
11+
12+
void draw() {
13+
background(0);
14+
15+
shader(pixlightShader);
16+
17+
pointLight(255, 255, 255, width/2, height, 200);
18+
19+
translate(width/2, height/2);
20+
rotateY(angle);
21+
shape(can);
22+
angle += 0.01;
23+
}
24+
25+
PShape createCan(float r, float h, int detail) {
26+
textureMode(NORMAL);
27+
PShape sh = createShape();
28+
sh.beginShape(QUAD_STRIP);
29+
sh.noStroke();
30+
for (int i = 0; i <= detail; i++) {
31+
float angle = TWO_PI / detail;
32+
float x = sin(i * angle);
33+
float z = cos(i * angle);
34+
float u = float(i) / detail;
35+
sh.normal(x, 0, z);
36+
sh.vertex(x * r, -h/2, z * r, u, 0);
37+
sh.vertex(x * r, +h/2, z * r, u, 1);
38+
}
39+
sh.endShape();
40+
return sh;
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifdef GL_ES
2+
precision mediump float;
3+
precision mediump int;
4+
#endif
5+
6+
varying vec4 vertColor;
7+
varying vec3 ecNormal;
8+
varying vec3 lightDir;
9+
10+
void main() {
11+
vec3 direction = normalize(lightDir);
12+
vec3 normal = normalize(ecNormal);
13+
float intensity = max(0.0, dot(direction, normal));
14+
gl_FragColor = vec4(intensity, intensity, intensity, 1) * vertColor;
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#define PROCESSING_LIGHT_SHADER
2+
3+
uniform mat4 modelview;
4+
uniform mat4 transform;
5+
uniform mat3 normalMatrix;
6+
7+
uniform vec4 lightPosition;
8+
uniform vec3 lightNormal;
9+
10+
attribute vec4 vertex;
11+
attribute vec4 color;
12+
attribute vec3 normal;
13+
14+
varying vec4 vertColor;
15+
varying vec3 ecNormal;
16+
varying vec3 lightDir;
17+
18+
void main() {
19+
gl_Position = transform * vertex;
20+
vec3 ecVertex = vec3(modelview * vertex);
21+
22+
ecNormal = normalize(normalMatrix * normal);
23+
lightDir = normalize(lightPosition.xyz - ecVertex);
24+
vertColor = color;
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Texture from Jason Liebig's FLICKR collection of vintage labels and wrappers:
2+
// http://www.flickr.com/photos/jasonliebigstuff/3739263136/in/photostream/
3+
4+
PImage label;
5+
PShape can;
6+
float angle;
7+
8+
PShader texlightShader;
9+
10+
void setup() {
11+
size(640, 360, P3D);
12+
label = loadImage("lachoy.jpg");
13+
can = createCan(100, 200, 32, label);
14+
texlightShader = loadShader("texlightfrag.glsl", "texlightvert.glsl");
15+
}
16+
17+
void draw() {
18+
background(0);
19+
20+
shader(texlightShader);
21+
22+
pointLight(255, 255, 255, width/2, height, 200);
23+
24+
translate(width/2, height/2);
25+
rotateY(angle);
26+
shape(can);
27+
angle += 0.01;
28+
}
29+
30+
PShape createCan(float r, float h, int detail, PImage tex) {
31+
textureMode(NORMAL);
32+
PShape sh = createShape();
33+
sh.beginShape(QUAD_STRIP);
34+
sh.noStroke();
35+
sh.texture(tex);
36+
for (int i = 0; i <= detail; i++) {
37+
float angle = TWO_PI / detail;
38+
float x = sin(i * angle);
39+
float z = cos(i * angle);
40+
float u = float(i) / detail;
41+
sh.normal(x, 0, z);
42+
sh.vertex(x * r, -h/2, z * r, u, 0);
43+
sh.vertex(x * r, +h/2, z * r, u, 1);
44+
}
45+
sh.endShape();
46+
return sh;
47+
}
193 KB
Loading

0 commit comments

Comments
 (0)