AIGA JWU NEWS & UPDATES

Augmented Reality Tutorial

posted by: Robert Erskine on December 1st, 2009

EDIT: I’ve reuploaded the necessary files to complete this exercise, as well as the completed tutorial files. If you have any questions please feel free to leave a comment below.

For the past month or so I’ve been reading a lot up on Augmented Reality in the flash environment. According to Wikipedia, Augmented Reality is “is a term for a live direct or indirect view of a physical real-world environment whose elements are merged with (or augmented by) virtual computer-generated imagery – creating a mixed reality.”

Hungry for some Augmented Reality

I observed many examples including useful applications like the United States Postal Service Virtual Box and GE’s Smart Grid. I wanted to develop something similar but on a simpler scale. After a couple of hours of work, and a few energy drinks later, I created a virtual world for my DME1020 class using Augmented Reality. I’d like to share this experience with the JWU AIGA community.

1 ) There are a few files we need before we begin the project. The Flash Augmented Reality Toolkit (FLAR), some attributes from the Flex framework, the Papervision3D library, and a small adobe air application to create our marker, as well as a .dat file that will control the parameters for our webcam. All of these files can be downloaded via this link.

2 ) We need a 3D object to load whenever we see our marker. Since it is so close to Thanksgiving, I’ve decided to use a turkey. Using Google SkethUp’s 3D warehouse I found a .zip of a turkey to use, which includes the necessary files and textures needed. First, click on “Download Model” and “download Collada .zip”. Extract the zip to a folder on your desktop.

3D Turkey

3 ) File and folder management is critical in this type of project. So in order to make sure we can access all of the files later on, we should invest some time in getting it neat and straight now. Below is a screen shot of proper folder management for this project:

File Managment

Copy the contents of the turkey folder we downloaded from Google SketchUp and paste them into the Flash Project > assets > models folder. Copy the files we downloaded earlier and put them in the corresponding folders.

4) Next, we can make a marker for our project. The marker is what the camera will recognize and use as an x, y, and z axis for rendering our turkey. I created a 600 by 600  px image. Black background with a 450 by 450 px white square in the middle. I put the text “JWU AIGA” in the middle, but you can put whatever you wish. It is important to follow this black and white colorscheme or else the FLAR package won’t recognize our marker. Save this in the root of our Flash Project Folder as a jpg, gif, or png.

MARKER

5 ) Next step is to print out, or load this new marker that you’ve made on your smart phone. Open up the ARToolKit Marker Generator that came in the zip file included above. If you hold up the marker you’ve made a red outline should appear around the marker. While this red line is there, click “Save Pattern” and save it as Flash Project > assests > FLAR > FLARpattern.pat.

markerscreenie

6 ) Now it’s time to jump into flash. Create a new Actionscript 3.0 .fla, 30 fps with dimensions 640 by 480 px.

7 ) Create a new Actionscript File (.as). This is where we will adding all of our code, we will attach this .as file to our .fla in a later step. First thing we will do in our code is add all of our imports:

--------------------
package {
import flash.display.BitmapData;
 import flash.display.Sprite;
 import flash.events.Event;
 import flash.media.Camera;
 import flash.media.Video;
 import flash.utils.ByteArray;

 import org.libspark.flartoolkit.core.FLARCode;
 import org.libspark.flartoolkit.core.param.FLARParam;
 import org.libspark.flartoolkit.core.raster.rgb.FLARRgbRaster_BitmapData;
 import org.libspark.flartoolkit.core.transmat.FLARTransMatResult;
 import org.libspark.flartoolkit.detector.FLARSingleMarkerDetector;
 import org.libspark.flartoolkit.pv3d.FLARBaseNode;
 import org.libspark.flartoolkit.pv3d.FLARCamera3D;

 import org.papervision3d.lights.PointLight3D;
 import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
 import org.papervision3d.materials.utils.MaterialsList;
 import org.papervision3d.objects.parsers.DAE;
 import org.papervision3d.objects.primitives.Cube;
 import org.papervision3d.render.BasicRenderEngine;
 import org.papervision3d.scenes.Scene3D;
 import org.papervision3d.view.Viewport3D;
--------------------

What this code does is imports all the external classes needed from the flash player, flash ar tool kit, and paper vision 3d needed for this project to work properly.

8 ) Next we will define our class, setup our class properties and variables

