首先away3d4.0和之前的版本不一样,away3d 4.0的显示物件分为两个部分,一个是材质Texture,另一个是Gemometry也就是几何定义,两者可以合成一个Mesh(个人理解,如有误导,实在抱歉)。
package samples
{
import away3d.entities.Mesh;
import away3d.materials.TextureMaterial;
import away3d.primitives.SphereGeometry;
import away3d.utils.Cast;
import template.AwayTemplate;
public class SphereTest extends AwayTemplate
{
[Embed(source=”assets/earth.jpg”)]
private var earth:Class;
private var sphere:Mesh;
public function SphereTest()
{
super();
}
override protected function initView():void
{
super.initView();
initSphere();
}
private function initSphere():void
{
//建立球体的几何对象
var geo:SphereGeometry = new SphereGeometry(400);
//建立球体的材质数据
var texture:TextureMaterial = new TextureMaterial(Cast.bitmapTexture(earth));
sphere = new Mesh(geo,texture);
//将物体添加到场景里面显示
_view.scene.addChild(sphere);
}
}
}
package
{
import away3d.containers.ObjectContainer3D;
import away3d.controllers.FirstPersonController;
import away3d.controllers.HoverController;
import away3d.controllers.LookAtController;
import away3d.events.AssetEvent;
import away3d.loaders.Loader3D;
import away3d.loaders.parsers.Max3DSParser;
import base.TemplateBase;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.geom.Vector3D;
import flash.net.URLRequest;
import flash.ui.Keyboard;
public class ModelLoaderTest extends TemplateBase
{
private var loader:Loader3D;
private var controler:HoverController;
private var step:Number=5;
private var placeHolder:ObjectContainer3D;
private var stageMouseX:Number;
private var stageMouseY:Number;
public function ModelLoaderTest()
{
super();
}
override protected function init(e:Event=null):void
{
super.init(e);
_view.camera.y = 500;
loader = new Loader3D();
Loader3D.enableParser(Max3DSParser);
loader.addEventListener(AssetEvent.ASSET_COMPLETE, onAssetComplete);
loader.load(new URLRequest(“assets/houseScene.3ds”));
loader.scale(10);
_view.scene.addChild(loader);
placeHolder = new ObjectContainer3D();
placeHolder.y = 100;
placeHolder.x = – 100;
_view.camera.addChild(placeHolder);
controler = new HoverController(_view.camera);
initEvents();
}
private function initEvents():void
{
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
stage.addEventListener(MouseEvent.MOUSE_WHEEL, onWheel);
}
private function mouseDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
stage.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
stageMouseX = stage.mouseX;
stageMouseY = stage.mouseY;
}
private function onWheel(e:MouseEvent):void
{
controler.distance *= (1+e.delta/100);
}
private function mouseUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);
var deltaX:Number = stage.mouseX – stageMouseX;
var deltaY:Number = stage.mouseY – stageMouseY;
controler.panAngle = 0.5*deltaX+controler.panAngle;
controler.tiltAngle = 0.5*deltaY+controler.tiltAngle;
}
private function onAssetComplete(e:AssetEvent):void
{
}
override protected function onEnter(e:Event):void
{
super.onEnter(e);
}
}
}