Advertisement
JontePonte

Procedural generaation code

Jun 13th, 2023
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.99 KB | Help | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Unity.Mathematics;
  5. using System;
  6. using Unity.VisualScripting;
  7.  
  8.  
  9. public class TerrainGenerator : MonoBehaviour
  10. {
  11.     public int TextureSize;
  12.     public int xSize = 20;
  13.     public int zSize = 20;
  14.     public float NoiseScale, IslandSize;
  15.     [Range(1, 20)] public int NoiseOctaves;
  16.     public Gradient ColorGradient;
  17.  
  18.  
  19.     // Privates
  20.     Color[] col;
  21.     Texture2D tex;
  22.     int Seed;
  23.     Mesh mesh;
  24.     Vector3[] vertices;
  25.     Vector2[] uvs;
  26.     int[] triangles;
  27.    
  28.  
  29.     private void Start()
  30.     {
  31.         mesh = new Mesh();
  32.         GetComponent<MeshFilter>().mesh = mesh;
  33.         Seed = UnityEngine.Random.Range(0, 99999);
  34.         tex = new Texture2D(TextureSize, TextureSize);
  35.         col = new Color[tex.height * tex.width];
  36.  
  37.  
  38.         Renderer rend = GetComponent<MeshRenderer>();
  39.         rend.sharedMaterial.mainTexture = tex;
  40.  
  41.         Vector2 Org = new Vector2(Mathf.Sqrt(Seed), Mathf.Sqrt(Seed));
  42.  
  43.         for (int x = 0, i = 0; x < TextureSize; x++)
  44.         {
  45.             for (int y = 0; y < TextureSize; y++, i++)
  46.             {
  47.                 col[i] = ColorGradient.Evaluate(Noisefunction((float)x, (float)y, Org));
  48.             }
  49.         }
  50.  
  51.         tex.SetPixels(col);
  52.         tex.Apply();
  53.         tex.wrapMode = TextureWrapMode.Clamp;
  54.     }
  55.  
  56.     private void Update()
  57.     {
  58.         CreateShape();
  59.         UpdateMesh();
  60.     }
  61.  
  62.     private void UpdateMesh()
  63.     {
  64.         mesh.Clear();
  65.         mesh.vertices = vertices;
  66.         mesh.triangles = triangles;
  67.         mesh.uv = uvs;
  68.         mesh.RecalculateNormals();
  69.     }
  70.  
  71.     private void CreateShape() // creates the mesh and vertices
  72.     {
  73.         Vector2 Org = new Vector2(Mathf.Sqrt(Seed), Mathf.Sqrt(Seed));
  74.         vertices = new Vector3[(xSize + 1) * (zSize + 1)];
  75.         for (int z = 0, i = 0; z <= zSize; z++)
  76.         {
  77.             for (int x = 0; x <= xSize; x++)
  78.             {
  79.  
  80.                 vertices[i] = new Vector3(x, 0, z); //where i set the height for the vertices
  81.                 i++;
  82.             }
  83.         }
  84.  
  85.         triangles = new int[xSize * zSize * 6];
  86.  
  87.         int vert = 0;
  88.         int tris = 0;
  89.         for (int z = 0; z < zSize; z++)
  90.         {
  91.             for (int x = 0; x < xSize; x++)
  92.             {
  93.                 triangles[tris + 0] = vert + 0;
  94.                 triangles[tris + 1] = vert + xSize + 1;
  95.                 triangles[tris + 2] = vert + 1;
  96.                 triangles[tris + 3] = vert + 1;
  97.                 triangles[tris + 4] = vert + xSize + 1;
  98.                 triangles[tris + 5] = vert + xSize + 2;
  99.                 vert++;
  100.                 tris += 6;
  101.             }
  102.             vert++;
  103.         }
  104.  
  105.         uvs = new Vector2[vertices.Length];
  106.         for (int z = 0, i = 0; z <= zSize; z++)
  107.         {
  108.             for (int x = 0; x <= xSize; x++)
  109.             {
  110.                 uvs[i] = new Vector2((float)x / xSize, (float)z / zSize);
  111.                 i++;
  112.             }
  113.         }
  114.     }
  115.    
  116.     //creates the noise
  117.     private float Noisefunction(float x, float y, Vector2 Origin)
  118.     {
  119.         float LocalnoiseValue = 0, noisesize = NoiseScale, opacity = 1;
  120.         for ( int octaves = 0; octaves < NoiseOctaves; octaves++)
  121.         {
  122.             float xVal = (x / (noisesize * TextureSize)) + Origin.x;
  123.             float yVal = (y / (noisesize * TextureSize)) - Origin.y;
  124.             float generateNoise = noise.snoise(new float2(xVal, yVal));
  125.             LocalnoiseValue += Mathf.InverseLerp(0, 1, generateNoise) / opacity;
  126.  
  127.             noisesize /= 2f;
  128.             opacity *= 2f;
  129.         }
  130.  
  131.         return LocalnoiseValue -= FallOffMap(x, y, TextureSize, IslandSize);
  132.     }
  133.  
  134.     private float FallOffMap(float x, float y, int size, float islandSize)
  135.     {
  136.         float gradient = 1;
  137.  
  138.         gradient /= (x * y) / (size * size) * (1 - (x / size)) * (1 - (y / size));
  139.         gradient -= 16;
  140.         gradient /= islandSize;
  141.  
  142.  
  143.         return gradient;
  144.     }
  145. }
Tags: Unity
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement