Tutorial: How to interact with a .DAE object in Papervision3D

November 13, 2008

How to interact with a .DAE object in Papervision3D

tutorial_Papervision3dNotes

I assume the reader has a moderate understanding of Actionscript 3, PV3D, and OOP.

I have found Nabble.com to be a great source for PV3D information. PV3D team members make comments here as well, which helped result in the solution below. The changing materials at runtime code was found here.I have only tested this with the ColorMaterial object, let me know if you get it to work with other materials, or shaders for that matter.

Introduction

I’ve seen many questions with respect to interacting with .DAE files using Papervision3D and have been searching for an answer myself for quite some time now. I have found a solution and have clarified a few things I was unsure of along the way.

1) You don’t interact with the .DAE itself, but the material(s) of the .DAE file.

2) You can change the materials at runtime.

Step 1 (Loading the .DAE)

Basically, after you load in your .DAE file (example code snippits below)…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//IMPORT FOR DAE

import org.papervision3d.objects.parsers.DAE;

______________________________________

//3D OBJECTS (DAE VARIABLE CREATION)

public var dae:                        DAE;

______________________________________

//EXAMPLE FUNCTION FOR LOADING DAE (MUST BE CALLED SOMEWHERE IN YOUR CODE)

public function loadDAE():void

{

//LOAD THE 3D MODEL (.DAE) (DON'T FORGET TO CREATE A MATERIALS LIST OBJECT)

dae = new DAE();

dae.addEventListener(FileLoadEvent.LOAD_COMPLETE, onDAELoaded);

dae.load("assets/daeFolder/3dObject.DAE", mainMaterialsList);

}

…and apply the materialsList, you need to iterate through all the children of the .DAE (down to the triangle faces of the 3D Object’s geometry) and addEventListeners for each child of the DisplayObject3D, essentially each face of the 3D object (.DAE).

* You need to open your .DAE in a text editor (to view the XML code of the .DAE) and find the snippit that resembles this…

<library_materials>

<material id=”_01___Default” name=”_01___Default”>

<instance_effect url=”#_01___Default-fx”/>

</material>

</library_materials>

… the value of the name attribute is what you pass as the second argument for the MaterialsList object. An example follows…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
mainMaterialsList.addMaterial(material_Default, "_01___Default");

... this is what allows the material(s) of the MaterialsList object to be applied to the .DAE.
<h4>Step 2 (Adding Event Listeners for each child of the .DAE)</h4>
The code for adding event listeners to each child, down to each face of the 3D object, is found in the block below.

//CALL THE ADDEVENTLISTENERS FUNCTION AND PASS IN ARGUMENTS (example below)

addEventListeners(tempClone, InteractiveScene3DEvent.OBJECT_OVER, onObjectOver);

______________________________________

//ENSURE ALL CHILDREN IN DAE LISTEN TO EVENTS

private function addEventListeners(displayObject:DisplayObject3D, eventType:String, listener:Function):void

{

//ADD LISTENER TO DISPLAY OBJECT

displayObject.addEventListener(eventType, listener);

//ADD LISTENER TO ALL CHILDREN OF DISPLAY OBJECT

for each(var child:DisplayObject3D in displayObject.children) {

addEventListeners(child, eventType, listener);

}

}

This ensures every face of your DisplayObject3D (.DAE) is listening for the specified InteractiveScene3DEvent. You then simply create the listener function and you now will have a .DAE file that can listen for an InteractiveScene3DEvent(s). If you want to learn how to change materials at runtime move on to Step 3.

Step 3 (Changing Materials at Runtime)

This changing materials at runtime code was found here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
public function changeMaterialsTo(object:DisplayObject3D, material:MaterialObject3D):void

{

if (object.children)

{

for each(var child:DisplayObject3D in object.children)

{

changeMaterialsTo(child, material);

}

}

if (!object.material)

{

return;

}

if (!object.geometry.faces[0]) // The object is a shape, skip

{

return;

}

var original:MaterialObject3D;

// This is a workaround for a bug. object.material should have the updated material

// but for some reason its stuck on WireframeMaterial

if (object.geometry.faces[0].material)

{

original = object.geometry.faces[0].material;

}

else

{

original = object.material;

}

object.material = material;

for each( var triangle:Triangle3D in object.geometry.faces )

{

triangle.material = object.material;

}

}

Hope this helps, if you have any questions don’t be afraid to ask.

7 Responses to “Tutorial: How to interact with a .DAE object in Papervision3D”

  1. Ronny says:

    Wonderful, but known =)

  2. miguel says:

    very helpful thanks

  3. Hello to all :) I can’t understand how to add your site in my rss reader. Help me, please

  4. Cricecepe says:

    yo, circstar.com great name for site)))
    ————————
    internet signature: http://qugor.ru

  5. Evewmeway says:

    yo, circstar.com great name for site)))
    ————————
    my blog: http://youraudiovox.com/music65/map.html

  6. Indyaner says:

    Would love to see the Code in Final because I find it hard to figure out on wich part I have to insert the specifics snipplets. Can you please add a link to your as?

    Aditional Question: Isnt this WAy not a Resourcekiller? I mean a EvenHandler for every Triangle on a DAE-Model… sounds pretty heavy if you ask me. But at least you found a way to interact with the Materials on a DAE. Thanks.

  7. circstar says:

    Indyaner reply…

    - The .as is part of a more complex project which I’m not releasing at this point. But if you have questions regarding “which part I have to insert the specific snippets” I can gladly help. If you send me a detailed email (dakknox@gmail.com) about what you need and are trying to accomplish, I’d be more than willing to help you out.

    - In testing, the interactive quality is very smooth (event handling runs fine), what bogs it down is when I use a more detailed model(s).

Leave a Reply