5

scris de:
Peter

Papervision 3D Loading Collada Files

08.06.09

Papervision 3d tutorialIn this new tutorial I will show you the basics of importing a 3d model into papervision 3D. For this tutorial you need Adobe Flash , the papervision 3d library ( you can download it here ), and a 3d Modeling application (like Google Sketchup or 3DS Max) that exports the model in Collada format ( with the .dae extension).

First you need to create you 3d model (for this tutorial I build a model that resembles a tank) or download a model from google 3D warehouse. You can get the starter files here.

To get things started we create a new flash file and start importing all the papervision classes that we need:


import org.papervision3d.scenes.Scene3D;
import org.papervision3d.view.Viewport3D;
import org.papervision3d.render.BasicRenderEngine;
import org.papervision3d.objects.DisplayObject3D;
import org.papervision3d.objects.parsers.Collada;
import org.papervision3d.cameras.Camera3D;
import org.papervision3d.materials.utils.MaterialsList;
import org.papervision3d.materials.shadematerials.FlatShadeMaterial;
import org.papervision3d.lights.PointLight3D;
import org.papervision3d.materials.shaders.CellShader;

Now let’s prepare the 3d scene:

var cam:Camera3D=new Camera3D();
var scena:Scene3D=new Scene3D();
var view:Viewport3D=new Viewport3D();
var pl:PointLight3D = new PointLight3D();
var render:BasicRenderEngine=new BasicRenderEngine();

So in order to create a papervision scene we need a camera object, a scene3D, viewport3D and a RenderEngine this is necesary for a papervision scene.

Now create the materials:

//the glow filter will give a nice outer stroke to the 3d model
var gf:GlowFilter=new GlowFilter(0x000000,1,5,5,4,1);
//this is a material from the papervision 3d library that uses a light to cast shadows on the objects
var cs:CellShader = new CellShader(pl);
//the material list contains all the materials for the different faces, in this case all the faces
//are flast shaded

var ml:MaterialsList = new MaterialsList( { all:new FlatShadeMaterial(pl) } );

Now import the 3d model and add it to the scene object:

var tank:DisplayObject3D=new Collada("tnk.dae",ml);
//because the 3d modeling tool used is different this values may need some
//trial and error before getting them right

tank.scaleX=tank.scaleY=tank.scaleZ=0.3;
tank.y=-300;
tank.z=300;
tank.rotationY=90;
//all you filters are not added to the actual model but on the viewport 3d
view.filters=[gf];
//add the tank object to the scene 3d
scena.addChild(tank);
//add the viewport to the flash display list
addChild(view);

To be able to render animations you need to encapsulate the render function in a EnterFrame event:

addEventListener(Event.ENTER_FRAME,anim);

function anim(e:Event){
tank.rotationY++;
//the renderScene function expects 3 parameters: the scene3D where all the objects are
//the camera and the viewport3D

render.renderScene(scena,cam,view);
}

Now test the movie and you should get the following result:

Get Adobe Flash player

You can download the final source files here.

Comentarii

Email-ul tau nu va fi publicat. Numele si email-ul sunt obligatorii.