google ebanulso na otlichnenko


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

5 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.?

Leave a Reply


Follow papervision2 on Twitter

Flash and the City banner
2010 Flash And The City Speaker

RSS Feed