using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.InputSystem; public class NoiseTesting : MonoBehaviour { System.Random rand = new System.Random(); Dictionary mapGen; Vector3 current; Vector3 next; public GameObject tilePrefab; // Start is called before the first frame update void Start() { mapGen = new Dictionary(); current = new Vector3(0, 0, 0); mapGen.Add(current, Instantiate(tilePrefab, current, Quaternion.identity)); applyNormalNoise(mapGen[current], mapGen); } // Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.Space)) { applyNormalNoise(mapGen[new Vector3(0, 0, 0)], mapGen); } if (Input.GetKeyDown(KeyCode.A)) { next = current - new Vector3(100, 0, 0); } if (Input.GetKeyDown(KeyCode.W)) { next = current - new Vector3(0, 0, -100); } if (Input.GetKeyDown(KeyCode.D)) { next = current - new Vector3(-100, 0, 0); } if (Input.GetKeyDown(KeyCode.S)) { next = current - new Vector3(0, 0, 100); } if (next != current) { current = next; if (!mapGen.ContainsKey(current)) { mapGen.Add(current, Instantiate(tilePrefab, current, Quaternion.identity)); applyNormalNoise(mapGen[current], mapGen); } } } private void applyNormalNoise(GameObject tile, Dictionary map) { //resetMesh(tile); Mesh mesh = tile.GetComponent().mesh; Vector3[] vertices = mesh.vertices; //float[] samples = calculateBasicSamples(tile); float[] samples = calculateConnectedSamples(tile); Color32[] colors; Color32 low = new Color32(0, 150, 0, 255); Color32 high = new Color32(0, 110, 20, 255); string tiletype = ""; float lowestValue = 10; float highestValue = 0; for (int i = 0; i < samples.Length; i++) { if (lowestValue > samples[i]) { lowestValue = samples[i]; } if (highestValue < samples[i]) { highestValue = samples[i]; } } float modifier = highestValue - lowestValue; colors = new Color32[samples.Length]; for (int i = 0; i < samples.Length; i++) { colors[i] = Color32.Lerp(low, high, (1 / modifier) * (samples[i] - lowestValue)); } for (int i = 0; i < samples.Length; i++) { vertices[i].y = samples[i];// * 3; } applyMesh(tile, vertices, mesh, colors); //tile.GetComponent().setType(tiletype); } private void resetMesh(GameObject tile) { Mesh mesh = tile.GetComponent().sharedMesh; Vector3[] vertices = mesh.vertices; for (int i = 0; i < vertices.Length; i++) { vertices[i].y = 0; } mesh.vertices = vertices; mesh.RecalculateBounds(); mesh.RecalculateNormals(); tile.GetComponent().sharedMesh = mesh; } private float[] calculateBasicSamples(GameObject tile) { Mesh mesh = tile.GetComponent().mesh; Vector3[] vertices = mesh.vertices; float[] samples = new float[vertices.Length]; for (int i = 0; i < vertices.Length; i++) { float xCord = vertices[i].x / (vertices.Length - 1) * 10; float zCord = vertices[i].z / (vertices.Length - 1) * 10; float sample = Mathf.PerlinNoise(xCord, zCord) - 0.5f * rand.Next(0, 6); samples[i] = sample; } return samples; } private float[] calculateConnectedSamples(GameObject tile) { //Working with "next" as index for tile to be generated //Working with "mapGen" as map Placeholder to check against neighbours Mesh mesh = tile.GetComponent().mesh; Vector3[] vertices = mesh.vertices; float[] samples = Enumerable.Repeat(-100f, vertices.Length).ToArray(); int sideLength = Mathf.RoundToInt(Mathf.Sqrt(vertices.Length)); samples = connectSamples(samples, sideLength); for (int i = 0; i < vertices.Length; i++) { if (samples[i] == -100) { float xCord = vertices[i].x / (vertices.Length - 1) * 10; float zCord = vertices[i].z / (vertices.Length - 1) * 10; float sample = Mathf.PerlinNoise(xCord, zCord) - 1f * rand.Next(0, 6); samples[i] = sample; } } return samples; } private void applyMesh(GameObject tile, Vector3[] vertices, Mesh mesh, Color32[] colors) { mesh.vertices = vertices; mesh.RecalculateBounds(); mesh.RecalculateNormals(); mesh.colors32 = colors; tile.GetComponent().sharedMesh = mesh; } private float[] connectSamples(float[] samples, int sideLength) { Mesh mesh; Vector3[] vertices; for (int i = 0; i < sideLength; i++) { if (mapGen.ContainsKey(next - new Vector3(100, 0, 0))) { //LINKS mesh = mapGen[next - new Vector3(100, 0, 0)].GetComponent().mesh; vertices = mesh.vertices; samples[sideLength - 1 + sideLength * i] = vertices[sideLength * i].y; } if (mapGen.ContainsKey(next - new Vector3(-100, 0, 0))) { //RECHTS mesh = mapGen[next - new Vector3(-100, 0, 0)].GetComponent().mesh; vertices = mesh.vertices; samples[sideLength * i] = vertices[sideLength - 1 + sideLength * i].y; } if (mapGen.ContainsKey(next - new Vector3(0, 0, -100))) { //OBEN mesh = mapGen[next - new Vector3(0, 0, -100)].GetComponent().mesh; vertices = mesh.vertices; samples[i] = vertices[(sideLength * sideLength) - (sideLength-i)].y; } if (mapGen.ContainsKey(next - new Vector3(0, 0, 100))) { //UNTEN mesh = mapGen[next - new Vector3(0, 0, 100)].GetComponent().mesh; vertices = mesh.vertices; samples[(sideLength * sideLength) - (sideLength-i)] = vertices[i].y; } } return samples; } }