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

📄 dae.as.svn-base

📁 一个2D基于verlet的Flash物理引擎。它用AS3编写而成。Fisix的目标是应用到游戏等计算量很大的实时应用中。尽管flash比c/c++要慢,很棒的物理引擎
💻 SVN-BASE
📖 第 1 页 / 共 4 页
字号:
		/**
		 * 
		 * @return
		 */
		private function buildColor( daeColor:Array ):uint
		{
			var r:uint = daeColor[0] * 0xff;
			var g:uint = daeColor[1] * 0xff;
			var b:uint = daeColor[2] * 0xff;
			return (r<<16|g<<8|b);
		}
		
		/**
		 * 
		 * @param	primitive
		 * @param	geometry
		 * @param	instance
		 * @param	material
		 * 
		 * @return
		 */
		private function buildFaces( primitive:DaePrimitive, geometry:GeometryObject3D, instance:DisplayObject3D, material:MaterialObject3D = null ):void
		{
			var i:int, j:int, k:int;
			
			material = _materialInstances[primitive.material] || material;
			
			material = material || MaterialObject3D.DEFAULT;
			/*
			if( !instance.materials )
				instance.materials = new MaterialsList();
				
			if( !instance.materials.getMaterialByName(primitive.material) )
			{
				instance.materials.addMaterial(material, primitive.material);
			}
			*/
			var texcoords:Array = new Array();
			
			// retreive correct texcoord-set for the material.
			var obj:DaeBindVertexInput = _materialTextureSets[primitive.material] is DaeBindVertexInput ? _materialTextureSets[primitive.material] : null;
			var setID:int = (obj is DaeBindVertexInput) ? obj.input_set : 0;
			var texCoordSet:Array = primitive.getTexCoords(setID); 
			
			// texture coords
			for( i = 0; i < texCoordSet.length; i++ ) 
			{
				var t:Array = texCoordSet[i];
				texcoords.push( new NumberUV( t[0], t[1] ) );
			}
			
			var hasUV:Boolean = (texcoords.length == primitive.vertices.length);

			var idx:Array = new Array();
			var v:Array = new Array();
			var uv:Array = new Array();
			
			switch( primitive.type ) 
			{
				// Each line described by the mesh has two vertices. The first line is formed 
				// from first and second vertices. The second line is formed from the third and fourth 
				// vertices and so on.
				case ASCollada.DAE_LINES_ELEMENT:
					for( i = 0; i < primitive.vertices.length; i += 2 ) 
					{
						v[0] = geometry.vertices[ primitive.vertices[i] ];
						v[1] = geometry.vertices[ primitive.vertices[i+1] ];
						uv[0] = hasUV ? texcoords[  i  ] : new NumberUV();
						uv[1] = hasUV ? texcoords[ i+1 ] : new NumberUV();
						//geometry.faces.push( new Triangle3D(instance, [v[0], v[1], v[1]], material, [uv[0], uv[1], uv[1]]) );
					}
					break;
					
				// Each line-strip described by the mesh has an arbitrary number of vertices. Each line 
				// segment within the line-strip is formed from the current vertex and the preceding 
				// vertex.
				case ASCollada.DAE_LINESTRIPS_ELEMENT:
					for( i = 1; i < primitive.vertices.length; i++ ) 
					{
						v[0] = geometry.vertices[ primitive.vertices[i-1] ];
						v[1] = geometry.vertices[ primitive.vertices[i] ];
						uv[0] = hasUV ? texcoords[i-1] : new NumberUV();
						uv[1] = hasUV ? texcoords[i] : new NumberUV();
						//geometry.faces.push( new Triangle3D(instance, [v[0], v[1], v[1]], material, [uv[0], uv[1], uv[1]]) );
					}
					break;
					
				// simple triangles
				case ASCollada.DAE_TRIANGLES_ELEMENT:
					for( i = 0, j = 0; i < primitive.vertices.length; i += 3, j++ ) 
					{
						idx[0] = primitive.vertices[i];
						idx[1] = primitive.vertices[i+1];
						idx[2] = primitive.vertices[i+2];
						
						v[0] = geometry.vertices[ idx[0] ];
						v[1] = geometry.vertices[ idx[1] ];
						v[2] = geometry.vertices[ idx[2] ];
						
						uv[0] = hasUV ? texcoords[ i+0 ] : new NumberUV();
						uv[1] = hasUV ? texcoords[ i+1 ] : new NumberUV();
						uv[2] = hasUV ? texcoords[ i+2 ] : new NumberUV();
						
						geometry.faces.push( new Triangle3D(instance, [v[0], v[1], v[2]], material, [uv[0], uv[1], uv[2]]) );
					}
					break;
				
				// Each triangle described by the mesh has three vertices. 
				// The first triangle is formed from the first, second, and third vertices. 
				// Each subsequent triangle is formed from the current vertex, reusing the 
				// first and the previous vertices.
				case ASCollada.DAE_TRIFANS_ELEMENT:
					v[0] = geometry.vertices[ primitive.vertices[0] ];
					v[1] = geometry.vertices[ primitive.vertices[1] ];
					v[2] = geometry.vertices[ primitive.vertices[2] ];
					uv[0] = hasUV ? texcoords[0] : new NumberUV();
					uv[1] = hasUV ? texcoords[1] : new NumberUV();
					uv[2] = hasUV ? texcoords[2] : new NumberUV();
					
					geometry.faces.push( new Triangle3D(instance, [v[0], v[1], v[2]], material, [uv[0], uv[1], uv[2]]) );
					
					for( i = 3; i < primitive.vertices.length; i++ ) 
					{
						v[1] = geometry.vertices[ primitive.vertices[i-1] ];
						v[2] = geometry.vertices[ primitive.vertices[i] ];
						uv[1] = hasUV ? texcoords[i-1] : new NumberUV();
						uv[2] = hasUV ? texcoords[i] : new NumberUV();
						
						geometry.faces.push( new Triangle3D(instance, [v[0], v[1], v[2]], material, [uv[0], uv[1], uv[2]]) );
					}
					break;
				
				// Each triangle described by the mesh has three vertices. The first triangle 
				// is formed from the first, second, and third vertices. Each subsequent triangle 
				// is formed from the current vertex, reusing the previous two vertices.
				case ASCollada.DAE_TRISTRIPS_ELEMENT:
					v[0] = geometry.vertices[ primitive.vertices[0] ];
					v[1] = geometry.vertices[ primitive.vertices[1] ];
					v[2] = geometry.vertices[ primitive.vertices[2] ];
					uv[0] = hasUV ? texcoords[0] : new NumberUV();
					uv[1] = hasUV ? texcoords[1] : new NumberUV();
					uv[2] = hasUV ? texcoords[2] : new NumberUV();
					
					geometry.faces.push( new Triangle3D(instance, [v[0], v[1], v[2]], material, [uv[0], uv[1], uv[2]]) );
					
					for( i = 3; i < primitive.vertices.length; i++ ) 
					{
						v[0] = geometry.vertices[ primitive.vertices[i-2] ];
						v[1] = geometry.vertices[ primitive.vertices[i-1] ];
						v[2] = geometry.vertices[ primitive.vertices[i] ];
						uv[0] = hasUV ? texcoords[i-2] : new NumberUV();
						uv[1] = hasUV ? texcoords[i-1] : new NumberUV();
						uv[2] = hasUV ? texcoords[i] : new NumberUV();
						
						geometry.faces.push( new Triangle3D(instance, [v[0], v[1], v[2]], material, [uv[0], uv[1], uv[2]]) );
					}
					break;
					
				// polygon with *no* holes
				case ASCollada.DAE_POLYLIST_ELEMENT:
					for( i = 0, k = 0; i < primitive.vcount.length; i++ ) 
					{
						var poly:Array = new Array();
						var uvs:Array = new Array();
						for( j = 0; j < primitive.vcount[i]; j++ ) 
						{
							uvs.push( (hasUV ? texcoords[ k ] : new NumberUV()) );
							poly.push( geometry.vertices[primitive.vertices[k++]] );
						}
						
						if( !geometry || !geometry.faces || !geometry.vertices )
							throw new Error( "no geomotry" );
						if( !instance )
							throw new Error( "no instance" );
							
						v[0] = poly[0];
						uv[0] = uvs[0];
						
						for( j = 1; j < poly.length - 1; j++ )
						{
							v[1] = poly[j];
							v[2] = poly[j+1];
							uv[1] = uvs[j];
							uv[2] = uvs[j+1];
							geometry.faces.push( new Triangle3D( instance, [v[0], v[1], v[2]], material, [uv[0], uv[1], uv[2]]) );
						}
					}
					break;
				
				// polygon with holes...
				case ASCollada.DAE_POLYGONS_ELEMENT:
					break;
					
				default:
					break;
			}
		}
		
		/**
		 * 
		 * @param	asset
		 * @return
		 */
		private function buildFileInfo( asset:* ):void
		{
			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 = "";
			}
		}
		
		/**
		 * 
		 * @param	daeId
		 * @param	instance
		 * @return
		 */
		private function buildGeometry( daeId:String, instance:DisplayObject3D, material:MaterialObject3D = null ):Boolean
		{
			var geom:DaeGeometry = document.geometries[ daeId ];
			
			if( !geom )
				return false;
				
			if( geom.mesh )
			{
				instance.geometry = instance.geometry ? instance.geometry : new GeometryObject3D();
				
				var geometry:GeometryObject3D = instance.geometry;
					
				geometry.vertices = buildVertices(geom.mesh);
				geometry.faces = new Array();
				
				for( var i:int = 0; i < geom.mesh.primitives.length; i++ )
					buildFaces(geom.mesh.primitives[i], geometry, instance, material);
					
				geometry.ready = true;
				
				Logger.trace( "created geometry v:" + geometry.vertices.length + " f:" + geometry.faces.length );
				
				return true;
			}
			
			return false;
		}
		
		/**
		 *
		 * @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;
		}
		
		/**
		 * 
		 * @return
		 */
		private function buildMaterials():void
		{
			var symbol2target:Object = document.materialSymbolToTarget;
				
			for( var materialId:String in document.materials )
			{
				var mat:DaeMaterial = document.materials[ materialId ];
				var exists:Boolean = false;
				
				for ( var name:String in this.materials.materialsByName )
				{
					if( symbol2target[name] == mat.id )
					{
						exists = true;
						break;
					}
				}
				
				if( exists )
					continue;
				
				var effect:DaeEffect = document.effects[ mat.effect ];
				
				var lambert:DaeLambert = effect.color as DaeLambert;
				
				if(lambert && lambert.diffuse.texture)
				{
					_materialTextureSets[mat.id] = lambert.diffuse.texture.texcoord;
				}
				
				var material:MaterialObject3D;
				
				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.tiled = true;
						material.addEventListener( FileLoadEvent.LOAD_COMPLETE, materialCompleteHandler );
						material.addEventListener( FileLoadEvent.LOAD_ERROR, materialErrorHandler );
						this.materials.addMaterial(material, mat.id );
						continue;
					}
				}					
				
				if( lambert && lambert.diffuse.color )
				{
					material = new ColorMaterial( buildColor(lambert.diffuse.color)/*, lambert.transparency*/ );
				}
				else
				{
					material = MaterialObject3D.DEFAULT;
				}
				
				this.materials.addMaterial(material, mat.id );
			}
		}
		
		/**
		 * builds material instances from loaded materials.
		 * 
		 * @param 	instances	Array of DaeInstanceMaterial. @see org.ascollada.fx.DaeInstanceMaterial
		 * @return
		 */
		private function buildMaterialInstances(instances:Array):MaterialObject3D
		{
			var firstMaterial:MaterialObject3D;
			
			for each( var instance_material:DaeInstanceMaterial in instances )
			{
				var material:MaterialObject3D = this.materials.getMaterialByName(instance_material.symbol);
					
				if( !material )
					material = this.materials.getMaterialByName(instance_material.target);
				
				if( !material )
					continue;
					
				_materialInstances[instance_material.symbol] = material;
				
				if( !firstMaterial )
					firstMaterial = material;
					
				// setup texcoord-set for the material.
				if(	_materialTextureSets[instance_material.target] )
				{
					var semantic:String = _materialTextureSets[instance_material.target];			
					var obj:DaeBindVertexInput = instance_material.findBindVertexInput(semantic);	
					if( obj )
						_materialTextureSets[instance_material.symbol] = obj;
				}
			}
			
			return firstMaterial;
		}
		
		/**
		 * builds a papervision Matrix3D from a node's matrices array. @see org.ascollada.core.DaeNode#transforms
		 * 
		 * @param	node
		 * 
		 * @return
		 */
		private function buildMatrix( node:DaeNode ):Matrix3D 
		{
			var matrix:Matrix3D = Matrix3D.IDENTITY;
			for( var i:int = 0; i < node.transforms.length; i++ ) 
			{
				var transform:DaeTransform = node.transforms[i];
				
				matrix = Matrix3D.multiply( matrix, new Matrix3D(transform.matrix) );
			}	
			return matrix;
		}
		
		/**
		 * 
		 * @param	node
		 * @return
		 */
		private function buildMatrixStack( node:DaeNode ):Array
		{
			var stack:Array = new Array();
			for( var i:int = 0; i < node.transforms.length; i++ ) 
			{
				var transform:DaeTransform = node.transforms[i];				
				var matrix:Matrix3D = new Matrix3D(transform.matrix);
				stack.push(matrix);
			}

⌨️ 快捷键说明

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