--------------------
public class AugmentedReality extends Sprite
 {

 private var video    : Video;
 private var webcam    : Camera;    

 private var flarBaseNode                : FLARBaseNode;
 private var flarParam                   : FLARParam;
 private var flarCode                    : FLARCode;
 private var flarRgbRaster_BitmapData    : FLARRgbRaster_BitmapData;
 private var flarSingleMarkerDetector    : FLARSingleMarkerDetector;
 private var flarCamera3D                : FLARCamera3D;
 private var flarTransMatResult          : FLARTransMatResult;
 private var bitmapData                  : BitmapData;
 private var FLAR_CODE_SIZE              : uint         = 16;
 private var MARKER_WIDTH                : uint         = 80;

 [Embed(source="./assets/FLAR/FLARPattern.pat", mimeType="application/octet-stream")]
 private var Pattern    : Class;

 [Embed(source="./assets/FLAR/FLARCameraParameters.dat", mimeType="application/octet-stream")]
 private var Params : Class;

 private var basicRenderEngine           : BasicRenderEngine;
 private var viewport3D                  : Viewport3D;
 private var scene3D                     : Scene3D;
 private var collada3DModel              : DAE;

 private var VIDEO_WIDTH                 : Number = 640;
 private var VIDEO_HEIGHT                : Number = 480;
 private var WEB_CAMERA_WIDTH            : Number = VIDEO_WIDTH/2;
 private var WEB_CAMERA_HEIGHT           : Number = VIDEO_HEIGHT/2;
 private var VIDEO_FRAME_RATE            : Number = 60;
 private var DETECTION_THRESHOLD         : uint   = 80;
 private var DETECTION_CONFIDENCE        : Number = 0.5;
 private var MODEL_SCALE                 : Number = 0.2;                

 private var COLLADA_3D_MODEL            : String = "./assets/models/models/bird.dae"; // make sure you rename the original .dae file
--------------------

9 ) Next we will setup the constructor. The constructor will do 4 things. It will prepare the webcam, the marker detection, and the rendering for papervision3D. Then, it will repeatedly loop to update the marker and the rendering of our turkey

--------------------
public function AugmentedReality ()
 {
 prepareWebCam();
 prepareMarkerDetection();
 preparePaperVision3D();
 addEventListener(Event.ENTER_FRAME, loopToDetectMarkerAndUpdate3D);
 }
--------------------

10 ) Next step we will define all of the methods listed in step 9.

--------------------
private function prepareWebCam () : void
 {
 video = new Video(VIDEO_WIDTH, VIDEO_HEIGHT);
 webcam = Camera.getCamera();
 webcam.setMode(WEB_CAMERA_WIDTH, WEB_CAMERA_HEIGHT, VIDEO_FRAME_RATE);
 video.attachCamera(webcam);
 addChild(video);
 }

private function prepareMarkerDetection () : void
 {
 flarParam = new FLARParam();
 flarParam.loadARParam(new Params() as ByteArray);
 flarCode = new FLARCode (FLAR_CODE_SIZE, FLAR_CODE_SIZE);
 flarCode.loadARPatt(new Pattern());

 bitmapData = new BitmapData(VIDEO_WIDTH, VIDEO_HEIGHT);
 bitmapData.draw(video);
 flarRgbRaster_BitmapData = new FLARRgbRaster_BitmapData(bitmapData);
 flarSingleMarkerDetector = new FLARSingleMarkerDetector (flarParam, flarCode, MARKER_WIDTH);
 }

private function preparePaperVision3D () : void
 {
 basicRenderEngine     = new BasicRenderEngine();
 flarTransMatResult     = new FLARTransMatResult();
 viewport3D             = new Viewport3D();
 flarCamera3D         = new FLARCamera3D(flarParam);
 flarBaseNode         = new FLARBaseNode();
 scene3D             = new Scene3D();
 scene3D.addChild(flarBaseNode);

 collada3DModel = new DAE ();
 collada3DModel.load(COLLADA_3D_MODEL);
 collada3DModel.scaleX = collada3DModel.scaleY = collada3DModel.scaleZ = MODEL_SCALE;
 collada3DModel.x = -30;
 collada3DModel.y = -530;
 collada3DModel.rotationX = 90;
 collada3DModel.rotationY = 0;
 collada3DModel.rotationZ = 0;

 flarBaseNode.addChild(collada3DModel);
 addChild (viewport3D);
 }

//and finally

private function loopToDetectMarkerAndUpdate3D (aEvent : Event) : void
 {
 bitmapData.draw(video);

 try {

 if(    flarSingleMarkerDetector.detectMarkerLite (flarRgbRaster_BitmapData, DETECTION_THRESHOLD) &&
 flarSingleMarkerDetector.getConfidence() > DETECTION_CONFIDENCE                            ) {

 flarSingleMarkerDetector.getTransformMatrix(flarTransMatResult);
 flarBaseNode.setTransformMatrix(flarTransMatResult);
 basicRenderEngine.renderScene(scene3D, flarCamera3D, viewport3D);
 }
 } catch (error : Error) {}
 }
 }
}
--------------------

