/*public void ClearTerrain(Blocks MyBlocks) { for (int i = 0; i < MyBlocks.Size.x; i++) for (int j = 0; j < MyBlocks.Size.y; j++) for (int k = 0; k < MyBlocks.Size.z; k++) { MyBlocks.ClearBlockMesh(new Vector3(i,j,k)); } }*/ public void GenerateAllMeshes(MyMesh MeshData, Blocks MyBlocks) { //CreateSunlight (); MeshData.ClearMesh (); if (!IsSmoothTerrain) { // Just want to update any blocks that were changed and its surrounding ones // MyBlocks class keeps the mesh data stored in memory rather then remaking the meshes every time if (IsSubDivided) { //CreateMeshFromBlocksSubdivision (DefaultSubDivisionLevel); } else { CreateMeshFromBlocks (MyBlocks, MeshData); } for (int i = 0; i < MyBlocks.Size.x; i++) for (int j = 0; j < MyBlocks.Size.y; j++) for (int k = 0; k < MyBlocks.Size.z; k++) { if (MyBlocks.GetBlockType(new Vector3(i,j,k)) != 0) { MeshData.Add (MyBlocks.GetBlock(new Vector3(i,j,k)).GetBlockMesh()); } } CanUpdateMesh = true; } else { //CreateSmoothTerrain (); //UpdateMesh (); } }
/*public void CreateMeshFromBlocksSubdivision(int SubDivisionLevel) { // example im coding for is 2 ClearMeshes (); TerrainMesh.FaceCount = 0; //customMesh.ClearMesh(); Debug.Log ("Adding Cubes as Models."); CubeCount = 0; float MaxSize = MyBlocks.Size.x; if (MaxSize < MyBlocks.Size.y) MaxSize = MyBlocks.Size.y; if (MaxSize < MyBlocks.Size.z) MaxSize = MyBlocks.Size.z; if (MaxSize % 2 == 1) MaxSize += 1; float MaxX = MaxSize; //MyBlocks.Size.x-(Mathf.RoundToInt(MyBlocks.Size.x) % SubDivisionLevel); float MaxY = MaxSize; //MyBlocks.Size.y-(Mathf.RoundToInt(MyBlocks.Size.y) % SubDivisionLevel); float MaxZ = MaxSize; //MyBlocks.Size.z-(Mathf.RoundToInt(MyBlocks.Size.z) % SubDivisionLevel); Debug.LogError ("MaxX: " + MaxX + " : MaxY: " + MaxY + " : MaxZ: " + MaxZ); for (int i = 0; i < MaxX; i += SubDivisionLevel) for (int j = 0; j < MaxY; j += SubDivisionLevel) for (int k = 0; k < MaxZ; k += SubDivisionLevel) { MyBlocks.GetBlockMesh(new Vector3(i,j,k)).ClearMesh (); int NewBlockType = GetBlockTypeInGroup(i, j, k, SubDivisionLevel); //Debug.LogError ("At block: " + i + ": j: " + j + ":k:" + k + " BlockType: " + NewBlockType); if (NewBlockType > 0) { // if no mesh create a mesh //if (MyBlocks.GetBlock(new Vector3(i,j,k)).HasChanged) { // if no mesh create a mesh MyBlocks.GetBlock(new Vector3(i,j,k)).HasChanged = false; CubeCount++; //Debug.Log ("Adding Cube number: " + CubeCount + " Pos: " + i + " : " + j + " : " + k); bool IsFrontFace = false; bool IsBackFace = false; bool IsLeftFace = false; bool IsRightFace = false; bool IsTopFace = false; bool IsBottomFace = false; // the bug is definitely here in the culling //if (i+SubDivisionLevel <= MaxX) if (!IsBlockInGroup(i+SubDivisionLevel+1,j,k,SubDivisionLevel)) // should be something that searches the chunk, then searches the world which is referenced in chunk IsRightFace = true; //if (i-SubDivisionLevel >= 0) if (!IsBlockInGroup(i-SubDivisionLevel-1,j,k,SubDivisionLevel)) IsLeftFace = true; //if (k+SubDivisionLevel <= MaxZ) if (!IsBlockInGroup(i,j,k+SubDivisionLevel+1,SubDivisionLevel)) IsFrontFace = true; //if (k-SubDivisionLevel >= 0) if (!IsBlockInGroup(i,j,k-SubDivisionLevel-1,SubDivisionLevel)) IsBackFace = true; //if (j+SubDivisionLevel <=MaxY) if (!IsBlockInGroup(i,j+SubDivisionLevel+1,k,SubDivisionLevel)) IsTopFace = true; //if (j-SubDivisionLevel >= 0) if (!IsBlockInGroup(i,j-SubDivisionLevel-1,k,SubDivisionLevel)) IsBottomFace = true; IsLeftFace = true; IsRightFace = true; IsBottomFace = true; IsTopFace = true; IsBackFace = true; // this one faces cam IsFrontFace = true; Vector3 TemporaryCubeSize = new Vector3(CubeSize.x*SubDivisionLevel, CubeSize.y*SubDivisionLevel,CubeSize.z*SubDivisionLevel); Vector3 NewCubePosition = new Vector3 (((float)(i)) * TemporaryCubeSize.x/((float)SubDivisionLevel), ((float)(j)) * TemporaryCubeSize.y/((float)SubDivisionLevel), ((float)(k)) * TemporaryCubeSize.z/((float)SubDivisionLevel)); //MyBlocks.Data [i].Data [j].Data [k].BlockMesh.FaceCount = TerrainMesh.FaceCount; // has to correspond to its positioning in the mesh int TileIndex = NewBlockType; //MyBlocks.GetBlockType (new Vector3(i,j,k)); //TileIndex = 2; Vector2 TilesPosition = new Vector2(); // Every Column TilesPosition.x = (TileIndex % MaxTiles); // 4 = 3,1 - 5 = 2,1 - 6 = 1,1 - 7 = 0,1 // Every Row TilesPosition.y = (TileIndex / MaxTiles); // 4 - 1 = 5 - 1 //Debug.LogError ("Now updating with tile index: " + TileIndex + " : At TilePosition: " + TilesPosition.ToString()); MyBlocks.GetBlockMesh(new Vector3(i,j,k)).CreateCube (NewCubePosition, TemporaryCubeSize, IsFrontFace, IsBackFace, IsLeftFace, IsRightFace, IsTopFace, IsBottomFace, TilesPosition, new Color32(255,255,255,255)); // MyBlocks.Data[i].Data[j].Data[k].MyColor); //MyBlocks.Data [i].Data [j].Data [k].BlockMesh.FaceCount -= TerrainMesh.FaceCount; //TerrainMesh.FaceCount += MyBlocks.Data [i].Data [j].Data [k].BlockMesh.FaceCount; //TerrainMesh.Add (MyBlocks.Data [i].Data [j].Data [k].BlockMesh); } else { //Debug.LogError ("At block: " + i + ": j: " + j + ":k:" + k + " BlockType: " + NewBlockType); } } }*/ // creates the mesh - should keep track of the mesh public void CreateMeshFromBlocks(Blocks MyBlocks, MyMesh TerrainMesh) { TerrainMesh.FaceCount = 0; for (int i = 0; i < MyBlocks.Size.x; i++) for (int j = 0; j < MyBlocks.Size.y; j++) for (int k = 0; k < MyBlocks.Size.z; k++) { //MyBlocks.Data [i].Data [j].Data [k].BlockMesh.ClearMesh (); //if (MyBlocks.Data [i].Data [j].Data [k].Type != 0) if (MyBlocks.GetBlock(new Vector3(i,j,k)).HasChanged()) { //has no mesh create a mesh MyBlocks.ClearBlockMesh(new Vector3(i,j,k)); //Debug.Log ("Adding Cube number: " + CubeCount + " Pos: " + i + " : " + j + " : " + k); bool IsFrontFace = false; bool IsBackFace = false; bool IsLeftFace = false; bool IsRightFace = false; bool IsTopFace = false; bool IsBottomFace = false; if (i != MyBlocks.Size.x - 1) if (MyBlocks.GetBlockType (new Vector3 (i + 1, j, k)) == 0) // should be something that searches the chunk, then searches the world which is referenced in chunk IsRightFace = true; if (i != 0) if (MyBlocks.GetBlockType (new Vector3 (i - 1, j, k)) == 0) IsLeftFace = true; if (k != MyBlocks.Size.z - 1) // if not on edge if (MyBlocks.GetBlockType (new Vector3 (i, j, k + 1)) == 0) IsFrontFace = true; if (k != 0) if (MyBlocks.GetBlockType (new Vector3 (i, j, k - 1)) == 0) IsBackFace = true; if (j != MyBlocks.Size.y - 1) if (MyBlocks.GetBlockType (new Vector3 (i, j + 1, k)) == 0) IsTopFace = true; if (j != 0) if (MyBlocks.GetBlockType (new Vector3 (i, j - 1, k)) == 0) IsBottomFace = true; if (i == 0) IsLeftFace = true; if (i == MyBlocks.Size.x - 1) IsRightFace = true; if (j == 0) IsBottomFace = true; if (j == MyBlocks.Size.y - 1) IsTopFace = true; if (k == 0) IsBackFace = true; if (k == MyBlocks.Size.z - 1) IsFrontFace = true; Vector3 NewCubePosition = new Vector3 (((float)(i)) * MyBlocks.Scale.x, ((float)(j)) * MyBlocks.Scale.y, ((float)(k)) * MyBlocks.Scale.z); //MyBlocks.Data [i].Data [j].Data [k].BlockMesh.FaceCount = TerrainMesh.FaceCount; int TileIndex = MyBlocks.GetBlockType (new Vector3(i,j,k)); //TileIndex = 2; Vector2 TilesPosition = new Vector2(); // Every Column TilesPosition.x = (TileIndex % TileMapLength); // 4 = 3,1 - 5 = 2,1 - 6 = 1,1 - 7 = 0,1 // Every Row TilesPosition.y = (TileIndex / TileMapLength); // 4 - 1 = 5 - 1 //Debug.LogError ("Now updating with tile index: " + TileIndex + " : At TilePosition: " + TilesPosition.ToString()); MyBlocks.GetBlockMesh(new Vector3(i,j,k)).CreateCube (NewCubePosition, MyBlocks.Scale, IsFrontFace, IsBackFace, IsLeftFace, IsRightFace, IsTopFace, IsBottomFace, TilesPosition, new Color32(255,255,255,255)); // MyBlocks.Data[i].Data[j].Data[k].MyColor); } } }
//List<float> MarchingCubesData = new List<float>(); // the outter edges of the data file will be the other chunks blocks, but they won't be rendered public void PolygonizeData(Blocks MyBlocks, Vector3 WorldSize_) { data.Clear (); for (int i = 0; i < (MyBlocks.Size.x)*(MyBlocks.Size.y)*(MyBlocks.Size.z); i++) data.Add(0.0f); for (int i = 0; i < MyBlocks.Size.x; i++) for (int j = 0; j < MyBlocks.Size.y; j++) for (int k = 0; k < MyBlocks.Size.z; k++) { Vector3 loc = new Vector3 (i, j, k); //Debug.Log ("Added cube " + MyBlocks.GetBlockType(loc).ToString()); if (MyBlocks.GetBlockType(loc) != 0) { // if no mesh create a mes AddCube (loc, new Vector3 (1, 1, 1)); //Debug.Log ("Added cube " + loc.ToString()); } } trilist.Clear(); CubeSize = WorldSize_; WorldSize = WorldSize_; int BlockType = 1; gridSize = WorldSize; //numxyz = WorldSize.x*WorldSize.y*WorldSize.z; worlddim = WorldSize; worldstride = new Vector3(gridSize.x / WorldSize.x, gridSize.y / WorldSize.y, gridSize.z / WorldSize.z); datastride = new Vector3(WorldSize_.x / gridSize.x, WorldSize_.y / gridSize.y, WorldSize_.z / gridSize.z); worldcenter = new Vector3(WorldSize.x / 2, WorldSize.x / 2, WorldSize.x / 2); for (int i = 0; i < MyBlocks.Size.x; i++) { for (int j = 0; j < MyBlocks.Size.y; j++) { for (int k = 0; k < MyBlocks.Size.z; k++) { //BlockType = NewBlockData[i].Data[j].Data[k].BlockType;// this doesn't work as the blocks don't perfec PolygoniseCube(i,j,k,BlockType); } } } }
public void ConvertTrianglesToVoxels(Blocks MyBlocks, Mesh InputMesh) { resolution = Mathf.Pow (resolution, 2); InputMesh.RecalculateBounds (); MyBounds = InputMesh.bounds; // draws the bounding box DebugShapes.DrawCube (transform.position + InputMesh.bounds.center, InputMesh.bounds.extents, Color.green); Debug.LogError ("Converting mesh to voxels"); MyBlocks = GetManager.GetDataManager().BlockStructuresList[BlockStructureIndex].MyBlocks; float SizeOfGrid = 1f; //real block size is x2 whatever i set here Vector3 Bounds = InputMesh.bounds.extents; float LargestSize = Bounds.x; if (LargestSize < Bounds.y) LargestSize = Bounds.y; if (LargestSize < Bounds.z) LargestSize = Bounds.z; //LargestSize = Mathf.CeilToInt (LargestSize); GridLength = Mathf.CeilToInt (LargestSize * resolution); Vector3 GridSize = new Vector3 (GridLength, GridLength, GridLength); BlockLength = Mathf.CeilToInt (LargestSize) / resolution; BlockSize = new Vector3 (BlockLength, BlockLength, BlockLength); //GridSize = GridSize / 2f; // calculate block size depending on the bounds size of the model // so if bounds size is 3, have block size as 3 // then set grid size to block size divided by resolution MyBlocks.Size = GridSize; MyBlocks.InitilizeData(); Vector3[] Verticies = InputMesh.vertices; int[] Indicies = InputMesh.GetIndices (0); Vector3[] Normals = InputMesh.normals; // i have to make the grid size the same as the bounding box //Vector3 BlockSize = new Vector3(resolution, resolution, resolution); //BlockSize = 2f*(BlockSize/(MyBounds.extents.magnitude)); Debug.LogError("Block Size is: " + BlockSize.ToString()); Debug.LogError("Indicies Size is: " + Indicies.Length.ToString()); for (int z = 0; z < Indicies.Length; z += 3) { Vector3 Vertex1 = Verticies[Indicies[z+0]]; Vector3 Vertex2 = Verticies[Indicies[z+1]]; Vector3 Vertex3 = Verticies[Indicies[z+2]]; if (IsNormalsDebug) { Debug.DrawLine (transform.position+Vertex1,transform.position+Vertex1 + Normals[Indicies[z+0]]*NormalsLength, Color.blue, 5); Debug.DrawLine (transform.position+Vertex2,transform.position+Vertex2 + Normals[Indicies[z+1]]*NormalsLength, Color.blue, 5); Debug.DrawLine (transform.position+Vertex3,transform.position+Vertex3 + Normals[Indicies[z+2]]*NormalsLength, Color.blue, 5); } //Debug.LogError ("Triangle " + (z/3) + ": " + Vertex1.ToString() + "---" + Vertex2.ToString() + "---" + Vertex3.ToString()); //Debug.DrawLine (transform.position+new Vector3(startX, startY, startZ),transform.position+new Vector3(endX,endY,endZ), Color.red, 5); // should only do this for the blocks around the triangles // need to optimize this asap // for blocks around triangle? //Vector3 StartBlock = new Vector3(); //StartBlock.x = Mathf.FloorToInt(); // minimum size of the Vertex AARB // find the size of each triangle as an Axis Aligned Rectangle Bounds - AARB Vector3 TrianglePosition = (Vertex2 + Vertex3)/2f; Vector3 TriangleMinimum = new Vector3 (Mathf.Min (Vertex1.x, Vertex2.x, Vertex3.x), Mathf.Min (Vertex1.y, Vertex2.y, Vertex3.y), Mathf.Min (Vertex1.z, Vertex2.z, Vertex3.z)); Vector3 TriangleMaximum = new Vector3 (Mathf.Max (Vertex1.x, Vertex2.x, Vertex3.x), Mathf.Max (Vertex1.y, Vertex2.y, Vertex3.y), Mathf.Max (Vertex1.z, Vertex2.z, Vertex3.z)); Vector3 TriangleSize = new Vector3(); TriangleSize = TriangleMaximum - TriangleMinimum; TriangleSize *= 0.5f; TrianglePosition = TriangleMinimum + TriangleSize; TrianglePosition += InputMesh.bounds.extents; int StartX = Mathf.FloorToInt((TrianglePosition.x-TriangleSize.x)); // before it was 0 int EndX = Mathf.CeilToInt((TrianglePosition.x+TriangleSize.x)/resolution);//MyBlocks.Size.x; i++) int StartY = Mathf.FloorToInt((TrianglePosition.y-TriangleSize.y)); int EndY = Mathf.CeilToInt((TrianglePosition.y+TriangleSize.y)/resolution); int StartZ = Mathf.FloorToInt((TrianglePosition.z-TriangleSize.z)); int EndZ = Mathf.CeilToInt((TrianglePosition.z+TriangleSize.z)/resolution); StartX = 0; StartY = 0; StartZ = 0; EndX = Mathf.CeilToInt(MyBlocks.Size.x)-1; EndY = Mathf.CeilToInt(MyBlocks.Size.y)-1; EndZ = Mathf.CeilToInt(MyBlocks.Size.z)-1; Debug.LogError (z + " || StartX: " + StartX + " - EndX: " + EndX + " || StartY: " + StartY + " - EndY: " + EndY + " || StartZ: " + StartZ + " - EndZ: " + EndZ); Debug.Break (); for (int i = StartX; i <= EndX; i++) for (int j = StartY; j <= EndY; j++) for (int k = StartZ; k <= EndZ; k++) { if (MyBlocks.GetBlockType(new Vector3(i,j,k)) == 0) { Vector3 BlockPosition = BlockSize + MyBounds.center; BlockPosition += 2f*(new Vector3(i*BlockSize.x,j*BlockSize.y,k*BlockSize.z)); if (IsTriangleInGrid(BlockPosition, BlockSize, TrianglePosition, TriangleSize)) { MyBlocks.UpdateBlock(new Vector3(i,j,k), 1); }// else //MyBlocks.UpdateBlock(new Vector3(i,j,k), 0); } } //break; // for now test it once } Debug.LogError ("Size of grid: " + MyBlocks.Size.ToString ()); if (IsDebugGrid) for (int i = 0; i < MyBlocks.Size.x; i++) for (int j = 0; j < MyBlocks.Size.y; j++) for (int k = 0; k < MyBlocks.Size.z; k++) { Vector3 BlockPosition = BlockSize + MyBounds.center; BlockPosition += 2f*(new Vector3(i*BlockSize.x,j*BlockSize.y,k*BlockSize.z)); if (MyBlocks.GetBlockType(new Vector3(i,j,k)) == 0) { if (IsDebugAllBlocks) DebugShapes.DrawCube (BlockPosition+transform.position - MyBounds.extents, BlockSize, Color.white, true); } else { DebugShapes.DrawCube (BlockPosition+transform.position - MyBounds.extents, BlockSize, Color.red, true); } } }
public void ConvertBlocksToColors(Blocks MyBlocks, float MapHeight) { for (int i = 0; i < MyBlocks.Size.x; i++) for (int k = 0; k < MyBlocks.Size.z; k++) { int PixelIndex = Mathf.RoundToInt (i * MyBlocks.Size.x + k); NewColors[PixelIndex] = BlockColors[MyBlocks.GetBlockType (new Vector3 (i, MapHeight, k))]; if (IsCircular) { float DistanceToMid = Vector2.Distance(new Vector2(i,k), new Vector2(MyBlocks.Size.x/2f,MyBlocks.Size.z/2f)); if (DistanceToMid > MyBlocks.Size.x/2f+outlineSize) { NewColors[PixelIndex] = new Color32(0,0,0,0); } else if (DistanceToMid > MyBlocks.Size.x/2f) { NewColors[PixelIndex] = new Color32(0,0,0,255); } } } }
// Searches the Blocks for a empty block, that is surrounded by 4 path blocks, then it removes it or filles it with a special pillar looking block void RemoveSinglePillars(Blocks Blocks, Vector3 Size, int PathBlockType) { if (bIsRemoveSinglePillars) for (int i = 0; i < Size.x; i++) for (int j = 0; j < Size.y; j++) for (int k = 0; k < Size.z; k++) { if (Blocks.GetBlockType(new Vector3(i, j, k)) == 0) { if (Blocks.GetBlockType(new Vector3(i + 1, j, k)) == PathBlockType && MyBlocks.GetBlockType(new Vector3(i - 1, j, k)) == PathBlockType && MyBlocks.GetBlockType(new Vector3(i, j + 1, k)) == PathBlockType && MyBlocks.GetBlockType(new Vector3(i, j - 1, k)) == PathBlockType) Blocks.UpdateBlock(new Vector3(i, j, k + MazeWallHeight), PathBlockType); } } }
void GenerateSpawnLocations(Blocks MyBlocks, Vector3 Size) { //MyBlocks.UpdateBlock(StartLocation, SpawnBlockType); int MaxBotSpawnBlocks = MaxSpawnPoints; int BotSpawnBlocksCount = 0; bool bIsBotSpawns = true; if (bIsBotSpawns) { while (BotSpawnBlocksCount <= MaxBotSpawnBlocks) { int i = Mathf.RoundToInt(Random.Range(0f, Size.x-1f)); int j = Mathf.RoundToInt(Random.Range(0f, Size.y-1f)); int k = Mathf.RoundToInt(Random.Range(0f, Size.z-1f)); if (IsVoidAxisZ) k = (int)StartLocation.z; if (IsVoidAxisY) j = (int)StartLocation.y; if (IsVoidAxisX) i = (int)StartLocation.x; if (MyBlocks.GetBlockType(new Vector3(i, j, k)) == PathBlockType) { SpawnLocations.Add (new Vector3(i,j,k)); //MyBlocks.UpdateBlock(new Vector3(i, j, k), SpawnBlockType); BotSpawnBlocksCount++; } } } }