增强现实的基本解释是:在真实环境上实时地附加图形。在Flash中,经常使用一个摄像头和一个标记卡片来实现。当你将有标记的卡片对准摄像头的时候,Flash程序可以检测到这个卡片的基本图形信息并将一个3d模型附加之上。这个技术在flash中实现是非常令人兴奋地。尽管它仍然在开发初期,但随着时间的推移,AR将会很可能在我们体验web的过程中扮演重要的角色。现在是一个踏入这个领域的绝佳时期,最好了解它的工作原理。 围绕着AR有很多的Flash应用,随便在哪都有AR的入门教程(这个在贵国有待发展……)。事实上这篇文章是我用来作为起步学习的,对于初学者的好处在于这个教程已经准备好了一系列封装了FLARToolKit的类文件。同时我们创建了debug模式,当你不用摄像头时用来测试你的标记。对教程的大致浏览可能会令人畏缩,但是我会详细地讲解构建的每一个细节step by step。 然而,如果你不想读过长的教程或者你只想阅读教程的最后部分,我建议下载Final Demo文件来check out。我的代码注释可以使你知道这一切都是如何运做的。 在我们开始之前,有一些术语你需要熟悉: FLARToolKit:我们将要在我们的工程中使用并实现AR的类库。这个类是由Saqoosha编写,以NyARToolKit 2.0.0为基础。现在它已经是开源的。 pv3d:papervision3d的缩写,这个是实时的3d引擎,也是开源项目。 FLARVision:我们将要创建的Demo的名字。 camera_para.dat是FLARToolKit将引用的一个二进制文件,其中记录了摄像头的设置和配置信息。 Marker or Pattern:FLAR将会分析和计算它的图形信息(orientation)。这个图形需要包含黑色边框和内侧的图形。你需要它来使AR工作起来。 CardEmulator:是一个简易的类,当你无法使用摄像头的时候(或者其他的情况下)用来模仿真实的标记卡片。我们将会通过鼠标移动来仿真一个人拿着卡片向着摄像头来回移动。 现在你已经熟悉了这些术语
安装papervision3d和FLARToolKit(略) 当用svn检出http://www.libspark.org/svn/as3/FLARToolKit/trunk时,lib包内是大量的文件夹形式的文件,如果你建立的是flex工程,请下载.swc格式的papervision和FLARToolKit。
Creating our Doc Class(创建我们的文件类):
现在我们要创建一个新的ActionScript工程叫做FLARVision。你需要将papervision和FLARToolKit工程添加到FLARVision的Library Path中。 我们要做的第一件事是构建一个简单的文件类。这个类将是你想要利用papervision和FLARToolKit创建的任何工程的基础。我已经构造了它,所以当你建立自己的FLAR工程的时候便会很容易通过重写核心的方法扩展它。让我们来看一看:
- /**
- * Original Author: Jesse Freeman
- * Class File: FLARVision.as
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- *
- * Licensed under The MIT License
- * Redistributions of files must retain the above copyright notice.
- *
- * Revisions
- * 1.0 Initial version April 29, 2009
- *
- */
- package
- {
- import flash.display.Sprite;
- import flash.display.StageAlign;
- import flash.display.StageQuality;
- import flash.display.StageScaleMode;
- import flash.events.Event;
-
- import org.papervision3d.cameras.Camera3D;
- import org.papervision3d.materials.WireframeMaterial;
- import org.papervision3d.objects.primitives.Plane;
- import org.papervision3d.render.BasicRenderEngine;
- import org.papervision3d.scenes.Scene3D;
- import org.papervision3d.view.Viewport3D;
-
- /**
- *
- * @author Jesse Freeman
- *
- */
- public class FLARVision extends Sprite
- {
- protected var scene:Scene3D;
- protected var camera:Camera3D;
- protected var viewport:Viewport3D;
- protected var renderer:BasicRenderEngine;
-
- /**
- * Main Constructor for the class.
- *
- */
- public function FLARVision()
- {
- configureStage();
- init();
- }
-
- /**
- * Configures the stage and sets the scale mode to keep visuals from
- * distorting when resizing the browser window.
- *
- */
- private function configureStage():void
- {
- stage.quality = StageQuality.HIGH;
- stage.align = StageAlign.TOP_LEFT;
- stage.scaleMode = StageScaleMode.NO_SCALE;
- }
-
- /**
- * Called at the construction of the class. Calls the setup Papervision
- * method and adds an event listener to the render the Viewport.
- *
- */
- protected function init():void {
- setupPapervision();
- addEventListener(Event.ENTER_FRAME, renderViewport);
- }
-
- /**
- * Sets up and configures Papervision. This function can be overridden
- * to accommodate any custom set ups you may need for your project.
- *
- */
- public function setupPapervision():void
- {
- scene = new Scene3D();
- camera = new Camera3D();
-
- // Create the Viewport
- viewport = new Viewport3D(stage.stageWidth, stage.stageHeight);
- addChild(viewport);
-
- create3dObjects();
-
- renderer = new BasicRenderEngine();
- }
-
- /**
- * This default function is where 3d Objects should be added to PV3D's
- * scenes.
- *
- */
- protected function create3dObjects():void
- {
- var plane:Plane = new Plane( new WireframeMaterial(0xff0000) );
- scene.addChild(plane);
- }
-
- /**
- * Renders the papervision scene.
- * @param event
- *
- */
- public function renderViewport(event:Event = null):void
- {
- renderer.renderScene(scene, camera, viewport);
- }
-
- }
- }
复制代码(这个类papervision已经构造好了叫做BasicView,相信经常做pv3d的朋友早就知道。而这个逐帧渲染的方法并不是一个很好的方法,由于pv3d非常占用资源,所以我们有时需要手工地stopRendering()和startRendering())。 (关于papervision3d的基础我不翻译,如果不熟悉的童鞋请移步天地会3d应用)。
转载http://miyaonanhai.blogbus.com/logs/70836695.html
|