読者です 読者をやめる 読者になる 読者になる

FlexでのAR

Flexで簡単にARできないかなぁってことで遊んでます。
こんな感じ

FLARのマトリックスをFlash10のマトリックスに入れてる感じでっす。
なんかうまーくいかないけど、とりあえずはなんちゃってって感じでできまっす。
main.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*"
     creationComplete="init()" width="640" height="480" focusEnabled="true">
<mx:Script>
<![CDATA[
import org.libspark.LiveChromaKey.LCK_Core;
import mx.effects.Move;
import mx.effects.easing.*;
private function init():void{
    //パースペクティブの変更
    var pp:PerspectiveProjection = new PerspectiveProjection();
    //中心に設定
    pp.projectionCenter = new Point( this.width/2 , this.height/2);
    pp.fieldOfView = 1.2;
    myImage.transform.perspectiveProjection = pp;
}
]]>
</mx:Script>
<local:SimpleARFlexComp id="simpleAR" width="{myUi.width}" height="{myUi.height}" activate="myUi.addChild( simpleAR );"/>
<mx:UIComponent id="myUi" width="640" height="480" x="0" y="0" verticalCenter="0" horizontalCenter="0"/>
<mx:Canvas  width="640" height="480" clipContent="false" borderThickness="2" borderStyle="solid" borderColor="0xff0000">
    <mx:Canvas id="myScene"  width="640" height="480" x="{this.width/2}" y="{this.height/2}" clipContent="false"
        borderThickness="2" borderStyle="solid" borderColor="0xff0000">
        <mx:Canvas id="myImage" width="1" height="1" clipContent="false" borderThickness="2" borderStyle="solid" borderColor="0xff0000">
            <mx:Image  source="7894.jpg" horizontalCenter="0" verticalCenter="0" scaleX="0.2" scaleY="0.2" />
        </mx:Canvas>
    </mx:Canvas>
</mx:Canvas>
<mx:TextArea id="myLog"/>
</mx:Application>

SimpleARFlexComp.as

