Loading Complex Models (v2)

complex-models

download-source

If you work long enough in Papervision you will eventually have to deal with loading complex models into Papervision. This is one of the more difficult things to do but not because of the actionscript or Papervision coding. Whenever we have had problems with models in the past; it has always been an issue model's source.

Hopefully you are one of the few blessed enough to have an resource available to modify and create Papervision ready models for you. If not, just know you're not alone.

Issues with 3D models aside, the code for loading a 3D model into Papervision is extremely easy. It is as simple as loading a plane, cube, or cone.

Actionscript:
  1. package 
  2. {
  3.     import flash.events.Event;
  4.     import org.papervision3d.materials.BitmapFileMaterial;
  5.     import org.papervision3d.materials.utils.MaterialsList;
  6.     import org.papervision3d.objects.DisplayObject3D;
  7.     import org.papervision3d.objects.parsers.Collada;
  8.     import org.papervision3d.view.BasicView;
  9.  
  10.     public class Main extends BasicView
  11.     {
  12.         protected var cow:DisplayObject3D;
  13.         protected var materialList:MaterialsList;
  14.         protected var bitmapFileMaterial:BitmapFileMaterial;
  15.         public function Main()
  16.         {
  17.             super();
  18.             createChildren();
  19.             startRendering();
  20.         }
  21.         public function createChildren():void
  22.         {
  23.             //Setup the materials manually (sometimes the dae handles this without issue)
  24.             materialList       = new MaterialsList();
  25.             bitmapFileMaterial  = new BitmapFileMaterial("daeModel/Cow.png");
  26.             materialList.addMaterial(bitmapFileMaterial,"all");
  27.            
  28.             //Create the new Collada Object with materialList
  29.             cow = new Collada("daeModel/cow.dae",materialList);
  30.  
  31.             //Set some properties
  32.             cow.moveDown(150);
  33.             cow.scale = 3;
  34.             cow.pitch( -10);
  35.  
  36.             //Add to scene
  37.             scene.addChild(cow);
  38.         }
  39.        
  40.         override protected function onRenderTick(event:Event = null):void
  41.         {
  42.             super.onRenderTick(event);
  43.  
  44.             //Rotate
  45.             cow.yaw(1);
  46.         }
  47.     }
  48. }

Once you have a good model, in papervision all you need for this to work properly is to:
1) Create the model new Collada()
2) Create the model's texture new BitmapFileMaterial()
3) Create and add the bitmap material to a materials list new MaterialsList()
4) Add the model to the scene scene.addChild(cow)
5) Render startRendering()

It's too much to cover all the issues you may run into here but if you're up for it open up the cow.dae file. You'll find it is just a lot of XML. Go through it, see if you can find the texture file.

download-source

Post to Twitter Post to Delicious Delicious Post to Digg Digg This Post Post to Facebook Facebook Post to StumbleUpon Stumble This Post

10 Responses to “Loading Complex Models (v2)”


  • Can this load .png textures that have transparency?

  • Hi,

    I’ve used my own model instead of the cow, everything seems to load perfectly except it doesn’t rotate. I’ve copied paste the exact same codes. Any ideas why? thanks !!

  • Hi Wolf,

    don’t be afraid, the rotation instruction are only called one time in the constructor (and so, applied one time only). If you wanna see the cow rotating, just copy paste

    cow.pitch( -10);

    in the rendering part (not because it call the render function, but because this function is called every frame. Any recurring instruction make the job done).

    Btw, you can also use cow.yaw(n), cow.rotationX += n, etc.

  • I change .dae

    but

    INFO: Viewport autoScaleToStage : Papervision has changed the Stage scale mode.
    INFO: BitmapFileMaterial: Loading bitmap from daeModel/guy.jpg
    ERROR: BitmapFileMaterial: Unable to load file daeModel/guy.jpg
    ERROR: COLLADA file load error

    Can you help me?

  • So can you animate the separate parts of the loaded object? In this example – can you make the bell on the cow’s neck swing, or make her walk etc.?

  • I am an experienced 3d artist, but new to actionscript in general.

    Simple question; I get it to compile without errors, but get a blank window.

    Where do I set up the texture + model so that it loads it?

  • Hi,

    Thanks for the great tutorial. I’m just starting to learn AS3 and Papervision for some projects. I have one question:

    The files work just fine, i’ve applied a new texture to the cow and it loads just fine, but when i try to replace the original .dae file with a model i need (also .dae, i just replace yours and keep the same name “cow.dae”), i get an error:

    “TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at org.papervision3d.objects.parsers::Collada/buildObject()
    at org.papervision3d.objects.parsers::Collada/parseGeometry()
    at org.papervision3d.objects.parsers::Collada/parseNode()
    at org.papervision3d.objects.parsers::Collada/parseScene()
    at org.papervision3d.objects.parsers::Collada/buildCollada()
    at org.papervision3d.objects.parsers::Collada/onComplete()
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at flash.net::URLLoader/onComplete()

    What’s this about? how can i make it work? Any ideeas?

    Thanks a lot :)
    Stefan

  • Hi, thanks for the tutorial.I have one question:
    everything is OK when I use your code and you .dae but when I change the .dae with mine, it seems OK, but the 3d model isn’t show up. Can you explain to me why?I already export the 3d model in blender with collada 1.4 but the .dae can’t be opened when I compile it.Thanks.

    Chrz

  • I get the exact same problem as Stefan and Chrz. What can I do?

Leave a Reply


Follow WovenCharlie on Twitter

Flash and the City banner
2010 Flash And The City Speaker

RSS Feed