Skip to content

Commit 5466625

Browse files
committed
updated examples
1 parent 2d62698 commit 5466625

File tree

4 files changed

+74
-84
lines changed

4 files changed

+74
-84
lines changed

debug/apps/arscene/src/main/java/arscene/Sketch.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
import processing.core.PApplet;
77

88
public class Sketch extends PApplet {
9-
float angle;
9+
ARTracker tracker;
1010
ARAnchor touchAnchor;
1111
ArrayList<ARAnchor> trackAnchors;
12-
13-
ARTracker tracker;
12+
float angle;
1413

1514
public void settings() {
1615
fullScreen(AR);
Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,61 @@
11
import processing.ar.*;
22

3+
ARTracker tracker;
4+
ARAnchor touchAnchor;
5+
ArrayList<ARAnchor> trackAnchors;
36
float angle;
4-
Anchor selAnchor;
5-
ArrayList<Anchor> regAnchors;
6-
7-
Tracker tracker;
87

98
void setup() {
109
fullScreen(AR);
11-
mat = new PMatrix3D();
12-
13-
tracker = new Tracker(this);
10+
tracker = new ARTracker(this);
1411
tracker.start();
15-
regAnchors = new ArrayList<Anchor>();
12+
trackAnchors = new ArrayList<ARAnchor>();
1613
}
1714

1815
void draw() {
1916
// The AR Core session, frame and camera can be accessed through Processing's surface object
2017
// to obtain the full information about the AR scene:
21-
// PSurfaceAR surface = (PSurfaceAR) getSurface();
18+
// ARSurface surface = (ARSurface) getSurface();
2219
// surface.camera.getPose();
2320
// surface.frame.getLightEstimate();
2421

25-
// No background call is needed, the screen is refreshed each frame with the image from the camera
26-
2722
lights();
2823

29-
3024
if (mousePressed) {
3125
// Create new anchor at the current touch point
32-
if (selAnchor != null) selAnchor.dispose();
33-
selAnchor = new Anchor(tracker, mouseX, mouseY);
26+
if (touchAnchor != null) touchAnchor.dispose();
27+
ARTrackable hit = tracker.get(mouseX, mouseY);
28+
if (hit != null) touchAnchor = new ARAnchor(hit);
29+
else touchAnchor = null;
3430
}
3531

3632
// Draw objects attached to each anchor
37-
for (Anchor anchor: regAnchors) {
33+
for (ARAnchor anchor : trackAnchors) {
34+
if (anchor.isTracking()) drawBox(anchor, 255, 255, 255);
3835

39-
int status = anchor.status(i);
40-
if (status == PAR.PAUSED || status == PAR.STOPPED) {
41-
if (status == PAR.STOPPED) anchor.dispose();
42-
continue;
43-
}
44-
45-
drawBox(anchor, 255, 255, 255);
36+
// It is very important to dispose anchors once they are no longer tracked.
37+
if (anchor.isStopped()) anchor.dispose();
4638
}
39+
if (touchAnchor != null) drawBox(touchAnchor, 255, 0, 0);
4740

48-
if (selAnchor != null) {
49-
drawBox(selAnchor, 255, 0, 0);
50-
}
51-
41+
// Conveniency function in the tracker object to remove disposed anchors from a list
42+
tracker.clearAnchors(trackAnchors);
5243

5344
// Draw trackable planes
5445
for (int i = 0; i < tracker.count(); i++) {
55-
Trackable trackable = tracker.get(i);
46+
ARTrackable trackable = tracker.get(i);
47+
if (!trackable.isTracking()) continue;
5648

57-
int status = trackable.status();
58-
if (status == PAR.PAUSED || status == PAR.STOPPED) continue;
59-
60-
if (status == PAR.CREATED && regAnchors.size() < 10) {
61-
// Add new anchor associated to this trackable, 0.3 meters above it
62-
Anchor anchor;
63-
if (trackable.type() == PAR.PLANE_WALL) {
64-
anchor = new Anchor(trackable, 0.3, 0, 0);
65-
} else {
66-
anchor = new Anchor(trackable, 0, 0.3, 0);
67-
}
68-
regAnchors.add(anchor);
69-
}
70-
71-
float[] points = trackable.getPolygon();
72-
7349
pushMatrix();
7450
trackable.transform();
75-
if (mousePressed && trackable.selected(mouseX, mouseY)) {
51+
if (mousePressed && trackable.isSelected(mouseX, mouseY)) {
7652
fill(255, 0, 0, 100);
7753
} else {
7854
fill(255, 100);
7955
}
80-
8156
beginShape();
82-
for (int n = 0; n < points.length/2; n++) {
57+
float[] points = trackable.getPolygon();
58+
for (int n = 0; n < points.length / 2; n++) {
8359
float x = points[2 * n];
8460
float z = points[2 * n + 1];
8561
vertex(x, 0, z);
@@ -91,10 +67,27 @@ void draw() {
9167
angle += 0.1;
9268
}
9369

94-
void drawBox(Anchor anchor, int r, int g, int b) {
70+
void drawBox(ARAnchor anchor, int r, int g, int b) {
9571
anchor.attach();
9672
fill(r, g, b);
9773
rotateY(angle);
98-
box(0.15);
99-
anchor.detach();
74+
box(0.15f);
75+
anchor.detach();
10076
}
77+
78+
void trackableEvent(ARTrackable t) {
79+
if (trackAnchors.size() < 10) {
80+
float x0 = 0, y0 = 0;
81+
if (t.isWallPlane()) {
82+
// The new trackable is a wall, so adding the anchor 0.3 meters to its side
83+
x0 = 0.3;
84+
} else if (t.isFloorPlane()) {
85+
// The new trackable is a floor plane, so adding the anchor 0.3 meters above it
86+
y0 = 0.3;
87+
} else {
88+
// The new trackable is a floor plane, so adding the anchor 0.3 meters below it
89+
y0 = -0.3;
90+
}
91+
trackAnchors.add(new ARAnchor(t, x0, y0, 0));
92+
}
93+
}

mode/libraries/ar/examples/ImportObj/ImportObj.pde

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
import processing.ar.*;
22

3-
Tracker tracker;
4-
Anchor anchor;
3+
ARTracker tracker;
4+
ARAnchor anchor;
55
PShape arObj;
66

77
void setup() {
88
fullScreen(AR);
99
arObj = loadShape("model.obj");
1010

11-
tracker = new Tracker(this);
11+
tracker = new ARTracker(this);
1212
tracker.start();
1313
}
1414

1515
void draw() {
1616
lights();
1717

1818
if (mousePressed) {
19-
// Delete the old touch anchor, if any.
19+
// Create new anchor at the current touch point
2020
if (anchor != null) anchor.dispose();
21-
22-
// Create a new anchor at the current touch position.
23-
anchor = new Anchor(tracker, mouseX, mouseY);
21+
ARTrackable hit = tracker.get(mouseX, mouseY);
22+
if (hit != null) anchor = new ARAnchor(hit);
23+
else anchor = null;
2424
}
2525

2626
if (anchor != null) {

mode/libraries/ar/examples/Spheres/Spheres.pde

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,61 @@
11
import processing.ar.*;
22

3-
Tracker tracker;
4-
Anchor anchor;
3+
ARTracker tracker;
4+
ARAnchor anchor;
5+
PShape arObj;
56
float angle;
67

78
void setup() {
89
fullScreen(AR);
10+
911
noStroke();
1012

11-
tracker = new Tracker(this);
12-
tracker.start();
13+
tracker = new ARTracker(this);
14+
tracker.start();
1315
}
1416

1517
void draw() {
1618
lights();
1719

1820
if (mousePressed) {
19-
// Delete the old touch anchor, if any.
21+
// Create new anchor at the current touch point
2022
if (anchor != null) anchor.dispose();
21-
22-
// Create a new anchor at the current touch position.
23-
anchor = new Anchor(tracker, mouseX, mouseY);
23+
ARTrackable hit = tracker.get(mouseX, mouseY);
24+
if (hit != null) anchor = new ARAnchor(hit);
25+
else anchor = null;
2426
}
2527

2628
if (anchor != null) {
2729
anchor.attach();
2830
fill(217, 121, 255);
29-
sphere(0.10f);
31+
sphere(0.1);
3032
rotateY(angle);
31-
translate(0, 0, 0.3f);
32-
sphere(0.05f);
33+
translate(0, 0, 0.3);
34+
sphere(0.05);
3335
angle += 0.1;
3436
anchor.detach();
3537
}
3638

3739
// Draw trackable planes
3840
for (int i = 0; i < tracker.count(); i++) {
39-
Trackable trackable = tracker.get(i);
40-
41-
int status = trackable.status();
42-
if (status == PAR.PAUSED || status == PAR.STOPPED) continue;
41+
ARTrackable trackable = tracker.get(i);
42+
if (!trackable.isTracking()) continue;
4343

44-
45-
float[] points = trackable.getPolygon();
46-
4744
pushMatrix();
4845
trackable.transform();
49-
if (mousePressed && trackable.selected(mouseX, mouseY)) {
46+
if (mousePressed && trackable.isSelected(mouseX, mouseY)) {
5047
fill(255, 0, 0, 100);
5148
} else {
5249
fill(255, 100);
5350
}
5451

55-
beginShape();
56-
for (int n = 0; n < points.length/2; n++) {
57-
float x = points[2 * n];
58-
float z = points[2 * n + 1];
59-
vertex(x, 0, z);
60-
}
52+
beginShape(QUADS);
53+
float lx = trackable.lengthX();
54+
float lz = trackable.lengthZ();
55+
vertex(-lx/2, 0, -lz/2);
56+
vertex(-lx/2, 0, +lz/2);
57+
vertex(+lx/2, 0, +lz/2);
58+
vertex(+lx/2, 0, -lz/2);
6159
endShape();
6260
popMatrix();
6361
}

0 commit comments

Comments
 (0)