Advertisement
raphael76280

Astrolabe MapGenerator

Mar 25th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.84 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5.  
  6. public class MapGenerator : MonoBehaviour {
  7.  
  8.     public int width;
  9.     public int height;
  10.  
  11.     public float scale = 20f;
  12.     public float offsetX;
  13.     public float offsetY;
  14.  
  15.     public int nbGrid = 10;
  16.  
  17.     public GameObject BlankSprite;
  18.  
  19.    
  20.     public Color Eau;
  21.     public Color EauProfonde;
  22.     public Color EauSemiProfonde;
  23.     public Color Plage;
  24.     public Color Terre;
  25.     public Color Sommet;
  26.  
  27.  
  28.     // Use this for initialization
  29.     void Start () {
  30.         Renderer renderer = this.GetComponent<Renderer>();
  31.         Texture2D texture = GenerateTexture();
  32.         renderer.material.mainTexture = texture;
  33.         GenerateSprites(texture);
  34.  
  35.     }
  36.  
  37.     Texture2D GenerateTexture()
  38.     {
  39.         Texture2D texture = new Texture2D(width, height);
  40.         for (int x = 0; x < width; x++)
  41.         {
  42.             for (int y = 0; y < width; y++)
  43.             {
  44.                 Color color = GenerateColor(x, y);
  45.                 texture.SetPixel(x, y, color);
  46.             }
  47.         }
  48.         texture.Apply();
  49.         return texture;
  50.     }
  51.  
  52.     Color GenerateColor(int x, int y)
  53.     {
  54.  
  55.  
  56.         float xCoord = (float)x / width * scale + offsetX;
  57.         float yCoord = (float)y / height * scale + offsetY;
  58.  
  59.         float sample = Mathf.PerlinNoise(xCoord, yCoord);
  60.         sample = (1 - sample)+ (float)0.025 *Modifier(width, x) + (float)0.025 * Modifier(height, y);
  61.         return new Color(sample, sample, sample);
  62.     }
  63.  
  64.     float Modifier(int width, int value)
  65.     {
  66.         float result = 0;
  67.         if (value > width/2 && value < (width-width/2))
  68.         {
  69.             result = 1;
  70.         }else if (value > width / 4 && value < (width - width / 4))
  71.         {
  72.             result = 0;
  73.         }
  74.         else
  75.         {
  76.             result = -2;
  77.         }
  78.             return result;
  79.     }
  80.  
  81.     void GenerateSprites(Texture2D texture)
  82.     {
  83.         int nbCoolX = nbGrid;
  84.         int nbCoolY = nbGrid;
  85.  
  86.         float scaleX = (float)1 / nbGrid;
  87.         float scaleY = (float)1 / nbGrid;
  88.  
  89.         int gridWidth = width / nbGrid;
  90.         int gridHeight = height / nbGrid;
  91.  
  92.         for (int coolX = 0; coolX < nbCoolX; coolX++)
  93.         {
  94.             for (int coolY = 0; coolY < nbCoolY; coolY++)
  95.             {
  96.                
  97.                 GameObject obj = Instantiate(BlankSprite, new Vector3(coolX, coolY, 0), Quaternion.identity, this.transform) as GameObject;
  98.                
  99.                
  100.                 obj.transform.localScale = new Vector3(scaleX, scaleY, 1);
  101.                 obj.transform.localPosition = new Vector3((float)(-0.5 + (scaleX * coolX)), (float)(-0.5 + (scaleY * coolY)), 0);
  102.                 SpriteRenderer spriteRenderer = obj.GetComponent<SpriteRenderer>();
  103.                 Texture2D spriteTex = new Texture2D(gridWidth, gridHeight);
  104.  
  105.                
  106.                 int x = 0;
  107.                 while (x != width)
  108.                 {
  109.                    
  110.                     int y = 0;
  111.                     while (y != height)
  112.                     {
  113.                        
  114.  
  115.                         Color pixel = texture.GetPixel(x + (gridWidth * (coolX + 1)), y + (gridWidth * (coolY + 1)));
  116.  
  117.                         Color result;
  118.                         if (pixel.grayscale > 0.9f)
  119.                         {
  120.                             result = Sommet;
  121.                         }
  122.                         else if (pixel.grayscale > 0.85f)
  123.                         {
  124.                             result = Terre;
  125.                         }
  126.                         else if (pixel.grayscale > 0.8f)
  127.                         {
  128.                             result = Plage;
  129.                         }
  130.                         else if (pixel.grayscale < 0.20f)
  131.                         {
  132.                             result = EauProfonde;
  133.                         }
  134.                         else if (pixel.grayscale < 0.25f)
  135.                         {
  136.                             result = EauSemiProfonde;
  137.                         }
  138.                         else
  139.                         {
  140.                             result = Eau;
  141.                         }
  142.                         spriteTex.SetPixel(x, y, result);
  143.  
  144.                         y++;
  145.                     }
  146.                     x++;
  147.                 }
  148.                
  149.                 spriteTex.Apply();
  150.  
  151.                 Sprite oldSprite = spriteRenderer.sprite;
  152.                 Sprite sprite = Sprite.Create(spriteTex, new Rect(0.0f, 0.0f, spriteTex.width, spriteTex.height), oldSprite.pivot, gridWidth);
  153.                 spriteRenderer.sprite = sprite;
  154.                 MapTile tile = obj.GetComponent<MapTile>();
  155.                 tile.coolX = coolX;
  156.                 tile.coolY = coolY;
  157.                 tile.Hook(this);
  158.  
  159.             }
  160.         }
  161.     }
  162.  
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement