//////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> Collate's the lightmap geometry from a COLLADA file into a usable format. </summary> /// /// <param name="colladaFile"> The collada file. </param> /// /// <returns> A list of lightmap mesh data. </returns> List<Lightmap> CollateLightmapGeometry(ColladaFile colladaFile) { var lightmapMeshes = new List<Lightmap>(); // Collect the geometry info foreach (var geometry in colladaFile.LibraryGeometries.Geometry) { if (geometry.Mesh == null) { SendMessage(String.Format("The geometry {0} does not have a mesh", geometry.Name)); return null; } if (geometry.Mesh.Triangles.Count == 0) { SendMessage(String.Format("The geometry {0} has no triangles", geometry.Name)); return null; } // Get the lightmap index from the end of the geometry name if(geometry.Name.EndsWith("-1")) { continue; } var indexString = Regex.Match(geometry.Name, @"\d+$").Value; if (System.String.IsNullOrEmpty(indexString)) { SendMessage(String.Format("The geometry {0} does not have the lightmap index at the end of it's name", geometry.Name)); return null; } int lightmapIndex = Int32.Parse(indexString); // Create the lightmap entry var lightmap = new Lightmap() { LightmapIndex = lightmapIndex, Faces = new List<Lightmap.Face>() }; foreach (var triangles in geometry.Mesh.Triangles) { // Get the input object containing the texture coordinates var texcoord1Input = triangles.Input.Find(entry => (entry.Set == 1) && (entry.Semantic == ColladaInputSharedSemantic.TEXCOORD)); if (texcoord1Input == null) { SendMessage(String.Format("Unable to find a second texture coordinate for geometry {0}", geometry.Name)); return null; } var sourceID = texcoord1Input.Source.TrimStart('#'); // Get the source array for the coord data var sourceArray = geometry.Mesh.Source.Find( entry => { return entry.ID.EndsWith(sourceID); } ); if (sourceArray == null) { SendMessage(String.Format("Unable to find a texture coordinate source array for geometry {0}", geometry.Name)); return null; } if (sourceArray.FloatArray == null) { SendMessage(String.Format("Unable to find a texture coordinate source array for geometry {0}", geometry.Name)); return null; } // Collect the vertex data int sourceArrayStride = (int)sourceArray.TechniqueCommon.Accessor.Stride; var indexStride = (triangles.P.Values.Count / 3) / (int)triangles.Count; foreach (var faceIndex in Enumerable.Range(0, (int)triangles.Count)) { var face = new Lightmap.Face(); // Get the index of the texture coordinates for the face vertices int faceOffset = (indexStride * 3) * faceIndex; // Offset to start of current face int vertex0Offset = faceOffset + (0 * indexStride) + (int)texcoord1Input.Offset; int vertex1Offset = faceOffset + (1 * indexStride) + (int)texcoord1Input.Offset; int vertex2Offset = faceOffset + (2 * indexStride) + (int)texcoord1Input.Offset; int vertex0Index = triangles.P.Values[vertex0Offset]; int vertex1Index = triangles.P.Values[vertex1Offset]; int vertex2Index = triangles.P.Values[vertex2Offset]; // Get the data from the float array int uIndex = (vertex0Index * sourceArrayStride); int vIndex = (vertex0Index * sourceArrayStride) + 1; face.Vertex0.X = sourceArray.FloatArray.Values[uIndex]; face.Vertex0.Y = (sourceArray.FloatArray.Values[vIndex] - 1) * -1; uIndex = (vertex1Index * sourceArrayStride); vIndex = (vertex1Index * sourceArrayStride) + 1; face.Vertex1.X = sourceArray.FloatArray.Values[uIndex]; face.Vertex1.Y = (sourceArray.FloatArray.Values[vIndex] - 1) * -1; uIndex = (vertex2Index * sourceArrayStride); vIndex = (vertex2Index * sourceArrayStride) + 1; face.Vertex2.X = sourceArray.FloatArray.Values[uIndex]; face.Vertex2.Y = (sourceArray.FloatArray.Values[vIndex] - 1) * -1; lightmap.Faces.Add(face); } } lightmapMeshes.Add(lightmap); } return lightmapMeshes; }
//////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> Collate's the lightmap geometry from a COLLADA file into a usable format. </summary> /// /// <param name="colladaFile"> The collada file. </param> /// /// <returns> A list of lightmap mesh data. </returns> List <Lightmap> CollateLightmapGeometry(ColladaFile colladaFile) { var lightmapMeshes = new List <Lightmap>(); // Collect the geometry info foreach (var geometry in colladaFile.LibraryGeometries.Geometry) { if (geometry.Mesh == null) { SendMessage(String.Format("The geometry {0} does not have a mesh", geometry.Name)); return(null); } if (geometry.Mesh.Triangles.Count == 0) { SendMessage(String.Format("The geometry {0} has no triangles", geometry.Name)); return(null); } // Get the lightmap index from the end of the geometry name if (geometry.Name.EndsWith("-1")) { continue; } var indexString = Regex.Match(geometry.Name, @"\d+$").Value; if (System.String.IsNullOrEmpty(indexString)) { SendMessage(String.Format("The geometry {0} does not have the lightmap index at the end of it's name", geometry.Name)); return(null); } int lightmapIndex = Int32.Parse(indexString); // Create the lightmap entry var lightmap = new Lightmap() { LightmapIndex = lightmapIndex, Faces = new List <Lightmap.Face>() }; foreach (var triangles in geometry.Mesh.Triangles) { // Get the input object containing the texture coordinates var texcoord1Input = triangles.Input.Find(entry => (entry.Set == 1) && (entry.Semantic == ColladaInputSharedSemantic.TEXCOORD)); if (texcoord1Input == null) { SendMessage(String.Format("Unable to find a second texture coordinate for geometry {0}", geometry.Name)); return(null); } var sourceID = texcoord1Input.Source.TrimStart('#'); // Get the source array for the coord data var sourceArray = geometry.Mesh.Source.Find( entry => { return(entry.ID.EndsWith(sourceID)); } ); if (sourceArray == null) { SendMessage(String.Format("Unable to find a texture coordinate source array for geometry {0}", geometry.Name)); return(null); } if (sourceArray.FloatArray == null) { SendMessage(String.Format("Unable to find a texture coordinate source array for geometry {0}", geometry.Name)); return(null); } // Collect the vertex data int sourceArrayStride = (int)sourceArray.TechniqueCommon.Accessor.Stride; var indexStride = (triangles.P.Values.Count / 3) / (int)triangles.Count; foreach (var faceIndex in Enumerable.Range(0, (int)triangles.Count)) { var face = new Lightmap.Face(); // Get the index of the texture coordinates for the face vertices int faceOffset = (indexStride * 3) * faceIndex; // Offset to start of current face int vertex0Offset = faceOffset + (0 * indexStride) + (int)texcoord1Input.Offset; int vertex1Offset = faceOffset + (1 * indexStride) + (int)texcoord1Input.Offset; int vertex2Offset = faceOffset + (2 * indexStride) + (int)texcoord1Input.Offset; int vertex0Index = triangles.P.Values[vertex0Offset]; int vertex1Index = triangles.P.Values[vertex1Offset]; int vertex2Index = triangles.P.Values[vertex2Offset]; // Get the data from the float array int uIndex = (vertex0Index * sourceArrayStride); int vIndex = (vertex0Index * sourceArrayStride) + 1; face.Vertex0.X = sourceArray.FloatArray.Values[uIndex]; face.Vertex0.Y = (sourceArray.FloatArray.Values[vIndex] - 1) * -1; uIndex = (vertex1Index * sourceArrayStride); vIndex = (vertex1Index * sourceArrayStride) + 1; face.Vertex1.X = sourceArray.FloatArray.Values[uIndex]; face.Vertex1.Y = (sourceArray.FloatArray.Values[vIndex] - 1) * -1; uIndex = (vertex2Index * sourceArrayStride); vIndex = (vertex2Index * sourceArrayStride) + 1; face.Vertex2.X = sourceArray.FloatArray.Values[uIndex]; face.Vertex2.Y = (sourceArray.FloatArray.Values[vIndex] - 1) * -1; lightmap.Faces.Add(face); } } lightmapMeshes.Add(lightmap); } return(lightmapMeshes); }