Embedding a Collada File

Saturday, December 6th, 2008 | examples, requests


source

package
{
	import flash.display.Bitmap;
	import flash.events.MouseEvent;
	import flash.utils.ByteArray;
 
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.objects.parsers.DAE;
	import org.papervision3d.render.QuadrantRenderEngine;
	import org.papervision3d.view.BasicView;
 
	[SWF(width="640", height="480", backgroundColor="#000000", frameRate="60")]
	public class ColladaExample extends BasicView
	{
		[Embed(source="assets/c.DAE", mimeType = "application/octet-stream")]
		private var daeAsset:Class;
 
		[Embed(source="assets/materials/c.png")]
		private var materialAsset:Class;
 
		private var cameraPitch:Number = 90;
		private var cameraYaw:Number = 270;
		private var isOrbiting:Boolean = false;
		private var previousMouseX:Number;
		private var previousMouseY:Number;
 
		public function ColladaExample()
		{
			var byteArray:ByteArray = new daeAsset() as ByteArray;
			var dae:DAE = new DAE();
			dae.load(byteArray);
 
			var bitmap:Bitmap = new materialAsset() as Bitmap;
			var bitmapMaterial:BitmapMaterial = new BitmapMaterial(bitmap.bitmapData, true);
			dae.materials.addMaterial(bitmapMaterial, "cMaterial");
 
			scene.addChild(dae);
 
			startRendering();
 
			stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			stage.addEventListener(MouseEvent.MOUSE_WHEEL, mouseWheelHandler);
		}
 
		private function mouseWheelHandler(event:MouseEvent):void
		{
			camera.moveForward(10 * event.delta);
		}
 
		private function onMouseDown(event:MouseEvent):void
		{
			isOrbiting = true;
			previousMouseX = event.stageX;
			previousMouseY = event.stageY;
		}
 
		private function onMouseUp(event:MouseEvent):void
		{
			isOrbiting = false;
		}
 
		private function onMouseMove(event:MouseEvent):void
		{
			var differenceX:Number = event.stageX - previousMouseX;
			var differenceY:Number = event.stageY - previousMouseY;
 
			if(isOrbiting)
			{
				cameraPitch += differenceY;
				cameraYaw += differenceX;
 
				cameraPitch %= 360;
				cameraYaw %= 360;
 
				cameraPitch = cameraPitch > 0 ? cameraPitch : 0.0001;
				cameraPitch = cameraPitch < 90 ? cameraPitch : 89.9999;
 
				previousMouseX = event.stageX;
				previousMouseY = event.stageY;
 
				camera.orbit(cameraPitch, cameraYaw);
			}
		}
 
	}
}

Tags: , ,

  • Hi!
    Thanks for this great tutorial! I'm familiar with collada, but it's the first time I try to load dae files into PV3D. One question: is it not possible for PV3D to read the from dae and directly apply the correct material to the correct geometry (as they are bound in the visual scene)? What's the use of visual_scenes if one cannot directly import a whole scene into PV3D? Or is it possible?

  • Nek

    Embedding resources is vital for online flash game making 'cause most of portals accept game only as single swf file. This makes your tutorial really valuable. Thanks!

  • Denis

    I am interesting how to add to it possibility to move camera above scene in horizontal and vertical directions?

  • sugilie

    really good tutorial, gotta try it out!

  • John Lindquist

    @Trevor - good point. I'll avoid using the mousewheel for camera control in the future :/

  • Mousewheel to zoom feature kinda sucks on a mac, since it doesn't work unless you use swfmousewheel. Weird that it isn't fixed in flash player 10...

    Awesome work as always.

blog comments powered by Disqus

Search

Recommended Books

Speaking at FITC Toronto

 

December 2008
M T W T F S S
« Nov   Jan »
1234567
891011121314
15161718192021
22232425262728
293031  

Preferred Video Tutorial Resolution

  • 1024x768 (53%, 85 Votes)
  • 1280x1024 (15%, 24 Votes)
  • 1920x1080 (15%, 24 Votes)
  • 800x600 (13%, 20 Votes)
  • 480x320 (4%, 6 Votes)
  • 640x480 (0%, 2 Votes)

Total Voters: 160

Loading ... Loading ...