2009年11月23日月曜日

[Away3D]UV座標とれた。

最近投稿のタイトルが適当です。

にわからないって言ってたUV座標の取り方がわかりました。
わからないって言ってたのが馬鹿みたいに簡単にできました。

こんなん。
そういえばちょっと前から作ったものをGoogle App Engineのほうに上げるようにしたんですけど問題なく見られますかね?あまり動作確認とかしてないので・・・

ソースコードはこんな感じ。
package {
 import away3d.cameras.HoverCamera3D;
 import away3d.containers.ObjectContainer3D;
 import away3d.containers.Scene3D;
 import away3d.containers.View3D;
 import away3d.core.utils.Cast;
 import away3d.events.MouseEvent3D;
 import away3d.lights.DirectionalLight3D;
 import away3d.loaders.Collada;
 import away3d.materials.MovieMaterial;

 import flash.display.Bitmap;
 import flash.display.MovieClip;
 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="mate.png")]
  public var materialImage:Class;
  [Embed(source="test.dae", mimeType="application/octet-stream")]
  public var colladaData:Class;

  private var material:MovieMaterial;
  private var collada:Collada;
  private var model:ObjectContainer3D;
  private var light:DirectionalLight3D;
  private var mc:MovieClip;

  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() {
   mc = new MovieClip();
   mc.base = mc.addChild(new Bitmap(Cast.bitmap(materialImage)));
   mc.surface = mc.addChild(new Sprite());
   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 = 50;
   camera.steps = 10;
   camera.targetpanangle = camera.panangle = 0;
   camera.targettiltangle = camera.tiltangle = 10;

   view = new View3D({camera: camera, scene: scene});
   addChild(view);

   stage.quality = StageQuality.HIGH;
  }

  /**
   * 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 MovieMaterial(mc);
   material.interactive = true;
  }

  /**
   * 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 = true;
   model.addOnMouseDown(onModelMouseDown);
   scene.addChild(model);
  }

  /**
   * 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
   if(isMouseDown) {
    camera.targetpanangle = 0.35 * (stage.mouseX - lastMouseX) + lastPanAngle;
    camera.targettiltangle = 0.35 * (stage.mouseY - lastMouseY) + lastTiltAngle;
   }
   camera.hover();

   //render scene
   view.render();
  }

  private function onModelMouseDown(e:MouseEvent3D):void {
   mc.surface.graphics.beginFill(0xFFFFFF);
   mc.surface.graphics.drawCircle(e.uv.u * mc.width, mc.height - (e.uv.v * mc.height), 100);
   mc.surface.graphics.endFill();
  }

  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 {
   camera.distance -= e.delta * 1.5;
   if(camera.distance < 0) {
    camera.distance = 0;
   } else if(camera.distance > 70) {
    camera.distance = 70;
   }
  }

  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;
  }
 }
}

UV座標は、
material = new MovieMaterial(mc);
material.interactive = true;
MovieMaterialを使ってinteractiveをtrueにして、
private function onModelMouseDown(e:MouseEvent3D):void {
 mc.surface.graphics.beginFill(0xFFFFFF);
 mc.surface.graphics.drawCircle(e.uv.u * mc.width, mc.height - (e.uv.v * mc.height), 100);
 mc.surface.graphics.endFill();
}
MouseEvent3Dのuvで取得できます。
Y座標は「e.uv.v * mc.height」かと思ったんですが、「mc.height - (e.uv.v * mc.height)」でした。
わかりづらいね。

参考サイト
blog.tartiflop » Blog Archive » First steps in Away3D : Part 3 - Texture mapping
Tutorials (category) > Tutorials > Flash Magazine

カメラの動きは相変わらずよくわからない。

0 件のコメント:

コメントを投稿