📄 dae.as
字号:
* @param channels * @return */ private function buildAnimationKeys( channels:Array ):Array { var keys:Array = new Array(); var tmp:Array = new Array(); var obj:Object = new Object(); var i:int, j:int; for( i = 0; i < channels.length; i++ ) { var channel:DaeChannel = channels[i]; for( j = 0; j < channel.input.length; j++ ) { if( !(obj[ channel.input[j] ]) ) { obj[ channel.input[j] ] = true; tmp.push( {time:channel.input[j]} ); } } } tmp.sortOn("time", Array.NUMERIC); for( i = 0; i < tmp.length; i++ ) keys.push( tmp[i].time ); return keys; } /** * * @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, voffset:uint = 0 ):void { var i:int, j:int, k:int; material = _materialInstances[primitive.material] || material; material = material || MaterialObject3D.DEFAULT; Papervision3D.log(" => vertex offset : " + voffset); /* 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] = voffset + primitive.vertices[i]; idx[1] = voffset + primitive.vertices[i+1]; idx[2] = voffset + 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[ voffset + primitive.vertices[0] ]; v[1] = geometry.vertices[ voffset + primitive.vertices[1] ]; v[2] = geometry.vertices[ voffset + 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[ voffset + primitive.vertices[i-2] ]; v[1] = geometry.vertices[ voffset + primitive.vertices[i-1] ]; v[2] = geometry.vertices[ voffset + 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 = geometry.vertices || new Array(); geometry.faces = geometry.faces || new Array(); var vertexOffset : uint = geometry.vertices.length; geometry.vertices = geometry.vertices.concat(buildVertices(geom.mesh)); for( var i:int = 0; i < geom.mesh.primitives.length; i++ ) { buildFaces(geom.mesh.primitives[i], geometry, instance, material, vertexOffset); } return true; } else if(geom.spline) { for each(var spline:DaeSpline in geom.splines) instance.addChild(buildSpline(spline)); } return false; } /** * * @return */ private function buildImagePath( meshFolderPath:String, imgPath:String ):String { if (texturePath != null) imgPath = texturePath + imgPath.slice( imgPath.lastIndexOf("/") + 1 ); 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 {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -