... so here is the code:
package
{
import away3d.containers.View3D;
import away3d.debug.AwayStats;
import away3d.entities.Mesh;
import away3d.lights.PointLight;
import away3d.loaders.Loader3D;
import away3d.loaders.parsers.OBJParser;
import away3d.materials.BitmapMaterial;
import away3d.materials.methods.BasicDiffuseMethod;
import away3d.materials.methods.BasicSpecularMethod;
import away3d.materials.methods.FresnelSpecularMethod;
import away3d.materials.methods.SubsurfaceScatteringDiffuseMethod;
import away3d.tools.Mirror;
import away3d.controllers.HoverController;
import away3d.containers.ObjectContainer3D;
import away3d.events.AssetEvent;
import away3d.events.LoaderEvent;
import flash.geom.Vector3D;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
[SWF(width="500", height="400", frameRate="60", backgroundColor="0xffffff")]
public class Cain_fabrice_2 extends Sprite
{
private var _view : View3D;
private var _hoverController:HoverController;
private var _light : PointLight;
/*[Embed(source="/../src/assets/Tank.obj", mimeType="application/octet-stream")]
private var OBJData : Class;
[Embed(source="/../src/assets/Tank_diff.png")]
private var Albedo : Class;*/
private var _camController : HoverController;
private var _count : Number = 0;
private var _subsurfaceMethod : SubsurfaceScatteringDiffuseMethod;
private var _diffuseMethod : BasicDiffuseMethod;
private var _material : BitmapMaterial;
private var _fresnelMethod : FresnelSpecularMethod;
private var _specularMethod : BasicSpecularMethod;
private var _lastPanAngle:Number;
private var _lastTiltAngle:Number;
private var _lastMouseX:Number;
private var _lastMouseY:Number;
private var _move:Boolean = false;
public function Cain_fabrice_2()
{
_view = new View3D();
_view.antiAlias = 2;
this.addChild(_view);
_light = new PointLight();
_light.x = 15000;
_light.z = 15000;
_light.color = 0xffddbb;
_view.scene.addChild(_light);
//_camController = new HoverDragController(_view.camera, stage);
//new camera
_view.camera.x=900;
_view.camera.y=-900;
_view.camera.z=-900;
_view.camera.lens.near = 10;
_view.camera.lens.far = 5000;
//addChild(new AwayStats(_view));
initMaterials();
initMesh();
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.addEventListener(Event.RESIZE, onStageResize);
}
private function onStageResize(event : Event) : void
{
_view.width = stage.stageWidth;
_view.height = stage.stageHeight;
}
private function initMaterials() : void
{
_material = new BitmapMaterial(new Albedo().bitmapData);
_diffuseMethod = new BasicDiffuseMethod();
_specularMethod = new BasicSpecularMethod();
_material.lights = [ _light ];
_material.gloss = 10;
_material.specular = 3;
_material.ambientColor = 0xe3a471;
_material.ambient = 1;
_material.specularMethod = _fresnelMethod;
}
private function initMesh() : void
{
var loader:Loader3D = new Loader3D();
Loader3D.enableParser(OBJParser);
loader.addEventListener(AssetEvent.MESH_COMPLETE, onMeshReady);
loader.addEventListener(LoaderEvent.RESOURCE_COMPLETE, onLoadedComplete);
loader.addEventListener(LoaderEvent.LOAD_ERROR, onLoadingError);
loader.loadData(new URLRequest("Tank.obj"), null, null, new OBJParser());
}
private function onLoadingError(e:LoaderEvent):void
{
trace("error during parsing-->"+e.message);
}
private function onMeshReady(event:AssetEvent):void
{
var mesh:Mesh = Mesh(event.asset);
mesh.geometry.scale(80);
mesh.material = _material;
}
private function onLoadedComplete(event : LoaderEvent) : void
{
var model:ObjectContainer3D = ObjectContainer3D(event.currentTarget);
_hoverController = new HoverController(_view.camera, model, 0, 90, 1000, -90, 90, NaN, NaN, 4, 1, true);
_view.scene.addChild(model);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseIsDown);
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseIsUp);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onKeyUp(event : KeyboardEvent) : void
{
_material.diffuseMethod = _material.diffuseMethod == _diffuseMethod ? _subsurfaceMethod : _diffuseMethod;
// _material.specularMethod = _material.specularMethod == _specularMethod? _fresnelMethod : _specularMethod;
}
private function onEnterFrame(ev : Event) : void
{
_count += .003;
// _container.rotationY += .3;
_light.x = 15000;
_light.y = 1000;
_light.z = 15000;
if (_move) {
_hoverController.panAngle = 0.7*(stage.mouseX - _lastMouseX) + _lastPanAngle;
_hoverController.tiltAngle = 0.7*(stage.mouseY - _lastMouseY) + _lastTiltAngle;
}
_view.render();
}
private function onMouseIsDown(event:MouseEvent):void
{
_lastPanAngle = _hoverController.panAngle;
_lastTiltAngle = _hoverController.tiltAngle;
_lastMouseX = stage.mouseX;
_lastMouseY = stage.mouseY;
_move = true;
stage.addEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
}
private function onMouseIsUp(event:MouseEvent):void
{
_move = false;
stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
}
private function onStageMouseLeave(event:Event):void
{
_move = false;
stage.removeEventListener(Event.MOUSE_LEAVE, onStageMouseLeave);
}
}
}