Papervision: Mouse events on Collada models.

Papervision provides a number of useful object parsers for loading 3D models, including the DAE and Collada classes. Ive found the DAE class to work more consistently than the Collada one, so thats how im rolling. Listening for mouse events on the loaded model should be simple right? Turns out its quite a pain in [...]

Papervision provides a number of useful object parsers for loading 3D models, including the DAE and Collada classes. Ive found the DAE class to work more consistently than the Collada one, so thats how im rolling.

Listening for mouse events on the loaded model should be simple right? Turns out its quite a pain in the arse, as serveral hours of my time, and some googlin has shown. It seems you cannot listen for events on the model itself, but rather you have to loop thru all its materials , and all children of those materials, attaching eventListeners and setting the interactive flag to true.

(NOTE, ensure you also set viewport.interactive = true, and if you want hand cursors viewport.buttonMode = true)

Finally you also need to attach your model to a DisplayObject3D in order to interactive with it within the scene. All too much for my small mind, so a custom class was required, here it is warts n all. Anyone want to refine this – please do!

  1.  
  2. package com.vivace.papervision.collada
  3. {
  4.  import flash.events.IOErrorEvent;
  5.  import flash.events.MouseEvent;
  6.  
  7.  import org.papervision3d.core.proto.MaterialObject3D;
  8.  import org.papervision3d.events.FileLoadEvent;
  9.  import org.papervision3d.events.InteractiveScene3DEvent;
  10.  import org.papervision3d.objects.DisplayObject3D;
  11.  import org.papervision3d.objects.parsers.DAE;
  12.  
  13.  public class InteractiveCollada extends DisplayObject3D
  14.  {
  15.   // pass .dae path to constructor
  16.   public function InteractiveCollada(path:String)
  17.   {
  18.    super();
  19.  
  20.    this.load(path);
  21.   }
  22.  
  23.   // [properties] ////////////////////////////////////////////////////////////////////////////////////////////////
  24.  
  25.   public var model:DAE = new DAE();
  26.  
  27.   // [events] ////////////////////////////////////////////////////////////////////////////////////////////////////
  28.  
  29.   private function onFileLoadEvent(event:FileLoadEvent):void
  30.   {
  31.    // handle the various events here, im just dealing with the basics – dont be as lazy as me!
  32.    switch(event.type)
  33.    {
  34.     case FileLoadEvent.LOAD_PROGRESS :     break;
  35.     case FileLoadEvent.LOAD_ERROR  : tracel(event.message); break;
  36.     case FileLoadEvent.LOAD_COMPLETE : this.finalizeModel(); break;
  37.    }
  38.   }
  39.  
  40.   private function onIOErrorEvent(event:IOErrorEvent):void
  41.   {
  42.    trace(event.text);
  43.   }
  44.  
  45.   // ……………………………………………………………………………………………….
  46.  
  47.   private function onInteractiveEvent(event:InteractiveScene3DEvent):void
  48.   {
  49.    // again, being lazy, theres a number of events to handle here, refer to InteractiveScene3DEvent.
  50.    this.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
  51.   }
  52.  
  53.   // [public] ////////////////////////////////////////////////////////////////////////////////////////////////////
  54.  
  55.   // if its an animated collada……
  56.   public function play(clip:String = "", loop:Boolean = false):void
  57.   {
  58.    this.model.play(clip, loop);
  59.   }
  60.  
  61.   // [private] ///////////////////////////////////////////////////////////////////////////////////////////////////
  62.  
  63.   private function load(path:String):void
  64.   {
  65.    this.initEventListeners();
  66.    this.model.load(path);
  67.   }
  68.  
  69.   private function finalizeModel():void
  70.   {
  71.    // add model as child
  72.    this.addChild(this.model);
  73.  
  74.    // loop thru model to find each material and add interactivity to each mesh.
  75.    for each(var mat:MaterialObject3D in this.model.materials.materialsByName)
  76.    {
  77.     mat.interactive = true;
  78.    }
  79.    // then loop thru children recursively adding event listeners
  80.    this.addInteractiveEventListeners(this.model, InteractiveScene3DEvent.OBJECT_CLICK, this.onInteractiveEvent);
  81.   }
  82.  
  83.   private function addInteractiveEventListeners(obj:DisplayObject3D, eventType:String, handler:Function):void
  84.   {
  85.    obj.addEventListener(eventType, handler);
  86.  
  87.    for each(var child:DisplayObject3D in obj.children)
  88.    {
  89.     this.addInteractiveEventListeners(child, eventType, handler);
  90.    }
  91.   }
  92.  
  93.   private function initEventListeners():void
  94.   {
  95.    this.model.addEventListener(FileLoadEvent.LOAD_PROGRESS,  this.onFileLoadEvent);
  96.    this.model.addEventListener(FileLoadEvent.LOAD_COMPLETE,   this.onFileLoadEvent);
  97.    this.model.addEventListener(FileLoadEvent.LOAD_ERROR,   this.onFileLoadEvent);
  98.    this.model.addEventListener(FileLoadEvent.SECURITY_LOAD_ERROR,  this.onFileLoadEvent);
  99.    this.model.addEventListener(FileLoadEvent.ANIMATIONS_COMPLETE, this.onFileLoadEvent);
  100.    this.model.addEventListener(FileLoadEvent.ANIMATIONS_PROGRESS, this.onFileLoadEvent);
  101.    this.model.addEventListener(IOErrorEvent.IO_ERROR,    this.onIOErrorEvent);
  102.   }
  103.  
  104.  }
  105. }
Tagged with: colladapapervision
 

7 Responses to “Papervision: Mouse events on Collada models.”

  1. Paweł says:

    Very useful post, thanks a lot!

  2. Welcome dude. it took me too damn long to suss it out so hoped i could save others the pain!

  3. Hi,

    I m trying to figure out whats the use of FileLoadEvent.ANIMATIONS_PROGRESS event. Do u knw when it gets dispatched?

    Thanks

  4. Hey Yogesh

    i could be wrong, but from memory with animated models, the ANIMATIONS_PROGRESS and ANIMATIONS_COMPLETE events are fired after the actual .dae fil load is complete, some kind of processing of the animation i think. so if you immediately try to play the animation on LOAD_COMPLETE it may fail depending on the size of the .dae…

  5. kioses says:

    oh.. I spend much time on this problem.
    thanks for useful post.

  6. Brett says:

    Thanks buddy, this was super helpful.

  7. vinnie says:

    yo. slow response, but yeah, glad it helped!

Leave a Reply



Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...