11 ) This concludes the amount of code we need. Lastly, let’s jump back to the .fla we created and attach this Actionscript (.as) file to it.
attach the constructor

12 ) That’s the last step, export the fla, grant the swf permission to use your camera and hold your marker up to the webcam.

All in all, augmented reality is still low in functionality and uses a large chunk of CPU power, however there is a pretty big “awe” factor involved. I put a custom marker of my own on the back of my business cards and it goes over extremely well during interviews. It shows that you are up-to-date on web trends and are there to impress.
And of course, I’ve included the source files with all of the work. However, in order to get the most out of this tutorial I recommend following the tutorial and only making references to the source for help if you get stuck. The source files are greatly //commented for this reason.

Thanks a lot for taking the time to read my blog post and tutorial. If you get the opportunity post your own AR experiments in a comment.

Also, if you have any questions please post them in a comment so everyone can benefit from them.

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to MySpace Post to StumbleUpon

Robert Erskine
President

22 Responses to “Augmented Reality Tutorial”

  1. Neo Says:

    Hi. The Link in step 1 no longer works.

    Could you please re-upload it.

    Thanks.

    Neo

  2. Rob Erskine Says:

    Sorry about that Neo,

    Here they are reuploaded.

    http://www.roberskine.com/ar_resources.zip

  3. Karol Says:

    Hello, Great tutorial!

    when i open AugmentedReality_v1.swf from your source files and show marker nothing happens. Do You now why?

    Thanks

  4. Amanda Says:

    hey.. thx for the tutorial but i don’t seem to be able to load other dae file in.. the prog still works but nth appear as in the 3d model….

  5. Mex Says:

    hey cool tut but i need ar_resources.zip
    or a list of all required files.

    at time i play with ARToolKit and can’t load some other objects.

    pls need more infos

    thx

  6. Robert Erskine Says:

    Hey Mex,

    Sorry for the late reply. Things have been hectic around here. I’ve reuploaded all of the necessary files. http://www.RobErskine.com/ar_files.zip. I’ll also upload a completed directory so you can test your camera / tutorial files.

    Thanks for checking us out,
    Rob

  7. Rico Says:

    Great Tutorial, but ive got some problems and i cant find the reason of the error. Can you please upload the .fla and .as file from this great tutorial?

    Thank you very much!

  8. Robert Erskine Says:

    Hey Rico,

    I just added the source files at the top of the post earlier in the week. You can find them here http://www.roberskine.com/ar_zip.zip . The marker is really sensitive with certain cameras. Be patient and move it around a bit to see your 3d model.

    Thanks and let me know if you need anything else,
    Rob

  9. Patrick Says:

    Hi,

    When I try and switch this out with other collada models, I am having issues. Do you know what rules apply to texturing.

    Specifically …
    1. jpg or png
    2. Why are there 2 “0″ textures ?
    3. Is there anyway to check if the model or the texture is broken.

    Thanks,
    Patrick

  10. Joel Says:

    Hi, that link you posted http://www.roberskine.com/ar_zip.zip doesnt seem to be working, also would you have to change the code to match the directory of your pc and i cant seem to get the as file connected to the fl file
    thanks

  11. Robert Erskine Says:

    Hey Joel,

    I don’t know what’s up with these source files, I’ve rehosted them numerous times and I guess they keep getting punted off.

    I’ve rehosted them, at the same location, http://www.roberskine.com/ar_zip.zip

    Thanks for checking us out,
    Rob

  12. Robert Erskine Says:

    Hey Patrick,

    Texturing is handled through the calada model. Unfortunately I don’t know the difference between jpg or png textures. I’ve only used textures from google collada files so they are all provided. Because I use google 3d files the textures are provided. So it’s kind of irrelevant that there are 2 “0″ textures. This varies from model to model.

    To my knowledge, there is no way to check if the model or texture is broken. However, each model varies on how it is displayed in the engine. Having to mess with the scale, x, y, and z coordinates is necessary to get the model to show up using your QR code. I know I had this issue when using the turkey model.

    Let me know if I can help with anything else,
    Rob

  13. Gordon Says:

    Hi when i try to play the swf i get these errors

    C:\Users\HP Pavilion\Desktop\FlashProject\AugmentedReality.as, Line 43 unable to resolve ‘./assets/FLAR/FLARPattern.pat’ for transcoding

    C:\Users\HP Pavilion\Desktop\FlashProject\AugmentedReality.as, Line 43 Unable to transcode ./assets/FLAR/FLARPattern.pat.

    C:\Users\HP Pavilion\Desktop\FlashProject\AugmentedReality.as, Line 46 unable to resolve ‘./assets/FLAR/FLARCameraParameters.dat’ for transcoding

    C:\Users\HP Pavilion\Desktop\FlashProject\AugmentedReality.as, Line 46 Unable to transcode ./assets/FLAR/FLARCameraParameters.dat.

    can you please help?

  14. Sarfaraz Hassan Says:

    Hi Gordon,

    I too got the same error. It’s because the code can’t find FlarPattern.pat under assets folder.
    To solve it simply replace:
    source=”./assets/FLAR/FLARPattern.pat”,
    with:
    source=”./FLAR/FLARPattern.pat”,
    in Line 43

    Similarly at Line 46
    Replace:
    source=”./assets/FLAR/FLARCameraParameters.dat”,
    with
    source=”./FLAR/FLARCameraParameters.dat”,

  15. Ruyahcreatives Says:

    Hi Gordon,
    After the line 43 and line 46 are solved you’ll also come across errors(I had 37 errors) like
    “Definition org.papervision3d.view:Viewport3D could not be found.”
    “type was not found or was not a compile-time constant: FLARCamera3D.”

    It’s because the code can’t find the folder org and it’s sub folders for the following lines:
    import org.libspark.flartoolkit.core.FLARCode;
    import org.libspark.flartoolkit.core.param.FLARParam;
    ..
    ..
    import org.papervision3d.scenes.Scene3D;
    import org.papervision3d.view.Viewport3D;

    To solve these, download FlarToolKit(I used version 2.5.4)from http://www.libspark.org/wiki/saqoosha/FLARToolKit/downloadlist

    unzip it.Inside src folder you’ll find the org folder.Copy this org folder to the root directory where you have saved the .fla file.

    You’ll also need to copy the ascollada and papervision3d folders inside the libs folder of the FlarToolKit to the org folder of your project.

    You can just keep the classes that are defined in the import codes and delete the one’s that are not required.

    Hope this helps.

  16. Ruyahcreatives Says:

    Hi Gordon,

    In line 43 replace
    source=”./assets/FLAR/FLARPattern.pat”,
    with
    source=”./FLAR/FLARPattern.pat”,

    In line 46 replace
    source=”./assets/FLAR/FLARCameraParameters.dat”,
    with
    source=”./FLAR/FLARCameraParameters.dat”,

  17. Jeff Kit Says:

    Everything looks ok on my side, no error message nothing!!

    But When it comes time to test my swf file, I just have a blank square and my webcam doesn’t start

    My webcam works with other application on my mac, no physical issue with it.

    I’m using a trial version of Flash CS5…

    Thanks

    JF

  18. Robert Erskine Says:

    Hey Jeff,

    I think I have a resolution to your problem. Right click on this blank square you’re seeing and select settings from the drop down menu. Click on the camera on the right hand side, and just play around with your input camera. It varies from machine to machine. Let me know if that works out for you, else I’ll be glad to help you.

    Rob

  19. jhony Says:

    Hi ,
    I am getting this error and then my webcam freezes out. Can anybody tell me the reason behind it and what can be possible solution??

    Error: Error #2032: Stream Error. URL: file:///C|/Documents%20and%20Settings/sharmaas/Adobe%20Flash%20Builder%204/FLARTest/bin%2Ddebug/
    at com.transmote.flar.tracker::FLARToolkitManager/onCameraParamsLoadError()[C:\Documents and Settings\sharmaas\Desktop\AR\Actionscripts\com\transmote\flar\tracker\FLARToolkitManager.as:343]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at flash.net::URLLoader/onComplete()

  20. jude Says:

    hi good tutorial BUT it just wont work with any other 3d model
    can i ask did anyone get this working with any other 3d model and also any tips on how to debug this part or what to try
    thanks
    jude

  21. Robert Erskine Says:

    Hey Jude,

    Try messing around with the x,y,z coordinates in the .as file. the source location varies between model to model.

    hope this helps,
    Rob

  22. Robert Erskine Says:

    Maybe try to adjust the framerate in the .dat file, might be causing your camera to freeze. let me know if this helps,
    Rob

Leave a Reply

Get Involved

Intersted in joining the group? Enter your email below and we will contact you about getting involved.

Calendar

Contact