static void F() { Utils.ClearLogConsole(); // Random.InitState(Time.tim); bool finished = false; OverlappingModel model = null; var name = "Dungeon"; // var name = "Flowers"; for (int i = 0; i < 1 && !finished; i++) { // model = new OverlappingModel(name, 3, 48, 48, true, true, 2, -4); model = new OverlappingModel(name, 3, 48, 48, true, true, 8, 0); finished = model.Run(42, 0);//(int) (DateTime.Now.Millisecond), 0); } Debug.Log("Finished: " + finished); if (finished) { var t = model.Graphics(); File.WriteAllBytes($"Assets\\{name}-result.png", t.EncodeToPNG()); } AssetDatabase.Refresh(); // AssetDatabase.CreateAsset(t, ""); }
// called once on creation private void Start() { // retrieve the output dimenions width = PlayerPrefs.GetInt("ppWidth"); height = PlayerPrefs.GetInt("ppHeight"); System.Random random = new System.Random(); // create the model Model model; model = new OverlappingModel(inputName, N, width, height, periodicInput, periodicOutput, symmetry); // iterate 10 times for (int i = 0; i < 10; i++) { // run the algorithm with a random seed and return whether or not it completed int seed = random.Next(); bool finished = model.Run(seed); // if the algorithm completed if (finished) { // get the output image Texture2D texture2D = model.Graphics(); //set the size of the dungeon array dungeon = new int[texture2D.width, texture2D.height]; // iterate through the dungeon while mapping the image to it for (int column = 0; column < dungeon.GetLength(0); column++) { for (int row = 0; row < dungeon.GetLength(1); row++) { // decide on whether the tile is a wall or not based on the greyscale value of the pixel float tempValue = texture2D.GetPixel(column, row).grayscale; if (tempValue < 0.51f) { dungeon[column, row] = 0; } else { dungeon[column, row] = 1; } } } // this block of code is just adding a border of walls around the generated dungeon int borderSize = 5; int[,] borderedDungeon = new int[width + borderSize * 2, height + borderSize * 2]; // iterate through the 2d array. GetLength(0) returns the width of the array for (int column = 0; column < borderedDungeon.GetLength(0); column++) { // GetLength(1) returns the height of the array for (int row = 0; row < borderedDungeon.GetLength(1); row++) { // putting the original array into the bordered one if (column >= borderSize && column < width + borderSize && row >= borderSize && row < height + borderSize) { borderedDungeon[column, row] = dungeon[column - borderSize, row - borderSize]; } else // add the border { borderedDungeon[column, row] = 1; } } } // create a MeshGenerator and generate a mesh based on the 2d array MeshGenerator gen = GetComponent <MeshGenerator>(); gen.GenerateMesh(borderedDungeon, 1); // pass the dungeon to be exported as a csv file FindObjectOfType <GameManager>().ExportDungeonData(borderedDungeon); // prints the output image inside unity Sprite sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), Vector2.zero); GameObject gameObject = new GameObject($"{name}"); SpriteRenderer spriteRenderer = gameObject.AddComponent <SpriteRenderer>(); spriteRenderer.sprite = sprite; break; } } }