Erain 3D
-->

Author: Max Pellizzaro
Date: November 1st 2007
version: 3.0

Importing 3DS MAX Part II

Objective of the tutorial

In this II Part tutorial we will learn now how to import a file directly from an ASE format, which is the text format file produced by 3DS MAX. The same consideration (and tutorial) can be repeated exchanging the ASE file with a 3DS file format. You will see there is a little more code to write and to understand, but I’m sure we will go through it without much problem.

How to

Set up

The Document class must be changed to Example007.as The name of the class in the .as file and the name of the constructor now is: Example007. In this archive you will find also a folder called asset where it is placed the exported file from 3DS MAX.

example007.zip

The AS Code

package {
  import flash.display.Sprite; 
  import flash.events.*;
  import sandy.core.Scene3D;
  import sandy.core.data.*;
  import sandy.core.scenegraph.*;
  import sandy.materials.*;
  import sandy.materials.attributes.*;
  import sandy.primitive.*;
  import sandy.parser.*;

  public class Example007 extends Sprite {
    private var scene:Scene3D;
    private var camera:Camera3D;
    private var pot:Shape3D;

    public function Example007() { 
      var parser:IParser = Parser.create("asset/teieraASE.ASE",Parser.ASE );
      
      parser.addEventListener( ParserEvent.FAIL, onError );
      parser.addEventListener( ParserEvent.INIT, createScene );
      parser.parse();
    }
  
    private function onError( pEvt:ParserEvent ):void {
      trace("there is an error in loading your stuff");
    }

    private function createScene(p_eEvent:ParserEvent ):void {
      camera = new Camera3D( 300, 300 );
      camera.y = 30;
      camera.z = -200;
      
      var g:Group = Group( p_eEvent.group);

      pot = g.children[0] as Shape3D;
      pot.x = 0;
      
      var materialAttr:MaterialAttributes = new MaterialAttributes(new LightAttributes( true, 0.2 ) );
      var material:Material = new ColorMaterial( 0xE0F87E, 0.9, materialAttr);
      material.lightingEnable = true;
      var app:Appearance = new Appearance( material);
      
      pot.appearance = app;
      
      scene = new Scene3D( "scene", this, camera, g );
      
      addEventListener( Event.ENTER_FRAME, enterFrameHandler ); 
    }

    private function enterFrameHandler( event : Event ) : void {
      pot.pan +=3;
      scene.render();
    }
  }
}

Examining the code

import sandy.parser.*;
private var pot:Shape3D;
var parser:IParser = Parser.create("asset/teieraASE.ASE",Parser.ASE );
parser.addEventListener( ParserEvent.FAIL, onError );
parser.addEventListener( ParserEvent.INIT, createScene );
parser.parse();

As you can see in the constructor for the class we are not placing the Camera3D object and then Scene3D. This is not done because we still do not have our model in as an object, and we still don’t know if we will be able to imported, how much time it will take to parse the file, or we don’t know if the file is corrupted or not. So a nice and clean way to work is this:

  1. We first instantiate a Parser Class telling which file we are going to parse.
  2. We then add two listener: one that will be invoked if any error will occur during the importing process, and the other one that will be invoked when the importing procedure will be done
  3. Then we will invoke the method to start the parsing process

The key point is this one:

var g:Group = Group( p_eEvent.group);

With this “simple” line we can see that the event that is been fired by the dispatcher contains a Node element, that is the Group we need to pass inside the Scene3D constructor. So, as simple as it looks, we have a group in hand that presumable contains our imported model.
The Group class has method that return an array of Node elements; we just need to test if these Nodes exist or if they represent Shape3D objects. Since this is an easy example and we know that there is only one Node inside the Group, we will cast this node to the Shape3D object that we have called “pot”.

pot = g.getChildFromId(1) as Shape3D;

We just need to apply a skin to the object and we are done!

The output

Can you see any difference with the previous one ?

Now you have two different possibility to import 3DS MAX file, I personally prefer to work with the previous mode, I want to work as simple as possibile in my Sandy project, and just leave the more complicated stuff to create model somewhere else. But this is my personal advice, feel free to choose the one you prefer or best fits your need. Keep in mind one important thing: big model comes with the price with huge calculation!