Very “efficient” DAE parser in papervision 3d

January 22nd, 2009
by admin

I’m currently workin on some bigger project, and I needed to load DAE file. It’s known, that papervision3d has DAE parser – so I used it. But when I tried to load a little bigger file ( only 400KB model ), flash thrown 15 seconds timeout exception. The exception was thrown during unpack process. “Wow, flash is so slow, that it can’t unpack 400KB zip in 15 seconds? This sucks” I thought. But later I looked deeper into DAE parser’s code. DAE parser uses KMZ parser, to unpack DAE’s content ( like textures and models ), and then parses XML ( or rather KMZ unpacks and then uses DAE to read model… ). I followed the code ( just like Dexter does ), and studied KMZ parser’s code. And it appears, that KMZ’s parser code, isn’t the most efficient code I’ve ever seen.

Just look @ it:

http://papervision3d.googlecode.com/svn/trunk/as3/trunk/src/org/papervision3d/objects/parsers/KMZ.as

After loading the zip file, it calls “parse” function, which calls “getColladaFromZip”. getColladaFromZip unopacks the zip file:

private function getColladaFromZip( zipFile : ZipFile ) : ByteArray  {
	for(var i:int = 0; i < zipFile.entries.length; i++) {
	    var entry:ZipEntry = zipFile.entries[i];

	    // extract the entry's data from the zip
	    var data:ByteArray = zipFile.getInput(entry);

		if(entry.name.toLowerCase().indexOf(".dae") != -1) {
			return data;
		}
	}
	return null;
}

It unpacks entire file, just to get the “dae” file, and then returns. More – it extracts content ( function “getInput” ) , and then checks if unpacked content is “dae” file, despite the fact, that to check it, we don’t need to unpack anything to check it.

It gets worse later – it unpacks entire file to check how many textures we have in the zip:

private function numTexturesInZip( zipFile : ZipFile ) : uint {
	var count : uint = 0;
	for(var i:int = 0; i < zipFile.entries.length; i++) {
	    var entry:ZipEntry = zipFile.entries[i];

	    // extract the entry's data from the zip
	    var data:ByteArray = zipFile.getInput(entry);

	    if(entry.name.toLowerCase().indexOf(".png") != -1 || entry.name.toLowerCase().indexOf(".jpg") != -1) {
		count++;
	 }
	 }
	return count;
}

Ok, and at the end, in the “parse” function, every file in archive is extracted again, only for purpose of loading textures from this archive.

Reassuming, if you have 1 meg model and several 200 KB textures, KMZ parser extracts geometry 3 times – second time, to count number of textures (?!?) and third, to load textures. Every texture is extrated 3 times too – first time – to ensure that the texture isn’t a model, second – to count it, and finally third time – to load texture. I think that somebody pasted the code of loading zip file from some tutorial and repasted it 3 times :)

I would paste here my code of better KMZ parser, but it’s too integrated with my project, so you’ll have to wait ; p

And I wrote this post, to enlight you people, that when you are using pv3d’s KMZ parser to load zipped DAE, it isn’t the best parser to use.

Tags: , , , , , ,
Posted in flash experiments | Comments (11)

11 Responses to “Very “efficient” DAE parser in papervision 3d”

  1. T Says:

    Thanks for the tip – would you be able to explain how to load in a .DAE file into Flash AS3?

    thanks

  2. teleranek Says:

    To load .DAE:

    import org.papervision3d.objects.parsers.*;
    import org.papervision3d.objects.DisplayObject3D;
    import org.papervision3d.events.FileLoadEvent;

    private var modelDAE:DAE;
    private function load(){
    modelDAE = new DAE( );
    modelDAE.texturePath = “”; // we assume that textures are in main DAE’s folder
    modelDAE.load( “path/to/file.dae” );
    modelDAE.addEventListener( FileLoadEvent.LOAD_COMPLETE , ldrComplete );
    }

    private function ldrComplete( e:Event ):void{
    try{
    model = DisplayObject3D( modelDAE );
    } catch( e:TypeError ){
    loading = ERROR;
    return;
    }

    And then you simply call the load function:
    load();

    I didn’t write how to handle errors, but it’s just simple snippet how to load DAE.

  3. T Says:

    thanks :)

  4. caydieu Says:

    Hi I want to ask bout your Rotating rings but you’ve disabled commenting on the other post and I couldn’t find your contact info. My question is; I have a sphere in the center of my ring, but the sphere always appears to be on top / in front. I have useOwnConvas turned on for both objects, please help thank you.

  5. teleranek Says:

    When an object has useOwnCanvas property turned on, the object is draw on separate DisplayObject and is rendered separately. Therefore, papervision can’t draw part of the ring which is deeper, then sphere and then part of the ring which is closer – it first renders ring and then sphere, so sphere is always on top – sphere is always rendered at the end.
    That’s why you shouldn’t use “useOwnCanvas” feature.
    But you can use zupko’s reflection and shadow classes to draw shadows and reflections on an array of bitmaps, and then rotate your sphere and ring, and animate previously drawn bitmaps as a background :)

    In the future i will be more generous and will provide some of my “dirty” code of things which has appeared on this blog

  6. caydieu Says:

    thanks for the quick reply, I’ll give zupko a shot. I’m using viewportLayer for now n it works but it wont let me apply filter, any idea?

  7. teleranek Says:

    I remember that this layers system in papervision is pretty complicated and could be simpler. Remember about all “getChildLayer” functions, and check if any settings of your materials or displayObjects3d don’t collide with papervision’s layers system. For example create new basic scene with one cube and apply filters, and then check what makes filters not work.
    I remember I’ve spotted some bugs, but I don’t remember what it was :p

  8. payday loans toronto Says:

    I want to thank the blogger very much not only for this post but also for his all previous efforts. I found blog.teleranek.org to be extremely interesting. I will be coming back to blog.teleranek.org for more information.

  9. TamVepereemom Says:

    But even if there does exist an application that interferes with your idea, see if you can improve it and make it more user-friendly and smarter. Lon Safko wrote a great piece yesterday about where to start when you’re just getting familiar with social media. And in addition to that, the number of Americans with smart phones will be growing from 30 to 60 percent within the next calendar year alone.

  10. Frankcot Says:

    Hi Frontline Shredding continues to provide dependable and inexpensive hard drive shredding in the pacific northwest for more than 20 years. Since we are family owned and operated means we take a huge amount of satisfaction in the quality of our work, as well as our great relationships with our customers. Our fleet of mobile shredding trucks will come right to your doorstep, office building, or storage unit and securely shred your documents on the spot. That way you can be rest assured that your information has actually been destroyed, before we leave the lot. Our on-site shredders quickly shred through clips, pins and most document related material, that way no sorting is ever required. undoubtably, the thing that keeps our clients coming back after all these years is our amazing Frontline team. All of our employees are part of our family, which means we value our customers and treat them like family With Frontline Shredding you get the fast and affordable shredding you need, with the old school customer service you deserve. We offer a wide range of services for any need from one time shredding, all the way to daily scheduled pickups, all without signing a contract. Who doesnt like saving money with drop off shredding at our Bothell Location. Same awesome service, new low price. come see us in downtown Bothell.

  11. Allensak Says:

    Hello. And Bye.

Leave a Reply