Skip to content

Commit 408d542

Browse files
authored
Updates from libprocessing. (#1392)
1 parent eee55b5 commit 408d542

File tree

14 files changed

+1308
-88
lines changed

14 files changed

+1308
-88
lines changed

core/examples/src/main/java/WebGPU.java

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package webgpu;
2+
3+
import processing.core.PApplet;
4+
5+
public class AnimatedMesh extends PApplet {
6+
7+
int gridSize = 20;
8+
float spacing = 10;
9+
float time = 0;
10+
11+
public void settings() {
12+
size(600, 600, WEBGPU);
13+
}
14+
15+
public void setup() {
16+
perspective(PI/3, (float)width/height, 0.1f, 1000);
17+
camera(150, 150, 150, 0, 0, 0, 0, 1, 0);
18+
}
19+
20+
public void draw() {
21+
background(13, 13, 26);
22+
23+
float offset = (gridSize * spacing) / 2.0f;
24+
25+
beginShape(TRIANGLES);
26+
for (int z = 0; z < gridSize - 1; z++) {
27+
for (int x = 0; x < gridSize - 1; x++) {
28+
float px0 = x * spacing - offset;
29+
float pz0 = z * spacing - offset;
30+
float px1 = (x + 1) * spacing - offset;
31+
float pz1 = (z + 1) * spacing - offset;
32+
33+
float y00 = wave(px0, pz0);
34+
float y10 = wave(px1, pz0);
35+
float y01 = wave(px0, pz1);
36+
float y11 = wave(px1, pz1);
37+
38+
fill(x * 255.0f / gridSize, 128, z * 255.0f / gridSize);
39+
normal(0, 1, 0);
40+
41+
vertex(px0, y00, pz0);
42+
vertex(px0, y01, pz1);
43+
vertex(px1, y10, pz0);
44+
45+
vertex(px1, y10, pz0);
46+
vertex(px0, y01, pz1);
47+
vertex(px1, y11, pz1);
48+
}
49+
}
50+
endShape();
51+
52+
time += 0.05f;
53+
}
54+
55+
float wave(float x, float z) {
56+
return sin(x * 0.1f + time) * cos(z * 0.1f + time) * 20;
57+
}
58+
59+
public static void main(String[] args) {
60+
PApplet.disableAWT = true;
61+
PApplet.main(AnimatedMesh.class.getName());
62+
}
63+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package webgpu;
2+
3+
import processing.core.PApplet;
4+
import processing.core.PImage;
5+
6+
public class BackgroundImage extends PApplet {
7+
8+
PImage img;
9+
10+
public void settings() {
11+
size(400, 400, WEBGPU);
12+
}
13+
14+
public void setup() {
15+
img = createImage(400, 400, RGB);
16+
img.loadPixels();
17+
for (int y = 0; y < img.height; y++) {
18+
for (int x = 0; x < img.width; x++) {
19+
int r = (int) (x * 255.0 / img.width);
20+
int g = (int) (y * 255.0 / img.height);
21+
int b = 128;
22+
img.pixels[y * img.width + x] = color(r, g, b);
23+
}
24+
}
25+
img.updatePixels();
26+
}
27+
28+
public void draw() {
29+
background(img);
30+
}
31+
32+
public static void main(String[] args) {
33+
PApplet.disableAWT = true;
34+
PApplet.main(BackgroundImage.class.getName());
35+
}
36+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package webgpu;
2+
3+
import processing.core.PApplet;
4+
5+
public class Box3D extends PApplet {
6+
7+
float angle = 0;
8+
9+
public void settings() {
10+
size(400, 400, WEBGPU);
11+
}
12+
13+
public void setup() {
14+
perspective(PI/3, (float)width/height, 0.1f, 1000);
15+
camera(200, 200, 300, 0, 0, 0, 0, 1, 0);
16+
}
17+
18+
public void draw() {
19+
background(26, 26, 38);
20+
21+
pushMatrix();
22+
rotateY(angle);
23+
rotateX(angle * 0.7f);
24+
fill(200, 100, 100);
25+
box(100);
26+
popMatrix();
27+
28+
angle += 0.02f;
29+
}
30+
31+
public static void main(String[] args) {
32+
PApplet.disableAWT = true;
33+
PApplet.main(Box3D.class.getName());
34+
}
35+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package webgpu;
2+
3+
import processing.core.PApplet;
4+
5+
public class Rectangle extends PApplet {
6+
7+
public void settings() {
8+
size(400, 400, WEBGPU);
9+
}
10+
11+
public void draw() {
12+
background(51);
13+
14+
fill(255);
15+
noStroke();
16+
rect(10, 10, 100, 100);
17+
}
18+
19+
public static void main(String[] args) {
20+
PApplet.disableAWT = true;
21+
PApplet.main(Rectangle.class.getName());
22+
}
23+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package webgpu;
2+
3+
import processing.core.PApplet;
4+
5+
public class Transforms extends PApplet {
6+
7+
float t = 0;
8+
9+
public void settings() {
10+
size(400, 400, WEBGPU);
11+
}
12+
13+
public void draw() {
14+
background(26);
15+
16+
noStroke();
17+
18+
for (int i = 0; i < 4; i++) {
19+
for (int j = 0; j < 4; j++) {
20+
pushMatrix();
21+
22+
translate(50 + j * 100, 50 + i * 100);
23+
24+
float angle = t + (i + j) * PI / 8.0f;
25+
rotate(angle);
26+
27+
float s = 0.8f + sin(t * 2.0f + (i * j)) * 0.2f;
28+
scale(s, s);
29+
30+
float r = j / 3.0f;
31+
float g = i / 3.0f;
32+
fill(r * 255, g * 255, 204);
33+
34+
rect(-20, -20, 40, 40);
35+
36+
popMatrix();
37+
}
38+
}
39+
40+
t += 0.02f;
41+
}
42+
43+
public static void main(String[] args) {
44+
PApplet.disableAWT = true;
45+
PApplet.main(Transforms.class.getName());
46+
}
47+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package webgpu;
2+
3+
import processing.core.PApplet;
4+
5+
public class UpdatePixels extends PApplet {
6+
7+
static final int RECT_W = 10;
8+
static final int RECT_H = 10;
9+
10+
boolean firstFrame = true;
11+
12+
public void settings() {
13+
size(100, 100, WEBGPU);
14+
}
15+
16+
public void draw() {
17+
background(0); // Black background
18+
19+
loadPixels();
20+
21+
for (int y = 20; y < 20 + RECT_H; y++) {
22+
for (int x = 20; x < 20 + RECT_W; x++) {
23+
pixels[y * width + x] = color(255, 0, 0);
24+
}
25+
}
26+
27+
for (int y = 60; y < 60 + RECT_H; y++) {
28+
for (int x = 60; x < 60 + RECT_W; x++) {
29+
pixels[y * width + x] = color(0, 0, 255);
30+
}
31+
}
32+
33+
updatePixels();
34+
35+
if (firstFrame) {
36+
firstFrame = false;
37+
38+
println("Total pixels: " + pixels.length);
39+
40+
for (int y = 0; y < height; y++) {
41+
StringBuilder row = new StringBuilder();
42+
for (int x = 0; x < width; x++) {
43+
int idx = y * width + x;
44+
int pixel = pixels[idx];
45+
float r = red(pixel);
46+
float b = blue(pixel);
47+
float a = alpha(pixel);
48+
49+
if (r > 127) {
50+
row.append("R");
51+
} else if (b > 127) {
52+
row.append("B");
53+
} else if (a > 127) {
54+
row.append(".");
55+
} else {
56+
row.append(" ");
57+
}
58+
}
59+
println(row.toString());
60+
}
61+
62+
println("\nSample pixels:");
63+
println("(25, 25): " + hex(pixels[25 * width + 25]));
64+
println("(65, 65): " + hex(pixels[65 * width + 65]));
65+
println("(0, 0): " + hex(pixels[0]));
66+
println("(50, 50): " + hex(pixels[50 * width + 50]));
67+
}
68+
}
69+
70+
public static void main(String[] args) {
71+
PApplet.disableAWT = true;
72+
PApplet.main(UpdatePixels.class.getName());
73+
}
74+
}

core/src/processing/core/PApplet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,8 +2008,8 @@ protected PGraphics createPrimaryGraphics() {
20082008
* @see PGraphics
20092009
*/
20102010
public PImage createImage(int w, int h, int format) {
2011-
PImage image = new PImage(w, h, format);
2012-
image.parent = this; // make save() work
2011+
PImage image = (g != null) ? g.createImage(w, h, format) : new PImage(w, h, format);
2012+
image.parent = this;
20132013
return image;
20142014
}
20152015

core/src/processing/core/PGraphics.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3830,6 +3830,11 @@ private void smoothWarning(String method) {
38303830
// IMAGE
38313831

38323832

3833+
public PImage createImage(int w, int h, int format) {
3834+
return new PImage(w, h, format);
3835+
}
3836+
3837+
38333838
/**
38343839
*
38353840
* Modifies the location from which images are drawn by changing the way in

0 commit comments

Comments
 (0)