Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEditor;
- public class MapGenerator : MonoBehaviour {
- public int width;
- public int height;
- public float scale = 20f;
- public float offsetX;
- public float offsetY;
- public int nbGrid = 10;
- public GameObject BlankSprite;
- public Color Eau;
- public Color EauProfonde;
- public Color EauSemiProfonde;
- public Color Plage;
- public Color Terre;
- public Color Sommet;
- // Use this for initialization
- void Start () {
- Renderer renderer = this.GetComponent<Renderer>();
- Texture2D texture = GenerateTexture();
- renderer.material.mainTexture = texture;
- GenerateSprites(texture);
- }
- Texture2D GenerateTexture()
- {
- Texture2D texture = new Texture2D(width, height);
- for (int x = 0; x < width; x++)
- {
- for (int y = 0; y < width; y++)
- {
- Color color = GenerateColor(x, y);
- texture.SetPixel(x, y, color);
- }
- }
- texture.Apply();
- return texture;
- }
- Color GenerateColor(int x, int y)
- {
- float xCoord = (float)x / width * scale + offsetX;
- float yCoord = (float)y / height * scale + offsetY;
- float sample = Mathf.PerlinNoise(xCoord, yCoord);
- sample = (1 - sample)+ (float)0.025 *Modifier(width, x) + (float)0.025 * Modifier(height, y);
- return new Color(sample, sample, sample);
- }
- float Modifier(int width, int value)
- {
- float result = 0;
- if (value > width/2 && value < (width-width/2))
- {
- result = 1;
- }else if (value > width / 4 && value < (width - width / 4))
- {
- result = 0;
- }
- else
- {
- result = -2;
- }
- return result;
- }
- void GenerateSprites(Texture2D texture)
- {
- int nbCoolX = nbGrid;
- int nbCoolY = nbGrid;
- float scaleX = (float)1 / nbGrid;
- float scaleY = (float)1 / nbGrid;
- int gridWidth = width / nbGrid;
- int gridHeight = height / nbGrid;
- for (int coolX = 0; coolX < nbCoolX; coolX++)
- {
- for (int coolY = 0; coolY < nbCoolY; coolY++)
- {
- GameObject obj = Instantiate(BlankSprite, new Vector3(coolX, coolY, 0), Quaternion.identity, this.transform) as GameObject;
- obj.transform.localScale = new Vector3(scaleX, scaleY, 1);
- obj.transform.localPosition = new Vector3((float)(-0.5 + (scaleX * coolX)), (float)(-0.5 + (scaleY * coolY)), 0);
- SpriteRenderer spriteRenderer = obj.GetComponent<SpriteRenderer>();
- Texture2D spriteTex = new Texture2D(gridWidth, gridHeight);
- int x = 0;
- while (x != width)
- {
- int y = 0;
- while (y != height)
- {
- Color pixel = texture.GetPixel(x + (gridWidth * (coolX + 1)), y + (gridWidth * (coolY + 1)));
- Color result;
- if (pixel.grayscale > 0.9f)
- {
- result = Sommet;
- }
- else if (pixel.grayscale > 0.85f)
- {
- result = Terre;
- }
- else if (pixel.grayscale > 0.8f)
- {
- result = Plage;
- }
- else if (pixel.grayscale < 0.20f)
- {
- result = EauProfonde;
- }
- else if (pixel.grayscale < 0.25f)
- {
- result = EauSemiProfonde;
- }
- else
- {
- result = Eau;
- }
- spriteTex.SetPixel(x, y, result);
- y++;
- }
- x++;
- }
- spriteTex.Apply();
- Sprite oldSprite = spriteRenderer.sprite;
- Sprite sprite = Sprite.Create(spriteTex, new Rect(0.0f, 0.0f, spriteTex.width, spriteTex.height), oldSprite.pivot, gridWidth);
- spriteRenderer.sprite = sprite;
- MapTile tile = obj.GetComponent<MapTile>();
- tile.coolX = coolX;
- tile.coolY = coolY;
- tile.Hook(this);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement