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

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

starling328

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 starling.core.Starling;
import starling.display.Sprite;
import starling.display.Sprite3D;
import starling.events.Event;
import starling.events.EnterFrameEvent;


internal class MainView extends Sprite {
  // プロパティ
  private var container:Sprite3D;
  private var objects:Array;
  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;

    var right:Object3D = new Object3D("right", 0xCC0066);
    container.addChild(right);
    right.rotationY = - 90*radian;
    right.x = 80;

    var left:Object3D = new Object3D("left", 0xFF6699);
    container.addChild(left);
    left.rotationY = 90*radian;
    left.x = - 80;

    var bottom:Object3D = new Object3D("bottom", 0x669900);
    container.addChild(bottom);
    bottom.rotationX = 90*radian;
    bottom.y = 80;

    var top:Object3D = new Object3D("top", 0x99CC00);
    container.addChild(top);
    top.rotationX = - 90*radian;
    top.y = - 80;

    var back:Object3D = new Object3D("back", 0xFF9900);
    container.addChild(back);
    back.rotationY = 180*radian;
    back.z = 80;

    var front:Object3D = new Object3D("front", 0xFFCC00);
    container.addChild(front);
    front.z = - 80;

    objects = [right, left, bottom, top, back, front];
    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;
    var n:uint;
    for (n = 0; n < 6; n++) {
      var object:Object3D = objects[n];
      stage.getCameraPosition(object, object.position);
      object.update();
    }
    objects.sortOn("depth", Array.DESCENDING | Array.NUMERIC);
    for (n = 0; n < 6; n++) {
      container.addChild(objects[n]);
    }
  }

}


import flash.geom.Vector3D;
import starling.display.Sprite3D;
import starling.display.Quad;

import starling.controls.Label;

internal class Object3D extends Sprite3D {
  // プロパティ
  private var txt:String = "";
  private var color:uint = 0xFFFFFF;
  private var quad:Quad;
  private var label:Label;
  public var position:Vector3D = new Vector3D();
  public var depth:Number = 0;

  // コンストラクタ
  public function Object3D(t:String = "", c:uint = 0xFFFFFF) {
    txt = t;
    color = c;
    init();
  }

  // メソッド
  private function init():void {
    quad = new Quad(160, 160, color);
    quad.pivotX = 80;
    quad.pivotY = 80;
    addChild(quad);
    label = new Label(160, 40, 40, Label.CENTER);
    label.pivotX = 80;
    label.pivotY = 20;
    addChild(label);
    label.textColor = 0xFFFFFF;
    label.text = txt;
  }
  public function update():void {
    depth = position.z;
  }

}