[AIR3.0] モーション・センサー

AIR for iOS で、モーション・センサーを試してみるよ。 :bouzu:

sensor_accelerometer

Accelerometerクラス を用いて、加速度を取得するよ。 :boy:

デバイスの傾きが計算できるよ。ホントかな? 😕

:caution: 要 Flash Player 11.8 以上

This movie requires Flash Player 11.8.0

Main.as
package {

  import flash.display.Sprite;
  import flash.display.StageScaleMode;
  import flash.display.StageAlign;
  import flash.system.System;
  import flash.events.Event;
  import flash.events.MouseEvent;
  import flash.display.BitmapData;
  import flash.geom.Matrix;
  import flash.geom.Vector3D;

  import flash.sensors.Accelerometer;
  import flash.events.AccelerometerEvent;

  import away3d.Away3D;
  import away3d.containers.View3D;
  import away3d.containers.Scene3D;
  import away3d.cameras.Camera3D;
  import away3d.lights.DirectionalLight;
  import away3d.containers.ObjectContainer3D;
  import away3d.entities.Mesh;
  import away3d.primitives.CubeGeometry;
  import away3d.materials.TextureMaterial;
  import away3d.textures.BitmapTexture;
  import away3d.primitives.PlaneGeometry;
  import away3d.primitives.ConeGeometry;
  import away3d.materials.ColorMaterial;
  import away3d.materials.lightpickers.StaticLightPicker;
  import away3d.primitives.WireframeSphere;
  import away3d.utils.Cast;


  [SWF(backgroundColor="#333333", width="480", height="640", frameRate="60")]

  public class Main extends Sprite {
    // プロパティ
    private var bw:uint = 480;
    private var bh:uint = 640;
    private var view:View3D;
    private var scene:Scene3D;
    private var camera:Camera3D;
    private var light:DirectionalLight;
    private var container:ObjectContainer3D;
    private var mobile:Mesh;
    private var pointer:Mesh;
    private static var radius:uint = 500;
    private static var center:Vector3D = new Vector3D();
    private static var radian:Number = Math.PI/180;
    private var accelerometer:Accelerometer;
    private var accelerationX:Number = 0;
    private var accelerationY:Number = 0;
    private var accelerationZ:Number = 1;
    private var acceleration:Vector3D = new Vector3D(0, 0, 1);
    private var position:Vector3D = new Vector3D();
    private static var deceleration:Number = 0.3;

    // コンストラクタ
    public function Main() {
      System.pauseForGCIfCollectionImminent(1);
      init();
    }

    // メソッド
    private function init():void {
      if (Accelerometer.isSupported) {
        accelerometer = new Accelerometer();
        accelerometer.setRequestedUpdateInterval(50);
        accelerometer.addEventListener(AccelerometerEvent.UPDATE, accelerate, false, 0, true);
      }

      view = new View3D();
      scene = view.scene;
      camera = view.camera;

      addChild(view);
      light = new DirectionalLight();
      scene.addChild(light);

      controller = new HoverController(camera, null, angle, degree);
      setup();
      initialize();
      addEventListener(Event.ENTER_FRAME, render, false, 0, true);
    }
    private function locate(evt:GeolocationEvent):void {
      if (!isNaN(evt.latitude)) latitude = evt.latitude;
      if (!isNaN(evt.longitude)) longitude = evt.longitude;
      if (!isNaN(evt.heading)) heading = int(evt.heading);
    }
    private function setup():void {
      view.backgroundColor = 0x333333;
      view.antiAlias = 4;
      camera.x = 0;
      camera.y = 0;
      camera.z = - radius;
      light.direction = new Vector3D(1, -1, 1);
      light.ambient = 0.5;
      light.diffuse = 0.8;
      light.specular = 0.2;

    }
    private function initialize():void {
      container = new ObjectContainer3D();
      scene.addChild(container);

      var lightPicker:StaticLightPicker = new StaticLightPicker([light]);
      var geometry:CubeGeometry = new CubeGeometry(192, 32, 256, 1, 1, 1, true);
      var material:TextureMaterial = new TextureMaterial(new BitmapTexture(tile()), true, false, true);

      material.lightPicker = lightPicker;
      mobile = new Mesh(geometry, material);
      container.addChild(mobile);

      mobile.pitch(90);
      var plane:PlaneGeometry = new PlaneGeometry(168, 224, 1, 1, true, false);
      var cmaterial:ColorMaterial = new ColorMaterial(0x666666, 1);

      caterial.lightPicker = lightPicker;
      var screen:Mesh = new Mesh(plane, cmaterial);
      container.addChild(screen);

      screen.z = -16.2;
      screen.y = 6;
      screen.pitch(-90);
      var _geometry:ConeGeometry = new ConeGeometry(6, 24, 8, 1, true, true);
      var _material:ColorMaterial = new ColorMaterial(0xFF0000, 1);

      _material.lightPicker = lightPicker;
      pointer = new Mesh(_geometry, _material);
      container.addChild(pointer);

      pointer.z = -28;
      pointer.pitch(90);
      var frame:WireframeSphere = new WireframeSphere(164, 16, 12, 0xCCCCCC, 0.5);
      scene.addChild(frame);

      frame.pitch(90);
    }
    private function render(evt:Event):void {
      direction();
      camera.lookAt(center);
      view.render();
    }
    private function direction():void {
      acceleration.x += (accelerationX - acceleration.x)*deceleration;
      acceleration.y += (accelerationY - acceleration.y)*deceleration;
      acceleration.z += (accelerationZ - acceleration.z)*deceleration;
      acceleration.normalize();
      position.x = radius*acceleration.x;
      position.y = radius*acceleration.y;
      position.z = - radius*acceleration.z;

      camera.position = position;
    }
    private function tile():BitmapData {
      var bitmapData:BitmapData = new BitmapData(512, 512, false, 0xFF000000);
      var bottom:Matrix = new Matrix();
      bottom.translate(0, 0);
      var top:Matrix = new Matrix();
      top.translate(171, 0);
      var front:Matrix = new Matrix();
      front.translate(341, 0);
      var back:Matrix = new Matrix();
      back.translate(171, 256);
      var left:Matrix = new Matrix();
      left.translate(0, 256)
      var right:Matrix = new Matrix();
      right.translate(341, 256);
      bitmapData.draw(new BitmapData(171, 256, false, 0xFFFFFFFF), bottom);
      bitmapData.draw(new BitmapData(171, 256, false, 0xFFFFFFFF), top);
      bitmapData.draw(new BitmapData(171, 256, false, 0xFF999999), front);
      bitmapData.draw(new BitmapData(171, 256, false, 0xFFCCCCCC), back);
      bitmapData.draw(new BitmapData(171, 256, false, 0xFF999999), left);
      bitmapData.draw(new BitmapData(171, 256, false, 0xFFCCCCCC), right);
      return bitmapData;
    }

  }

}

Flash CC + Flex 4 SDK でパブリッシュ。


[修正] (14/01/12 Sun 16:40)
参考資料「Away3D Forum HoverController with infinite tilt
HoverControllerJB.as を参考に FullHoverController.as を作成。(away3d.controllers.FullHoverController.as)
デモ版で、ドラッグして全方位に回転できるように変更。ただし、デバイス版でも、全方位に回転するが、挙動がおかしい。 :rain:
[修正] (14/01/12 Sun 17:00)
3Dオブジェクトとしてデバイス画面を追加。