Webカメラ空間上にオブジェクトを配置
せっかくWiiで傾きとか検出できるので、カメラにWiiコンをつけて遊んでみました。
まだまだ、精度や機能が足りませんが、面白いアプリができるかも?
こんな感じに配置
今回はPaperVision3Dのプレーン自体を回転させているため、手抜きです。本来ならPaperVision3Dのカメラを動かす方がオブジェクトが増えた時に楽ちんだと思います。
動作ムービーはこちら
http://jp.youtube.com/watch?v=Y9SLh2HZXco
今後はオブジェの移動でなくカメラの移動やオブジェクトの追加などやってみたいなぁ
ソースコードはこちら。
WiiFlash Server , PaperVision3Dが必要になります。
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()"> <mx:Script> <![CDATA[ import org.wiiflash.Wiimote; import org.papervision3d.scenes.*; import org.papervision3d.objects.*; import org.papervision3d.cameras.*; import org.papervision3d.materials.*; import org.papervision3d.core.Matrix3D; import caurina.transitions.Tweener; private var container:Sprite; private var scene:MovieScene3D; private var camera3D:Camera3D; private var camera:Camera; private var wiimote:Wiimote = new Wiimote(); //初期化関数 private function init():void{ setWiiCon(); setCamera(); setStage3D(); } //Wiiコントローラの作成 private function setWiiCon():void{ wiimote.connect(); } //カメラの作成 private function setCamera():void{ //カメラの設定 camera = Camera.getCamera(); if (camera != null) { camera.setQuality(0, 95); camera.setMode(640 , 480 , 15 , true ); camera.setMotionLevel(0); myFlv.attachCamera( camera ); } else { trace("You need a camera."); } } //3Dステージの作成 private function setStage3D():void{ container = new Sprite(); container.x = myFlv.width/2; container.y = myFlv.height/2 myImage.addChild( container ); scene = new MovieScene3D( container ); camera3D = new Camera3D(); camera3D.focus = 300; camera3D.zoom = 2; setPlane(); var timer:Timer = new Timer(20); timer.addEventListener(TimerEvent.TIMER , render ); timer.start() } //テクスチャなプレーン作成 private var plane:Plane; private function setPlane():void{ var material:BitmapFileMaterial = new BitmapFileMaterial( "image.jpg" ); material.doubleSided = true; plane = new Plane( material , 300 , 300 , 1 , 1 , null ); scene.addChild( plane ); plane.container.alpha = 0.5; } //wiiコン用変数 private var mx:Number =0; private var my:Number =0; private var mz:Number =0; private var x1:Number = 0; private var y1:Number = 0; private var x2:Number = 0; private var y2:Number = 0; //レンダー private function render(e:Event):void{ //プレーンを回転 if( wiimote.sensorX*90 != 0){ mx = wiimote.sensorX*90; } if( wiimote.sensorY*90 != 0){ my = wiimote.sensorY*90; } if( wiimote.sensorZ*90 != 0){ mz = wiimote.sensorZ*90; } plane.rotationX = mx; plane.rotationY = my; plane.rotationZ = mz; //プレーンを移動 if( wiimote.ir.x1 * 100 != 0 ){ x1 = wiimote.ir.x1 * 100; } if( wiimote.ir.y1 * 100 != 0 ){ y1 = wiimote.ir.y1 * 100; } if( wiimote.ir.x2 * 100 != 0 ){ x2 = wiimote.ir.x2 * 100; } if( wiimote.ir.y2 * 100 != 0 ){ y2 = wiimote.ir.y2 * 100; } plane.x = x1*15 - myFlv.width; plane.y = y1*15 - myFlv.height + 200; myLog.text = "rx=>" + mx + "\nry=>" + my + "\nrz=>" + mz + "\n\n" ; myLog.text += "x1=>" + x1 + "\ny1=>" + y1 + "\nx2=>" + x2 + "\ny2=>" + y2 + "\n"; //レンダリング scene.renderCamera(camera3D); } ]]> </mx:Script> <mx:VideoDisplay x="10" y="25" width="686" height="515" id="myFlv"/> <mx:Image x="0" y="10" width="709" height="542" id="myImage"/> <mx:TextArea x="827" y="10" width="254" height="515" id="myLog" backgroundAlpha="0.1"/> </mx:Application>