// Use this for initialization void Start() { mapCenter = new Vector3(mapSize / 2, mapSize / 2, 0); cells = new DungeonCell[mapSize * mapSize]; for (int i = 0; i < mapSize * mapSize; i++) { float value = Random.Range(0, 100); cells[i] = new DungeonCell { dungeonCellType = value < 45 ? 1 : 0, dungeonCellFillType = 0 }; } for (int i = 0; i < 12; i++) { CalculateCell(); } CreateMapMesh(texture, CreateMesh()); }
void CalculateCell() { int numCells = mapSize * mapSize; var oldCellData = new DungeonCell[numCells]; for (int i = 0; i < numCells; i++) { oldCellData[i].dungeonCellType = cells[i].dungeonCellType; oldCellData[i].dungeonCellFillType = cells[i].dungeonCellFillType; } var newCellData = new DungeonCell[numCells]; var oldCellBuffer = new ComputeBuffer(numCells, sizeof(int) * 2); var newCellBuffer = new ComputeBuffer(numCells, sizeof(int) * 2); oldCellBuffer.SetData(oldCellData); newCellBuffer.SetData(newCellData); shader.SetBuffer(0, "oldCells", oldCellBuffer); shader.SetBuffer(0, "newCells", newCellBuffer); shader.SetInt("numCells", cells.Length); shader.SetInt("boundLimit", mapSize - 1); int threadGroups = Mathf.CeilToInt(numCells / (float)threadGroupSize); shader.Dispatch(0, threadGroups, 8, 1); newCellBuffer.GetData(newCellData); for (int i = 0; i < cells.Length; i++) { cells[i].dungeonCellType = newCellData[i].dungeonCellType; cells[i].dungeonCellFillType = newCellData[i].dungeonCellFillType; } oldCellBuffer.Dispose(); newCellBuffer.Dispose(); }