Advertisement
JontePonte

Procedural terrain code

Jun 5th, 2023
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.00 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. using Unity.VisualScripting.Dependencies.Sqlite;
  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.    
  17.    
  18.     // Privates
  19.  
  20.     Color[] col;
  21.     Texture2D tex;
  22.     int Seed;
  23.     Mesh mesh;
  24.     Vector3[] vertices;
  25.     Vector2[] uvs;
  26.     int[] triangles;
  27.     float noiseValue;
  28.     public Gradient ColorGradient;
  29.  
  30.     private void Start()
  31.     {
  32.         mesh = new Mesh();
  33.         GetComponent<MeshFilter>().mesh = mesh;
  34.         Seed = UnityEngine.Random.Range(0, 99999);
  35.         tex = new Texture2D(TextureSize, TextureSize);
  36.         col = new Color[tex.height * tex.width];
  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.         tex.SetPixels(col);
  51.         tex.Apply();
  52.         tex.wrapMode = TextureWrapMode.Clamp;
  53.     }
  54.  
  55.     private void Update()
  56.     {
  57.         CreateShape();
  58.         UpdateMesh();
  59.     }
  60.  
  61.     private void UpdateMesh()
  62.     {
  63.         mesh.Clear();
  64.         mesh.vertices = vertices;
  65.         mesh.triangles = triangles;
  66.         mesh.uv = uvs;
  67.         mesh.RecalculateNormals();
  68.     }
  69.  
  70.     //creates the mesh
  71.     private void CreateShape()
  72.     {      
  73.         vertices = new Vector3[(xSize + 1) * (zSize + 1)];
  74.         for (int z = 0, i = 0; z <= zSize; z++)
  75.         {
  76.             for (int x = 0; x <= xSize; x++)
  77.             {              
  78.                 vertices[i] = new Vector3(x, noiseValue, z);
  79.                 i++;
  80.             }
  81.         }
  82.         triangles = new int[xSize * zSize * 6];
  83.  
  84.         int vert = 0;
  85.         int tris = 0;
  86.         for (int z = 0; z < zSize; z++)
  87.         {
  88.             for (int x = 0; x < xSize; x++)
  89.             {
  90.                 triangles[tris + 0] = vert + 0;
  91.                 triangles[tris + 1] = vert + xSize + 1;
  92.                 triangles[tris + 2] = vert + 1;
  93.                 triangles[tris + 3] = vert + 1;
  94.                 triangles[tris + 4] = vert + xSize + 1;
  95.                 triangles[tris + 5] = vert + xSize + 2;
  96.                 vert++;
  97.                 tris += 6;
  98.             }
  99.             vert++;
  100.         }
  101.  
  102.         uvs = new Vector2[vertices.Length];
  103.         for (int z = 0, i = 0; z <= zSize; z++)
  104.         {
  105.             for (int x = 0; x <= xSize; x++)
  106.             {
  107.                 uvs[i] = new Vector2((float)x / xSize, (float)z / zSize);
  108.                 i++;
  109.             }
  110.         }
  111.     }
  112.  
  113.  
  114.     //creates noise
  115.     private float Noisefunction(float x, float y, Vector2 Origin)
  116.     {
  117.         float a = 0, noisesize = NoiseScale, opacity = 1;
  118.  
  119.         for (int octaves = 0; octaves < NoiseOctaves; octaves++)
  120.         {
  121.            
  122.             float xVal = (x / (noisesize * TextureSize)) + Origin.x;
  123.             float yVal = (y / (noisesize * TextureSize)) - Origin.y;
  124.             noiseValue = noise.snoise(new float2(xVal, yVal)); // thought this was the line to get the noise value
  125.             a += Mathf.InverseLerp(0, 1, noiseValue) / opacity;
  126.  
  127.             noisesize /= 2f;
  128.             opacity *= 2f;
  129.         }
  130.  
  131.         return a -= FallOffMap(x, y, TextureSize, IslandSize);
  132.     }
  133.  
  134.     //makes it an island
  135.     private float FallOffMap(float x, float y, int size, float islandSize)
  136.     {
  137.         float gradient = 1;
  138.  
  139.         gradient /= (x * y) / (size * size) * (1 - (x / size)) * (1 - (y / size));
  140.         gradient -= 16;
  141.         gradient /= islandSize;
  142.  
  143.  
  144.         return gradient;
  145.     }
  146. }
Tags: C#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement