Papervision Collada Interactivity

Getting interactivity to work with Papervision3D Collada objects can be a little tricky.

There are two main confusion points:

  1. The Collada class is a container for the 3D objects defined in the .dae file. You will need to get a reference to the child object of the Collada class for which you want to interact with. I found this to be a little troublesome because of some getChildByName not working as I expected it to. I also found it clunky to have to hardcode somewhere the name of the object as defined by the designer. So I cobbled together the following function to return the first child DirectObject3D for a given Collada object. Fortunately for me, the collada files I am using are simple and only contain one object per. If yours are more complex, you may have to go a different route. It's a little hacky as you can tell, but it works.
  2. public function getColladaDisplayObj(tempCO:DisplayObject3D):DisplayObject3D {
    var returnObj:DisplayObject3D;
    
    try {
    var childname:String = String(tempCO.childrenList()).substring(0,String(tempCO.childrenList()).indexOf(": "));
    
    returnObj = tempCO.getChildByName(childname);
    this.myDisplayObj = returnObj;
    
    //trace("found colladaNode: " + childname);
    } catch(errObject:Error) {
    trace(errObject.message);
    }
    
    return returnObj;
    }
    
  3. Interactivity events are based on the material that is applied to the object, not the object itself. So, you need to obtain a reference to the object's material, set interactive=true, and then add your event listener to the object. Here is another function that I hacked together to retrieve a reference to a Collada child object's material.

    private function getColladaNodeMaterial(colladaNode:DisplayObject3D):MaterialObject3D {
    var returnVal:MaterialObject3D;
    
    try {
    var materialName:String = StringUtil.trim(String(colladaNode.materials));
    trace("materialName: '" + materialName + "'");
    returnVal = colladaNode.getMaterialByName(materialName);
    } catch(errObject:Error) {
    trace(errObject.message);
    }
    
    return returnVal;
    }
    

Another way to add interactivity is to create a material instance and set materialName.interactive = true, add that material to a MaterialList and then pass that MaterialList as a parameter when instantiating the Collada class. I personally am not a big fan of this approach however, because the Collada files I am using already have the material defined internally. Doing it the way I described above, I don't need to know how the object is named by the designer or the name of the material file.
If you have any suggestions, questions, or know a better way please let me know!

Comments

Unknown said…
Hello.
Where can i find the StringUtil Class ?

Thanks.
CodeJockey said…
StringUtil is from the AS3 CoreLib on GoogleCode.

http://code.google.com/p/as3corelib/
Juan said…
Hi there... im trying to change some propertys of the object ones i get it... but i cant... any idea?

thanks CJ in advance.
Juan said…
sorry.. i forgot to tell you is a collada with two objects inside...

i allready now the object name...

Popular Posts