Max 3ds Parser

What is a 3ds file?

The .3ds file is exported from 3ds Max and contains information about cameras, materials, meshes, lighting and can have some animation attached to it as well.

Max3ds Parsing Example

  1. package {
  2.  // flash class
  3.  import flash.events.Event;
  4.  // pv3d class
  5.  import org.papervision3d.events.FileLoadEvent;
  6.  import org.papervision3d.objects.DisplayObject3D;
  7.  import org.papervision3d.objects.parsers.Max3DS;
  8.  import org.papervision3d.lights.PointLight3D;
  9.  import org.papervision3d.materials.shadematerials.GouraudMaterial;
  10.  import org.papervision3d.view.BasicView;
  11.  
  12.  public class Max3dsParser_v1 extends BasicView {
  13.   // create a class-level var for the collada scene
  14.   private var _3ds:Max3DS;
  15.   // create the light
  16.   private var _light:PointLight3D;
  17.  
  18.   public function Max3dsParser_v1() {
  19.    lights();
  20.    init3d();
  21.   }
  22.  
  23.   private function lights():void {
  24.    // create the light to the scene
  25.    _light = new PointLight3D(true, true);
  26.    // place it in the same position as the camera (0, 0, -1000);
  27.    _light.copyPosition(camera);
  28.   }
  29.  
  30.   private function init3d():void {
  31.  
  32.    // instantiate the 3ds obj and load the knot object
  33.    _3ds = new Max3DS();
  34.    _3ds.load("assets/knot.3ds");
  35.    // wait for the object to load
  36.    _3ds.addEventListener(FileLoadEvent.LOAD_COMPLETE, on3dsLoad);
  37.    // scale the object
  38.    _3ds.scale = 5;
  39.    // add the 3ds file to the scene
  40.    scene.addChild(_3ds);
  41.    // start rendering (activated onRenderTick)
  42.    startRendering();
  43.   }
  44.  
  45.   /**
  46.    * called when the object loads
  47.    * @param ARG_evt
  48.    */
  49.   private function on3dsLoad(ARG_evt:FileLoadEvent):void {
  50.    // we are now going to use a gouraud material for shading
  51.    var knotMaterial:GouraudMaterial = new GouraudMaterial(_light);
  52.    // get the knot model from the 3ds loaded file
  53.    var knot:DisplayObject3D = _3ds.getChildByName("Torus Knot");
  54.    // apply the material to the knot
  55.    _3ds.setChildMaterial(knot, knotMaterial);
  56.   }
  57.  
  58.   override protected function onRenderTick(event:Event = null):void {
  59.    // rotate the object
  60.    _3ds.yaw(1);
  61.    super.onRenderTick();
  62.   }
  63.  }
  64. }

For the Max 3ds files, things work a little bit differently. I spread things out a little differently to demonstrate something that I find useful. Just like in the DAE parser, the constructor is left blank. The load method is where I had to make my choice, it accepts materials and texture paths as parameters but I opted out of that to demonstrate another method.

You will notice a function called on3dsLoad. This is where I am able to load materials to specific objects in the 3d scene. I used this method in the example because it allows you to go through a scene and add custom materials to each mesh once the scene has been loaded (also helps in avoiding a couple errors if you are using bitmapfilematerials).

  1. _3ds.load("assets/knot.3ds"); // could have also loaded the material here
  2. _3ds.addEventListener(FileLoadEvent.LOAD_COMPLETE, on3dsLoad);

Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 Page 9