Posted in labs, as3 by nicoB on October 1st, 2007

In Papervision, the 3D Objects are defined in Collada files. Collada files are just XML files with a specific DTD (or schema). In consequence, colladas inherit the same qualities and defects as XML. One of the main defect is the file size which can be a major problem if you’re doing an online application.

Here a little tutorial to display a zipped collada in a papervision application : demo
First of all, you’ll need :

  • Papervision
  • FZIP from CĂ´deazur
  • Python to insert asher32 CRC in the zip (to be readable from flash)
  • a valid Collada file (I took the duck demo taken from the basic samples on collada.org)

1. Zipping the collada

Install Python.Zip

the collada file (only the .dae file, as the materials are in JPG no need to be compressed twice)

Launch the fzip-prepare.py from a command line:

fzip_prepare

2. We’ll create the document class, Papervision_fzip.as :

package com.nicoB {

        import flash.display.Sprite;
        /**
        * @author nicoB
        */

        public class Papervision_fzip extends Sprite {

                public function Papervision_fzip(){
                        var unzipper : ColladaUnzipper = new ColladaUnzipper("Duck/Duck.zip");
                        unzipper.addEventListener(ColladaUnzipperEvent.ONGETCOLLADA, _onGetCollada);
                }

                private function _onGetCollada(e : ColladaUnzipperEvent) : void {
                       var oDuck : Duck3D = new Duck3D(e.collada);
                       addChild(oDuck);
                }

        }

}
 

3. Let’s create the zip manager which will load and unzip the collada, ColladaUnzipper.as :

package com.nicoB {
        import deng.fzip.FZip;
        import flash.net.URLRequest;
        import flash.events.ProgressEvent;

        import deng.fzip.*;

        import flash.events.EventDispatcher;

        import flash.events.Event;
        /**
        * @author nicoB
        */

        public class ColladaUnzipper extends EventDispatcher {
               
                var oFZip : FZip;

                public function ColladaUnzipper(sPath : String){
                       oFZip = new FZip();
                        oFZip.addEventListener(ProgressEvent.PROGRESS, _onLoadProgress);
                       oFZip.addEventListener(Event.COMPLETE, _onLoadComplete);
                       
                        var oURLRequest : URLRequest = new URLRequest(sPath);
                       oFZip.load(oURLRequest);
               }

                private function _onLoadProgress(e : ProgressEvent) : void{
       
                       var iPrct : Number = Math.round(e.bytesLoaded/e.bytesTotal*100);
       
                       var event : ColladaUnzipperEvent = new ColladaUnzipperEvent(ColladaUnzipperEvent.ONCOLLADALOADING);
                       event.prct = iPrct;
                       dispatchEvent(event);
                }

                private function _onLoadComplete(e : Event) : void{
                        var collada : XML = new XML(oFZip.getFileAt(0).content);
               
                        var event : ColladaUnzipperEvent = new ColladaUnzipperEvent(ColladaUnzipperEvent.ONGETCOLLADA);
                       event.collada = collada;
                       dispatchEvent(event);
                }

        }

}
 

4. An event class, ColladaUnzipperEvent.as :

package com.nicoB {
        import flash.events.Event;
        /**
        * @author nicoB
        */

        public class ColladaUnzipperEvent extends Event {

                public static const ONGETCOLLADA: String = "onGetCollada";
                public static const ONCOLLADALOADING: String = "onColladaLoading";
                public var collada : XML;

               public var prct : Number;

                public function ColladaUnzipperEvent(type : String, bubbles : Boolean = false, cancelable : Boolean = false) {
                       super(type, bubbles, cancelable);
                }
        }
}
 

4. Finally, we create the class, which will initialize the papervision engine, Duck3D.as :

package com.nicoB {
       
        import flash.display.Sprite;
        import flash.events.Event;
        import org.papervision3d.cameras.Camera3D;
        import org.papervision3d.objects.Collada;
        import org.papervision3d.objects.DisplayObject3D;
        import org.papervision3d.scenes.Scene3D;

        /**
        * @author nicoB
        */

        public class Duck3D extends Sprite {
               
                private var oScene : Scene3D;
                private var oCamera : Camera3D;

               private var oContainer : DisplayObject3D;

               private var oObj : DisplayObject3D;

               private var oCollada : XML;

                public function Duck3D(collada : XML) : void{

                       oCollada = collada;
                        _buildScene();
                       _buildCamera();
                       _buildObject();

                        //onEnterFrame
                        this.addEventListener(Event.ENTER_FRAME, _draw);

                }

                //——————————–
               //               SCENE
               //——————————–

                private function _buildScene():void{
                       oScene = new Scene3D(this);
                }

                //——————————–
               //               CAMERA
               //——————————–

                private function _buildCamera():void{
                       oCamera = new Camera3D();
                       oCamera.z = 60000;
                      oCamera.zoom = 10;
                      oCamera.focus = 100;
               }     

                //——————————–
               //               OBJECT
               //——————————–     

                private function _buildObject():void{
                       oContainer = new DisplayObject3D();
                       oScene.addChild(oContainer);   

                        oObj = new Collada(oCollada);
                       oObj.y -= 20000;
                      oObj.x -= 15000;
               
                        oContainer.addChild(oObj);

                }

                //——————————–
               //               DRAW
               //——————————–

               private function _draw(e : Event = null):void{
                      oObj.rotationY += 1;
                      oScene.renderCamera(oCamera);
               }

        }

}
 

And here you are, you read a zipped collada. Not hard, is it?

sources files

List of replies :

  1. chamans, says

    Salut super source
    pas besoin de cree un nouveau xml
    remplace le type par :*
    var Objet3D:*;
    var file:FZipFile = zip.getFileAt(index);
    if(file.filename.indexOf(”.DAE” != -1) {
    Objet3D=file.content;
    }
    apres
    var daeAE=new DAE(Objet3D);


Leave a Reply