/// <summary> /// 为每个面重新设置坐标uv偏移 /// </summary> /// <param name="Pos"></param> /// <param name="materialNumber"></param> /// <returns></returns> IEnumerator ReSetMaterialIEnumerator(int Pos, int materialNumber, string textureNumber) { byte byteForMaterial = (byte)materialNumber; Block block = BlockList.GetBlock(byteForMaterial, textureNumber); Block block2 = BlockList2.GetBlock(byteForMaterial); uv[Pos * 4] = new Vector2(block.textureLeftX * textureOffset, block.textureLeftY * textureOffset) + new Vector2(shrinkSize, shrinkSize); uv[Pos * 4 + 1] = new Vector2(block.textureLeftX * textureOffset + textureOffset, block.textureLeftY * textureOffset) + new Vector2(-shrinkSize, shrinkSize); uv[Pos * 4 + 2] = new Vector2(block.textureLeftX * textureOffset + textureOffset, block.textureLeftY * textureOffset + textureOffset) + new Vector2(-shrinkSize, -shrinkSize); uv[Pos * 4 + 3] = new Vector2(block.textureLeftX * textureOffset, block.textureLeftY * textureOffset + textureOffset) + new Vector2(shrinkSize, -shrinkSize); uv2[Pos * 4] = new Vector2(block2.textureLeftX * textureOffset, block2.textureLeftY * textureOffset) + new Vector2(shrinkSize, shrinkSize); uv2[Pos * 4 + 1] = new Vector2(block2.textureLeftX * textureOffset + textureOffset, block2.textureLeftY * textureOffset) + new Vector2(-shrinkSize, shrinkSize); uv2[Pos * 4 + 2] = new Vector2(block2.textureLeftX * textureOffset + textureOffset, block2.textureLeftY * textureOffset + textureOffset) + new Vector2(-shrinkSize, -shrinkSize); uv2[Pos * 4 + 3] = new Vector2(block2.textureLeftX * textureOffset, block2.textureLeftY * textureOffset + textureOffset) + new Vector2(shrinkSize, -shrinkSize); yield return(null); isWorking = false; }
/// <summary> /// 生成Chunk所需要的所有面 /// </summary> /// <returns></returns> IEnumerator CreateMesh() { vertices.Clear(); triangles.Clear(); //把所有面的点和面的索引添加进去 for (int z = 0; z < width; z++) { for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { ///所有面的第几个面(从下往上从左往右根据行排序) int meshNumberForLine = (Mathf.FloorToInt(number / Map.instance.chunkLineNumber) * Map.instance.ChunkWidth + z) * Map.instance.MapLength + number % Map.instance.chunkLineNumber * Map.instance.ChunkWidth + x; byte tempByte = (byte)Map.instance.textureMap[meshNumberForLine]; char[] tempChar = new char[8]; if (meshNumberForLine + Map.instance.MapLength - 1 < Map.instance.textureMap.Length && Map.instance.textureMap[meshNumberForLine + Map.instance.ChunkWidth * Map.instance.chunkLineNumber - 1] == tempByte) { tempChar[7] = '1'; } else { tempChar[7] = '0'; } if (meshNumberForLine + Map.instance.MapLength < Map.instance.textureMap.Length && Map.instance.textureMap[meshNumberForLine + Map.instance.MapLength] == tempByte) { tempChar[6] = '1'; } else { tempChar[6] = '0'; } if (meshNumberForLine + Map.instance.MapLength + 1 < Map.instance.textureMap.Length && Map.instance.textureMap[meshNumberForLine + Map.instance.MapLength + 1] == tempByte) { tempChar[5] = '1'; } else { tempChar[5] = '0'; } if (meshNumberForLine - 1 >= 0 && Map.instance.textureMap[meshNumberForLine - 1] == tempByte) { tempChar[4] = '1'; } else { tempChar[4] = '0'; } if (meshNumberForLine + 1 < Map.instance.textureMap.Length && Map.instance.textureMap[meshNumberForLine + 1] == tempByte) { tempChar[3] = '1'; } else { tempChar[3] = '0'; } if (meshNumberForLine - Map.instance.MapLength - 1 >= 0 && Map.instance.textureMap[meshNumberForLine - Map.instance.MapLength - 1] == tempByte) { tempChar[2] = '1'; } else { tempChar[2] = '0'; } if (meshNumberForLine - Map.instance.MapLength >= 0 && Map.instance.textureMap[meshNumberForLine - Map.instance.MapLength] == tempByte) { tempChar[1] = '1'; } else { tempChar[1] = '0'; } if (meshNumberForLine - Map.instance.MapLength + 1 >= 0 && Map.instance.textureMap[meshNumberForLine - Map.instance.MapLength + 1] == tempByte) { tempChar[0] = '1'; } else { tempChar[0] = '0'; } //获取当前坐标的Block对象 Block block = BlockList.GetBlock(tempByte, new string(tempChar)); Block block2 = BlockList2.GetBlock(tempByte); if (block == null) { continue; } AddTopFace(x, y, z, block, block2); } } } //为点和index赋值 mesh.vertices = vertices.ToArray(); mesh.triangles = triangles.ToArray(); mesh.uv4 = uv.ToArray(); mesh.uv = uv2.ToArray(); //重新计算顶点和法线 mesh.RecalculateBounds(); mesh.RecalculateNormals(); //将生成好的面赋值给组件 GetComponent <MeshFilter>().mesh = mesh; GetComponent <MeshCollider>().sharedMesh = mesh; yield return(null); isWorking = false; }