Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class NoiseDisplay : MonoBehaviour
- {
- public int resolution = 512;
- public float zoom = 100f;
- public Color oceanColor = Color.cyan;
- public float seaLevel = 0.5f;
- public float oceanZoom = 3.0f;
- FastNoise noise = new FastNoise();
- public void Start()
- {
- noise.SetNoiseType(FastNoise.NoiseType.SimplexFractal);
- noise.SetFractalOctaves(6);
- }
- public float[,] GenerateNoise()
- {
- float[,] values = new float[resolution, resolution];
- for (int x = 0; x < resolution; x++) {
- for (int y = 0; y < resolution; y++) {
- float val = sample(x, y, zoom);
- values[x, y] = val;
- }}
- return values;
- }
- public float sample(int x, int y, float zoom)
- {
- return RangeToRange( noise.GetNoise(x / zoom, y / zoom), -1f, 1f, 0f, 1f);
- }
- public float RangeToRange(float _input, float _low, float _high, float _newLow, float _newHigh)
- {
- return ((_input - _low) / (_high - _low)) * (_newHigh - _newLow) + _newLow;
- }
- public bool update = false;
- public Texture2D texture;
- public void Update()
- {
- if (update)
- {
- Render( GenerateNoise() );
- update = false;
- }
- }
- public void Render(float[,] values)
- {
- int width = resolution, height = resolution;
- Texture2D tex = new Texture2D(width, height);
- Color[] colors = new Color[width * height];
- for (int x = 0; x < height; x++)
- {
- for (int y = 0; y < width; y++)
- {
- Color color = Color.Lerp(Color.black, Color.white, values[x, y]);
- if (values[x, y] < seaLevel)
- {
- color *= oceanColor;
- }
- colors[toIndex(x, y)] = color;
- }
- }
- tex.SetPixels(colors);
- tex.Apply();
- texture = tex;
- }
- public int toIndex(int _x, int _y)
- {
- if (_x < 0) _x = 0;
- if (_y < 0) _y = 0;
- if (_x >= resolution) _x = resolution - 1;
- if (_y >= resolution) _y = resolution - 1;
- return (_y * resolution) + _x;
- }
- private void OnGUI()
- {
- if (Event.current.type.Equals(EventType.Repaint))
- {
- if (texture != null) Graphics.DrawTexture(new Rect(0, 0, resolution, resolution), texture);
- }
- }
- }
Add Comment
Please, Sign In to add comment