IEnumerator CreateMesh() { vertices.Clear(); triangles.Clear(); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { for (int z = 0; z < width; z++) { Block block = BlockList.GetBlock(blocks[x, y, z]); if (block == null) { continue; } // 判断是否有相邻方块,及相邻方块是否透明,是则渲染该面 if (IsBlockTransparent(x + 1, y, z)) { AddFrontFace(x, y, z, block); } if (IsBlockTransparent(x - 1, y, z)) { AddBackFace(x, y, z, block); } if (IsBlockTransparent(x, y, z + 1)) { AddRightFace(x, y, z, block); } if (IsBlockTransparent(x, y, z - 1)) { AddLeftFace(x, y, z, block); } if (IsBlockTransparent(x, y + 1, z)) { AddTopFace(x, y, z, block); } if (IsBlockTransparent(x, y - 1, z)) { AddBottomFace(x, y, z, block); } } } } mesh.vertices = vertices.ToArray(); mesh.triangles = triangles.ToArray(); mesh.uv = uv.ToArray(); mesh.RecalculateBounds(); mesh.RecalculateNormals(); GetComponent <MeshFilter>().mesh = mesh; GetComponent <MeshCollider>().sharedMesh = mesh; yield return(null); isWorking = false; }
public static void UpdateMesh(Chunk c, BlockList Blocks, bool isMainThread = true) { Parallel.For(0, Chunk.maxX, (int x, ParallelLoopState _) => { Vector3 coords = new Vector3(); Block tb; if (vertss[x] == null) { vertss[x] = new List <Vector3>(); triss[x] = new List <int>(); uvss[x] = new List <Vector2>(); } else { vertss[x].Clear(); triss[x].Clear(); uvss[x].Clear(); } for (int y = 0; y < Blocks.SizeY; y++) { for (int z = 0; z < Chunk.maxZ; z++) { tb = Blocks.GetBlock(x, y, z); coords.Set(x + .5f, y, z + .5f); if (tb == null) { continue; } if (tb.Info.mesh != null) { AddMesh(x, y, z, tb, c, coords); } else { AddCube(x, y, z, tb.Info.IsTransparent, tb.Info.uvs, c, (int _x, int _y, int _z) => c.GetBlock(_x, _y, _z) == null || c.GetBlock(_x, _y, _z).Info.IsTransparent); } } } }); CombineArrays(); DivideMeshesAndSet((sve, eve, str, etr, meshInd) => c.SetMesh(verts.GetRange(sve, eve).ToArray(), tris.GetRange(str, etr).ToArray(), uv.GetRange(sve, eve).ToArray(), meshInd, isMainThread)); }
public IEnumerator CreateMesh() { for (int x = 0; x < Chunk.width; x++) { for (int y = 0; y < Chunk.height; y++) { for (int z = 0; z < Chunk.width; z++) { cubePrefab = BlockList.GetBlock(blocks[x, y, z]); if (cubePrefab == null) { continue; } if (IsBlockTransparent(x + 1, y, z)) { AddCube(x, y, z); } else if (IsBlockTransparent(x - 1, y, z)) { AddCube(x, y, z); } else if (IsBlockTransparent(x, y, z + 1)) { AddCube(x, y, z); } else if (IsBlockTransparent(x, y, z - 1)) { AddCube(x, y, z); } else if (IsBlockTransparent(x, y + 1, z)) { AddCube(x, y, z); } else if (IsBlockTransparent(x, y - 1, z)) { AddCube(x, y, z); } } } } yield return(null); isWorking = false; }
public void CreateMesh() { vertices.Clear(); triangles.Clear(); uv.Clear(); mesh.Clear(); //把所有面的点和面的索引添加进去 for (int x = 0; x < ChunkBuild.width; x++) { for (int y = 0; y < ChunkBuild.height; y++) { for (int z = 0; z < ChunkBuild.width; z++) { //获取当前坐标的Block对象 Block block = BlockList.GetBlock(this.blocks[x, y, z]); if (block == null) { continue; } //通过查看相邻块是否透明来检查此面是否需要开启 if (IsBlockTransparent(x + 1, y, z)) { AddFrontFace(x, y, z, block); } if (IsBlockTransparent(x - 1, y, z)) { AddBackFace(x, y, z, block); } if (IsBlockTransparent(x, y, z + 1)) { AddRightFace(x, y, z, block); } if (IsBlockTransparent(x, y, z - 1)) { AddLeftFace(x, y, z, block); } if (IsBlockTransparent(x, y + 1, z)) { AddTopFace(x, y, z, block); } if (IsBlockTransparent(x, y - 1, z)) { AddBottomFace(x, y, z, block); } } } } //为点和index赋值 mesh.vertices = vertices.ToArray(); mesh.triangles = triangles.ToArray(); mesh.uv = uv.ToArray(); //重新计算顶点和法线 mesh.RecalculateBounds(); mesh.RecalculateNormals(); //将生成好的面赋值给组件 this.GetComponent <MeshFilter>().mesh = mesh; this.GetComponent <MeshCollider>().sharedMesh = mesh; isWorking = false; return; }
public IEnumerator CalculateMap() { working = true; Block b = null; bool render = false; for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { for (int z = 0; z < Width; z++) { Block bloco = GetTheoreticalBlock(transform.position + new Vector3(x, y, z)); if (x == 0 && y == 0 && z == 0) { b = bloco; } if (bloco != null && bloco.BlockName == "Dirt") { if (GetTheoreticalBlock(transform.position + new Vector3(x, y + 1, z)) == null) { map[x, y, z] = BlockList.GetBlock("Grass"); } else { map[x, y, z] = bloco; } } else { map[x, y, z] = bloco; } if (render == false) { if (map[x, y, z] != b) { render = true; } } } } } /// count = 0; for (int x = 0; x < Width; x++) { for (int y = 0; y < Height; y++) { for (int z = 0; z < Width; z++) { Block bloco = GetTheoreticalBlock(transform.position + new Vector3(x, y, z)); if (map[x, y, z] == BlockList.GetBlock("Grass") && Tmp < 50) { count++; if (count % 100 == 0) { Instantiate(chunckPrefab, new Vector3(transform.position.x + x - 0.5f, transform.position.y + y + 1.5f, transform.position.z + z + 0.5f), Quaternion.identity); } } if (map[x, y, z] == BlockList.GetBlock("Grass") && Tmp < 10) { if (count % 300 == 0) { Instantiate(sundriesPrefab, new Vector3(transform.position.x + x - 0.5f, transform.position.y + y + 1f, transform.position.z + z + 0.5f), Quaternion.identity); Debug.Log("Sundries is Ready"); } } } } } /// generatedMap = true; yield return(0); if (render) { StartCoroutine(CalculateMesh()); } else { working = false; } }