Real-time 3D model viewer (almost ready!)

Software: Away3D 4.x

Monguron, Newbie
Posted: 23 November 2011 07:13 PM   Total Posts: 27

Hey Guys,

Up to now my model viewer was embedding the .obj and the .png files, but I want to change it to real-time loading, so then I could pass the filename of the object and the filename of the texture with php/javascript (with the flashvar).

80prozent offered this:

AssetLibrary.enableParser(OBJParser);   AssetLibrary.addEventListener(AssetEvent.ASSET_COMPLETEonAssetComplete);
AssetLibrary.load(new URLRequest("yourObject.obj"));


private function 
onAssetComplete(event:AssetEvent):void
  {
  
if (event.asset.assetType == AssetType.MESH)
   
{
    trace
("Mesh parsed");
                                
//setup materials, apply lights......

}

... but I don’t know how to implement it into the code. Also I don’t really understand how to give any variable in javascript through the flashvar, and how to implement them into my .as ...

I’ve attached my .as file.

Thanks for your support in advance! I would really appreciate some help, cause these two major steps should be solved somehow, and then the project is ready.

Hmm ... the attachment only opens a blank page, but nothing can be downloaded ...

 

 

File Attachments
Cain_fabrice_2.rar  (File Size: 2KB - Downloads: 0)
   

Monguron, Newbie
Posted: 23 November 2011 07:52 PM   Total Posts: 27   [ # 1 ]

... 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.15000;
   
_light.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_UPonKeyUp);
   
stage.scaleMode StageScaleMode.NO_SCALE;
   
stage.align StageAlign.TOP_LEFT;
   
stage.addEventListener(Event.RESIZEonStageResize);
  
}

  
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_COMPLETEonMeshReady);
   
loader.addEventListener(LoaderEvent.RESOURCE_COMPLETEonLoadedComplete);
   
loader.addEventListener(LoaderEvent.LOAD_ERRORonLoadingError);
   
loader.loadData(new URLRequest("Tank.obj"), nullnull, 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.cameramodel0901000, -9090NaNNaN41true);
   
   
_view.scene.addChild(model);
   
stage.addEventListener(MouseEvent.MOUSE_DOWNonMouseIsDown);
   
stage.addEventListener(MouseEvent.MOUSE_UPonMouseIsUp);
   
addEventListener(Event.ENTER_FRAMEonEnterFrame);
  
}   

  
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.15000;
   
_light.1000;
   
_light.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_LEAVEonStageMouseLeave);
   
        
}
         
        
private function onMouseIsUp(event:MouseEvent):void
        {
            _move 
false;
            
stage.removeEventListener(Event.MOUSE_LEAVEonStageMouseLeave);     
        
}
        
        
private function onStageMouseLeave(event:Event):void
        {
            _move 
false;
            
stage.removeEventListener(Event.MOUSE_LEAVEonStageMouseLeave);     
        
}
 }
   
   

X

Away3D Forum

Member Login

Username

Password

Remember_me



X