[Stage3D] Starlingを試すのだ! (27) ~Sprite3D〜

Flash Player 11 から採用された Stage3D を学習するよ。 :bouzu:
Starling Framework を試しちゃうんだからね。 :girl:

starling327

Starling1.6から追加されたSprite3Dクラスを使ってみちゃうよ。 :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 starling.core.Starling;

  [SWF(backgroundColor="#FFFFFF", width="600", height="400", frameRate="60")]

  public class Main extends Sprite {
    // プロパティ
    private var starling:Starling;

    // コンストラクタ
    public function Main() {
      stage.scaleMode = StageScaleMode.NO_SCALE;
      stage.align = StageAlign.TOP_LEFT;
      init();
    }

    // メソッド
    private function init():void {
      starling = new Starling(MainView, stage, null, null);
      starling.enableErrorChecking = true;
      starling.start();
    }

  }

}



import flash.geom.Vector3D;
import starling.core.Starling;
import starling.display.Sprite;
import starling.display.Sprite3D;
import starling.display.Image;
import starling.events.Event;
import starling.events.EnterFrameEvent;


internal class MainView extends Sprite {
  [Embed(source="assets/cards/piyo.png")]
  private var FrontImage:Class;
  [Embed(source="assets/cards/paper.png")]
  private var BackImage:Class;
  // プロパティ
  private var container:Sprite3D;
  private var front:Image;
  private var back:Image;
  private var position:Vector3D = new Vector3D();
  private static var radian:Number = Math.PI/180;

  // コンストラクタ
  public function MainView() {
    addEventListener(Event.ADDED_TO_STAGE, init);
    addEventListener(Event.REMOVED_FROM_STAGE, remove);
  }

  // メソッド
  private function init(evt:Event):void {
    removeEventListener(Event.ADDED_TO_STAGE, init);
    stage.fieldOfView = 60*radian;
    stage.focalLength = 500;

    container = new Sprite3D();
    addChild(container);
    container.x = 300;
    container.y = 200;

    front = Image.fromBitmap(new FrontImage());
    container.addChild(front);
    front.pivotX = 100;
    front.pivotY = 100;

    back = Image.fromBitmap(new BackImage());
    container.addChild(back);
    back.pivotX = 100;
    back.pivotY = 100;

    back.visible = false;
    addEventListener(EnterFrameEvent.ENTER_FRAME, update);
  }
  private function remove(evt:Event):void {
    removeEventListener(Event.REMOVED_FROM_STAGE, remove);
  }
  override public function dispose():void {
    removeEventListener(Event.ADDED_TO_STAGE, init);
    removeEventListener(Event.REMOVED_FROM_STAGE, remove);
    super.dispose();
  }
  private function update(evt:EnterFrameEvent):void {
    container.rotationX -= 1.0*radian;
    container.rotationY += 1.5*radian;
    stage.getCameraPosition(container, position);
    var direction:Boolean = position.z < 0;
    front.visible = direction;
    back.visible = !direction;
  }

}


[修正] (14/12/25 Thu 14:10)
Sprite3D の使用数を減らした。