/// <summary> /// Builds all <see cref="Mesh"/> objects for the <see cref="Entity"/> in <paramref name="instance"/> instance, /// combines them if necessary using <see cref="settings"/>.meshCombineOptions and adds the meshes to the /// <see cref="GameObject"/> in <paramref name="instance"/>. /// </summary> /// <param name="instance">The <see cref="EntityInstance"/> to build <see cref="Mesh"/>es for.</param> protected void BuildMesh(EntityInstance instance) { int modelNumber = instance.entity.modelNumber; Model model = bsp.models[modelNumber]; Dictionary <string, List <Mesh> > textureMeshMap = new Dictionary <string, List <Mesh> >(); GameObject gameObject = instance.gameObject; List <Face> faces = bsp.GetFacesInModel(model); int i = 0; for (i = 0; i < faces.Count; ++i) { Face face = faces[i]; if (face.numEdges <= 0 && face.numVertices <= 0) { continue; } int textureIndex = bsp.GetTextureIndex(face); string textureName = ""; if (textureIndex >= 0) { LibBSP.Texture texture = bsp.textures[textureIndex]; textureName = texture.name; if (!textureMeshMap.ContainsKey(textureName) || textureMeshMap[textureName] == null) { textureMeshMap[textureName] = new List <Mesh>(); } textureMeshMap[textureName].Add(CreateFaceMesh(face, textureName)); } } if (modelNumber == 0) { if (bsp.lodTerrains != null) { foreach (LODTerrain lodTerrain in bsp.lodTerrains) { if (lodTerrain.texture >= 0) { LibBSP.Texture texture = bsp.textures[lodTerrain.texture]; string textureName = texture.name; if (!textureMeshMap.ContainsKey(textureName) || textureMeshMap[textureName] == null) { textureMeshMap[textureName] = new List <Mesh>(); } textureMeshMap[textureName].Add(CreateLoDTerrainMesh(lodTerrain, textureName)); } } } } if (settings.meshCombineOptions != MeshCombineOptions.None) { Mesh[] textureMeshes = new Mesh[textureMeshMap.Count]; Material[] materials = new Material[textureMeshes.Length]; i = 0; foreach (KeyValuePair <string, List <Mesh> > pair in textureMeshMap) { textureMeshes[i] = MeshUtils.CombineAllMeshes(pair.Value.ToArray(), true, false); if (materialDirectory.ContainsKey(pair.Key)) { materials[i] = materialDirectory[pair.Key]; } if (settings.meshCombineOptions == MeshCombineOptions.PerMaterial) { GameObject textureGameObject = new GameObject(pair.Key); textureGameObject.transform.parent = gameObject.transform; textureGameObject.transform.localPosition = Vector3.zero; if (textureMeshes[i].normals.Length == 0 || textureMeshes[i].normals[0] == Vector3.zero) { textureMeshes[i].RecalculateNormals(); } textureMeshes[i].AddMeshToGameObject(new Material[] { materials[i] }, textureGameObject); #if UNITY_EDITOR if (!IsRuntime && (settings.assetSavingOptions & AssetSavingOptions.Meshes) > 0) { string meshPath = Path.Combine(Path.Combine(Path.Combine("Assets", settings.meshPath), bsp.MapNameNoExtension), "mesh_" + textureMeshes[i].GetHashCode() + ".asset").Replace('\\', '/'); Directory.CreateDirectory(Path.GetDirectoryName(meshPath)); AssetDatabase.CreateAsset(textureMeshes[i], meshPath); } #endif } ++i; } if (settings.meshCombineOptions != MeshCombineOptions.PerMaterial) { Mesh mesh = MeshUtils.CombineAllMeshes(textureMeshes, false, false); mesh.TransformVertices(gameObject.transform.localToWorldMatrix); if (mesh.normals.Length == 0 || mesh.normals[0] == Vector3.zero) { mesh.RecalculateNormals(); } mesh.AddMeshToGameObject(materials, gameObject); #if UNITY_EDITOR if (!IsRuntime && (settings.assetSavingOptions & AssetSavingOptions.Meshes) > 0) { string meshPath = Path.Combine(Path.Combine(Path.Combine("Assets", settings.meshPath), bsp.MapNameNoExtension), "mesh_" + mesh.GetHashCode() + ".asset").Replace('\\', '/'); Directory.CreateDirectory(Path.GetDirectoryName(meshPath)); AssetDatabase.CreateAsset(mesh, meshPath); } #endif } } else { i = 0; foreach (KeyValuePair <string, List <Mesh> > pair in textureMeshMap) { GameObject textureGameObject = new GameObject(pair.Key); textureGameObject.transform.parent = gameObject.transform; textureGameObject.transform.localPosition = Vector3.zero; Material material = materialDirectory[pair.Key]; foreach (Mesh mesh in pair.Value) { GameObject faceGameObject = new GameObject("Face"); faceGameObject.transform.parent = textureGameObject.transform; faceGameObject.transform.localPosition = Vector3.zero; if (mesh.normals.Length == 0 || mesh.normals[0] == Vector3.zero) { mesh.RecalculateNormals(); } mesh.AddMeshToGameObject(new Material[] { material }, faceGameObject); #if UNITY_EDITOR if (!IsRuntime && (settings.assetSavingOptions & AssetSavingOptions.Meshes) > 0) { string meshPath = Path.Combine(Path.Combine(Path.Combine("Assets", settings.meshPath), bsp.MapNameNoExtension), "mesh_" + mesh.GetHashCode() + ".asset").Replace('\\', '/'); Directory.CreateDirectory(Path.GetDirectoryName(meshPath)); AssetDatabase.CreateAsset(mesh, meshPath); } #endif } ++i; } } }