1
1
import processing.ar.* ;
2
2
3
+ ARTracker tracker;
4
+ ARAnchor touchAnchor;
5
+ ArrayList<ARAnchor > trackAnchors;
3
6
float angle;
4
- Anchor selAnchor;
5
- ArrayList<Anchor > regAnchors;
6
-
7
- Tracker tracker;
8
7
9
8
void setup () {
10
9
fullScreen(AR );
11
- mat = new PMatrix3D ();
12
-
13
- tracker = new Tracker (this );
10
+ tracker = new ARTracker (this );
14
11
tracker. start();
15
- regAnchors = new ArrayList<Anchor > ();
12
+ trackAnchors = new ArrayList<ARAnchor > ();
16
13
}
17
14
18
15
void draw () {
19
16
// The AR Core session, frame and camera can be accessed through Processing's surface object
20
17
// to obtain the full information about the AR scene:
21
- // PSurfaceAR surface = (PSurfaceAR ) getSurface();
18
+ // ARSurface surface = (ARSurface ) getSurface();
22
19
// surface.camera.getPose();
23
20
// surface.frame.getLightEstimate();
24
21
25
- // No background call is needed, the screen is refreshed each frame with the image from the camera
26
-
27
22
lights ();
28
23
29
-
30
24
if (mousePressed ) {
31
25
// 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 ;
34
30
}
35
31
36
32
// 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 );
38
35
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();
46
38
}
39
+ if (touchAnchor != null ) drawBox(touchAnchor, 255 , 0 , 0 );
47
40
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);
52
43
53
44
// Draw trackable planes
54
45
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 ;
56
48
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
-
73
49
pushMatrix ();
74
50
trackable. transform();
75
- if (mousePressed && trackable. selected (mouseX , mouseY )) {
51
+ if (mousePressed && trackable. isSelected (mouseX , mouseY )) {
76
52
fill (255 , 0 , 0 , 100 );
77
53
} else {
78
54
fill (255 , 100 );
79
55
}
80
-
81
56
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++ ) {
83
59
float x = points[2 * n];
84
60
float z = points[2 * n + 1 ];
85
61
vertex (x, 0 , z);
@@ -91,10 +67,27 @@ void draw() {
91
67
angle += 0.1 ;
92
68
}
93
69
94
- void drawBox (Anchor anchor , int r , int g , int b ) {
70
+ void drawBox (ARAnchor anchor , int r , int g , int b ) {
95
71
anchor. attach();
96
72
fill (r, g, b);
97
73
rotateY (angle);
98
- box (0.15 );
99
- anchor. detach();
74
+ box (0.15f );
75
+ anchor. detach();
100
76
}
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
+ }
0 commit comments