[Papervision3D] Collada mouse events
Admiral
admiral at nuclearpixel.com
Fri Jun 27 13:33:19 PDT 2008
Actually, it's not the collada object itself that you're adding an event
listener to, or the collada file's material that you are making interactive
at all. Collada files are actually whole scenes that you load! Collada scene
files that you load can have multiple mesh objects, and each of those mesh
objects can have it's own material lists applied to it. if you don't want to
deal with the pains of setting each object in that Collada scene to
interactive, make sure to merge all of your mesh objects into one before you
export your DAE from 3d app.
It is important to know the name of the mesh objects that you are exporting
from your 3d app, because that's how you are going to reference those mesh
objects inside of the collada scene that you have placed in your papervision
scene.
Let's say that I have an object in my 3d app called 'boat'. Let's say I
exported that as 'boat.dae'. I would first need to wait for my collada file
to finish loading into flash before I could start accessing it's contents.
In papervision, I would reference that boat mesh object with the following:
Admiral wrote:
>
> var boatDAE:DisplayObject3D = new Collada("boat.dae",null,1,null);
> boatDAE.addEventListener(FileLoadEvent.LOAD_COMPLETE, boatLoadHandler);
>
> function boatLoadHandler(){
> var boatIwant:DisplayObject3D = boatDAE.getChildByName('boat');
> boatIwant.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK,
> functionToExecute);
> }
>
You will note that the variable 'boatIwant' is now an object of type
'DisplayObject3D' which is the mesh object that you actually want to
manipulate, and you can reference it just like you would your cube.
You would then want to apply the addEventListner to the 'boatIwant' object.
Here comes the painful part. You need to know the name of the material on
the mesh object that you want to make interactive. To find out the materials
on that object, you need to access the 'materials' property the mesh object,
and turn those materials into a string so you can trace them.
...so to do that, you would modify the 'boatLoadHandler' function to look
like this.
Admiral wrote:
>
> function boatLoadHandler(){
> var boatIwant:DisplayObject3D = boatDAE.getChildByName('boat');
> boatIwant.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK,
> functionToExecute);
> trace("Materials on this Mesh object:
> "+boatIwant.materials.toString());
> }
>
The output window will tell you the names of the materials you have on that
mesh object. If there is only one material, great. let's say my output
window gives me the following:
Admiral wrote:
>
> Materials on this Mesh object: boat_texture_jpg
>
I would then know that the material on that mesh object is named
'boat_texture-jpg'. I can get access to that material and set that material
interactive by modifying the 'boadLoadHandler' function to match the
following.
Admiral wrote:
>
> function boatLoadHandler(){
> var boatIwant:DisplayObject3D = boatDAE.getChildByName('boat');
> boatIwant.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK,
> functionToExecute);
> trace("Materials on this Mesh object:
> "+boatIwant.materials.toString());
> var boatMaterial =
> boatIwant.materials.getMaterialByName('boat_texture_jpg');
> boatMaterial.interactive = true;
> }
>
At this point, when you click on your 3d object inside of the Collada scene
that you've loaded, 'functionToExecute' should execute, provided that you
have written it. ;)
Doing all of this specifying each mesh object and material for each DAE file
that I loaded didn't sound appealing to me, so I wrote a little function to
execute each time I load a DAE file. It recurses through the contents of a
Collada scene, and adds an event handler to each mesh object, then recurses
through each material on that mesh object and sets them all to interactive.
It also spits information on the mesh objects and materials on those mesh
object out to the buffer, so you get a very good idea of the contents of a
DAE scene if you don't know what's in there. I'm really not super familiar
with ActionScript 3 (or even Papervison) yet, so this is kind of dirty in
terms of 'best coding practice', but it has been working for me so far. If
anyone can improve this function and help all of us beginning users
understand the hierarchy of Papervison and Collada little more, I would much
appreciate it!
Anyway, here's the function, and how I've been able to use it.
Admiral wrote:
>
> var brain:DisplayObject3D = new Collada("brain.dae",null,1,null);
> brain.addEventListener(FileLoadEvent.LOAD_COMPLETE, daeLoadHandler);
>
> function daeLoadHandler(event:Event):void {
> trace('------daeLoadHandler-----');
> var daeInstance = event.target;
> trace('daeInstance: '+daeInstance);
> for( var name:String in daeInstance.children)
> {
> var mesh = daeInstance.getChildByName(name);
> trace('mesh: '+mesh);
> for(var name:String in mesh.materials.materialsByName)
> {
> var mesh_mat:MaterialObject3D =
> mesh.materials.materialsByName[ name ];
> trace('mesh_mat: '+name+': '+mesh_mat.toString());
> mesh_mat.interactive = true;
> trace('mesh_mat.interactive: '+mesh_mat.interactive);
> }
> mesh.addEventListener(InteractiveScene3DEvent.OBJECT_CLICK,
> animateBrain);
> }
> trace('/------daeLoadHandler-----');
> }
>
> function animateBrain(event:Event):void
> {
> trace(event.target.parent.name+' got a click');
> //you must have the tweener classes installed to use the following
> line. If you don't, just comment it out.
> Tweener.addTween(event.target, {rotationY:event.target.rotationY+360,
> time: 1, transition:"easeOutCubic"});
> }
>
I hope this has been able to help someone, I know that I struggled on this
topic in brain-melting agony for about 2 weeks before I finally got any
results.
-Admiral
Jethro Grassie wrote:
>
> I cannot find a way to make my collada objects dispatch mouse events.
> This has come up before on this list but there isnt any clear solution.
>
> In my case, I have a scene with some primitives in it which have
> interactive
> materials so respond to mouse events fine.
> Now I have a collada file with one object in it that has an image UV
> mapped
> onto it.
> I load this in using:
> var collada:DisplayObject3D = new Collada(file.dae);
> add it to the view and renders fine (and with the UV mapped material which
> is referenced in the collada file).
>
> However, adding a listener:
> collada.addEventListener(InteractiveScene3DEvent.OBJECT_OVER,
> onLocatorMouseOver);
> fails.
>
> As does:
> var cube = collada.getChildByName("cube");
> cube.addEventListener(InteractiveScene3DEvent.OBJECT_OVER,
> onLocatorMouseOver);
>
> Even when I set:
> cube.material.interactive = true;
>
> I just cannot get it to fire mouse events.
>
> Has anyone got any ideas?
>
> Thanks
>
> _______________________________________________
> Papervision3D mailing list
> Papervision3D at osflash.org
> http://osflash.org/mailman/listinfo/papervision3d_osflash.org
>
>
--
View this message in context: http://www.nabble.com/Collada-mouse-events-tp18155848p18163319.html
Sent from the Papervision3D mailing list archive at Nabble.com.
More information about the Papervision3D
mailing list