Created
March 11, 2021 19:14
-
-
Save codeanticode/73ba7ff0b5b41baf6516f86377c2dcdc to your computer and use it in GitHub Desktop.
Create multiple objects attached to trackables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import processing.ar.*; | |
ARTracker tracker; | |
ArrayList<ARAnchor> trackAnchors = new ArrayList<ARAnchor>(); | |
void setup() { | |
fullScreen(AR); | |
tracker = new ARTracker(this); | |
tracker.start(); | |
noStroke(); | |
} | |
void draw() { | |
lights(); | |
drawAnchors(); | |
drawTrackables(); | |
} | |
void mousePressed() { | |
ARTrackable hit = tracker.get(mouseX, mouseY); | |
if (hit != null) trackAnchors.add(new ARAnchor(hit)); | |
} | |
void drawAnchors() { | |
for (ARAnchor anchor : trackAnchors) { | |
if (anchor.isTracking()) drawSphere(anchor, 0.05); | |
if (anchor.isStopped()) anchor.dispose(); | |
} | |
tracker.clearAnchors(trackAnchors); | |
} | |
void drawTrackables() { | |
for (int i = 0; i < tracker.count(); i++) { | |
ARTrackable t = tracker.get(i); | |
pushMatrix(); | |
t.transform(); | |
float lx = t.lengthX(); | |
float lz = t.lengthZ(); | |
if (mousePressed && t.isSelected(mouseX, mouseY)) { | |
fill(255, 0, 0, 100); | |
} else { | |
fill(255, 100); | |
} | |
drawPlane(lx, lz); | |
popMatrix(); | |
} | |
} | |
void drawSphere(ARAnchor anchor, float r) { | |
anchor.attach(); | |
fill(#CF79F5); | |
sphere(r); | |
anchor.detach(); | |
} | |
void drawPlane(float lx, float lz) { | |
beginShape(QUADS); | |
vertex(-lx/2, 0, -lz/2); | |
vertex(-lx/2, 0, +lz/2); | |
vertex(+lx/2, 0, +lz/2); | |
vertex(+lx/2, 0, -lz/2); | |
endShape(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment