Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Unity.Mathematics;
- using System;
- using Unity.VisualScripting;
- public class TerrainGenerator : MonoBehaviour
- {
- public int TextureSize;
- public int xSize = 20;
- public int zSize = 20;
- public float NoiseScale, IslandSize;
- [Range(1, 20)] public int NoiseOctaves;
- public Gradient ColorGradient;
- // Privates
- Color[] col;
- Texture2D tex;
- int Seed;
- Mesh mesh;
- Vector3[] vertices;
- Vector2[] uvs;
- int[] triangles;
- private void Start()
- {
- mesh = new Mesh();
- GetComponent<MeshFilter>().mesh = mesh;
- Seed = UnityEngine.Random.Range(0, 99999);
- tex = new Texture2D(TextureSize, TextureSize);
- col = new Color[tex.height * tex.width];
- Renderer rend = GetComponent<MeshRenderer>();
- rend.sharedMaterial.mainTexture = tex;
- Vector2 Org = new Vector2(Mathf.Sqrt(Seed), Mathf.Sqrt(Seed));
- for (int x = 0, i = 0; x < TextureSize; x++)
- {
- for (int y = 0; y < TextureSize; y++, i++)
- {
- col[i] = ColorGradient.Evaluate(Noisefunction((float)x, (float)y, Org));
- }
- }
- tex.SetPixels(col);
- tex.Apply();
- tex.wrapMode = TextureWrapMode.Clamp;
- }
- private void Update()
- {
- CreateShape();
- UpdateMesh();
- }
- private void UpdateMesh()
- {
- mesh.Clear();
- mesh.vertices = vertices;
- mesh.triangles = triangles;
- mesh.uv = uvs;
- mesh.RecalculateNormals();
- }
- private void CreateShape() // creates the mesh and vertices
- {
- Vector2 Org = new Vector2(Mathf.Sqrt(Seed), Mathf.Sqrt(Seed));
- vertices = new Vector3[(xSize + 1) * (zSize + 1)];
- for (int z = 0, i = 0; z <= zSize; z++)
- {
- for (int x = 0; x <= xSize; x++)
- {
- vertices[i] = new Vector3(x, 0, z); //where i set the height for the vertices
- i++;
- }
- }
- triangles = new int[xSize * zSize * 6];
- int vert = 0;
- int tris = 0;
- for (int z = 0; z < zSize; z++)
- {
- for (int x = 0; x < xSize; x++)
- {
- triangles[tris + 0] = vert + 0;
- triangles[tris + 1] = vert + xSize + 1;
- triangles[tris + 2] = vert + 1;
- triangles[tris + 3] = vert + 1;
- triangles[tris + 4] = vert + xSize + 1;
- triangles[tris + 5] = vert + xSize + 2;
- vert++;
- tris += 6;
- }
- vert++;
- }
- uvs = new Vector2[vertices.Length];
- for (int z = 0, i = 0; z <= zSize; z++)
- {
- for (int x = 0; x <= xSize; x++)
- {
- uvs[i] = new Vector2((float)x / xSize, (float)z / zSize);
- i++;
- }
- }
- }
- //creates the noise
- private float Noisefunction(float x, float y, Vector2 Origin)
- {
- float LocalnoiseValue = 0, noisesize = NoiseScale, opacity = 1;
- for ( int octaves = 0; octaves < NoiseOctaves; octaves++)
- {
- float xVal = (x / (noisesize * TextureSize)) + Origin.x;
- float yVal = (y / (noisesize * TextureSize)) - Origin.y;
- float generateNoise = noise.snoise(new float2(xVal, yVal));
- LocalnoiseValue += Mathf.InverseLerp(0, 1, generateNoise) / opacity;
- noisesize /= 2f;
- opacity *= 2f;
- }
- return LocalnoiseValue -= FallOffMap(x, y, TextureSize, IslandSize);
- }
- private float FallOffMap(float x, float y, int size, float islandSize)
- {
- float gradient = 1;
- gradient /= (x * y) / (size * size) * (1 - (x / size)) * (1 - (y / size));
- gradient -= 16;
- gradient /= islandSize;
- return gradient;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement