/// <summary> /// Loads the terrain from a given folder /// </summary> /// <param name="path">the path to the folder containing the files</param> public void Load(string path) { if (!File.Exists(path + "/terrain.dat")) { throw new Exception("terrain file not found"); } saveFile = path; BinaryReader reader = new BinaryReader(File.Open(path + "/terrain.dat", FileMode.Open)); MarchingCubesChunk.size = reader.ReadInt32(); int chunkCount = reader.ReadInt32(); reader.Close(); reader.Dispose(); MarchingCubesChunk[] ccs = GetComponentsInChildren <MarchingCubesChunk>(); chunks = new List <MarchingCubesChunk>(); for (int i = 0; i < ccs.Length; i++) { #if UNITY_EDITOR DestroyImmediate(ccs[i].gameObject); #else Destroy(ccs[i].gameObject); #endif } for (int i = 0; i < chunkCount; i++) { reader = new BinaryReader(File.Open(path + "/chunk" + i + ".dat", FileMode.Open)); Vector3Int pos = new Vector3Int(0, 0, 0); pos.x = reader.ReadInt32(); pos.y = reader.ReadInt32(); pos.z = reader.ReadInt32(); MarchingCubesChunk cc = AddChunk(pos); for (int x = 0; x < chunks[i].xLength; x++) { for (int y = 0; y < chunks[i].yLength; y++) { for (int z = 0; z < chunks[i].zLength; z++) { chunks[i].values[x, y, z].v = reader.ReadSingle(); chunks[i].values[x, y, z].c = new Color(reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); } } } reader.Close(); reader.Dispose(); } Generate(); }
/// <summary> /// Removes a chunk from the terrain /// </summary> /// <param name="position">The position of the Chunk to remove in the chunk grid</param> public void EradicateChunk(Vector3Int position) { if (ChunkAvailable(position)) { MarchingCubesChunk chunk = GetChunk(position); if (ChunkAvailable(position + new Vector3Int(-1, 0, 0))) { GetChunk(position + new Vector3Int(-1, 0, 0)).neighbours[0] = null; } if (ChunkAvailable(position + new Vector3Int(1, 0, 0))) { GetChunk(position + new Vector3Int(1, 0, 0)).neighbours[1] = null; } if (ChunkAvailable(position + new Vector3Int(0, -1, 0))) { GetChunk(position + new Vector3Int(0, -1, 0)).neighbours[2] = null; } if (ChunkAvailable(position + new Vector3Int(0, 1, 0))) { GetChunk(position + new Vector3Int(0, 1, 0)).neighbours[3] = null; } if (ChunkAvailable(position + new Vector3Int(0, 0, -1))) { GetChunk(position + new Vector3Int(0, 0, -1)).neighbours[4] = null; } if (ChunkAvailable(position + new Vector3Int(0, 0, 1))) { GetChunk(position + new Vector3Int(0, 0, 1)).neighbours[5] = null; } chunks.Remove(chunk); #if UNITY_EDITOR DestroyImmediate(chunk.gameObject); #else Destroy(chunk.gameObject); #endif } }
private void OnEnable() { mcc = (MarchingCubesChunk)target; //Selection.activeGameObject = ((MarchingCubesChunk)target).transform.parent.gameObject; }
/// <summary> /// Adds a chunk to the terrain /// </summary> /// <param name="position">the position for the new chunk in the chunk grid</param> /// <returns>the created chunk or null if no chunk could be created</returns> public MarchingCubesChunk AddChunk(Vector3Int position) { if (!ChunkAvailable(position)) { GameObject chunk = new GameObject(); chunk.name = "Chunk " + position.x + ":" + position.y + ":" + position.z; chunk.transform.parent = transform; chunk.transform.position = new Vector3(position.x * MarchingCubesChunk.size, position.y * MarchingCubesChunk.size, position.z * MarchingCubesChunk.size); MarchingCubesChunk cc = chunk.AddComponent <MarchingCubesChunk>(); cc.position = position; chunks.Add(cc); for (int i = 0; i < chunks.Count; i++) { if (chunks[i].position.x == position.x && chunks[i].position.y == position.y) { if (chunks[i].position.z == position.z - 1) { chunks[i].neighbours[4] = cc; cc.neighbours[5] = chunks[i]; } else if (chunks[i].position.z == position.z + 1) { chunks[i].neighbours[5] = cc; cc.neighbours[4] = chunks[i]; } } else if (chunks[i].position.x == position.x && chunks[i].position.z == position.z) { if (chunks[i].position.y == position.y - 1) { chunks[i].neighbours[2] = cc; cc.neighbours[3] = chunks[i]; } else if (chunks[i].position.y == position.y + 1) { chunks[i].neighbours[3] = cc; cc.neighbours[2] = chunks[i]; } } else if (chunks[i].position.y == position.y && chunks[i].position.z == position.z) { if (chunks[i].position.x == position.x - 1) { chunks[i].neighbours[0] = cc; cc.neighbours[1] = chunks[i]; } else if (chunks[i].position.x == position.x + 1) { chunks[i].neighbours[1] = cc; cc.neighbours[0] = chunks[i]; } } } chunk.GetComponent <MeshFilter>(); MeshRenderer mr = chunk.GetComponent <MeshRenderer>(); mr.sharedMaterial = Resources.Load <Material>("MarchingCubes/Terrain"); cc.values[2, 2, 2].v = 1; for (int x = 0; x < cc.xLength; x++) { for (int y = 0; y < cc.yLength; y++) { for (int z = 0; z < cc.zLength; z++) { cc.values[x, y, z].c = Color.green; } } } #if UNITY_EDITOR EditorUtility.SetDirty(this); EditorUtility.SetDirty(gameObject); #endif if (chunks.Count == 1) { Generate(); } return(cc); } return(null); }