// Mesh public static Mesh GenerateMesh(List <Node> nodes, Vector3 position, out Material[] _materials) { //List<CombineInstance[]> List <int> materialsID = new List <int>(); //CombineInstance[] combine = new CombineInstance[nodes.Count]; Dictionary <int, List <CombineInstance> > combine = new Dictionary <int, List <CombineInstance> >(); // Material for (int i = 0; i < nodes.Count; i++) { // Mesh Mesh nodeMesh = nodes[i].GenerateMesh(); int subMeshIndex; if (materialsID.Contains(nodes[i].terrainId)) { subMeshIndex = materialsID.IndexOf(nodes[i].terrainId); } else { materialsID.Add(nodes[i].terrainId); subMeshIndex = materialsID.IndexOf(nodes[i].terrainId); } List <CombineInstance> ciList; if (!combine.TryGetValue(nodes[i].terrainId, out ciList)) { ciList = new List <CombineInstance>(); combine.Add(nodes[i].terrainId, ciList); } CombineInstance ci = new CombineInstance(); ci.mesh = nodeMesh; ci.transform = Matrix4x4.TRS(position + nodes[i].position, Quaternion.identity, Vector3.one); ciList.Add(ci); /* * combine[i].mesh = nodeMesh; * combine[i].subMeshIndex = subMeshIndex; * combine[i].transform = Matrix4x4.TRS(position + nodes[i].position, Quaternion.identity, Vector3.one); */ } // Prepare submeshes CombineInstance[] finalCombine = new CombineInstance[materialsID.Count]; for (int i = 0; i < materialsID.Count; i++) { List <CombineInstance> ciList; if (combine.TryGetValue(materialsID[i], out ciList)) { Mesh subMesh = new Mesh(); subMesh.CombineMeshes(ciList.ToArray(), true); finalCombine[i].mesh = subMesh; finalCombine[i].transform = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one); } } // Merge meshes Mesh output = new Mesh(); output.subMeshCount = materialsID.Count; output.CombineMeshes(finalCombine, false); //output.CombineMeshes(combine.TryGetValue(, true); Material[] materials = new Material[materialsID.Count]; for (int i = 0; i < materials.Length; i++) { materials[i] = TerrainHolder.GetTerrainFromId(materialsID[i]).material; } _materials = materials; return(output); }
public Mesh GenerateMesh() { if (terrainId == 0) { return(null); } // Retrieve terrain TerrainHolder.Terrain terrain = TerrainHolder.GetTerrainFromId(terrainId); if (terrain == null) { return(null); } List <CombineInstance> meshCombiner = new List <CombineInstance>(); CombineInstance tempCombiner; // Top if (configuration[1, 1]) { // Bottom left #region Bottom left Matrix4x4 transformMatrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-90, 180, 0), Vector3.one); // Angle tempCombiner = new CombineInstance(); tempCombiner.mesh = terrain.angleConvexUnder; tempCombiner.transform = transformMatrix; meshCombiner.Add(tempCombiner); #endregion // Bottom right #region Bottom right transformMatrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-90, 90, 0), Vector3.one); // Angle tempCombiner = new CombineInstance(); tempCombiner.mesh = terrain.angleConvexUnder; tempCombiner.transform = transformMatrix; meshCombiner.Add(tempCombiner); #endregion // Top left #region Top left transformMatrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-90, -90, 0), Vector3.one); // Angle tempCombiner = new CombineInstance(); tempCombiner.mesh = terrain.angleConvexUnder; tempCombiner.transform = transformMatrix; meshCombiner.Add(tempCombiner); #endregion // Top right #region Top left transformMatrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-90, 0, 0), Vector3.one); // Angle tempCombiner = new CombineInstance(); tempCombiner.mesh = terrain.angleConvexUnder; tempCombiner.transform = transformMatrix; meshCombiner.Add(tempCombiner); #endregion } else { // Bottom left #region Bottom left Matrix4x4 transformMatrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-90, 180, 0), Vector3.one); if (configuration[0, 1]) { if (configuration[1, 0]) { if (configuration[0, 0]) { // Fill tempCombiner = new CombineInstance(); tempCombiner.mesh = terrain.fill; tempCombiner.transform = transformMatrix; meshCombiner.Add(tempCombiner); } else { // Angle concave tempCombiner = new CombineInstance(); tempCombiner.mesh = terrain.angleConcave; tempCombiner.transform = transformMatrix; meshCombiner.Add(tempCombiner); } } else { // Line B tempCombiner = new CombineInstance(); tempCombiner.mesh = terrain.lineB; tempCombiner.transform = transformMatrix; meshCombiner.Add(tempCombiner); } } else { if (configuration[1, 0]) { // Line A tempCombiner = new CombineInstance(); tempCombiner.mesh = terrain.lineA; tempCombiner.transform = transformMatrix; meshCombiner.Add(tempCombiner); } else { // Angle convex tempCombiner = new CombineInstance(); tempCombiner.mesh = terrain.angleConvex; tempCombiner.transform = transformMatrix; meshCombiner.Add(tempCombiner); } } #endregion // Bottom right #region Bottom right transformMatrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-90, 90, 0), Vector3.one); if (configuration[1, 0]) { if (configuration[2, 1]) { if (configuration[2, 0]) { // Fill tempCombiner = new CombineInstance(); tempCombiner.mesh = terrain.fill; tempCombiner.transform = transformMatrix; meshCombiner.Add(tempCombiner); } else { // Angle concave tempCombiner = new CombineInstance(); tempCombiner.mesh = terrain.angleConcave; tempCombiner.transform = transformMatrix; meshCombiner.Add(tempCombiner); } } else { // Line B tempCombiner = new CombineInstance(); tempCombiner.mesh = terrain.lineB; tempCombiner.transform = transformMatrix; meshCombiner.Add(tempCombiner); } } else { if (configuration[2, 1]) { // Line A tempCombiner = new CombineInstance(); tempCombiner.mesh = terrain.lineA; tempCombiner.transform = transformMatrix; meshCombiner.Add(tempCombiner); } else { // Angle convex tempCombiner = new CombineInstance(); tempCombiner.mesh = terrain.angleConvex; tempCombiner.transform = transformMatrix; meshCombiner.Add(tempCombiner); } } #endregion // Top left #region Top left transformMatrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-90, -90, 0), Vector3.one); if (configuration[1, 2]) { if (configuration[0, 1]) { if (configuration[0, 2]) { // Fill tempCombiner = new CombineInstance(); tempCombiner.mesh = terrain.fill; tempCombiner.transform = transformMatrix; meshCombiner.Add(tempCombiner); } else { // Angle concave tempCombiner = new CombineInstance(); tempCombiner.mesh = terrain.angleConcave; tempCombiner.transform = transformMatrix; meshCombiner.Add(tempCombiner); } } else { // Line B tempCombiner = new CombineInstance(); tempCombiner.mesh = terrain.lineB; tempCombiner.transform = transformMatrix; meshCombiner.Add(tempCombiner); } } else { if (configuration[0, 1]) { // Line A tempCombiner = new CombineInstance(); tempCombiner.mesh = terrain.lineA; tempCombiner.transform = transformMatrix; meshCombiner.Add(tempCombiner); } else { // Angle convex tempCombiner = new CombineInstance(); tempCombiner.mesh = terrain.angleConvex; tempCombiner.transform = transformMatrix; meshCombiner.Add(tempCombiner); } } #endregion // Top right #region Top left transformMatrix = Matrix4x4.TRS(Vector3.zero, Quaternion.Euler(-90, 0, 0), Vector3.one); if (configuration[2, 1]) { if (configuration[1, 2]) { if (configuration[2, 2]) { // Fill tempCombiner = new CombineInstance(); tempCombiner.mesh = terrain.fill; tempCombiner.transform = transformMatrix; meshCombiner.Add(tempCombiner); } else { // Angle concave tempCombiner = new CombineInstance(); tempCombiner.mesh = terrain.angleConcave; tempCombiner.transform = transformMatrix; meshCombiner.Add(tempCombiner); } } else { // Line B tempCombiner = new CombineInstance(); tempCombiner.mesh = terrain.lineB; tempCombiner.transform = transformMatrix; meshCombiner.Add(tempCombiner); } } else { if (configuration[1, 2]) { // Line A tempCombiner = new CombineInstance(); tempCombiner.mesh = terrain.lineA; tempCombiner.transform = transformMatrix; meshCombiner.Add(tempCombiner); } else { // Angle convex tempCombiner = new CombineInstance(); tempCombiner.mesh = terrain.angleConvex; tempCombiner.transform = transformMatrix; meshCombiner.Add(tempCombiner); } } #endregion } Mesh output = new Mesh(); output.CombineMeshes(meshCombiner.ToArray()); return(output); }