Everyday Flash

Creative use of technology // A blog about 3D Flash and Actionscript by Bartek Drozdz

Using the bend modifier with Collada objects

Papervision3D comes with a set of primitive 3d shapes - such as a plane, a cube and a sphere. It is possible to do quite a lot with those, but the real power lies in importing objects from 3d editors like Maya, 3dsmax or, in that case - Blender. In Papervision3D, the format of choice for importing meshes from 3d editors is Collada.

Some time ago I actually did export a Collada file from Blender and import it into Papervision3D. You can see the result in one of my early posts. Back then, it was like riding a bike, downhill, in a forest, blindfolded - you don't register much, and at the end you are just happy that you made it. This time I wanted to actually understand what is going on.

For my first experiment I decided to export only the geometry and not the texture. I wanted to see my bend modifier applied to such an object. The task turned out to be not difficult at all. The only thing I needed to understand was the way Collada files are represented in Papervision3D.

A Collada is basically an XML file (with a *.dae extension), so it is perfectly human readable. It contains a part that describes its contents. In my file it can be found somewhere towards the end, and it starts like this:

XML:
  1. <library_visual_scenes>
  2.         <visual_scene id="Scene" name="Scene">
  3.             <node layer="L2" id="LadyDae" name="LadyDae">
  4. [...]

The first interesting point is that a Collada file represents a scene, not a single 3d object. In my case the scene has only one object, but it is still a scene. That is why Papervision3D will first create a DisplayObject3D that represents this scene, and only then it will add a child that represents the 3d object itself. So in the code, once the Collada is loaded, I can access the object "LadyDae" like that:

Actionscript:
  1. var mesh:DisplayObject3D = dae.getChildByName("COLLADA_Scene").getChildByName("LadyDae");

Note that the scene is named "COLLADA_Scene" not "Scene" as you could expect from the XML. It's because this name is hard coded and it will always be the same, no matter what the name is in your file. Also note that the attribute you need to refer too is the 'name' attribute, not 'id'.

Now, that the 'mesh' variable contains a reference to the object holding the geometry applying the Bend modifier is pretty simple:

Actionscript:
  1. var b:Bend = new Bend(mesh);
  2. b.quickBend(1, .5);

So, I'd love to tell you stories on how, after a many sleepless nights, I finally made a breakthrough and now the Bend modifier works with Collada... but the fact is it always did. The thing I failed to grasp so far was the structure of the Collada object itself.

This unexpected success left me some time and energy to play a bit with another topic from my must-explore-soon list - ShadedMaterials. I did not do much. Basically I just followed some of Ralph Hauwerts old demos, but it resulted in some effects that are nice to look at.

Credits. The model of the girl is a very simplified version of the original made by Tiziana. The background tile comes, as usual, from SquidFingers.

Source. The source code is available here. In the example, I load a ZIP containing the Collada file. A class called KMZ is able to load and unpack it correctly, and it saves a lot of bandwidth. I got this tip on the Pv3d forum (thanks!)

Categories: 3D, Actionscript 3, Blender, Papervision3D

comments RSS

18 Comments

  1. Very nice example…just seen Andy’s reflection demo aswel. You guys keep posting really quality stuff. This type of usage for bend modifier could save a lot of CPU cycles… Just a wacky thought, could you make a plane mesh imitate a walking like motion.

    cheers, tim

  2. Aloha Bartek,

    Very nice work !

    I want to add that getChildByName has a recursive boolean which can be handy for big scenes. So i your case dae.getChildByName(LadyDae,true) should work aswell.

    Pay a visit are irc server freenode #papervision and meet more pv3d developers or use the webirc chat at:

    http://www.flashbookmarks.com/pv3dchat/

    Hope to see you soon :)

  3. @FlashBookmarks right! I missed the second argument in getChildByName, thanks.

  4. flashmaker

    private function onResize(e:Event=null):void {
    topBar.resize();
    blogLink.resize();
    pattern.graphics.clear(); //********************** 2x lower CPU usage while scaling
    pattern.graphics.beginBitmapFill((new Pattern() as Bitmap).bitmapData, null, true);
    pattern.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
    pattern.graphics.endFill();
    }

    Fajny blog ;-)

  5. Great one Bartek! Keep it up!

  6. Willem Van den Broeck

    Bartok, this is very interesting.

  7. Bob

    Excellent learning material.

    Works fine but just to give you a heads up of an initial error on load.
    [Fault] exception, information=TypeError: Error #1009: Cannot access a property or method of a null object reference. Fault, bendToMouse() at LadyDae.as:212
    Using current FlashDevelop3 + Flex3SDK + pv3d revision714

    Thanks for all your examples

  8. Bob

    Actually scratch that. It only happens when FD set to “Play in pop up” and not present when viewed by other methods like “Play in external player” so it is not to do with your code.

  9. mark

    Are you able to use the bend modifier on collada animations… or perhaps on instances of Fabrice’s Away3d Animator class?

  10. @mark I did not do any tests with those formats yet, but I plan to work more on integrating Bend and the other AS3Dmod modifiers with Away3D so I might be posting something on that subject.

  11. andre

    Hey Bartek!

    Have you tried loading textures for a DAE with that KMZ method?

    If i include them in the ZIP, it throws an error, saying “uknown type”.

    If i let them out of the zip, it will look for them in a meshes/whatever folder referenced in my dae, and even if i put that folder inside a “meshes” folder, it doesn’t work! :(

    bummer

  12. andre

    Well, i fixed it. Basically once you load a zip, the path to the textures becomes relative to the swf, not to the Dae.

    Still cant zip the textures, but at least it works :)

    thanks

  13. @andre nope. this example features only PV3D builtin shaded materials. The DAE itself had no material in this case. I didn’t experiment anymore with this.

  14. martin

    nice example – thx!
    my question: which program i need for this example, to open this project?
    which program can open as3proj?
    thanks

  15. martin

    ok – i have have configure this project in flashdevelop3 + flex_sdk_3.0.3.2490 + Papervision3D_rev851.
    then i get the debugmessage:
    C:\…\ladydae\src\org\papervision3d\objects\parsers\KMZ.as(76): col: 49 Error: Typ wurde nicht gefunden oder war keine Kompilierungszeit-Konstante: ZipFile.
    private function getColladaFromZip( zipFile : ZipFile ) : ByteArray {
    ^
    C:\…\ladydae\src\org\papervision3d\objects\parsers\KMZ.as(196): col: 48 Error: Typ wurde nicht gefunden oder war keine Kompilierungszeit-Konstante: ZipFile.
    private function numTexturesInZip( zipFile : ZipFile ) : uint {
    ^
    Build halted with errors (mxmlc).
    anyone has a solution?

  16. [...] within Papervision3D. I was asked about how to deform a Collada model, here is links to resources: Using Collada Bend Modifier which uses as3dmod (see below for further [...]

  17. Interestig work, this could be useful for medical education and webbased surgical simulation in the future. Added you to Twitter and Google Reader!

  18. [...] of the children from the Object3D that I get from the Collada .parseGeometry(). This is because, as Bartek Drozdz points out, “a Collada file represents a scene, not a single 3d [...]

Leave a comment



  • FITC10