//////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> Verifies that the lightmap mesh data is compatible with the loaded bsp. </summary> /// /// <param name="lightmapMeshes"> The lightmap meshes. </param> /// <param name="BSP"> The bsp. </param> /// /// <returns> true if it passes, false if it fails. </returns> bool VerifyMeshData(List <Lightmap> lightmapMeshes, structure_bsp_group BSP) { // Verify the BSP matches the loaded geometry foreach (var lightmap in lightmapMeshes) { // Get the lightmap objects from the bsp and collada file if (lightmap.LightmapIndex < 0 || lightmap.LightmapIndex >= BSP.Lightmaps.Count) { SendMessage(String.Format("A mesh in the COLLADA file has an out of bounds lightmap index ({0})", lightmap.LightmapIndex)); return(false); } var bspLightmap = BSP.Lightmaps[lightmap.LightmapIndex]; // Check the face count matches int colladaFaceCount = lightmap.Faces.Count; int bspFaceCount = 0; bspLightmap.Materials.Elements.ForEach(entry => bspFaceCount += entry.SurfaceCount); SendMessage(String.Format("COLLADA lightmap mesh {0}'s face count: {1}", lightmap.LightmapIndex, colladaFaceCount)); SendMessage(String.Format("BSP lightmap mesh {0}'s face count: {1}", lightmap.LightmapIndex, bspFaceCount)); if (colladaFaceCount != bspFaceCount) { SendMessage(String.Format("COLLADA lightmap mesh {0}'s face count does not match the bsp", lightmap.LightmapIndex)); return(false); } } return(true); }
//////////////////////////////////////////////////////////////////////////////////////////////////// /// <summary> Overwrite the lightmap uv's in the target bsp. </summary> /// /// <param name="lightmapMeshes"> The lightmap meshes. </param> /// <param name="BSP"> The bsp. </param> /// /// <returns> true if it succeeds, false if it fails. </returns> bool OverwriteLightmapUVs(List <Lightmap> lightmapMeshes, structure_bsp_group BSP) { int bspLightmapCount = BSP.Lightmaps.Count - 1; // Replace the vertex uv's foreach (var lightmap in lightmapMeshes) { var bspLightmap = BSP.Lightmaps[lightmap.LightmapIndex]; int startFaceOffset = 0; foreach (var material in bspLightmap.Materials) { // Write the lightmap UV's to the BSP's vertex data try { var binaryWriter = new BinaryWriter(new MemoryStream(material.UncompressedVertices.Value)); // Skip past the normal texture coordinates int vertexOffset = material.VerticesCount * 56; int faceIndex = startFaceOffset; foreach (int surfaceIndex in Enumerable.Range(0, material.SurfaceCount)) { var colladaFace = lightmap.Faces[faceIndex]; var surface = BSP.Surfaces[material.Surfaces + surfaceIndex]; // Write the coordinate values binaryWriter.Seek(vertexOffset + ((int)surface.A1.Value * 20) + 12, SeekOrigin.Begin); binaryWriter.Write(colladaFace.Vertex2.X); binaryWriter.Write(colladaFace.Vertex2.Y); binaryWriter.Seek(vertexOffset + ((int)surface.A2.Value * 20) + 12, SeekOrigin.Begin); binaryWriter.Write(colladaFace.Vertex1.X); binaryWriter.Write(colladaFace.Vertex1.Y); binaryWriter.Seek(vertexOffset + ((int)surface.A3.Value * 20) + 12, SeekOrigin.Begin); binaryWriter.Write(colladaFace.Vertex0.X); binaryWriter.Write(colladaFace.Vertex0.Y); faceIndex++; } binaryWriter.Flush(); } catch (Exception exception) { SendMessage(String.Format("Failed to write to the target BSP due to an exception: {0}", exception.Message)); return(false); } startFaceOffset += material.SurfaceCount; } } return(true); }