Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.97 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class PerlinNoise : MonoBehaviour
  4. {
  5.  
  6.     public int width = 256;
  7.     public int height = 256;
  8.  
  9.     public float scale = 20f;
  10.  
  11.     private void Update()
  12.     {
  13.         Renderer renderer = GetComponent<Renderer>();
  14.         renderer.material.mainTexture = GenerateTexture();
  15.     }
  16.  
  17.     Texture2D GenerateTexture ()
  18.     {
  19.         Texture2D texture = new Texture2D(width, height);
  20.  
  21.         for (int x = 0; x < width; x++)
  22.         {
  23.             for (int y = 0; x < height; y++)
  24.             {
  25.                 Color color = CalculateColor(x, y);
  26.                 texture.SetPixel(x, y, color);
  27.             }
  28.         }
  29.  
  30.         texture.Apply();
  31.         return texture;
  32.     }
  33.  
  34.     Color CalculateColor (int x, int y)
  35.     {
  36.         float xCoord = (float)x / width * scale;
  37.         float yCoord = (float)y / height * scale;
  38.  
  39.         float sample = Mathf.PerlinNoise(xCoord, yCoord);
  40.         return new Color(sample, sample, sample);
  41.     }
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement