Erain 3D
--> FDT Pure coding comfort


Author: Max Pellizzaro
Date: December 5th 2008
Update: July 12th 2009
version: 3.1.1

Advance Parsing 3DS MAX - automatic texture loading

Objective of the tutorial

This is a very simple tutorial, but it will show a great new functionality added in the Parser class: the automatic loading of the texture. Before this new functionality in order to load a 3DS object into Sandy you would always need three steps:

  1. Loading the object
  2. Loading the skin/texture
  3. Applying the texture to the object

With this new functionality Sandy parser does it all for you. You must only be sure that whoever gives you the 3DS models gives you a coherent set of files: 3DS + all the required textures.

How to

Set up

In this archive you will find all the Flash and AS files, plus a folder called asset where you will find the 3DS model and the texture of this element (a gun).

example0074.zip

The new updated version can be found here:
example0074_v3.1.1.zip

The AS Code

package
{
   import flash.display.*; 
   import flash.events.*;
   import flash.ui.*;
   import flash.net.URLRequest;
   
   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.*;
   import sandy.util.*;
   import sandy.events.*;

   public class Example0074 extends Sprite 
   {
      private var scene:Scene3D;
	  private var camera:Camera3D;
	  private var rifle:Shape3D;
	  private var queue:LoaderQueue;
      private var parserStack:ParserStack;
	  
      public function Example0074()
      { 
		 var parser:IParser = Parser.create("asset/mar_rifle.3ds",Parser.MAX_3DS, 1, "JPG");
		 parserStack = new ParserStack();
		 parserStack.add("rifle",parser);
		 
		 parserStack.addEventListener(ParserStack.COMPLETE, parserComplete );
		 parserStack.start();
		 
    }
 
    private function onError( pEvt:ParserEvent ):void {
	  trace("there is an error in loading your stuff");
    }
 
     private function parserComplete(pEvt:Event ):void
     {
		 rifle = parserStack.getGroupByName("rifle").children[0] as Shape3D;
	     
		 var root:Group = createScene();
		 camera = new Camera3D( 600, 300 );
         camera.y = 5;
         camera.z = -150;
		 scene = new Scene3D( "scene", this, camera, root );
		 
		 // Listen to the heart beat and render the scene
         addEventListener( Event.ENTER_FRAME, enterFrameHandler );
		 stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedHandler);
      }
 
    private function createScene():Group {
      
      var g:Group = new Group();
      rifle.x = 0;
	  rifle.y = 0;
	  rifle.pan = -90;
 
      g.addChild(rifle);
	  
      return g;
    }
 
 private function keyPressedHandler(event:flash.events.KeyboardEvent):void 
	  {
	   switch(event.keyCode) {
		case Keyboard.UP:
			rifle.roll +=5;
			break;
		case Keyboard.DOWN:
			rifle.roll -=5;
			break;
		case Keyboard.LEFT:
			rifle.pan -=5;
			break;
		case Keyboard.RIGHT:
			rifle.pan +=5;
			break;
		}
	  }
 
    private function enterFrameHandler( event : Event ) : void {
      scene.render();
    }
  }
}

Examining the code

 
var parser:IParser = Parser.create("asset/mar_rifle.3ds",Parser.MAX_3DS, 1, "JPG");
parserStack = new ParserStack();
parserStack.add("rifle",parser);

As you can see there is no much difference here either. We have just introduced a new parameter and the end of the constructor telling us what is the format of the texture. In this example we have declared a “JPG” format. One other format is supported: “png”, but I don't think more… well, do some tests and you will find it out.
Well, we are done, we don't need anymore to load any texture and to apply the texture to the loaded object.

The output

You can move the arrow key to see the rifle rotating.