root/as3/FLARToolKit/trunk/samples/org/tarotaro/flash/ar/SimpleFLARToolKitView.as

リビジョン 1786, 5.8 kB (コミッタ: tarotarorg, コミット時期: 3 週間 前)

--

Line 
1 package org.tarotaro.flash.ar {
2        
3         import net.saqoosha.flartoolkit.example.ARAppBase;
4         //import org.libspark.flartoolkit.core.FLARTransMatResult;
5         import org.libspark.flartoolkit.core.transmat.FLARTransMatResult;
6         import org.libspark.flartoolkit.pv3d.FLARCamera3D;
7         //import org.libspark.flartoolkit.scene.FLARCamera3D;
8         import flash.events.TimerEvent;
9         import flash.utils.Timer;
10        
11         import flash.display.Sprite;
12         import flash.display.StageQuality;
13         import flash.display.StageScaleMode;
14         import flash.events.Event;
15         import flash.events.MouseEvent;
16        
17         import org.papervision3d.core.math.Matrix3D;
18         import org.papervision3d.lights.PointLight3D;
19         import org.papervision3d.materials.WireframeMaterial;
20         import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
21         import org.papervision3d.materials.utils.MaterialsList;
22         import org.papervision3d.objects.DisplayObject3D;
23         import org.papervision3d.objects.primitives.Cube;
24         import org.papervision3d.objects.primitives.Plane;
25         import org.papervision3d.render.LazyRenderEngine;
26         import org.papervision3d.scenes.Scene3D;
27         import org.papervision3d.view.Viewport3D;
28        
29
30         /**
31          * FLARToolKitのサンプルを、汎用的に使えるよう改造したクラス。
32          * @example <listing version="3.0" >
33          * //画面への表示方法
34          * var ar:SimpleFLARToolKitView = new SimpleFLARToolKitView();
35          * ar.addEventListener(Event.COMPLETE,function(e:Event):void
36          * {
37          *     addChild(ar);
38          *     ar.start();
39          * });
40          * ar.setupAR();
41          *
42          * //モデル変更方法
43          * var wmat:WireframeMaterial = new WireframeMaterial(0xff0000, 1, 2);
44          * wmat.doubleSided = true;
45          * var basePlane:Plane = new Plane(wmat, 80, 80);
46          *
47          * ar.model = basePlane;
48          * </listing>
49          */
50         public class SimpleFLARToolKitView extends ARAppBase {
51                
52                 private static const PATTERN_FILE:String = "Data/cube-basic.pat";
53                 private static const CAMERA_FILE:String = "Data/camera_para.dat";
54                
55                 private var _base:Sprite;
56                 private var _scene:Scene3D;
57                 private var _camera3d:FLARCamera3D;
58                 private var _viewport:Viewport3D;
59                 private var _renderer:LazyRenderEngine;
60                
61                 private var _transGrp:DisplayObject3D;
62                
63                 private var _resultMat:FLARTransMatResult = new FLARTransMatResult();
64                
65                 private var _isMirror:Boolean = false;
66                
67                 /**
68                  * 拡張現実用のカメラ映像表示領域を作成する。
69                  */
70                 public function SimpleFLARToolKitView() {
71                         this.addEventListener(Event.INIT, this._onInit);
72                 }
73                
74                 /**
75                  * カメラ・パターンファイルを読み込み、ARコンテンツの設定を行う。
76                  * 設定完了後、Event.COMPLETEを送出する。
77                  * @param       cameraFile              カメラ設定ファイル
78                  * @param       codeFile                パターンファイル
79                  * @param       canvasWidth             表示領域の幅
80                  * @param       canvasHeight    表示領域の高さ
81                  * @param       codeWidth               パターンの幅
82                  */
83                 public function setupAR(cameraFile:String = CAMERA_FILE,
84                                                                 codeFile:String = PATTERN_FILE,
85                                                                 canvasWidth:int = 320,
86                                                                 canvasHeight:int = 240,
87                                                                 codeWidth:int = 80):void
88                 {
89                         this.init(cameraFile, codeFile, canvasWidth, canvasHeight, codeWidth);
90                 }
91                
92                 /**
93                  * マーカーの認識と3次元モデルの描写を開始する
94                  */
95                 public function start():void
96                 {
97                         this.addEventListener(Event.ENTER_FRAME, this._onEnterFrame);
98                 }
99                
100                 /**
101                  * マーカーの認識と3次元モデルの描写を終了する
102                  */
103                 public function stop():void
104                 {
105                         this.removeEventListener(Event.ENTER_FRAME, this._onEnterFrame);
106                 }
107                
108                 /**
109                  * @private
110                  * カメラ・パターンファイル読み込み完了後の初期化処理
111                  * @param       e
112                  */
113                 private function _onInit(e:Event):void {
114                         this.removeEventListener(Event.INIT, this._onInit);
115                        
116                         this._base = this.addChild(new Sprite()) as Sprite;
117
118                         this._base.addChild(this._capture);
119                        
120                         this._viewport = this._base.addChild(new Viewport3D(this._capture.width, this._capture.height, false, false, false, false)) as Viewport3D;
121                         this._viewport.x = -4; // 4pix ???
122                        
123                         this._camera3d = new FLARCamera3D(this._param);
124                        
125                         this._scene = new Scene3D();
126                         this._transGrp = this._scene.addChild(new DisplayObject3D()) as DisplayObject3D;
127                        
128                         this._renderer = new LazyRenderEngine(this._scene, this._camera3d, this._viewport);
129                        
130                         dispatchEvent(new Event(Event.COMPLETE));
131                 }
132                
133                 /**
134                  * @private
135                  * フレームごとの処理。マーカーを認識し、3次元モデルを描画する。
136                  * @param       e
137                  */
138                 private function _onEnterFrame(e:Event = null):void {
139                         this._capture.bitmapData.draw(this._video);
140                         if (this._detector.detectMarkerLite(this._raster, 80)) {
141                                 trace("confidence:", this._detector.getConfidence()," direction:",this._detector.getDirection());
142                                 if (this._detector.getConfidence() < .5) {
143                                         this._viewport.visible = false;
144                                         return;
145                                 }
146                                 this._detector.getTransformMatrix(this._resultMat);
147                                 var mtx:Matrix3D = this._transGrp.transform;
148                                 mtx.n11 =  this._resultMat.m01; mtx.n12 =  this._resultMat.m00;
149                                 mtx.n13 =  this._resultMat.m02; mtx.n14 =  this._resultMat.m03;
150                                 mtx.n21 = -this._resultMat.m11; mtx.n22 = -this._resultMat.m10;
151                                 mtx.n23 = -this._resultMat.m12; mtx.n24 = -this._resultMat.m13;
152                                 mtx.n31 =  this._resultMat.m21; mtx.n32 =  this._resultMat.m20;
153                                 mtx.n33 =  this._resultMat.m22; mtx.n34 =  this._resultMat.m23;
154                                 this._viewport.visible = true;
155                                 this._renderer.render();
156                         } else {
157                                 this._viewport.visible = false;
158                         }
159                 }
160                
161                 /**
162                  * 表示する3次元モデルを切り替える
163                  */
164                 public function set model(model:DisplayObject3D):void
165                 {
166                         try {
167                                 this._transGrp.removeChildByName("model");
168                         } catch (e:Error) {
169                                 trace("remove失敗");
170                         }
171                         if (model) {
172                                 this._transGrp.addChild(model, "model");
173                         }
174                 }
175         }
176        
177 }
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。