Importing Collada into Papervison3D

Intro

In PV3D there are two Collada parsers: Collada and DAE. The DAE parser is actually an improved version of Collada parser which includes animation.

When working with Collada files, there are five commonly used scenarios found in PV3D:

  • Single Objects with UVMaps
  • Multiple Objects with UVMaps
  • Single Objects with Multiple Images
  • Interactive Objects (or Vehicles) and Environments
  • Animated Objects

In this post, you learn about Single and Multiple Objects with Simple Images. Click the image below to see a demo:

Moon Orbiting the Earth in 3DSMax

Moon Orbiting the Earth in 3DSMax

Demo

Source

YouTube

Discussion

Single and Multiple Objects with Simple Images are obviously the simplest of all the scenarios in PV3D. In the example above of the moon orbiting the earth, 3DSMax is used to do the modeling. But the procedure presented in this post is similar for other modeling software packages as well.

This is how you do it!

An object is created by a modeling program, such as 3DSMax, and a simple image is placed on that object. In 3DSMax this is done using the material editor. Each material and object is given a name so that it can be controlled by the PV3D program once the Collada file, created by your modeling program, is imported into PV3D.

Once the Collada file is exported from 3DSMax and brought into PV3D, the created objects can be easily manipulated by referring to the object names contained in the exported Collada file. If you forget what you called them during the modeling process, these names can be viewed by opening the Collada file in an editor such as Dreamweaver or Flex and navigating to the geometries library and geometry tags.

<geometry id=”earth-mesh” name=”earth”>
<geometry id=”moon-mesh” name=”moon”>

In this case the names are earth and moon.

Manipulating objects in a Collada file using AS3 is key to creating dynamic game content and use this procedure often. Importing a Collada file into PV3D is an easy 3 step process:

Importing Collada files into PV3D

Step 1: Import the Collada parser
import org.papervision3d.objects.parsers.Collada;

Step 2: Declare collada var and datatype it to Collada
private var collada:Collada;

Step 3: Instantiate the Collada class and fill in the appropriate parameters
collada = new Collada(“MoonEarth.DAE”,null,.1);
In this case, the material parameter of the Collada parser is set to null since the material is imported from the reference inside of the Collada file, found in the images library, as shown below.

<image id=”earth.jpg” name=”earth_jpg”>
<init_from>./assets/earth.jpg</init_from>
</image><image id=”moon.jpg” name=”moon_jpg”>
<init_from>./assets/moon.jpg</init_from>
</image>

Once the images are loaded, the onColladaDone method is executed which sets the planets into place and motion.

collada.addEventListener(FileLoadEvent.COLLADA_MATERIALS_DONE, onColladaDone);

To see the rest of the discussion check out Chapter 5 of the book. To see the complete code click the more button below or download the source

Collada Wrapper File

package
{
import flash.events.Event;

import org.papervision3d.events.FileLoadEvent;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.objects.parsers.Collada;
import org.papervision3d.objects.parsers.DAE;
import org.papervision3d.view.BasicView;

public class HelloCollada extends BasicView
{
private var collada:Collada;

private var earth:DisplayObject3D;
private var pivot:DisplayObject3D;
private var moon:DisplayObject3D;

public function HelloCollada()
{
//Set the size to scale to stage
super(0,0,true);
initScene();
}

private function initScene():void
{
//Create the Collada Instance, file, materials are null, and scale to a tenth.
collada = new Collada(“MoonEarth.DAE”,null,.1);
opaqueBackground=0;
//Add Listeners
collada.addEventListener(FileLoadEvent.COLLADA_MATERIALS_DONE, onColladaDone);
collada.addEventListener(FileLoadEvent.LOAD_ERROR, onColladaError);
}

private function onColladaDone(event:FileLoadEvent):void
{
trace(“loaded collada, added to scene”);

//Get a reference to earth
earth = collada.getChildByName(“earth”);

//Get a reference to moon
moon = collada.getChildByName(“moon”);

//Remove collada from the collada object.
collada.removeChild(moon);

//Create a new pivot point for the moon.
pivot = new DisplayObject3D();

//Add the moon to the pivot point
pivot.addChild(moon);

//Add the pivot point to collada
collada.addChild(pivot);

//Add the collada to the scene.
scene.addChild(collada);

//Start rendering.
startRendering();
}

private function onColladaError(event:FileLoadEvent):void
{
trace(“error loading collada”);
}

override protected function onRenderTick(event:Event=null):void
{
//Rotate the moon around the earth.
pivot.yaw(1/3);
//Rotate the earth around it’s axis.
earth.yaw(1);
//Rotate the moon around it’s axis.
moon.yaw(1);
super.onRenderTick(event);
}

}
}

About these ads

6 Responses to Importing Collada into Papervison3D

  1. […] > Importing Collada into Papervison3D « Professional Papervision3D Book […]

  2. PeZ says:

    It’s still not clear for me when you have to choose DAE parser over the Collada one

    I tought you would use DAE only when your file contains animation information but I found some cases when a simple dae file with no animation didn’t work with the Collada parser (but worked great with the DAE class)

    • Mike Lively says:

      Definitely if you are using (vertex) animation – choose DAE since the Collada parser doesn’t have it.

      The PV3D team advises that you use the DAE parser exclusively…but most examples on the web use Collada…hey it came first…so we all feel really comfortable with it.

  3. Eric says:

    When I download the files and create an empty CS4 FLA saved in the same folder with HelloCollada.as, assets, and MoonEarth.DAE and set the document class in the FLA to HelloCollada, I get a blank swf with this in my output panel:

    INFO: Papervision3D 2.0.0 (March 12th, 2009)

    INFO: Viewport autoScaleToStage : Papervision has changed the Stage scale mode.
    INFO: DisplayObject3D: earth
    WARNING: Collada material _3_-_Default not found.
    INFO: DisplayObject3D: moon
    WARNING: Collada material nextskin not found.
    INFO: DisplayObject3D:

    My version of papervision3D was downloaded from the svn checkout a few days ago. See top INFO comment above:”Papervision3D 2.0.0 (March 12th, 2009)”

    Am I missing something?

    • Mike Lively says:

      Thanks so much for this question. I was really surprised. This project was created as an ActionScript project in Flex. And to this point every ActionScript project that I have created goes over to Flash (in the way you described above) without a problem.

      But once again, this really shows the flakiness of working with Collada. I tested a similar project produced by Ralph Hauwert (main creator of PV3D 2.0) and it also failed…so I didn’t feel too bad. I would need to start from scratch in Flash and build this project up. The book’s website comes out in June. I’ll try to get it up by then. Presently, we are racing to complete the book. When I write the chapter on gaming, I’ll fix this problem and I really appreciate you alerting me to it!!!

      Once again, thanks for the question…in the words of Gomer Pile…surprise, surprise, surprise…

  4. Eric says:

    I’m glad I could help! Since struggling to find a solution for creating .dae files with UVMaps that work with PV3D, I found that using the PV3D DAE parser instead of their Collada parser has been working perfectly with my .dae files exported as Collada 1.4.1. I am exporting them directly from Modo 302 via the “Collada Exporter for modo 302, Build 19″ plugin on SourceForge.net. I haven’t had any problems with it yet and have also been able to export as polys instead of having to triangulate or convert the geometry to triangles. (Triangulate is not an option in the current release of the Modo plugin and it will produce .dae files with polygon nodes which are not suitable for the PV3D Collada parser). I am just getting started with Papervision3D and am looking forward to your book!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: