Skip to content

Instantly share code, notes, and snippets.

@darkwave
Created November 11, 2013 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darkwave/7409455 to your computer and use it in GitHub Desktop.
Save darkwave/7409455 to your computer and use it in GitHub Desktop.
GarbagePatchState Augmented Reality prototype made using ARsenico Licensed under GPL v. 3
class Animation3D {
ArrayList psystems;
Animation3D() {
psystems = new ArrayList();
//for(int i = 0; i < 30; i++)
float offset = -120;
for (int i = 0; i < 4; i++) {
psystems.add(new ParticleSystem(10, new PVector(offset, 0)));
offset += 60;
}
}
void run() {
for (int i = psystems.size()-1; i >= 0; i--) {
ParticleSystem psys = (ParticleSystem) psystems.get(i);
psys.run();
//if (psys.dead()) {
//psystems.remove(i);
//int randomN = int(random(5, 10));
//for (int pIndex = 0; pIndex < randomN; pIndex++)
if (psys.particles.size() < 10)
psys.addParticle();
//}
}
}
}
import com.mondonerd.ARsenico.*;
import android.os.Environment;
import android.media.MediaScannerConnection;
import android.net.Uri;
import ketai.camera.*;
int PHOTO = 0;
int WEBCAM = 1;
int mode = PHOTO;
KetaiCamera cam;
AugmentedReality ar;
//SceneGraph scene;
PImage photo, iconPhoto;
String SAVE_DIR = "GarbagePatchState";
boolean mustSavePhoto = false;
String savePhotoPath = "";
int FRAMERATE = 20;
//boolean readyForTheShow = false;
PShape[] taps = new PShape[3];
//1) animations
Animation3D[] animations = new Animation3D[1];
int currentAnimation = 0;
void onCameraPreviewEvent()
{
cam.read();
ar.refreshInput(cam);
}
void setup() {
photo = loadImage("arsenico_test.png");
iconPhoto = loadImage("photo.png");
// test = loadImage("test.png");
for (int i = 0; i < taps.length; i++)
taps[i] = loadShape("tappo" + i + ".obj");
cam = new KetaiCamera(this, 640, 480, FRAMERATE);
frameRate(30);
ar = new AugmentedReality(this, 640, 480);
ar.setFrameDrop(500);
ar.loadMarkers("marker0.png", "marker1.png", "markers/4x4_1.patt", "markers/4x4_2.patt", "markers/4x4_3.patt", "markers/4x4_4.patt", "markers/4x4_5.patt", "markers/4x4_6.patt");
//, "markers/4x4_7.patt", "markers/4x4_8.patt", "markers/4x4_9.patt", "markers/4x4_10.patt");
//scene = new SceneGraph(this);
noStroke();
background(0);
registerMethod("exit", this);
// thread("loadAsset");
colorMode(HSB, 360, 100, 100);
animations[0] = new Animation3D();
}
void draw() {
/*
if (readyForTheShow == false) {
background(0);
fill(255, 255);
textAlign(CENTER, CENTER);
text("Loading...", width / 2, height / 2);
return;
}
*/
if (mode == PHOTO) {
background(255);
imageMode(CENTER);
image(photo, width / 2, height / 2);
imageMode(CORNER);
return;
}
hint(PApplet.DISABLE_DEPTH_TEST);
imageMode(CORNER);
//PImage reality = ar.getReality();
//if (reality != null)
image(cam, 0, 0, width, height);
imageMode(CENTER);
image(iconPhoto, 50, 50);
fill(255, 128);
text(frameRate, width - 100, 50);
hint(PApplet.ENABLE_DEPTH_TEST);
//directionalLight(255, 255, 255, 0, 1, 0);
//pointLight(255, 255, 0, 0, 0, 0);
ar.display();
lights();
if (mustSavePhoto) {
savePhoto();
mustSavePhoto = false;
}
}
void lights() {
ambientLight(255, 255, 255);
}
/*
void loadAsset() {
readyForTheShow = false;
//scene.load("test.json", true);
readyForTheShow = true;
}*/
public String sketchRenderer() {
return P3D;
}
void exit() {
if (cam != null)
cam.stop();
super.exit();
}
void displayScene(int markerID) {
//scene.display(markerID);
animations[0].run();
}
boolean savePhoto() {
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), SAVE_DIR);
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
PApplet.println("failed to create directory to save photo: "
+ mediaStorageDir.getAbsolutePath());
return false;
}
}
savePhotoPath = mediaStorageDir.getAbsolutePath() + File.separator;
String filename = millis() + ".png";
save(savePhotoPath + "AR_" + filename);
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(this,
new String[] {
//savePhotoPath + "Reality_" + filename,
savePhotoPath + "AR_" + filename
}
, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
println("ExternalStorage:" + "Scanned " + path + ":");
println("ExternalStorage " + "-> uri=" + uri);
}
}
);
return true;
}
void mousePressed() {
if (mode == PHOTO) {
cam.start();
//ar.refreshInput(test);
mode = WEBCAM;
}
else if (mode == WEBCAM) {
if (dist(mouseX, mouseY, 50, 50) < 40) {
mustSavePhoto = true;
}
else {
cam.stop();
mode = PHOTO;
}
}
}
void keyPressed() {
if (key == CODED) {
if (keyCode == MENU) {
if (cam.isFlashEnabled())
cam.disableFlash();
else
cam.enableFlash();
}
}
}
// A simple Particle class
int tapsCounter = 0;
class Particle {
PVector loc;
PVector vel;
PVector acc;
float r;
float timer;
color fillColor;
float angleX, angleY;
int tapIndex = 0;
// One constructor
Particle(PVector a, PVector v, PVector l, float r_) {
acc = a.get();
vel = v.get();
loc = l.get();
r = r_;
timer = 150.0;
fillColor = color(random(0, 360), 100, 100);
angleX = random(0, 360);
angleY = random(0, 360);
}
// Another constructor (the one we are using here)
Particle(PVector l) {
acc = new PVector(0,-0.3,0);
vel = new PVector(random(-1,1),random(2, 4),random(1, 10));
loc = l.get();
r = 5.0;
timer = random(100, 200);
fillColor = color(random(0, 360), 100, 100);
angleX = random(0, 360);
angleY = random(0, 360);
tapIndex = tapsCounter++;
}
void run() {
update();
render();
}
// Method to update location
void update() {
vel.add(acc);
loc.add(vel);
timer -= 1.0;
if (loc.y < -500)
vel = new PVector(random(-2,2),random(2, 10),random(1, 2));
}
// Method to display
void render() {
pushMatrix();
fill(fillColor);
translate(loc.x, loc.y, loc.z);
//box(r);
rotateX(radians(angleX));
rotateY(radians(angleY));
scale(10, 10, 10);
shape(taps[tapIndex % taps.length]);
popMatrix();
}
// Is the particle still useful?
boolean dead() {
if (timer <= 0.0) {
return true;
} else {
return false;
}
}
}
// Particle system from Processing 2.1 examples ;-)
class ParticleSystem {
ArrayList particles; // An arraylist for all the particles
PVector origin; // An origin point for where particles are birthed
ParticleSystem(int num, PVector v) {
particles = new ArrayList(); // Initialize the arraylist
origin = v.get(); // Store the origin point
for (int i = 0; i < num; i++) {
particles.add(new Particle(origin));
}
}
void run() {
// Cycle through the ArrayList backwards b/c we are deleting
for (int i = particles.size()-1; i >= 0; i--) {
Particle p = (Particle) particles.get(i);
p.run();
if (p.dead()) {
particles.remove(i);
}
}
}
void addParticle() {
particles.add(new Particle(origin));
}
void addParticle(Particle p) {
particles.add(p);
}
// A method to test if the particle system still has particles
boolean dead() {
if (particles.isEmpty()) {
return true;
}
else {
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment