Tutorial Details
- Difficulty: Intermediate
- Platform: Flash
- Language: AS3
- Software used: Flash
- Estimated Completion Time: 15-25 mins
In this Quick Tip we’ll take a look at how to embed and display a 3D model in Flash, using Papervision3D.
Final Result Preview
Let’s take a look at the final result we will be working towards:
Introduction
To use this tutorial you will need to have a 3D model, exported as a .dae file, and its texture, as an image file.
I’m going to be using this low-poly mountain bike model from 3DOcean, created by OneManBand (who also created this neat 3D Object Viewer in AIR).
You will need to download a copy of Papervision3D (you can also find a copy in the source files)
Step 1: Creating the Flash File
Create a new ActionScript 3 document with dimensions of 550x200px and set the frame rate to 30fps. Also, set the document class to “EmbeddingDAE”.
Create a rectangle that covers the whole stage, and fill it with a radial gradient of #FFFFFF to #D9D9D9. Adjust the gradient with the Gradient Transform Tool, so it looks like this:

Step 2: Setting up the Document Class
Create a new ActionScript 3 file and name it “EmbeddingDAE”. This class will extend a class from Papervision that has all the basic functionality set up.
As we’re going to be embedding the 3D model in your SWF, we need to make sure the file has been fully loaded before trying to make use of it.
Here is the code for that:
package
{
import flash.events.Event;
import org.papervision3d.view.BasicView;
public class EmbeddingDAE extends BasicView
{
public function EmbeddingDAE()
{
this.loaderInfo.addEventListener ( Event.COMPLETE, onFullyLoaded ) ;
}
private function onFullyLoaded(e:Event):void
{
}
}
}
Step 3: Embedding the Resources
Instead of hosting our resources at a webserver and loading them from there, we’re simply going to embed them in the SWF. We do this by using the Flex SDK Embed tag. If you don’t have the Flex SDK, or are having trouble with the pre-installed version, you can download it here
Flash knows how to deal with certain types of files, like my .png texture file, but it doesn’t know the .dae file format. Therefore we have to set a secondary parameter, the MIME type, to application/octet-stream – this means the file will be transformed into a ByteArray.
When using the Embed tag, we need to be referring the relative (or full) path of the file, and assigning a class to this file. Later we can create an instance of the embedded file using this class.
Here you can see the code:
public class EmbeddingDAE extends BasicView
{
[Embed(source="mountain_bike.dae", mimeType="application/octet-stream")]
private var bikeModelClass:Class;
[Embed(source="bike_texture.png")]
private var bikeTextureClass:Class;
public function EmbeddingDAE()
You will need to replace the paths so they match your own files.
Step 4: Handling the Texture
To use our texture with our model in Papervision3D, we need to do three things:
- Create an instance of the texture as a
Bitmap– so we can access itsbitmapData. - Create a
Materialwith thisbitmapData— this will function like a texture. - Create a
MaterialsList, which will link our material to our model. It will need the name of the material used for the model. If you only have one texture file (which is most common) you do not need to worry about this, just use “all”.
Here is the code doing this (added to onFullyLoaded()):
var bitmap:Bitmap = new bikeTextureClass ( ) ; var bitmapMaterial:BitmapMaterial = new BitmapMaterial ( bitmap.bitmapData ) ; var materialsList:MaterialsList = new MaterialsList ( ) ; materialsList.addMaterial ( bitmapMaterial, "all" ) ;
Remember to import:
import flash.display.Bitmap; import org.papervision3d.materials.BitmapMaterial; import org.papervision3d.materials.utils.MaterialsList;
Step 5: Load the Model
To load our model, we need to do four things:
- Create a variable for our model – think of this as an empty shell.
- Create an instance of the
ByteArraycontaining our model. - Create an instance of the variable for our model – creating the shell.
- Load our model by passing the
ByteArrayand theMaterialsListto our empty shell.
First create the variable:
private var bikeModelDAE:DAE;
Then do the rest (adding to onFullyLoaded)
var byteArray:ByteArray = new bikeModelClass ( ) ; bikeModelDAE = new DAE ( ) ; bikeModelDAE.load ( byteArray, materialsList ) ;
Remember to import:
import flash.utils.ByteArray; import org.papervision3d.objects.parsers.DAE;
Step 6: Displaying the Model
Now all we are missing is being able to see the model, which is a piece of cake. I’m also adjusting the position of the camera so we can get a good look at this model. Then I’m telling Papervision3D to re-render every frame.
Here’s the code (again adding to onFullyLoaded()):
this.scene.addChild ( bikeModelDAE ) ; this.camera.z = 500; this.startRendering ( ) ;
This is what it will look like:

Step 7: Adding Rotation
Now we can see the model, but only from one point of view. That is a little dull isn’t it? Lets add some rotation! Here we’re going to override a function that is being called every frame by the Papervision3D engine.
override protected function onRenderTick(event:Event = null):void
{
super.onRenderTick ( event ) ;
bikeModelDAE.yaw ( 1 ) ;
}
Here it is once again:
Conclusion
Now you know how to add 3D models to your Flash projects, and it is actually quite simple. I hope you enjoyed reading and found it useful.
Thanks for reading!
where can i get loads of 3D models in .dae format for free i know there is google but……….
the help will be appreciated….:)
some free dae files:
http://www.turbosquid.com/Search/Index.cfm?keyword=&file_type=194&sort_column=A5&sort_order=asc
Nice tut thank you
@kunel:
http://sketchup.google.com/3dwarehouse/
cheers
Well, actually, you cannot use sketchup models. The structure of a DAE file formed in sketchup is very different from that made in 3ds max or maya or even blender. The sketchup dae file will not work in any of flash 3D frameworks neither in Papervision3D nor in Away3D or Alternativa3D etc.
So don’t lead people astray, Kahled ;)
Sweet!
cool!
very nice….
please post some tutorials like this….
Thanks for all the positive comments!
@ rayo
What would you specifically like to see more about?
Nice Post
Thanks for this post i really enjoy your post and i implement & i spread this info in my friends.
http://www.actionscriptinstitute.com
How can you adjust models y position?
Excellent post :)
Nice post indeed!
I can import my .dae file and see my model when I publish to debug flash player, but when I put it on website the model won’t show. I think it has something to to do with embedding the .dae file so it is part of the swf. file but I am new to this and stuck on how to do this properly. Can anyone help me with this ?????
package {
import flash.events.Event;
import org.papervision3d.view.BasicView;
import org.papervision3d.objects.parsers.DAE;
import org.papervision3d.events.FileLoadEvent;
import org.papervision3d.core.proto.MaterialObject3D;
import org.papervision3d.materials.shadematerials.GouraudMaterial;
import org.papervision3d.lights.PointLight3D;
import org.papervision3d.objects.primitives.Plane;
import org.papervision3d.objects.primitives.Cube;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.materials.ColorMaterial;
[SWF (width="1000", height="800", backgroundColor="0x000000", frameRate="30")]
public class DAE1 extends BasicView
{
public var dae:DAE;
public var cube : Cube
public function DAE1()
{
super(1000,800, false);
dae = new DAE();
dae.load(“../assets/BRAINM.dae”);
dae.scale = 50;
dae.y = -180;
scene.addChild(dae);
var mat : ColorMaterial = new ColorMaterial(0×880000);
var ml : MaterialsList = new MaterialsList();
ml.addMaterial(mat, “all”);
ml.addMaterial(new ColorMaterial(0×660000),”right”);
ml.addMaterial(new ColorMaterial(0×440000),”bottom”);
ml.addMaterial(new ColorMaterial(0xdd0000),”top”);
ml.addMaterial(new ColorMaterial(0xdd0000),”left”);
cube = new Cube(ml,1,10,10,3,3,3);
cube.x = 10;
cube.y = 10;
dae.addChild(cube);
cube = new Cube(ml,1,10,10,3,3,3);
cube.x = -10;
cube.y = 10;
dae.addChild(cube);
cube = new Cube(ml,10,1,10,3,3,3);
cube.z = -10;
cube.y = 10;
dae.addChild(cube);
cube = new Cube(ml,10,1,10,3,3,3);
cube.z = 10;
cube.y = 10;
dae.addChild(cube);
dae.addEventListener(FileLoadEvent.LOAD_COMPLETE, daeLoadComplete);
camera.fov = 45;
camera.z = -2000;
addEventListener(Event.ENTER_FRAME, enterFrame);
}
public function daeLoadComplete(e:FileLoadEvent) : void
{
trace(“DAE LOADED!”);
trace(dae.materials);
}
public function enterFrame(e:Event) :void
{
dae.yaw((500-mouseX)*0.0001);
singleRender();
}
}
}
how about away3d. is it the same concept like papervision3d to import 3d model into flash? can you do a tutorial on how to import 3d model into flash using away3d