⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dae.as

📁 一个可以解析Collada文件格式的AS3类库
💻 AS
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright 2007 (c) Tim Knip, ascollada.org.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
 
package org.papervision3d.objects 
{
	import flash.events.*;
	import flash.geom.Matrix;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	import flash.utils.getTimer;
	import org.ascollada.core.*;
	import org.ascollada.fx.*;
	import org.ascollada.io.DaeReader;
	import org.ascollada.types.DaeColorOrTexture;
	import org.ascollada.utils.Logger;
	import org.papervision3d.animation.core.AnimationChannel;
	import org.papervision3d.animation.core.AnimationController;
	import org.papervision3d.animation.core.AnimationCurve;
	import org.papervision3d.core.*;
	import org.papervision3d.core.geom.*;
	import org.papervision3d.core.proto.*;
	import org.papervision3d.events.*;
	import org.papervision3d.materials.*;
	import org.papervision3d.objects.DisplayObject3D;
	
	/**
	 * 
	 */
	public class DAE extends DisplayObject3D
	{
		/** the filename */
		public var filename:String;
		
		/** */
		public var fileTitle:String;
		
		/** */
		public var baseUrl:String;
		
		/** */
		public function get document():DaeDocument { return _document; }
		
		/**
		 * constructor.
		 * 
		 * @param	asset		filename or ByteArray of the dae.
		 * @param	materials	optional MaterialsList, will override any material defined in the dae.
		 * @param	async		load some stuff async to prevent timeout errors.
		 * @return
		 */
		public function DAE(asset:*, materials:MaterialsList = null, async:Boolean = false):void
		{
			_reader = new DaeReader( async );
			
			this.materials = materials || new MaterialsList();
			
			this.filename = asset is String ? String(asset) : "../../../meshes/rawdata_dae";
			
			// make sure we've got forward slashes!
			this.filename = this.filename.split("\\").join("/");
				
			if( this.filename.indexOf("/") != -1 )
			{
				// dae is located in a sub-directory of the swf.
				var parts:Array = this.filename.split("/");
				this.fileTitle = String( parts.pop() );
				this.baseUrl = parts.join("/");
			}
			else
			{
				// dae is located in root directory of swf.
				this.fileTitle = this.filename;
				this.baseUrl = "";
			}
				
			super( fileTitle, new GeometryObject3D() );
				
			_scaling = 1;
			
			if( asset is ByteArray )
			{
				_reader.addEventListener( Event.COMPLETE, buildDAE );
				_reader.addEventListener( ProgressEvent.PROGRESS, loadProgressHandler );
				
				_asset = asset;
				
				var timer:Timer = new Timer(500);
				timer.addEventListener(TimerEvent.TIMER, delayedLoadHandler);
				timer.start();
			}
			else if( asset is XML )
			{
				
			}
			else if( asset is String )
			{		
				_reader.addEventListener( Event.COMPLETE, buildDAE );
				_reader.addEventListener( ProgressEvent.PROGRESS, loadProgressHandler );
				_reader.read( this.filename );
			}
		}
		
		/**
		 * 
		 * @param	name
		 * @return
		 */
		override public function getChildByName(name:String):DisplayObject3D
		{
			return this.findChildByName(this, name);
		}
		
		/**
		 * 
		 * @param	parent
		 * @param	camera
		 * @param	sorted
		 * @return
		 */
		override public function project( parent:DisplayObject3D, camera:CameraObject3D, sorted :Array=null ):Number
		{
			Node3D.dt = (getTimer() - _dt) * 0.001;
			
			return super.project(parent, camera, sorted);
		}
		
		/**
		 * 
		 * @param	event
		 * @return
		 */
		private function buildDAE( event:Event ):void
		{
			if(_reader.hasEventListener(Event.COMPLETE))
				_reader.removeEventListener( Event.COMPLETE, buildDAE );
				
			_document = _reader.document;
			_queuedBitmaps = new Object();
			_skins = new Dictionary();
			
			if( _document.numQueuedGeometries ) 
			{
				_reader.addEventListener( Event.COMPLETE, geometryCompleteHandler );
				_reader.addEventListener( ProgressEvent.PROGRESS, geometryProgressHandler );
			
				_reader.readGeometries();
			}
			else
			{
				geometryCompleteHandler(null);
			}
		}
				
		/**
		 * 
		 * @param	node
		 * @param	instance
		 * @return
		 */
		private function buildControllers( node:DaeNode, instance:DisplayObject3D ):void
		{
		}
		
		/**
		 * builds the papervision geometry for a collada <mesh>.
		 * 
		 * @param	geometry
		 * @param	scaling
		 * @param	yUP
		 * 
		 * @return
		 */
		private function buildGeometry( geometry:DaeMesh, scaling:Number = 1.0, yUP:Boolean = true ):GeometryObject3D
		{
			var geom:GeometryObject3D = new GeometryObject3D();
			
			geom.vertices = new Array();
			geom.faces = new Array();
			
			var v:Array = geometry.vertices;
			var f:Array = geometry.faces;
			var t:Array = geometry.texcoords;
			var n:Array = geometry.normals;
			var m:Array = geometry.materials;
			var i:int;
			
			// create vertices
			for( i = 0; i < v.length; i++ )
				geom.vertices.push( new Vertex3D(v[i][0] * scaling, v[i][1] * scaling, v[i][2] * scaling) );
			
			// create faces 
			for( i = 0; i < f.length; i++ ) {
				
				var material:MaterialObject3D = this.materials.getMaterialByName(m[i]);
				
				// get triangle's vertex indices
				var tri:Array = f[i];
				
				// get vertex refs
				var p0:Vertex3D = geom.vertices[ tri[0] ];
				var p1:Vertex3D = geom.vertices[ tri[1] ];
				var p2:Vertex3D = geom.vertices[ tri[2] ];
				
				var uvs:Array = null;
				
				// create uvs
				if( t[i] && t[i].length > 2 ) {
					var ti:Array = t[i];
					if( ti[0] && ti[1] && ti[2] ) {
						var t0:NumberUV = new NumberUV( ti[0][0], ti[0][1] );
						var t1:NumberUV = new NumberUV( ti[1][0], ti[1][1] );
						var t2:NumberUV = new NumberUV( ti[2][0], ti[2][1] );
						uvs = [t2, t1, t0];
					}
				}
				geom.faces.push( new Face3D( [p2, p1, p0], material, uvs ) ); 
			}	

			Logger.trace( "new GeometryObject3D: v:" + geom.vertices.length + " f:" + geom.faces.length );
			return geom;
		}
		
		/**
		 *
		 * @return
		 */
		private function buildImagePath( meshFolderPath:String, imgPath:String ):String
		{
			var baseParts:Array = meshFolderPath.split("/");
			var imgParts:Array = imgPath.split("/");
			
			while( baseParts[0] == "." )
				baseParts.shift();
				
			while( imgParts[0] == "." )
				imgParts.shift();
				
			while( imgParts[0] == ".." )
			{
				imgParts.shift();
				baseParts.pop();
			}
						
			var imgUrl:String = baseParts.length > 1 ? baseParts.join("/") : (baseParts.length?baseParts[0]:"");
						
			imgUrl = imgUrl != "" ? imgUrl + "/" + imgParts.join("/") : imgParts.join("/");
			
			return imgUrl;
		}
		
		/**
		 * 
		 * @param	obj
		 * @return
		 */
		private function buildJoints( obj:DisplayObject3D ):void
		{
		}
		
		private function buildMaterial( instance_material:DaeInstanceMaterial ):MaterialObject3D
		{
			var material:MaterialObject3D = this.materials.getMaterialByName(instance_material.symbol);
			
			// already in library
			if( material )
				return material;
			
			// get material from library
			var dae_material:DaeMaterial = _document.materials[ instance_material.target ];
			
			if( dae_material )
			{	
				var effect:DaeEffect = _document.effects[ dae_material.effect ];
				if( effect && effect.texture_url )
				{
					var img:DaeImage = _document.images[effect.texture_url];
					if( img )
					{
						var path:String = buildImagePath(this.baseUrl, img.init_from);
						
						material = new BitmapFileMaterial( path );
						material.addEventListener( FileLoadEvent.LOAD_COMPLETE, materialCompleteHandler );
						material.addEventListener( FileLoadEvent.LOAD_ERROR, materialErrorHandler );
						
						_queuedBitmaps[path] = instance_material.symbol;
					}
				}
				else if( effect.color is DaeLambert )
				{
					var lambert:DaeLambert = effect.color as DaeLambert;
					var c:DaeColorOrTexture = lambert.diffuse;
					
					var r:uint = Math.round(c.color[0] * 255);
					var g:uint = Math.round(c.color[1] * 255);
					var b:uint = Math.round(c.color[2] * 255);
					
					var col:uint = (r<<16 | g<<8 | b);
					
					material = new ShadedColorMaterial( col );
				}
			}
			
			materials.addMaterial( material, instance_material.symbol ); // C4

			return material;
		}
		
		/**
		 * builds a papervision Matrix3D from a node's matrices array. @see DaeNode#matrices
		 * 
		 * @param	node
		 * 
		 * @return
		 */
		private function buildMatrix( node:DaeNode ):Matrix3D {
			var matrix:Matrix3D = Matrix3D.IDENTITY;
			for( var i:int = 0; i < node.matrices.length; i++ ) {
				matrix = Matrix3D.multiply( matrix, new Matrix3D(node.matrices[i]) );
			}
			return matrix;
		}
		
		/**
		 * 
		 * @param	node
		 * 
		 * @return
		 */
		private function buildMesh( node:DaeNode ):DisplayObject3D
		{
			var newNode:DisplayObject3D = new Node3D(node.id, node.sid);
			
			for( var i:int = 0; i < node.geometries.length; i++ )
			{
				var geom_instance:DaeInstanceGeometry = node.geometries[i];
				
				var geom:DaeGeometry = _document.geometries[geom_instance.url];
				
				for each( var m:DaeInstanceMaterial in geom_instance.materials )
					buildMaterial( m );
				
				var mesh:Mesh3D = new Mesh3D(new WireframeMaterial(), new Array(), new Array());
				mesh.materials = this.materials;
				try
				{
					mesh.geometry = buildGeometry(geom.mesh);
				}
				catch( e:Error )
				{
					Logger.trace( "[ERROR] buildGeometry " + e.toString() );
				}
				mesh.geometry.ready = true;
				
				newNode.addChild(mesh);
			}
			
			return newNode;
		}
		
		/**
		 * 
		 * @param	node
		 * @param	parent
		 * 
		 * @return
		 */
		private function buildScene( node:DaeNode, parent:DisplayObjectContainer3D ):DisplayObject3D
		{	
			var newNode:DisplayObject3D;
			var skinController:DaeInstanceController = findSkinController(node);
			

			if( skinController )
			{
				newNode = buildSkin(node, skinController);
			}
			else if( node.geometries.length )
			{
				newNode = buildMesh(node);
			}
			else
			{
				newNode = new Node3D(node.id, node.sid);
			}
			
			// node instances!
			for( var j:int = 0; j < node.instance_nodes.length; j++ )
			{
				var instance_node:DaeInstanceNode = node.instance_nodes[j];
				var iNode:DaeNode = _document.getDaeNodeById(instance_node.url);
				
				var iObj:DisplayObject3D = getChildByName(iNode.id);
				
				if( iObj )
					newNode.addChild(iObj);
				else if(iNode)
					buildScene(iNode, newNode);
			}
			
			var instance:DisplayObject3D = parent.addChild(newNode);
			

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -