Using Papervision Object Parsers

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

package {
	// flash class
	import flash.events.Event;
	// pv3d class
	import org.papervision3d.events.FileLoadEvent;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.parsers.Max3DS;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.shadematerials.GouraudMaterial;
	import org.papervision3d.view.BasicView;

	public class Max3dsParser_v1 extends BasicView {
		// create a class-level var for the collada scene
		private var _3ds:Max3DS;
		// create the light
		private var _light:PointLight3D;

		public function Max3dsParser_v1() {
			lights();
			init3d();
		}

		private function lights():void {
			// create the light to the scene
			_light = new PointLight3D(true, true);
			// place it in the same position as the camera (0, 0, -1000);
			_light.copyPosition(camera);
		}

		private function init3d():void {

			// instantiate the 3ds obj and load the knot object
			_3ds = new Max3DS();
			_3ds.load("assets/knot.3ds");
			// wait for the object to load
			_3ds.addEventListener(FileLoadEvent.LOAD_COMPLETE, on3dsLoad);
			// scale the object
			_3ds.scale = 5;
			// add the 3ds file to the scene
			scene.addChild(_3ds);
			// start rendering (activated onRenderTick)
			startRendering();
		}

		/**
		 * called when the object loads
		 * @param	ARG_evt
		 */
		private function on3dsLoad(ARG_evt:FileLoadEvent):void {
			// we are now going to use a gouraud material for shading
			var knotMaterial:GouraudMaterial = new GouraudMaterial(_light);
			// get the knot model from the 3ds loaded file
			var knot:DisplayObject3D = _3ds.getChildByName("Torus Knot");
			// apply the material to the knot
			_3ds.setChildMaterial(knot, knotMaterial);
		}

		override protected function onRenderTick(event:Event = null):void {
			// rotate the object
			_3ds.yaw(1);
			super.onRenderTick();
		}
	}
}

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).

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

Leave a Reply

Your email address will not be published. Required fields are marked *