2009年11月10日火曜日

[Away3D]Away3DでShader試してみた[AS3.0]

前回言ってた続きです。

Papervision3DでUVマッピングを使用したColladaがうまく読み込めなかったので、Away3Dで試してみました。
Away3Dは検索しても解説してくれているところがあまりないので、サンプルファイルを参考にしながら(というかコピーして)、UVマッピングを使用したColladaを表示してみました。
それでできたものがこれ

Pv3Dで表示されてた黒い線が消えてるじゃないですかー。
ぅわーい。

あ、Away3DはこちらにあるようにShaderの種類がPv3Dより少ないので、PhongShaderを使ったんですが、非常に重かったのでモデルデータを作り直しました。

というわけで全然完成してないけどソースコード公開するよ。
package {
 import away3d.cameras.HoverCamera3D;
 import away3d.containers.ObjectContainer3D;
 import away3d.containers.Scene3D;
 import away3d.containers.View3D;
 import away3d.core.base.Mesh;
 import away3d.core.utils.Cast;
 import away3d.lights.DirectionalLight3D;
 import away3d.loaders.Collada;
 import away3d.materials.PhongBitmapMaterial;
 
 import flash.display.Sprite;
 import flash.display.StageQuality;
 import flash.events.Event;
 import flash.events.MouseEvent;

 public class away3 extends Sprite {

  private var scene:Scene3D;
  private var camera:HoverCamera3D;
  private var view:View3D;

  //[Embed(source="mat.png")]
  //public var materialImage:Class;
  //[Embed(source="sample.dae", mimeType="application/octet-stream")]
  //public var colladaData:Class;
  [Embed(source="mate.png")]
  public var materialImage:Class;
  [Embed(source="test.dae", mimeType="application/octet-stream")]
  public var colladaData:Class;

  private var material:PhongBitmapMaterial;
  private var collada:Collada;
  private var model:ObjectContainer3D;
  private var mesh:Mesh;
  private var light:DirectionalLight3D;

  private var isMouseDown:Boolean;
  //navigation variables
  private var lastPanAngle:Number;
  private var lastTiltAngle:Number;
  private var lastMouseX:Number;
  private var lastMouseY:Number;

  public function away3() {
   initEngine();
   initLights();
   initMaterials();
   initObjects();
   initListeners();

   isMouseDown = false;
  }

  /**
   * Initialise the engine
   */
  private function initEngine():void {
   scene = new Scene3D();
   camera = new HoverCamera3D();
   camera.zoom = 1;
   camera.focus = 100;
   camera.distance = 80;
   camera.targetpanangle = camera.panangle = -10;
   camera.targettiltangle = camera.tiltangle = 20;

   view = new View3D({camera: camera, scene: scene});
   //view.session = new BitmapRenderSession(1);

   //view.mouseZeroMove = true;
   addChild(view);

   stage.quality = StageQuality.HIGH;
   //stage.quality = StageQuality.LOW;
  }

  /**
   * Initialise the lights
   */
  private function initLights():void {

   //light = new DirectionalLight3D({color:0xFFFFFF, ambient:0.25, diffuse:0.75, specular:0.9, x:40000, y:40000, z:40000});
   light = new DirectionalLight3D();
   light.color = 0xFFFFFF;
   light.ambient = 0.25;
   light.diffuse = 0.75;
   light.specular = 0.9;
   light.x = 0;
   light.y = 1000;
   light.z = 1000;

   scene.addChild(light);
  }

  /**
   * Initialise the materials
   */
  private function initMaterials():void {
   material = new PhongBitmapMaterial(Cast.bitmap(materialImage));
   material.specular = 0.5;
  }

  /**
   * Initialise the scene objects
   */
  private function initObjects():void {
   //model1 = Collada.parse(Charmesh, {scaling:10, material:material, mouseEnabled:false});
   collada = new Collada();
   //collada.scaling = 0.1;
   model = collada.parse(colladaData) as ObjectContainer3D;
   model.materialLibrary.getMaterial("mat1").material = material;
   model.mouseEnabled = false;

   scene.addChild(model);

   //mesh = model.getChildByName("obj1-node") as Mesh;
  }

  /**
   * Initialise the listeners
   */
  private function initListeners():void {
   addEventListener(Event.ENTER_FRAME, onEnterFrame);
   stage.addEventListener(Event.RESIZE, onResize);
   stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
   stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
   //stage.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
   onResize();
  }

  /**
   * Navigation and render loop
   */
  private function onEnterFrame(event:Event):void {
   //update the camera position
   //camera.moveTo(0, 70, -10);
   if(isMouseDown) {
    camera.targetpanangle = 0.3 * (stage.mouseX - lastMouseX) + lastPanAngle;
    camera.targettiltangle = 0.3 * (stage.mouseY - lastMouseY) + lastTiltAngle;
   }
   camera.hover();

   //render scene
   view.render();
  }

  private function onMouseDown(event:MouseEvent):void {
   lastPanAngle = camera.targetpanangle;
   lastTiltAngle = camera.targettiltangle;
   lastMouseX = stage.mouseX;
   lastMouseY = stage.mouseY;
   isMouseDown = true;
   stage.addEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
  }

  private function onMouseUp(event:MouseEvent):void {
   isMouseDown = false;
   stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
  }
  
  /*private function onMouseWheel(e:MouseEvent):void {
   if(camera.distance > 20) {
    camera.distance -= e.delta * 1.5;
   }
  }*/

  private function onStageMouseLeave(event:Event):void {
   isMouseDown = false;
   stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
  }

  /**
   * stage listener for resize events
   */
  private function onResize(event:Event = null):void {
   view.x = stage.stageWidth / 2;
   view.y = stage.stageHeight / 2;
  }
 }
}

0 件のコメント:

コメントを投稿