package
{
    import __AS3__.vec.Vector;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.events.Event;
    import flash.geom.Rectangle;
    import flash.media.Camera;
    import flash.media.Video;
    import flash.net.URLRequest;
    import flash.utils.ByteArray;
    import mx.core.Application;
    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.core.math.Matrix3D;
    import org.papervision3d.materials.MovieMaterial;
    import org.papervision3d.objects.primitives.Plane;
    import org.papervision3d.view.BasicView;
    [SWF(width="640", height="480", backgroundColor="#ffffff", frameRate="30")]
    public class SimpleARFlexComp extends BasicView
    {
        [Embed(source="../assets/flar/camera_para.dat", mimeType="application/octet-stream")]
        private var CameraParameters:Class;
        [Embed(source="../assets/flar/papervision3d_marker.pat", mimeType="application/octet-stream")]
        private var MarkerPattern:Class;
        private static const WIDTH:Number = 640;
        private static const HEIGHT:Number = 480;
        private static const FRAMES_PER_SECOND:int = 60;
        private var cameraParameters:FLARParam;
        private var markerPattern:FLARCode;
        private var raster:FLARRgbRaster_BitmapData;
        private var detector:FLARSingleMarkerDetector;
        private var webCam:Camera;
        private var webcamDisplay:Video;
        private var capture:BitmapData;
        private var flarCamera3D:FLARCamera3D;
        private var flarBase:FLARBaseNode;
        private var transformationResult:FLARTransMatResult;
        private var plane:Plane;
        private var loader:Loader;
        public function SimpleARFlexComp():void
        {
            super(WIDTH * 2, HEIGHT * 2, false);
                        init();
        }
        private function init():void
        {
            setupFlarCamera();
            setupFlarMarker();
            setupWebcam();
            setupWebcamDisplay();
            setupFlar();
            setupPapervision3D();
            startRendering();
        }
        private function setupFlarCamera():void
        {
            cameraParameters = new FLARParam();
            cameraParameters.loadARParam(new CameraParameters() as ByteArray);
        }
        private function setupFlarMarker():void
        {
            markerPattern = new FLARCode(16, 16);
            markerPattern.loadARPatt(new MarkerPattern());
        }
        private function setupWebcam():void
        {
            webCam = Camera.getCamera();
            webCam.setMode(WIDTH / 2, HEIGHT / 2, FRAMES_PER_SECOND);
        }
        private function setupWebcamDisplay():void
        {
            webcamDisplay = new Video();
            webcamDisplay.width = WIDTH;
            webcamDisplay.height = HEIGHT;
            webcamDisplay.attachCamera(webCam);
            addChildAt(webcamDisplay, getChildIndex(viewport));
            capture = new BitmapData(webcamDisplay.width, webcamDisplay.height, false, 0x0);
            capture.draw(webcamDisplay);
            raster = new FLARRgbRaster_BitmapData(capture);
            detector = new FLARSingleMarkerDetector(cameraParameters, markerPattern, 80);
        }
        private function setupFlar():void
        {
            flarCamera3D = new FLARCamera3D(cameraParameters);
            flarBase = new FLARBaseNode();
            scene.addChild(flarBase);
            transformationResult = new FLARTransMatResult();
        }
        private function setupPapervision3D():void
        {
            loader =new Loader;
            loader.width = 320;
            loader.height = 240;
            loader.load(new URLRequest("../assets/flex/SimpleFlexPanel.swf"));
            this.viewport.interactive = true;
            var material:MovieMaterial = new MovieMaterial(loader, true, true, true, new Rectangle(0, 0, 320, 420));
            material.interactive =true;
            material.smooth = true;
            material.doubleSided = true;
            viewport.interactive = true;
            viewport.buttonMode = true;
            plane = new Plane(material, WIDTH, HEIGHT);
            plane.rotationY = 180;
            plane.scale = .31;
//            flarBase.addChild(plane);
        }
        override protected function onRenderTick(event:Event = null):void
        {
            capture.draw(webcamDisplay);
            if (detector.detectMarkerLite(raster, 80) && detector.getConfidence() > 0.5)
            {
                detector.getTransformMatrix(transformationResult);
//                flarBase.setTransformMatrix(transformationResult);
                var m:Matrix3D = flarBase.transform;
                var r:FLARTransMatResult = transformationResult;
                var speed:Number = .4;
                var vertices:Vector.<Number> = new Vector.<Number>();
                vertices.push(r.m00);
                vertices.push(r.m10);
                vertices.push(r.m20);
                vertices.push(0);
                vertices.push(r.m01);
                vertices.push(r.m11);
                vertices.push(r.m21);
                vertices.push(0);
                vertices.push(r.m02);
                vertices.push(r.m12);
                vertices.push(r.m22);
                vertices.push(0);
                vertices.push(r.m03);
                vertices.push(r.m13);
//                vertices.push(-r.m23/100);
                vertices.push(0);
                vertices.push(1);
                var mat:flash.geom.Matrix3D = new flash.geom.Matrix3D();
                mat.rawData = vertices;
                Application.application.myImage.transform.matrix3D = mat;
                Application.application.myLog.text ="" + Application.application.myImage.z;
                Application.application.myImage.x = 640/2 + Application.application.myImage.x *2;
                Application.application.myImage.y = 480/2 + Application.application.myImage.y *2;
                m.n11 +=  (r.m01 - m.n11) * speed;
                m.n12 +=  (r.m00 - m.n12) * speed;
                m.n13 +=  (r.m02 - m.n13) * speed;
                m.n14 +=  (r.m03 - m.n14) * speed;
                m.n21 += (-r.m11 - m.n21) * speed;
                m.n22 += (-r.m10 - m.n22) * speed;
                m.n23 += (-r.m12 - m.n23) * speed;
                m.n24 += (-r.m13 - m.n24) * speed;
                m.n31 +=  (r.m21 - m.n31) * speed;
                m.n32 +=  (r.m20 - m.n32) * speed;
                m.n33 +=  (r.m22 - m.n33) * speed;
                m.n34 +=  (r.m23 - m.n34) * speed;
            }
            renderer.renderScene(scene, flarCamera3D, viewport);
        }
    }
}