Ultimga

Untitled

Apr 20th, 2021 (edited)
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.57 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class NoiseDisplay : MonoBehaviour
  6. {
  7.     public int resolution = 512;
  8.     public float zoom = 100f;
  9.  
  10.     public Color oceanColor = Color.cyan;
  11.     public float seaLevel = 0.5f;
  12.     public float oceanZoom = 3.0f;
  13.  
  14.     FastNoise noise = new FastNoise();
  15.  
  16.     public void Start()
  17.     {
  18.         noise.SetNoiseType(FastNoise.NoiseType.SimplexFractal);
  19.         noise.SetFractalOctaves(6);
  20.     }
  21.  
  22.     public float[,] GenerateNoise()
  23.     {
  24.         float[,] values = new float[resolution, resolution];
  25.  
  26.         for (int x = 0; x < resolution; x++) {
  27.         for (int y = 0; y < resolution; y++) {
  28.  
  29.             float val = sample(x, y, zoom);
  30.  
  31.             values[x, y] = val;
  32.  
  33.         }}
  34.  
  35.         return values;
  36.     }
  37.  
  38.     public float sample(int x, int y, float zoom)
  39.     {
  40.         return RangeToRange( noise.GetNoise(x / zoom, y / zoom), -1f, 1f, 0f, 1f);
  41.     }
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.     public float RangeToRange(float _input, float _low, float _high, float _newLow, float _newHigh)
  57.     {
  58.         return ((_input - _low) / (_high - _low)) * (_newHigh - _newLow) + _newLow;
  59.     }
  60.  
  61.     public bool update = false;
  62.     public Texture2D texture;
  63.  
  64.     public void Update()
  65.     {
  66.         if (update)
  67.         {
  68.             Render( GenerateNoise() );
  69.             update = false;
  70.         }
  71.     }
  72.  
  73.     public void Render(float[,] values)
  74.     {
  75.         int width = resolution, height = resolution;
  76.  
  77.         Texture2D tex = new Texture2D(width, height);
  78.  
  79.         Color[] colors = new Color[width * height];
  80.  
  81.         for (int x = 0; x < height; x++)
  82.         {
  83.             for (int y = 0; y < width; y++)
  84.             {
  85.                 Color color = Color.Lerp(Color.black, Color.white, values[x, y]);
  86.  
  87.                 if (values[x, y] < seaLevel)
  88.                 {
  89.                     color *= oceanColor;
  90.                 }
  91.  
  92.                 colors[toIndex(x, y)] = color;
  93.             }
  94.         }
  95.  
  96.         tex.SetPixels(colors);
  97.         tex.Apply();
  98.  
  99.         texture = tex;
  100.     }
  101.  
  102.     public int toIndex(int _x, int _y)
  103.     {
  104.         if (_x < 0) _x = 0;
  105.         if (_y < 0) _y = 0;
  106.         if (_x >= resolution) _x = resolution - 1;
  107.         if (_y >= resolution) _y = resolution - 1;
  108.  
  109.         return (_y * resolution) + _x;
  110.     }
  111.  
  112.     private void OnGUI()
  113.     {
  114.         if (Event.current.type.Equals(EventType.Repaint))
  115.         {
  116.             if (texture != null) Graphics.DrawTexture(new Rect(0, 0, resolution, resolution), texture);
  117.         }
  118.     }
  119. }
  120.  
Add Comment
Please, Sign In to add comment