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

みくと一緒にふりふり

はちゅねと一緒にふりふりしてみました。
WiiFlashとFLARのコンボです。
こんな感じ

DAE形式のパーツを取得する方法。

var arm3d:DisplayObject3D  = simpleAR.dae.children[ DAE.ROOTNODE_NAME ].children["R_arm"];

覚えておいて損はないと思います。
んで、なんか右手とねぎを一緒に回したかったんだけどできなかったのであきらめ。
ソースはこちら。
main.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
    xmlns:local="*" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#FFFFFF, #D1D1D1]" >
<mx:Script>
<![CDATA[
import org.papervision3d.objects.parsers.DAE;
import org.papervision3d.objects.DisplayObject3D;
import org.wiiflash.Wiimote;
import org.wiiflash.events.ButtonEvent;
private var wiimote:Wiimote;
private function setWiimote():void{
    wiimote = new Wiimote();
    wiimote.addEventListener(Event.CONNECT , onWiimoteReady );
    wiimote.connect();
}
private var arm3d:DisplayObject3D;
private var negi3d:DisplayObject3D;
private function onReadyAR():void{
    simpleAR.dae.rotationX = -270;
    simpleAR.dae.rotationY = 0;
    simpleAR.dae.rotationZ = 90;
    simpleAR.dae.x         = 50;
    simpleAR.dae.scale     = 10;
    arm3d  = simpleAR.dae.children[ DAE.ROOTNODE_NAME ].children["R_arm"];
    negi3d = simpleAR.dae.children[ DAE.ROOTNODE_NAME ].children["negi"];
    negi3d.visible = false;
    myUi.addChild( simpleAR );
       arm3d.addChild( negi3d );
    setWiimote();
}
private function onWiimoteReady(e:Event):void{
    this.addEventListener( Event.ENTER_FRAME, onEnterFrame );
}
private function onEnterFrame( e:Event ):void{
    arm3d.rotationX = -20 + Math.round( wiimote.pitch * 180 / Math.PI );
    negi3d.transform = arm3d.transform;
}
]]>
</mx:Script>
<local:SimpleARFlexComp id="simpleAR" width="{myUi.width}" height="{myUi.height}" activate="onReadyAR();"/>
<mx:UIComponent id="myUi" width="320" height="240" x="10" y="10" scaleX="3" scaleY="3"/>
</mx:Application>

SimpleARFlexComp.as

package
{
    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 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.parsers.DAE;
    import org.papervision3d.objects.primitives.Plane;
    import org.papervision3d.view.BasicView;
    [SWF(width="640", height="480", backgroundColor="#ffffff", frameRate="24")]
    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 = 24;
        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();
        }
        public var dae:DAE = new DAE();
        private function setupPapervision3D():void
        {
            dae = new DAE();
            dae.load("assets/negimiku.dae");
            flarBase.addChild( dae );
        }
        override protected function onRenderTick(event:Event = null):void
        {
            capture.draw(webcamDisplay);
            if (detector.detectMarkerLite(raster, 80) && detector.getConfidence() > 0.5)
            {
                detector.getTransformMatrix(transformationResult);
                var m:Matrix3D = flarBase.transform;
                var r:FLARTransMatResult = transformationResult;
                var speed:Number = .4;
                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);
        }
    }
}