Unity柏林噪声图代码保存


using System;

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class mapC : MonoBehaviour  //地图控制器

{

    public int mapSize; //设置地图大小

    public float scale;  //柏林噪声倍率

    public Gradient gradient;

    void Start()

    {

        GameObject map = new GameObject("map");

        map.AddComponent().sprite = CreatSprit();

    }

    Sprite CreatSprit()

    {

        Texture2D texture2D = new Texture2D(mapSize, mapSize);

        texture2D.SetPixels(CreatColors(mapSize, scale, gradient));              //设置颜色,需要颜色数组

        texture2D.Apply();

        Rect rect = new Rect(default, new Vector2(texture2D.width, texture2D.height));

        return Sprite.Create(texture2D, rect, default);

    }

    Color[] CreatColors(int mapSize, float scale, Gradient gradient)

    {

        Color[] colors = new Color[mapSize * mapSize];

        float[,] perlinColors = new float[mapSize, mapSize];

        for (int y = 0; y < mapSize; y++)

        {

            for (int x = 0; x < mapSize; x++)

            {

                //colors[y * mapSize + x] = new Color(

                //    Mathf.PerlinNoise(x / 100f * scale, y / 100f * scale),

                //    Mathf.PerlinNoise(x / 100f * scale, y / 100f * scale),

                //    Mathf.PerlinNoise(x / 100f * scale, y / 100f * scale));

                perlinColors[x, y] = Mathf.PerlinNoise(x / 100f * scale, y / 100f * scale);

                colors[y * mapSize + x] = gradient.Evaluate(perlinColors[x, y]);

            }

        }

        return colors;

    }

}

你可能感兴趣的:(Unity柏林噪声图代码保存)