├── Camera Related ├── CameraCollisionDetection.cs ├── CameraController.cs └── CameraFollow.cs ├── Cellular Automata ├── Cellular Automata Using Tilemaps.cs └── CellularAutomata V1.cs ├── Chunks And Terrain ├── ChunkLoading.cs ├── LodSystem.cs └── LowPolyTerrain.cs ├── Dialogue Systems ├── DialogueSystemWithSentences.cs └── TextScroller.cs ├── Marching Things ├── InterpolatedMarchingSquares.cs ├── MarchingCubes.cs └── MarchingSquares.cs ├── Mesh Generators ├── Circle.cs ├── HexagonGrid.cs ├── TerrainMeshGenerator.cs └── TreeGenerator.cs ├── Procedural Noise Islands ├── Island Texture With Customisable Octaves and Seed.cs ├── Noise Map Generation V1 ├── Noise Map Generation V2 └── Noise With Biomes │ ├── Biome Texture.png │ ├── Noise Generator.cs │ └── Noise function.cs ├── README.md ├── Shader Graph └── Water Shaders.zip ├── Texture Generation Misc └── Voronoi Diagram Script ├── Voxel Mesh Generators ├── TexAtlas.png └── Voxel Mesh Episode 1.cs └── Water Scripts ├── UnderWaterDetection.cs └── Water.cs /Camera Related/CameraCollisionDetection.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class CameraCollisionDetection : MonoBehaviour 4 | { 5 | public Transform referenceTransform; 6 | public float collisionOffset = 0.3f; 7 | public float cameraSpeed = 15f; 8 | 9 | Vector3 defaultPos, directionNormalized; 10 | Transform parentTransform; 11 | float defaultDistance; 12 | 13 | private void Start() 14 | { 15 | defaultPos = transform.localPosition; 16 | directionNormalized = defaultPos.normalized; 17 | parentTransform = transform.parent; 18 | defaultDistance = Vector3.Distance(defaultPos, Vector3.zero); 19 | } 20 | private void LateUpdate() 21 | { 22 | RaycastHit hit; 23 | Vector3 dirTmp = parentTransform.TransformPoint(defaultPos) - referenceTransform.position; 24 | if (Physics.SphereCast(referenceTransform.position, collisionOffset, dirTmp, out hit, defaultDistance)){ 25 | transform.localPosition = (directionNormalized * (hit.distance - collisionOffset)); 26 | } 27 | else transform.localPosition = Vector3.Lerp(transform.localPosition, defaultPos, Time.deltaTime * cameraSpeed); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Camera Related/CameraController.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class CameraController : MonoBehaviour 6 | { 7 | public float Sensitivity, xlim, MovementSpeed, jumpHeight, CamFollowSpeed; 8 | public GameObject Camera; 9 | Vector2 rotation = Vector2.zero; 10 | private float variableSpeed, cameramovementlerp; 11 | public Rigidbody rb; 12 | public bool grounded; 13 | private Vector3 Offset; 14 | 15 | private void Start() 16 | { 17 | Cursor.lockState = CursorLockMode.Confined; 18 | Cursor.visible = false; 19 | Offset = Camera.transform.position - transform.position; 20 | 21 | Cursor.lockState = CursorLockMode.Locked; 22 | Cursor.visible = false; 23 | } 24 | 25 | private void Update() 26 | { 27 | 28 | rotation.y += Input.GetAxis("Mouse X") * Sensitivity; 29 | rotation.x += -Input.GetAxis("Mouse Y") * Sensitivity; 30 | rotation.x = Mathf.Clamp(rotation.x, -xlim, xlim); 31 | Camera.transform.localRotation = Quaternion.Euler(rotation.x, transform.eulerAngles.y, 0); 32 | transform.eulerAngles = new Vector2(0, rotation.y); 33 | 34 | if (Input.GetKey("w")) transform.position = transform.position + transform.forward * variableSpeed / 10; 35 | if (Input.GetKey("s")) transform.position = transform.position + -transform.forward * variableSpeed / 10; 36 | if (Input.GetKey("d")) transform.position = transform.position + transform.right * variableSpeed / 10; 37 | if (Input.GetKey("a")) transform.position = transform.position + -transform.right * variableSpeed / 10; 38 | 39 | if (Input.GetKeyDown(KeyCode.Space) && grounded) rb.AddForce(new Vector3(0, jumpHeight, 0)); 40 | 41 | if (Input.GetKey(KeyCode.LeftShift)){ 42 | variableSpeed = MovementSpeed * 2f; 43 | cameramovementlerp = 70; 44 | } 45 | else{ 46 | variableSpeed = MovementSpeed; 47 | cameramovementlerp = 60; 48 | } 49 | Camera.transform.position = Vector3.Lerp(Camera.transform.position, transform.position + Offset, Time.smoothDeltaTime * CamFollowSpeed); 50 | 51 | if (Input.GetKey(KeyCode.LeftControl)) variableSpeed = MovementSpeed / 3f; 52 | else variableSpeed = MovementSpeed; 53 | float cml = Camera.GetComponentInChildren().fieldOfView; 54 | Camera.GetComponentInChildren().fieldOfView = Mathf.Lerp(cml, cameramovementlerp, Time.deltaTime * 6); 55 | } 56 | 57 | private void OnCollisionEnter(Collision collision) 58 | { 59 | if (collision.gameObject.tag == "Floor") grounded = true; 60 | } 61 | 62 | private void OnCollisionExit(Collision collision) 63 | { 64 | if (collision.gameObject.tag == "Floor") grounded = false; 65 | 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /Camera Related/CameraFollow.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class CameraFollow : MonoBehaviour 6 | { 7 | public GameObject Camera; 8 | public float PlayerSpeed = 10; 9 | public float CameraFollowSpeed; 10 | private Vector3 CamOffset; 11 | 12 | void Start() 13 | { 14 | CamOffset = Camera.transform.position - transform.position; 15 | } 16 | 17 | void Update() 18 | { 19 | transform.position += new Vector3(Input.GetAxis("Horizontal") * PlayerSpeed * Time.smoothDeltaTime, 0, Input.GetAxis("Vertical") * PlayerSpeed * Time.smoothDeltaTime); 20 | Camera.transform.position = Vector3.Lerp(Camera.transform.position, transform.position + CamOffset, Time.smoothDeltaTime * CameraFollowSpeed); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Cellular Automata/Cellular Automata Using Tilemaps.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using UnityEngine.Tilemaps; 3 | 4 | public class Generator : MonoBehaviour 5 | { 6 | public Vector2Int XYSize; 7 | public Tilemap Tilemap; 8 | public Tile TileObject; 9 | 10 | public int SmoothingSteps, BorderThickness; 11 | [Range(0, 100)] public float FillPercent; 12 | private int[,] TileNumbers; 13 | 14 | private void Awake() 15 | { 16 | TileNumbers = new int[XYSize.x, XYSize.y]; 17 | 18 | for (int x = 0; x < XYSize.x; x++) 19 | for (int y = 0; y < XYSize.y; y++){ 20 | if (x < BorderThickness || x > XYSize.x - BorderThickness - 1 || y < BorderThickness || y > XYSize.y - BorderThickness - 1) TileNumbers[x, y] = 0; 21 | else TileNumbers[x, y] = FillPercent > Random.Range(0.0f, 100.0f) ? 1 : 0; 22 | } 23 | 24 | for (int i = 0; i < SmoothingSteps; i++) 25 | for (int x = 0; x < XYSize.x; x++) 26 | for (int y = 0; y < XYSize.y; y++) { 27 | if (x < BorderThickness || x > XYSize.x - BorderThickness - 1 || y < BorderThickness || y > XYSize.y - BorderThickness - 1) TileNumbers[x, y] = 0; 28 | else { 29 | int total = 0; 30 | for (int xc = -1; xc <= 1; xc++) 31 | for (int yc = -1; yc <= 1; yc++) 32 | total += TileNumbers[x + xc, y + yc]; 33 | if (total >= 5) TileNumbers[x, y] = 1; 34 | else if (total < 4) TileNumbers[x, y] = 0; 35 | } 36 | } 37 | 38 | for (int x = 0; x < XYSize.x; x++) 39 | for (int y = 0; y < XYSize.y; y++) 40 | { 41 | Tilemap.SetTile(new Vector3Int(x, y, 0), null); 42 | if (TileNumbers[x, y] == 1) Tilemap.SetTile(new Vector3Int(Mathf.RoundToInt(x - XYSize.x / 2f), Mathf.RoundToInt(y - XYSize.y / 2f), 0), TileObject); 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Cellular Automata/CellularAutomata V1.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using UnityEngine; 5 | 6 | public class CellularAutomata : MonoBehaviour 7 | { 8 | public int xSize, ySize; 9 | private int[,] Walltype; 10 | public GameObject Tile; 11 | [Range(0, 15)] 12 | public int SmoothingSteps; 13 | [Range(0, 100)] 14 | public float FillPercent; 15 | 16 | private void Start() 17 | { 18 | Walltype = new int[xSize, ySize]; 19 | 20 | // Set Values 21 | 22 | for (int x = 0; x < xSize; x++) 23 | { 24 | for (int y = 0; y < ySize; y++) 25 | { 26 | if (x == 0 || x == xSize - 1 || y == 0 || y == ySize - 1) 27 | Walltype[x, y] = 0; 28 | else 29 | { 30 | if (FillPercent * 10 > UnityEngine.Random.Range(0, 1000)) 31 | Walltype[x, y] = 1; 32 | else 33 | Walltype[x, y] = 0; 34 | } 35 | } 36 | } 37 | 38 | // Spawning The cubes 39 | 40 | void instansiateCubes() 41 | { 42 | for (int x = 0; x < xSize; x++) 43 | { 44 | for (int y = 0; y < ySize; y++) 45 | { 46 | if (Walltype[x, y] == 1) 47 | Instantiate(Tile, new Vector3(x, 5, y), Quaternion.identity); 48 | } 49 | } 50 | } 51 | 52 | // Smoothing 53 | 54 | for (int a = 0; a < SmoothingSteps; a++) 55 | { 56 | for (int x = 1; x < (xSize - 1); x++) 57 | { 58 | for (int y = 1; y < (ySize - 1); y++) 59 | { 60 | int tilecount = findTileCount(x, y); 61 | 62 | if (tilecount >= 5) 63 | Walltype[x, y] = 1; 64 | else if (tilecount < 4) 65 | Walltype[x, y] = 0; 66 | } 67 | } 68 | } 69 | 70 | int findTileCount(int x, int y) 71 | { 72 | int finalCount = 0; 73 | 74 | int a = Walltype[x - 1, y - 1]; 75 | if (a == 1) 76 | finalCount += 1; 77 | int b = Walltype[x - 1, y]; 78 | if (b == 1) 79 | finalCount += 1; 80 | int c = Walltype[x- 1, y + 1]; 81 | if (c == 1) 82 | finalCount += 1; 83 | int d = Walltype[x, y + 1]; 84 | if (d == 1) 85 | finalCount += 1; 86 | int e = Walltype[x + 1, y + 1]; 87 | if (e == 1) 88 | finalCount += 1; 89 | int f = Walltype[x + 1, y]; 90 | if (f == 1) 91 | finalCount += 1; 92 | int g = Walltype[x + 1, y - 1]; 93 | if (g == 1) 94 | finalCount += 1; 95 | int h = Walltype[x, y - 1]; 96 | if (h == 1) 97 | finalCount += 1; 98 | 99 | return finalCount; 100 | } 101 | 102 | instansiateCubes(); 103 | 104 | } 105 | 106 | } 107 | -------------------------------------------------------------------------------- /Chunks And Terrain/ChunkLoading.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class ChunkLoading : MonoBehaviour 4 | { 5 | public int ChunkView; 6 | Vector2 PlayerChunkPos, lastPlayerChunk; 7 | public GameObject Player, Chunk; 8 | bool ChunksLoaded; 9 | 10 | private void FixedUpdate() 11 | { 12 | PlayerChunkPos = new Vector2(Mathf.Floor((Player.transform.position.x + 5) / 10), Mathf.Floor((Player.transform.position.z + 5) / 10)); 13 | if (lastPlayerChunk != PlayerChunkPos || !ChunksLoaded) LoadChunks(); 14 | } 15 | 16 | private void LoadChunks() 17 | { 18 | foreach (GameObject Plane in GameObject.FindGameObjectsWithTag("Plane")) Destroy(Plane); 19 | lastPlayerChunk = PlayerChunkPos; 20 | for (int x = -ChunkView; x <= ChunkView; x++) 21 | for (int y = -ChunkView; y <= ChunkView; y++) 22 | if (Vector2.Distance(PlayerChunkPos, PlayerChunkPos + new Vector2(x, y)) <= ChunkView / 1.5f) 23 | Instantiate(Chunk, new Vector3(x * 10 + PlayerChunkPos.x * 10, 0, y * 10 + PlayerChunkPos.y * 10), Quaternion.identity); 24 | 25 | ChunksLoaded = true; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Chunks And Terrain/LodSystem.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class LodSystem : MonoBehaviour 4 | { 5 | public float MaxDistance; 6 | public GameObject Player; 7 | GameObject CurrentActive; 8 | public GameObject[] Objects; 9 | float step; 10 | 11 | private void Start() 12 | { 13 | step = MaxDistance / (Objects.Length - 1); 14 | CurrentActive = Instantiate(Objects[0], transform.position, Objects[0].transform.rotation); 15 | CurrentActive.transform.parent = this.transform; 16 | } 17 | private void FixedUpdate() 18 | { 19 | float DistToPlayer = Vector3.Distance(Player.transform.position, transform.position); 20 | int indexobj = (int)((float)DistToPlayer / (float)step); 21 | if (indexobj != 0){ 22 | Destroy(CurrentActive); 23 | if (CurrentActive != Objects[indexobj]){ 24 | CurrentActive = Instantiate(Objects[indexobj], transform.position, Objects[0].transform.rotation); 25 | CurrentActive.transform.parent = this.transform; 26 | } 27 | } 28 | else{ 29 | Destroy(CurrentActive); 30 | CurrentActive = Instantiate(Objects[0], transform.position, Objects[0].transform.rotation); 31 | CurrentActive.transform.parent = this.transform; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Chunks And Terrain/LowPolyTerrain.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class LandGenerator : MonoBehaviour 4 | { 5 | public Gradient Color; 6 | public GameObject Tile; 7 | public GameObject[] Props; 8 | public float NoiseScale; 9 | [Range(0, 1)] 10 | public float FillPercent; 11 | public int size; 12 | 13 | private void Start() 14 | { 15 | for (int x = 0; x <= size; x++){ 16 | for (int y = 0; y <= size; y++){ 17 | float a = Mathf.PerlinNoise((float)x / NoiseScale, (float)y / NoiseScale); 18 | float h = a > 1 - (float)FillPercent ? 0 : -0.1f; 19 | GameObject g = Instantiate(Tile, new Vector3(x, h, y), Quaternion.identity); 20 | g.GetComponent().material.color = a > 1 - (float)FillPercent ? Color.Evaluate(a + 0.3f) : Color.Evaluate(a); 21 | g.transform.parent = transform; 22 | g.transform.localScale = a > 1 - (float)FillPercent ? new Vector3(1, 0.5f, 1) : new Vector3(1, 0.3f, 1); 23 | GameObject l = Random.value > 0.9f && a > 1 - (float)FillPercent ? 24 | Instantiate(Props[Random.Range(0, Props.Length)], new Vector3(x, 0.2f, y), Quaternion.Euler(-90, Random.Range(0, 360), 0)) : null; 25 | 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Dialogue Systems/DialogueSystemWithSentences.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using UnityEngine; 3 | using TMPro; 4 | using System; 5 | 6 | public class DialogueSystemTutorial : MonoBehaviour 7 | { 8 | public TextMeshProUGUI TextObj; 9 | public TextVars[] text; 10 | 11 | void Start() 12 | { 13 | StartCoroutine(ShowText()); 14 | } 15 | 16 | private IEnumerator ShowText() 17 | { 18 | foreach (TextVars t in text) 19 | { 20 | string v = ""; 21 | foreach (char c in t.Sentence) 22 | { 23 | v += c.ToString(); 24 | TextObj.text = v; 25 | TextObj.fontSize = t.TextSize; 26 | TextObj.color = t.TextColor; 27 | yield return new WaitForSeconds(t.Speed / 60); 28 | } 29 | yield return new WaitForSeconds(t.EndDelay); 30 | } 31 | } 32 | } 33 | 34 | [Serializable] 35 | public struct TextVars { 36 | [TextArea(3, 20)] 37 | public string Sentence; 38 | public float Speed, EndDelay, TextSize; 39 | public Color TextColor; 40 | 41 | } 42 | 43 | -------------------------------------------------------------------------------- /Dialogue Systems/TextScroller.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using UnityEngine; 5 | using UnityEngine.UI; 6 | 7 | public class TextScroller : MonoBehaviour 8 | { 9 | public string Text; 10 | public Text textObject; 11 | 12 | 13 | private void Start() 14 | { 15 | StartCoroutine(ShowText("")); 16 | } 17 | 18 | private IEnumerator ShowText(string s) 19 | { 20 | foreach (char c in Text){ 21 | s += c.ToString(); 22 | textObject.text = s; 23 | yield return new WaitForSeconds(0.05f); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Marching Things/InterpolatedMarchingSquares.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using B83.MeshHelper; 5 | 6 | public class InterpolatedMarchingSquares : MonoBehaviour 7 | { 8 | public int size; 9 | public float scale; 10 | int[,] binaryVals; 11 | float[,] VertexValues; 12 | List Verts = new List(); 13 | List tris = new List(); 14 | private int[,] TriangulationTable = new int[,] 15 | { 16 | {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 17 | {8, 7, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 18 | {7, 6, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 19 | {8, 4, 3, 8, 6, 4, -1, -1, -1, -1, -1, -1}, 20 | {6, 5, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 21 | {7, 8, 4, 4, 8, 1, 4, 1, 6, 6, 1, 5}, 22 | {7, 5, 4, 4, 5, 2, -1, -1, -1, -1, -1, -1}, 23 | {3, 8, 7, 7, 8, 5, 7, 5, 2, 7, 2, 4}, 24 | {5, 8, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 25 | {3, 1, 7, 7, 1, 5, -1, -1, -1, -1, -1, -1}, 26 | {7, 8, 4, 4, 8, 1, 4, 1, 5, 4, 5, 6}, 27 | {3, 1, 4, 4, 1, 5, 4, 5, 6, -1, -1, -1}, 28 | {6, 8, 1, 6, 1, 2, -1, -1, -1, -1, -1, -1}, 29 | {1, 2, 3, 3, 2, 7, 7, 2, 6, -1, -1, -1}, 30 | {7, 8, 4, 4, 8, 1, 4, 1, 2, -1, -1, -1}, 31 | {3, 1, 4, 4, 1, 2, -1, -1, -1, -1, -1, -1} 32 | }; 33 | 34 | 35 | private void Start() 36 | { 37 | binaryVals = new int[size + 1, size + 1]; 38 | VertexValues = new float[size + 1, size + 1]; 39 | 40 | for (int x1 = 0; x1 <= size; x1++) 41 | { 42 | for (int y1 = 0; y1 <= size; y1++) 43 | { 44 | binaryVals[x1, y1] = noise((float)x1, (float)y1) > (scale / 2) ? 1 : 0; 45 | VertexValues[x1, y1] = noise((float)x1, (float)y1); 46 | } 47 | } 48 | float noise(float x, float y) => Mathf.PerlinNoise(x / scale, y / scale) * scale; 49 | 50 | for (int x = 0; x < size; x++) 51 | { 52 | for (int y = 0; y < size; y++) 53 | { 54 | int g = binaryVals[x, y + 1] * 8 + binaryVals[x + 1, y + 1] * 4 + binaryVals[x + 1, y] * 2 + binaryVals[x, y]; 55 | 56 | for (int i = 1; i <= 4; i++) 57 | { 58 | Vector3 a = TriangulationTable[g, i * 3 - 3] != -1 ? point(x, y, TriangulationTable[g, i * 3 - 3] - 1) : Vector3.zero; 59 | Vector3 b = TriangulationTable[g, i * 3 - 2] != -1 ? point(x, y, TriangulationTable[g, i * 3 - 2] - 1) : Vector3.zero; 60 | Vector3 c = TriangulationTable[g, i * 3 - 1] != -1 ? point(x, y, TriangulationTable[g, i * 3 - 1] - 1) : Vector3.zero; 61 | 62 | if (a != Vector3.zero || b != Vector3.zero || c != Vector3.zero) 63 | { 64 | tris.AddRange(new List() { Verts.Count, Verts.Count + 1, Verts.Count + 2 }); 65 | Verts.AddRange(new List() { a, b, c }); 66 | } 67 | } 68 | } 69 | } 70 | 71 | GetComponent().mesh = new Mesh() 72 | { 73 | vertices = Verts.ToArray(), 74 | triangles = tris.ToArray() 75 | }; 76 | 77 | var welder = new MeshWelder(GetComponent().mesh); 78 | welder.Weld(); 79 | } 80 | 81 | Vector3 point(int x, int y, int i) 82 | { 83 | float[] Edge = new float[] { VertexValues[x, y + 1], VertexValues[x + 1, y + 1], VertexValues[x + 1, y], VertexValues[x, y] }; 84 | 85 | switch (i) 86 | { 87 | case 0: return new Vector3(x - 0.5f, 0, y + 0.5f); 88 | case 1: return new Vector3(x + 0.5f, 0, y + 0.5f); 89 | case 2: return new Vector3(x - 0.5f, 0, y - 0.5f); 90 | case 3: return new Vector3(x + 0.5f, 0, y - 0.5f); 91 | case 4: return new Vector3(x - 0.5f + lerp(Edge[0], Edge[1]), 0, y + 0.5f); 92 | case 5: return new Vector3(x + 0.5f, 0, y + 0.5f - lerp(Edge[1], Edge[2])); 93 | case 6: return new Vector3(x + 0.5f - lerp(Edge[2], Edge[3]), 0, y - 0.5f); 94 | case 7: return new Vector3(x - 0.5f, 0, y - 0.5f + lerp(Edge[3], Edge[0])); 95 | default: return Vector3.zero; 96 | } 97 | 98 | float lerp(float a, float b) => Mathf.InverseLerp(a, b, scale / 2); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /Marching Things/MarchingCubes.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using UnityEngine; 3 | 4 | public class MarchingCubes : MonoBehaviour 5 | { 6 | public int Size, Ps; 7 | [Range(0, 100)] public float FillPercent; 8 | List Verticies = new List(); 9 | List Triangles = new List(); 10 | Mesh mesh; 11 | int[,,] VertexValues, CubeValues; 12 | 13 | private void Start() 14 | { 15 | VertexValues = new int[Size + 1, Size + 1, Size + 1]; 16 | CubeValues = new int[Size, Size, Size]; 17 | GenerateVertexValues(); AddMesh(); 18 | mesh = new Mesh(); 19 | mesh.vertices = Verticies.ToArray(); 20 | mesh.triangles = Triangles.ToArray(); 21 | mesh.RecalculateNormals(); 22 | GetComponent().mesh = mesh; 23 | } 24 | void GenerateVertexValues() 25 | { 26 | for (int x = 0; x < Size + 1; x++){ 27 | for (int y = 0; y < Size + 1; y++){ 28 | for (int z = 0; z < Size + 1; z++){ 29 | if (x == 0 || y == 0 | z == 0 | x == Size || y == Size || z == Size) 30 | VertexValues[x, y, z] = 0; 31 | else 32 | VertexValues[x, y, z] = PerlinNoise((float)x, (float)y, (float)z) < FillPercent / 100 ? 1 : 0; 33 | } 34 | } 35 | } 36 | } 37 | private float PerlinNoise(float x, float y, float z){ 38 | float ab = Mathf.PerlinNoise(x / Ps, y / Ps), bc = Mathf.PerlinNoise(y / Ps, z / Ps), ac = Mathf.PerlinNoise(z / Ps, x / Ps); 39 | float ba = Mathf.PerlinNoise(y / Ps, x / Ps), cb = Mathf.PerlinNoise(z / Ps, y / Ps), ca = Mathf.PerlinNoise(x / Ps, z / Ps); 40 | return (ab + ba + ac + ca + bc + cb) / 6; 41 | } 42 | private void AddMesh() 43 | { 44 | for (int x = 0; x < Size; x++){ 45 | for (int y = 0; y < Size; y++){ 46 | for (int z = 0; z < Size; z++){ 47 | CubeValues[x, y, z] = 48 | (VertexValues[x, y + 1, z] * 128) + (VertexValues[x + 1, y + 1, z] * 64) + 49 | (VertexValues[x + 1, y + 1, z + 1] * 32) + (VertexValues[x, y + 1, z + 1] * 16) + 50 | (VertexValues[x, y, z] * 8) + (VertexValues[x + 1, y, z] * 4) + 51 | (VertexValues[x + 1, y, z + 1] * 2) + (VertexValues[x, y, z + 1]); 52 | 53 | Vector3[] EdgePoints = new Vector3[]{ 54 | new Vector3(x, y - 0.5f, z + 0.5f),new Vector3(x + 0.5f, y - 0.5f, z), 55 | new Vector3(x, y - 0.5f, z - 0.5f),new Vector3(x - 0.5f, y - 0.5f, z), 56 | new Vector3(x, y + 0.5f, z + 0.5f),new Vector3(x + 0.5f, y + 0.5f, z), 57 | new Vector3(x, y + 0.5f, z - 0.5f),new Vector3(x - 0.5f, y + 0.5f, z), 58 | new Vector3(x - 0.5f, y, z + 0.5f),new Vector3(x + 0.5f, y, z + 0.5f), 59 | new Vector3(x + 0.5f, y, z - 0.5f),new Vector3(x - 0.5f, y, z - 0.5f)}; 60 | 61 | for (int i = 1; i < 5; i++){ 62 | Vector3 a = new Vector3(), b = new Vector3(), c = new Vector3(); 63 | if (TriangulationTable[CubeValues[x, y, z], (i * 3) - 1] != -1) 64 | a = EdgePoints[TriangulationTable[CubeValues[x, y, z], (i * 3) - 1]]; 65 | if (TriangulationTable[CubeValues[x, y, z], (i * 3) - 2] != -1) 66 | b = EdgePoints[TriangulationTable[CubeValues[x, y, z], (i * 3) - 2]]; 67 | if (TriangulationTable[CubeValues[x, y, z], (i * 3) - 3] != -1) 68 | c = EdgePoints[TriangulationTable[CubeValues[x, y, z], (i * 3) - 3]]; 69 | if (a != Vector3.zero || b != Vector3.zero || c != Vector3.zero){ 70 | Triangles.Add(Verticies.Count); Triangles.Add(Verticies.Count + 1); Triangles.Add(Verticies.Count + 2); 71 | Verticies.Add(a); Verticies.Add(b); Verticies.Add(c); 72 | } 73 | } 74 | } 75 | } 76 | } 77 | } 78 | 79 | int[,] TriangulationTable = new int[,]{ 80 | {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 81 | {0, 8, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 82 | {0, 1, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 83 | {1, 8, 3, 9, 8, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 84 | {1, 2, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 85 | {0, 8, 3, 1, 2, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 86 | {9, 2, 10, 0, 2, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 87 | {2, 8, 3, 2, 10, 8, 10, 9, 8, -1, -1, -1, -1, -1, -1}, 88 | {3, 11, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 89 | {0, 11, 2, 8, 11, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 90 | {1, 9, 0, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 91 | {1, 11, 2, 1, 9, 11, 9, 8, 11, -1, -1, -1, -1, -1, -1}, 92 | {3, 10, 1, 11, 10, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 93 | {0, 10, 1, 0, 8, 10, 8, 11, 10, -1, -1, -1, -1, -1, -1}, 94 | {3, 9, 0, 3, 11, 9, 11, 10, 9, -1, -1, -1, -1, -1, -1}, 95 | {9, 8, 10, 10, 8, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 96 | {4, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 97 | {4, 3, 0, 7, 3, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 98 | {0, 1, 9, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 99 | {4, 1, 9, 4, 7, 1, 7, 3, 1, -1, -1, -1, -1, -1, -1}, 100 | {1, 2, 10, 8, 4, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 101 | {3, 4, 7, 3, 0, 4, 1, 2, 10, -1, -1, -1, -1, -1, -1}, 102 | {9, 2, 10, 9, 0, 2, 8, 4, 7, -1, -1, -1, -1, -1, -1}, 103 | {2, 10, 9, 2, 9, 7, 2, 7, 3, 7, 9, 4, -1, -1, -1}, 104 | {8, 4, 7, 3, 11, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 105 | {11, 4, 7, 11, 2, 4, 2, 0, 4, -1, -1, -1, -1, -1, -1}, 106 | {9, 0, 1, 8, 4, 7, 2, 3, 11, -1, -1, -1, -1, -1, -1}, 107 | {4, 7, 11, 9, 4, 11, 9, 11, 2, 9, 2, 1, -1, -1, -1}, 108 | {3, 10, 1, 3, 11, 10, 7, 8, 4, -1, -1, -1, -1, -1, -1}, 109 | {1, 11, 10, 1, 4, 11, 1, 0, 4, 7, 11, 4, -1, -1, -1}, 110 | {4, 7, 8, 9, 0, 11, 9, 11, 10, 11, 0, 3, -1, -1, -1}, 111 | {4, 7, 11, 4, 11, 9, 9, 11, 10, -1, -1, -1, -1, -1, -1}, 112 | {9, 5, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 113 | {9, 5, 4, 0, 8, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 114 | {0, 5, 4, 1, 5, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 115 | {8, 5, 4, 8, 3, 5, 3, 1, 5, -1, -1, -1, -1, -1, -1}, 116 | {1, 2, 10, 9, 5, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 117 | {3, 0, 8, 1, 2, 10, 4, 9, 5, -1, -1, -1, -1, -1, -1}, 118 | {5, 2, 10, 5, 4, 2, 4, 0, 2, -1, -1, -1, -1, -1, -1}, 119 | {2, 10, 5, 3, 2, 5, 3, 5, 4, 3, 4, 8, -1, -1, -1}, 120 | {9, 5, 4, 2, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 121 | {0, 11, 2, 0, 8, 11, 4, 9, 5, -1, -1, -1, -1, -1, -1}, 122 | {0, 5, 4, 0, 1, 5, 2, 3, 11, -1, -1, -1, -1, -1, -1}, 123 | {2, 1, 5, 2, 5, 8, 2, 8, 11, 4, 8, 5, -1, -1, -1}, 124 | {10, 3, 11, 10, 1, 3, 9, 5, 4, -1, -1, -1, -1, -1, -1}, 125 | {4, 9, 5, 0, 8, 1, 8, 10, 1, 8, 11, 10, -1, -1, -1}, 126 | {5, 4, 0, 5, 0, 11, 5, 11, 10, 11, 0, 3, -1, -1, -1}, 127 | {5, 4, 8, 5, 8, 10, 10, 8, 11, -1, -1, -1, -1, -1, -1}, 128 | {9, 7, 8, 5, 7, 9, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 129 | {9, 3, 0, 9, 5, 3, 5, 7, 3, -1, -1, -1, -1, -1, -1}, 130 | {0, 7, 8, 0, 1, 7, 1, 5, 7, -1, -1, -1, -1, -1, -1}, 131 | {1, 5, 3, 3, 5, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 132 | {9, 7, 8, 9, 5, 7, 10, 1, 2, -1, -1, -1, -1, -1, -1}, 133 | {10, 1, 2, 9, 5, 0, 5, 3, 0, 5, 7, 3, -1, -1, -1}, 134 | {8, 0, 2, 8, 2, 5, 8, 5, 7, 10, 5, 2, -1, -1, -1}, 135 | {2, 10, 5, 2, 5, 3, 3, 5, 7, -1, -1, -1, -1, -1, -1}, 136 | {7, 9, 5, 7, 8, 9, 3, 11, 2, -1, -1, -1, -1, -1, -1}, 137 | {9, 5, 7, 9, 7, 2, 9, 2, 0, 2, 7, 11, -1, -1, -1}, 138 | {2, 3, 11, 0, 1, 8, 1, 7, 8, 1, 5, 7, -1, -1, -1}, 139 | {11, 2, 1, 11, 1, 7, 7, 1, 5, -1, -1, -1, -1, -1, -1}, 140 | {9, 5, 8, 8, 5, 7, 10, 1, 3, 10, 3, 11, -1, -1, -1}, 141 | {5, 7, 0, 5, 0, 9, 7, 11, 0, 1, 0, 10, 11, 10, 0}, 142 | {11, 10, 0, 11, 0, 3, 10, 5, 0, 8, 0, 7, 5, 7, 0}, 143 | {11, 10, 5, 7, 11, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 144 | {10, 6, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 145 | {0, 8, 3, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 146 | {9, 0, 1, 5, 10, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 147 | {1, 8, 3, 1, 9, 8, 5, 10, 6, -1, -1, -1, -1, -1, -1}, 148 | {1, 6, 5, 2, 6, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 149 | {1, 6, 5, 1, 2, 6, 3, 0, 8, -1, -1, -1, -1, -1, -1}, 150 | {9, 6, 5, 9, 0, 6, 0, 2, 6, -1, -1, -1, -1, -1, -1}, 151 | {5, 9, 8, 5, 8, 2, 5, 2, 6, 3, 2, 8, -1, -1, -1}, 152 | {2, 3, 11, 10, 6, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 153 | {11, 0, 8, 11, 2, 0, 10, 6, 5, -1, -1, -1, -1, -1, -1}, 154 | {0, 1, 9, 2, 3, 11, 5, 10, 6, -1, -1, -1, -1, -1, -1}, 155 | {5, 10, 6, 1, 9, 2, 9, 11, 2, 9, 8, 11, -1, -1, -1}, 156 | {6, 3, 11, 6, 5, 3, 5, 1, 3, -1, -1, -1, -1, -1, -1}, 157 | {0, 8, 11, 0, 11, 5, 0, 5, 1, 5, 11, 6, -1, -1, -1}, 158 | {3, 11, 6, 0, 3, 6, 0, 6, 5, 0, 5, 9, -1, -1, -1}, 159 | {6, 5, 9, 6, 9, 11, 11, 9, 8, -1, -1, -1, -1, -1, -1}, 160 | {5, 10, 6, 4, 7, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 161 | {4, 3, 0, 4, 7, 3, 6, 5, 10, -1, -1, -1, -1, -1, -1}, 162 | {1, 9, 0, 5, 10, 6, 8, 4, 7, -1, -1, -1, -1, -1, -1}, 163 | {10, 6, 5, 1, 9, 7, 1, 7, 3, 7, 9, 4, -1, -1, -1}, 164 | {6, 1, 2, 6, 5, 1, 4, 7, 8, -1, -1, -1, -1, -1, -1}, 165 | {1, 2, 5, 5, 2, 6, 3, 0, 4, 3, 4, 7, -1, -1, -1}, 166 | {8, 4, 7, 9, 0, 5, 0, 6, 5, 0, 2, 6, -1, -1, -1}, 167 | {7, 3, 9, 7, 9, 4, 3, 2, 9, 5, 9, 6, 2, 6, 9}, 168 | {3, 11, 2, 7, 8, 4, 10, 6, 5, -1, -1, -1, -1, -1, -1}, 169 | {5, 10, 6, 4, 7, 2, 4, 2, 0, 2, 7, 11, -1, -1, -1}, 170 | {0, 1, 9, 4, 7, 8, 2, 3, 11, 5, 10, 6, -1, -1, -1}, 171 | {9, 2, 1, 9, 11, 2, 9, 4, 11, 7, 11, 4, 5, 10, 6}, 172 | {8, 4, 7, 3, 11, 5, 3, 5, 1, 5, 11, 6, -1, -1, -1}, 173 | {5, 1, 11, 5, 11, 6, 1, 0, 11, 7, 11, 4, 0, 4, 11}, 174 | {0, 5, 9, 0, 6, 5, 0, 3, 6, 11, 6, 3, 8, 4, 7}, 175 | {6, 5, 9, 6, 9, 11, 4, 7, 9, 7, 11, 9, -1, -1, -1}, 176 | {10, 4, 9, 6, 4, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 177 | {4, 10, 6, 4, 9, 10, 0, 8, 3, -1, -1, -1, -1, -1, -1}, 178 | {10, 0, 1, 10, 6, 0, 6, 4, 0, -1, -1, -1, -1, -1, -1}, 179 | {8, 3, 1, 8, 1, 6, 8, 6, 4, 6, 1, 10, -1, -1, -1}, 180 | {1, 4, 9, 1, 2, 4, 2, 6, 4, -1, -1, -1, -1, -1, -1}, 181 | {3, 0, 8, 1, 2, 9, 2, 4, 9, 2, 6, 4, -1, -1, -1}, 182 | {0, 2, 4, 4, 2, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 183 | {8, 3, 2, 8, 2, 4, 4, 2, 6, -1, -1, -1, -1, -1, -1}, 184 | {10, 4, 9, 10, 6, 4, 11, 2, 3, -1, -1, -1, -1, -1, -1}, 185 | {0, 8, 2, 2, 8, 11, 4, 9, 10, 4, 10, 6, -1, -1, -1}, 186 | {3, 11, 2, 0, 1, 6, 0, 6, 4, 6, 1, 10, -1, -1, -1}, 187 | {6, 4, 1, 6, 1, 10, 4, 8, 1, 2, 1, 11, 8, 11, 1}, 188 | {9, 6, 4, 9, 3, 6, 9, 1, 3, 11, 6, 3, -1, -1, -1}, 189 | {8, 11, 1, 8, 1, 0, 11, 6, 1, 9, 1, 4, 6, 4, 1}, 190 | {3, 11, 6, 3, 6, 0, 0, 6, 4, -1, -1, -1, -1, -1, -1}, 191 | {6, 4, 8, 11, 6, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 192 | {7, 10, 6, 7, 8, 10, 8, 9, 10, -1, -1, -1, -1, -1, -1}, 193 | {0, 7, 3, 0, 10, 7, 0, 9, 10, 6, 7, 10, -1, -1, -1}, 194 | {10, 6, 7, 1, 10, 7, 1, 7, 8, 1, 8, 0, -1, -1, -1}, 195 | {10, 6, 7, 10, 7, 1, 1, 7, 3, -1, -1, -1, -1, -1, -1}, 196 | {1, 2, 6, 1, 6, 8, 1, 8, 9, 8, 6, 7, -1, -1, -1}, 197 | {2, 6, 9, 2, 9, 1, 6, 7, 9, 0, 9, 3, 7, 3, 9}, 198 | {7, 8, 0, 7, 0, 6, 6, 0, 2, -1, -1, -1, -1, -1, -1}, 199 | {7, 3, 2, 6, 7, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 200 | {2, 3, 11, 10, 6, 8, 10, 8, 9, 8, 6, 7, -1, -1, -1}, 201 | {2, 0, 7, 2, 7, 11, 0, 9, 7, 6, 7, 10, 9, 10, 7}, 202 | {1, 8, 0, 1, 7, 8, 1, 10, 7, 6, 7, 10, 2, 3, 11}, 203 | {11, 2, 1, 11, 1, 7, 10, 6, 1, 6, 7, 1, -1, -1, -1}, 204 | {8, 9, 6, 8, 6, 7, 9, 1, 6, 11, 6, 3, 1, 3, 6}, 205 | {0, 9, 1, 11, 6, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 206 | {7, 8, 0, 7, 0, 6, 3, 11, 0, 11, 6, 0, -1, -1, -1}, 207 | {7, 11, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 208 | {7, 6, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 209 | {3, 0, 8, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 210 | {0, 1, 9, 11, 7, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 211 | {8, 1, 9, 8, 3, 1, 11, 7, 6, -1, -1, -1, -1, -1, -1}, 212 | {10, 1, 2, 6, 11, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 213 | {1, 2, 10, 3, 0, 8, 6, 11, 7, -1, -1, -1, -1, -1, -1}, 214 | {2, 9, 0, 2, 10, 9, 6, 11, 7, -1, -1, -1, -1, -1, -1}, 215 | {6, 11, 7, 2, 10, 3, 10, 8, 3, 10, 9, 8, -1, -1, -1}, 216 | {7, 2, 3, 6, 2, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 217 | {7, 0, 8, 7, 6, 0, 6, 2, 0, -1, -1, -1, -1, -1, -1}, 218 | {2, 7, 6, 2, 3, 7, 0, 1, 9, -1, -1, -1, -1, -1, -1}, 219 | {1, 6, 2, 1, 8, 6, 1, 9, 8, 8, 7, 6, -1, -1, -1}, 220 | {10, 7, 6, 10, 1, 7, 1, 3, 7, -1, -1, -1, -1, -1, -1}, 221 | {10, 7, 6, 1, 7, 10, 1, 8, 7, 1, 0, 8, -1, -1, -1}, 222 | {0, 3, 7, 0, 7, 10, 0, 10, 9, 6, 10, 7, -1, -1, -1}, 223 | {7, 6, 10, 7, 10, 8, 8, 10, 9, -1, -1, -1, -1, -1, -1}, 224 | {6, 8, 4, 11, 8, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 225 | {3, 6, 11, 3, 0, 6, 0, 4, 6, -1, -1, -1, -1, -1, -1}, 226 | {8, 6, 11, 8, 4, 6, 9, 0, 1, -1, -1, -1, -1, -1, -1}, 227 | {9, 4, 6, 9, 6, 3, 9, 3, 1, 11, 3, 6, -1, -1, -1}, 228 | {6, 8, 4, 6, 11, 8, 2, 10, 1, -1, -1, -1, -1, -1, -1}, 229 | {1, 2, 10, 3, 0, 11, 0, 6, 11, 0, 4, 6, -1, -1, -1}, 230 | {4, 11, 8, 4, 6, 11, 0, 2, 9, 2, 10, 9, -1, -1, -1}, 231 | {10, 9, 3, 10, 3, 2, 9, 4, 3, 11, 3, 6, 4, 6, 3}, 232 | {8, 2, 3, 8, 4, 2, 4, 6, 2, -1, -1, -1, -1, -1, -1}, 233 | {0, 4, 2, 4, 6, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 234 | {1, 9, 0, 2, 3, 4, 2, 4, 6, 4, 3, 8, -1, -1, -1}, 235 | {1, 9, 4, 1, 4, 2, 2, 4, 6, -1, -1, -1, -1, -1, -1}, 236 | {8, 1, 3, 8, 6, 1, 8, 4, 6, 6, 10, 1, -1, -1, -1}, 237 | {10, 1, 0, 10, 0, 6, 6, 0, 4, -1, -1, -1, -1, -1, -1}, 238 | {4, 6, 3, 4, 3, 8, 6, 10, 3, 0, 3, 9, 10, 9, 3}, 239 | {10, 9, 4, 6, 10, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 240 | {4, 9, 5, 7, 6, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 241 | {0, 8, 3, 4, 9, 5, 11, 7, 6, -1, -1, -1, -1, -1, -1}, 242 | {5, 0, 1, 5, 4, 0, 7, 6, 11, -1, -1, -1, -1, -1, -1}, 243 | {11, 7, 6, 8, 3, 4, 3, 5, 4, 3, 1, 5, -1, -1, -1}, 244 | {9, 5, 4, 10, 1, 2, 7, 6, 11, -1, -1, -1, -1, -1, -1}, 245 | {6, 11, 7, 1, 2, 10, 0, 8, 3, 4, 9, 5, -1, -1, -1}, 246 | {7, 6, 11, 5, 4, 10, 4, 2, 10, 4, 0, 2, -1, -1, -1}, 247 | {3, 4, 8, 3, 5, 4, 3, 2, 5, 10, 5, 2, 11, 7, 6}, 248 | {7, 2, 3, 7, 6, 2, 5, 4, 9, -1, -1, -1, -1, -1, -1}, 249 | {9, 5, 4, 0, 8, 6, 0, 6, 2, 6, 8, 7, -1, -1, -1}, 250 | {3, 6, 2, 3, 7, 6, 1, 5, 0, 5, 4, 0, -1, -1, -1}, 251 | {6, 2, 8, 6, 8, 7, 2, 1, 8, 4, 8, 5, 1, 5, 8}, 252 | {9, 5, 4, 10, 1, 6, 1, 7, 6, 1, 3, 7, -1, -1, -1}, 253 | {1, 6, 10, 1, 7, 6, 1, 0, 7, 8, 7, 0, 9, 5, 4}, 254 | {4, 0, 10, 4, 10, 5, 0, 3, 10, 6, 10, 7, 3, 7, 10}, 255 | {7, 6, 10, 7, 10, 8, 5, 4, 10, 4, 8, 10, -1, -1, -1}, 256 | {6, 9, 5, 6, 11, 9, 11, 8, 9, -1, -1, -1, -1, -1, -1}, 257 | {3, 6, 11, 0, 6, 3, 0, 5, 6, 0, 9, 5, -1, -1, -1}, 258 | {0, 11, 8, 0, 5, 11, 0, 1, 5, 5, 6, 11, -1, -1, -1}, 259 | {6, 11, 3, 6, 3, 5, 5, 3, 1, -1, -1, -1, -1, -1, -1}, 260 | {1, 2, 10, 9, 5, 11, 9, 11, 8, 11, 5, 6, -1, -1, -1}, 261 | {0, 11, 3, 0, 6, 11, 0, 9, 6, 5, 6, 9, 1, 2, 10}, 262 | {11, 8, 5, 11, 5, 6, 8, 0, 5, 10, 5, 2, 0, 2, 5}, 263 | {6, 11, 3, 6, 3, 5, 2, 10, 3, 10, 5, 3, -1, -1, -1}, 264 | {5, 8, 9, 5, 2, 8, 5, 6, 2, 3, 8, 2, -1, -1, -1}, 265 | {9, 5, 6, 9, 6, 0, 0, 6, 2, -1, -1, -1, -1, -1, -1}, 266 | {1, 5, 8, 1, 8, 0, 5, 6, 8, 3, 8, 2, 6, 2, 8}, 267 | {1, 5, 6, 2, 1, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 268 | {1, 3, 6, 1, 6, 10, 3, 8, 6, 5, 6, 9, 8, 9, 6}, 269 | {10, 1, 0, 10, 0, 6, 9, 5, 0, 5, 6, 0, -1, -1, -1}, 270 | {0, 3, 8, 5, 6, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 271 | {10, 5, 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 272 | {11, 5, 10, 7, 5, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 273 | {11, 5, 10, 11, 7, 5, 8, 3, 0, -1, -1, -1, -1, -1, -1}, 274 | {5, 11, 7, 5, 10, 11, 1, 9, 0, -1, -1, -1, -1, -1, -1}, 275 | {10, 7, 5, 10, 11, 7, 9, 8, 1, 8, 3, 1, -1, -1, -1}, 276 | {11, 1, 2, 11, 7, 1, 7, 5, 1, -1, -1, -1, -1, -1, -1}, 277 | {0, 8, 3, 1, 2, 7, 1, 7, 5, 7, 2, 11, -1, -1, -1}, 278 | {9, 7, 5, 9, 2, 7, 9, 0, 2, 2, 11, 7, -1, -1, -1}, 279 | {7, 5, 2, 7, 2, 11, 5, 9, 2, 3, 2, 8, 9, 8, 2}, 280 | {2, 5, 10, 2, 3, 5, 3, 7, 5, -1, -1, -1, -1, -1, -1}, 281 | {8, 2, 0, 8, 5, 2, 8, 7, 5, 10, 2, 5, -1, -1, -1}, 282 | {9, 0, 1, 5, 10, 3, 5, 3, 7, 3, 10, 2, -1, -1, -1}, 283 | {9, 8, 2, 9, 2, 1, 8, 7, 2, 10, 2, 5, 7, 5, 2}, 284 | {1, 3, 5, 3, 7, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 285 | {0, 8, 7, 0, 7, 1, 1, 7, 5, -1, -1, -1, -1, -1, -1}, 286 | {9, 0, 3, 9, 3, 5, 5, 3, 7, -1, -1, -1, -1, -1, -1}, 287 | {9, 8, 7, 5, 9, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 288 | {5, 8, 4, 5, 10, 8, 10, 11, 8, -1, -1, -1, -1, -1, -1}, 289 | {5, 0, 4, 5, 11, 0, 5, 10, 11, 11, 3, 0, -1, -1, -1}, 290 | {0, 1, 9, 8, 4, 10, 8, 10, 11, 10, 4, 5, -1, -1, -1}, 291 | {10, 11, 4, 10, 4, 5, 11, 3, 4, 9, 4, 1, 3, 1, 4}, 292 | {2, 5, 1, 2, 8, 5, 2, 11, 8, 4, 5, 8, -1, -1, -1}, 293 | {0, 4, 11, 0, 11, 3, 4, 5, 11, 2, 11, 1, 5, 1, 11}, 294 | {0, 2, 5, 0, 5, 9, 2, 11, 5, 4, 5, 8, 11, 8, 5}, 295 | {9, 4, 5, 2, 11, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 296 | {2, 5, 10, 3, 5, 2, 3, 4, 5, 3, 8, 4, -1, -1, -1}, 297 | {5, 10, 2, 5, 2, 4, 4, 2, 0, -1, -1, -1, -1, -1, -1}, 298 | {3, 10, 2, 3, 5, 10, 3, 8, 5, 4, 5, 8, 0, 1, 9}, 299 | {5, 10, 2, 5, 2, 4, 1, 9, 2, 9, 4, 2, -1, -1, -1}, 300 | {8, 4, 5, 8, 5, 3, 3, 5, 1, -1, -1, -1, -1, -1, -1}, 301 | {0, 4, 5, 1, 0, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 302 | {8, 4, 5, 8, 5, 3, 9, 0, 5, 0, 3, 5, -1, -1, -1}, 303 | {9, 4, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 304 | {4, 11, 7, 4, 9, 11, 9, 10, 11, -1, -1, -1, -1, -1, -1}, 305 | {0, 8, 3, 4, 9, 7, 9, 11, 7, 9, 10, 11, -1, -1, -1}, 306 | {1, 10, 11, 1, 11, 4, 1, 4, 0, 7, 4, 11, -1, -1, -1}, 307 | {3, 1, 4, 3, 4, 8, 1, 10, 4, 7, 4, 11, 10, 11, 4}, 308 | {4, 11, 7, 9, 11, 4, 9, 2, 11, 9, 1, 2, -1, -1, -1}, 309 | {9, 7, 4, 9, 11, 7, 9, 1, 11, 2, 11, 1, 0, 8, 3}, 310 | {11, 7, 4, 11, 4, 2, 2, 4, 0, -1, -1, -1, -1, -1, -1}, 311 | {11, 7, 4, 11, 4, 2, 8, 3, 4, 3, 2, 4, -1, -1, -1}, 312 | {2, 9, 10, 2, 7, 9, 2, 3, 7, 7, 4, 9, -1, -1, -1}, 313 | {9, 10, 7, 9, 7, 4, 10, 2, 7, 8, 7, 0, 2, 0, 7}, 314 | {3, 7, 10, 3, 10, 2, 7, 4, 10, 1, 10, 0, 4, 0, 10}, 315 | {1, 10, 2, 8, 7, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 316 | {4, 9, 1, 4, 1, 7, 7, 1, 3, -1, -1, -1, -1, -1, -1}, 317 | {4, 9, 1, 4, 1, 7, 0, 8, 1, 8, 7, 1, -1, -1, -1}, 318 | {4, 0, 3, 7, 4, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 319 | {4, 8, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 320 | {9, 10, 8, 10, 11, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 321 | {3, 0, 9, 3, 9, 11, 11, 9, 10, -1, -1, -1, -1, -1, -1}, 322 | {0, 1, 10, 0, 10, 8, 8, 10, 11, -1, -1, -1, -1, -1, -1}, 323 | {3, 1, 10, 11, 3, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 324 | {1, 2, 11, 1, 11, 9, 9, 11, 8, -1, -1, -1, -1, -1, -1}, 325 | {3, 0, 9, 3, 9, 11, 1, 2, 9, 2, 11, 9, -1, -1, -1}, 326 | {0, 2, 11, 8, 0, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 327 | {3, 2, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 328 | {2, 3, 8, 2, 8, 10, 10, 8, 9, -1, -1, -1, -1, -1, -1}, 329 | {9, 10, 2, 0, 9, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 330 | {2, 3, 8, 2, 8, 10, 0, 1, 8, 1, 10, 8, -1, -1, -1}, 331 | {1, 10, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 332 | {1, 3, 8, 9, 1, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 333 | {0, 9, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 334 | {0, 3, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, 335 | {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1} 336 | }; 337 | } 338 | -------------------------------------------------------------------------------- /Marching Things/MarchingSquares.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using UnityEngine; 3 | using UnityEditor; 4 | 5 | [ExecuteInEditMode] 6 | public class MarchingSquares : MonoBehaviour 7 | { 8 | 9 | // To get Mathematics to work you will need to type this com.unity.mathematics in to the Package manager 10 | 11 | [Range(0, 100)] 12 | public int Size, FillPercent, NoiseScale; 13 | int[] BinaryVals, Base10Nums; 14 | [Range(0, 150), Tooltip("This Will Set The Size Of The Circle That Your island Is On")] 15 | public float Falloff; 16 | [Header("")] 17 | public bool AutoUpdate; 18 | public bool InvertMesh; 19 | [Range(0, 100000)] 20 | public int Seed; 21 | 22 | // Mesh 23 | 24 | 25 | private List Verticies = new List(); 26 | private List Triangles = new List(); 27 | Mesh mesh; 28 | 29 | 30 | public void GetMainMeshValues() 31 | { 32 | 33 | BinaryVals = new int[(Size + 1) * (Size + 1)]; 34 | Base10Nums = new int[Size * Size]; 35 | for (int x = 0; x < Size; x++) 36 | { 37 | for (int y = 0; y < Size; y++) 38 | { 39 | float l = Vector3.Distance(new Vector3(x, 0, y), new Vector3(Size / 2, 0, Size / 2)) / Falloff; 40 | float z = (((Unity.Mathematics.noise.snoise(new Unity.Mathematics.float2(x + Seed, y + Seed) / NoiseScale) + 1) / 2) 41 | + ((Unity.Mathematics.noise.snoise(new Unity.Mathematics.float2((x + Seed) * 6, (y + Seed) * 6) / (NoiseScale * 2)) + 1) / 2)) / 2; 42 | float a = ((100 - (float)FillPercent) / 100); 43 | 44 | float b = z - l; 45 | if (InvertMesh) 46 | { 47 | if (b < a) 48 | BinaryVals[(int)x * Size + (int)y] = 1; 49 | else 50 | BinaryVals[(int)x * Size + (int)y] = 0; 51 | } 52 | else 53 | { 54 | if (a < b) 55 | BinaryVals[(int)x * Size + (int)y] = 1; 56 | else 57 | BinaryVals[(int)x * Size + (int)y] = 0; 58 | } 59 | } 60 | } 61 | 62 | GenerateGridValues(); 63 | 64 | // Mesh 65 | 66 | GetComponent().mesh = mesh; 67 | mesh.vertices = Verticies.ToArray(); 68 | mesh.triangles = Triangles.ToArray(); 69 | mesh.RecalculateNormals(); 70 | mesh.name = "Marching Squares Mesh"; 71 | } 72 | public void Update() 73 | { 74 | if (AutoUpdate) 75 | GenerateMesh(); 76 | } 77 | 78 | public void GenerateMesh() 79 | { 80 | mesh = new Mesh(); 81 | mesh.Clear(); 82 | Verticies.Clear(); 83 | Triangles.Clear(); 84 | GetMainMeshValues(); 85 | } 86 | public void CompleteClear() 87 | { 88 | mesh.Clear(); 89 | Verticies.Clear(); 90 | Triangles.Clear(); 91 | } 92 | public void GenerateGridValues() 93 | { 94 | for (int x = 0, i = 0; x < (Size); x++) 95 | { 96 | for (int y = 0; y < (Size); y++) 97 | { 98 | int z = ConvertToDec( 99 | BinaryVals[i], 100 | BinaryVals[i + Size], 101 | BinaryVals[i + Size + 1], 102 | BinaryVals[i + 1] 103 | ); 104 | Base10Nums[i] = z; 105 | AddMarchedSquare(x, y, z); 106 | i++; 107 | } 108 | } 109 | } 110 | 111 | public int ConvertToDec(int a, int b, int c, int d) 112 | { 113 | int e = (a * 8) + (b * 4) + (c * 2) + (d); 114 | return e; 115 | } 116 | private void AddTriangle(Vector3 a, Vector3 b, Vector3 c) 117 | { 118 | int vertexIndex = Verticies.Count; 119 | Verticies.Add(a); 120 | Verticies.Add(b); 121 | Verticies.Add(c); 122 | Triangles.Add(vertexIndex); 123 | Triangles.Add(vertexIndex + 1); 124 | Triangles.Add(vertexIndex + 2); 125 | } 126 | private void AddQuad(Vector3 a, Vector3 b, Vector3 c, Vector3 d) 127 | { 128 | int vertexIndex = Verticies.Count; 129 | Verticies.Add(a); 130 | Verticies.Add(b); 131 | Verticies.Add(c); 132 | Verticies.Add(d); 133 | Triangles.Add(vertexIndex); 134 | Triangles.Add(vertexIndex + 1); 135 | Triangles.Add(vertexIndex + 2); 136 | Triangles.Add(vertexIndex); 137 | Triangles.Add(vertexIndex + 2); 138 | Triangles.Add(vertexIndex + 3); 139 | } 140 | private void AddPentagon(Vector3 a, Vector3 b, Vector3 c, Vector3 d, Vector3 e) 141 | { 142 | int vertexIndex = Verticies.Count; 143 | Verticies.Add(a); 144 | Verticies.Add(b); 145 | Verticies.Add(c); 146 | Verticies.Add(d); 147 | Verticies.Add(e); 148 | Triangles.Add(vertexIndex); 149 | Triangles.Add(vertexIndex + 1); 150 | Triangles.Add(vertexIndex + 2); 151 | Triangles.Add(vertexIndex); 152 | Triangles.Add(vertexIndex + 2); 153 | Triangles.Add(vertexIndex + 3); 154 | Triangles.Add(vertexIndex); 155 | Triangles.Add(vertexIndex + 3); 156 | Triangles.Add(vertexIndex + 4); 157 | } 158 | 159 | public void AddMarchedSquare(int x, int y, int i) 160 | { 161 | if (i == 0) 162 | return; 163 | if (i == 1) 164 | AddTriangle(new Vector3(x, 0, y + 0.5f), new Vector3(x, 0, y + 1), new Vector3(x + 0.5f, 0, y + 1)); 165 | if (i == 2) 166 | AddTriangle(new Vector3(x + 0.5f, 0, y + 1), new Vector3(x + 1, 0, y + 1), new Vector3(x + 1, 0, y + 0.5f)); 167 | if (i == 3) 168 | AddQuad(new Vector3(x, 0, y + 0.5f), new Vector3(x, 0, y + 1), new Vector3(x + 1, 0, y + 1), new Vector3(x + 1, 0, y + 0.5f)); 169 | if (i == 4) 170 | AddTriangle(new Vector3(x + 0.5f, 0, y), new Vector3(x + 1, 0, y + 0.5f), new Vector3(x + 1, 0, y)); 171 | if (i == 5) 172 | { 173 | AddQuad(new Vector3(x, 0, y + 1), new Vector3(x + 0.5f, 0, y + 1), new Vector3(x + 1, 0, y + 0.5f), new Vector3(x + 1, 0, y)); 174 | AddQuad(new Vector3(x, 0, y + 1), new Vector3(x + 1, 0, y), new Vector3(x + 0.5f, 0, y), new Vector3(x, 0, y + 0.5f)); 175 | } 176 | if (i == 6) 177 | AddQuad(new Vector3(x + 0.5f, 0, y), new Vector3(x + 0.5f, 0, y + 1), new Vector3(x + 1, 0, y + 1), new Vector3(x + 1, 0, y)); 178 | if (i == 7) 179 | AddPentagon(new Vector3(x, 0, y + 0.5f), new Vector3(x, 0, y + 1), new Vector3(x + 1, 0, y + 1), new Vector3(x + 1, 0, y), new Vector3(x + 0.5f, 0, y)); 180 | if (i == 8) 181 | AddTriangle(new Vector3(x, 0, y), new Vector3(x, 0, y + 0.5f), new Vector3(x + 0.5f, 0, y)); 182 | if (i == 9) 183 | AddQuad(new Vector3(x, 0, y), new Vector3(x, 0, y + 1), new Vector3(x + 0.5f, 0, y + 1), new Vector3(x + 0.5f, 0, y)); 184 | if (i == 10) 185 | { 186 | AddQuad(new Vector3(x, 0, y), new Vector3(x, 0, y + 0.5f), new Vector3(x + 0.5f, 0, y + 1), new Vector3(x + 1, 0, y + 1)); 187 | AddQuad(new Vector3(x, 0, y), new Vector3(x + 1, 0, y + 1), new Vector3(x + 1, 0, y + 0.5f), new Vector3(x + 0.5f, 0, y)); 188 | } 189 | if (i == 11) 190 | AddPentagon(new Vector3(x, 0, y), new Vector3(x, 0, y + 1), new Vector3(x + 1, 0, y + 1), new Vector3(x + 1, 0, y + 0.5f), new Vector3(x + 0.5f, 0, y)); 191 | if (i == 12) 192 | AddQuad(new Vector3(x, 0, y), new Vector3(x, 0, y + 0.5f), new Vector3(x + 1, 0, y + 0.5f), new Vector3(x + 1, 0, y)); 193 | if (i == 13) 194 | AddPentagon(new Vector3(x, 0, y), new Vector3(x, 0, y + 1), new Vector3(x + 0.5f, 0, y + 1), new Vector3(x + 1, 0, y + 0.5f), new Vector3(x + 1, 0, y)); 195 | if (i == 14) 196 | AddPentagon(new Vector3(x, 0, y), new Vector3(x, 0, y + 0.5f), new Vector3(x + 0.5f, 0, y + 1), new Vector3(x + 1, 0, y + 1), new Vector3(x + 1, 0, y)); 197 | if (i == 15) 198 | AddQuad(new Vector3(x, 0, y), new Vector3(x, 0, y + 1), new Vector3(x + 1, 0, y + 1), new Vector3(x + 1, 0, y)); 199 | } 200 | } 201 | 202 | [CustomEditor(typeof(MarchingSquares))] 203 | public class MarchingSquaresEditor : Editor 204 | { 205 | public override void OnInspectorGUI() 206 | { 207 | base.OnInspectorGUI(); 208 | 209 | MarchingSquares March = (MarchingSquares)target; 210 | 211 | if (!March.AutoUpdate) 212 | { 213 | if (GUILayout.Button("Generate Mesh")) 214 | March.GenerateMesh(); 215 | if (GUILayout.Button("Clear Mesh")) 216 | March.CompleteClear(); 217 | } 218 | } 219 | } -------------------------------------------------------------------------------- /Mesh Generators/Circle.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using UnityEngine; 3 | public class Circle : MonoBehaviour 4 | { 5 | public int circle, radius; 6 | List v = new List(); 7 | List t = new List(); 8 | 9 | private void Start() 10 | { 11 | for (int i = 0; i < circle; i++){ 12 | float y = radius * Mathf.Sin(Mathf.Deg2Rad * (360 / ((float)circle)*i)); 13 | float x = radius * Mathf.Cos(Mathf.Deg2Rad * (360 / ((float)circle)*i)); 14 | v.Add(new Vector3(x, 0, y)); 15 | } 16 | for (int l = 0; l < circle; l++){ 17 | t.AddRange(new List() {v.Count, v.Count + 1,v.Count + 2 }); 18 | v.AddRange(new List(){v[l],v[l + 1],Vector3.zero}); 19 | } 20 | Mesh mesh = new Mesh(); 21 | mesh.vertices = v.ToArray(); 22 | mesh.triangles = t.ToArray(); 23 | GetComponent().mesh = mesh; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Mesh Generators/HexagonGrid.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using UnityEngine; 5 | 6 | public class HexagonGrid : MonoBehaviour 7 | { 8 | public int size; 9 | 10 | private List Vecticies = new List(); 11 | private List Triangles = new List(); 12 | 13 | public const float outerRadius = 1f; 14 | 15 | public const float innerRadius = outerRadius * 0.866025808f; 16 | 17 | [Range(0, 100)] 18 | public float noisescale, Falloff, fillpercent; 19 | 20 | public static Vector3[] corners = 21 | { 22 | new Vector3(0, 0, outerRadius), 23 | new Vector3(innerRadius, 0, outerRadius * 0.5f), 24 | new Vector3(innerRadius, 0, -0.5f * outerRadius), 25 | new Vector3(0, 0, -outerRadius), 26 | new Vector3(-innerRadius, 0, -0.5f * outerRadius), 27 | new Vector3(-innerRadius, 0,0.5f * outerRadius), 28 | }; 29 | 30 | Mesh mesh; 31 | 32 | private void Start() 33 | { 34 | generateGrid(); 35 | 36 | mesh = new Mesh(); 37 | mesh.vertices = Vecticies.ToArray(); 38 | mesh.triangles = Triangles.ToArray(); 39 | mesh.RecalculateNormals(); 40 | GetComponent().mesh = mesh; 41 | mesh.name = "Hex Grid"; 42 | } 43 | 44 | private void generateGrid() 45 | { 46 | int Seed = UnityEngine.Random.Range(0, 999999); 47 | for (int x = 0; x < size; x++) 48 | { 49 | for (int y = 0; y < size; y++) 50 | { 51 | float x1 = (x + y * 0.5f - y / 2) * (innerRadius * 2); 52 | float z1 = 0; 53 | float y1 = y * (outerRadius * 1.5f); 54 | 55 | float f = Vector3.Distance(new Vector3(x, 0, y), new Vector3(size / 2, 0, size / 2)) / Falloff; 56 | float n = (((Unity.Mathematics.noise.snoise(new Unity.Mathematics.float2(x + Seed, y + Seed) / noisescale)) + 1) / 2) 57 | + (((Unity.Mathematics.noise.snoise(new Unity.Mathematics.float2((x + Seed) * 6f, (y + Seed) * 6) / (noisescale * 2))) + 1) / 2); 58 | float a = ((100 - (float)fillpercent) / 100); 59 | 60 | float b = n - f; 61 | 62 | if (b > a) 63 | { 64 | addHexagon(corners[0] + new Vector3(x1, z1, y1) 65 | , corners[1] + new Vector3(x1, z1, y1) 66 | , corners[2] + new Vector3(x1, z1, y1) 67 | , corners[3] + new Vector3(x1, z1, y1) 68 | , corners[4] + new Vector3(x1, z1, y1) 69 | , corners[5] + new Vector3(x1, z1, y1)); 70 | } 71 | } 72 | } 73 | } 74 | 75 | private void addHexagon(Vector3 a, Vector3 b, Vector3 c, Vector3 d, Vector3 e, Vector3 f) 76 | { 77 | int index = Vecticies.Count; 78 | Vecticies.Add(a); 79 | Vecticies.Add(b); 80 | Vecticies.Add(c); 81 | Vecticies.Add(d); 82 | Vecticies.Add(e); 83 | Vecticies.Add(f); 84 | Triangles.Add(index); 85 | Triangles.Add(index + 1); 86 | Triangles.Add(index + 2); 87 | Triangles.Add(index); 88 | Triangles.Add(index + 2); 89 | Triangles.Add(index + 3); 90 | Triangles.Add(index); 91 | Triangles.Add(index + 3); 92 | Triangles.Add(index + 4); 93 | Triangles.Add(index); 94 | Triangles.Add(index + 4); 95 | Triangles.Add(index + 5); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /Mesh Generators/TerrainMeshGenerator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using UnityEngine; 3 | using Unity.Burst; 4 | using Unity.Collections; 5 | using Unity.Jobs; 6 | using Unity.Mathematics; 7 | 8 | public class TerrainMeshGenerator : MonoBehaviour 9 | { 10 | public static TerrainMeshGenerator Instance; 11 | 12 | public MeshFilter meshFilter; 13 | public TerrainMeshVariables meshVariables; 14 | public TerrainHeightmapVariables heightmapVariables; 15 | public Gradient heightmapGradient; 16 | 17 | private void Awake() { 18 | if (Instance == null) Instance = this; 19 | else Destroy(gameObject); 20 | } 21 | 22 | public void OnValidate(){ 23 | GenerateMap(); 24 | } 25 | 26 | public void GenerateMap() 27 | { 28 | NativeArray _gradientColorArray = new NativeArray(100, Allocator.Persistent); 29 | 30 | for (int i = 0; i < 100; i++) { 31 | _gradientColorArray[i] = heightmapGradient.Evaluate(i / 100f); 32 | } 33 | 34 | HeightMapGenerator heightmapGenerator = new HeightMapGenerator(meshVariables, heightmapVariables, _gradientColorArray); 35 | heightmapGenerator.Schedule(meshVariables.TotalVerts, 10000).Complete(); 36 | Maps _maps = heightmapGenerator.ReturnAndDispose(); 37 | 38 | MeshGenerator meshGenerator = new MeshGenerator(meshVariables, _maps); 39 | meshGenerator.Schedule(meshVariables.terrainMeshDetail * meshVariables.terrainMeshDetail, 10000).Complete(); 40 | 41 | meshFilter.mesh = meshGenerator.DisposeAndGetMesh(); 42 | 43 | _gradientColorArray.Dispose(); 44 | } 45 | } 46 | 47 | [BurstCompile] 48 | public struct HeightMapGenerator : IJobParallelFor { 49 | 50 | [NativeDisableParallelForRestriction,] private NativeArray _heightMap; 51 | [NativeDisableParallelForRestriction] private NativeArray _colMap; 52 | private readonly TerrainMeshVariables _meshVariables; 53 | private readonly TerrainHeightmapVariables _heightmapVariables; 54 | [NativeDisableParallelForRestriction] private NativeArray _gradient; 55 | 56 | 57 | public HeightMapGenerator(TerrainMeshVariables mv, TerrainHeightmapVariables hv, NativeArray grad) 58 | { 59 | _meshVariables = mv; 60 | _heightmapVariables = hv; 61 | _colMap = new NativeArray(_meshVariables.TotalVerts, Allocator.TempJob); 62 | _heightMap = new NativeArray(_meshVariables.TotalVerts, Allocator.TempJob); 63 | _gradient = grad; 64 | } 65 | 66 | public void Execute(int threadIndex) 67 | { 68 | float x = threadIndex / /*(float)*/(_meshVariables.terrainMeshDetail+1); 69 | float y = threadIndex % (_meshVariables.terrainMeshDetail+1); 70 | float2 pos = new float2(x, y); 71 | 72 | float h = Mathf.Clamp((OctavedSimplexNoise(pos) + OctavedRidgeNoise(pos))/2f * FalloffMap(pos) * _meshVariables.height, _heightmapVariables.waterLevel, 1000); 73 | 74 | _heightMap[threadIndex] = h / _meshVariables.TileEdgeLength; 75 | _colMap[threadIndex] = _gradient[Mathf.Clamp(Mathf.RoundToInt(h), 0, 99)]; 76 | } 77 | 78 | public Maps ReturnAndDispose() => new Maps(_heightMap, _colMap); 79 | 80 | float OctavedRidgeNoise(float2 pos) 81 | { 82 | float noiseVal = 0, amplitude = 1, freq = _heightmapVariables.noiseScale, weight = 1; 83 | 84 | for (int o = 0; o < _heightmapVariables.octaves; o++) 85 | { 86 | float v = 1 - Mathf.Abs(noise.snoise(pos / freq / _meshVariables.terrainMeshDetail)); 87 | v *= v; 88 | v *= weight; 89 | weight = Mathf.Clamp01(v * _heightmapVariables.weight); 90 | noiseVal += v * amplitude; 91 | 92 | freq /= _heightmapVariables.frequency; 93 | amplitude /= _heightmapVariables.lacunarity; 94 | } 95 | 96 | return noiseVal; 97 | } 98 | float OctavedSimplexNoise(float2 pos) 99 | { 100 | float noiseVal = 0, amplitude = 1, freq = _heightmapVariables.noiseScale; 101 | 102 | for (int o = 0; o < _heightmapVariables.octaves; o++) 103 | { 104 | float v = (noise.snoise(pos / freq / _meshVariables.terrainMeshDetail) + 1) / 2f; 105 | noiseVal += v * amplitude; 106 | 107 | freq /= _heightmapVariables.frequency; 108 | amplitude /= _heightmapVariables.lacunarity; 109 | } 110 | 111 | return noiseVal; 112 | } 113 | float FalloffMap(float2 pos) 114 | { 115 | float x = (pos.x / (_meshVariables.terrainMeshDetail+1)) * 2 - 1; 116 | float y = (pos.y / (_meshVariables.terrainMeshDetail+1)) * 2 - 1; 117 | 118 | float value = Mathf.Max(Mathf.Abs(x), Mathf.Abs(y)); 119 | 120 | float a = _heightmapVariables.falloffSteepness; 121 | float b = _heightmapVariables.falloffOffset; 122 | 123 | return 1 - (Mathf.Pow(value, a) / (Mathf.Pow(value, a) + Mathf.Pow((b - b * value), a))); 124 | } 125 | } 126 | 127 | [BurstCompile] 128 | public struct MeshGenerator : IJobParallelFor 129 | { 130 | 131 | [NativeDisableParallelForRestriction] private NativeArray _verticies; 132 | [NativeDisableParallelForRestriction] private NativeArray _triangleIndicies; 133 | private TerrainMeshVariables _meshVariables; 134 | private Maps _maps; 135 | 136 | public MeshGenerator(TerrainMeshVariables mv, Maps m) 137 | { 138 | _meshVariables = mv; 139 | 140 | _verticies = new NativeArray(_meshVariables.TotalVerts, Allocator.TempJob); 141 | _triangleIndicies = new NativeArray(_meshVariables.TotalTriangles, Allocator.TempJob); 142 | _maps = m; 143 | } 144 | 145 | public void Execute(int threadIndex) 146 | { 147 | int x = threadIndex / _meshVariables.terrainMeshDetail; 148 | int y = threadIndex % _meshVariables.terrainMeshDetail; 149 | 150 | // c - - - - d 151 | // | | 152 | // | | 153 | // | | 154 | // a - - - - b 155 | // a is bottom left and the rest of the points are calculated using the index of a 156 | // we are only looping through each square to calculate the triangle and other bs 157 | 158 | int a = threadIndex + Mathf.FloorToInt(threadIndex / (float)_meshVariables.terrainMeshDetail); 159 | int b = a + 1; 160 | int c = b + _meshVariables.terrainMeshDetail; 161 | int d = c + 1; 162 | 163 | _verticies[a] = new Vector3(x + 0, _maps.HeightMap[a], y + 0) * _meshVariables.TileEdgeLength; 164 | _verticies[b] = new Vector3(x + 0, _maps.HeightMap[b], y + 1) * _meshVariables.TileEdgeLength; 165 | _verticies[c] = new Vector3(x + 1, _maps.HeightMap[c], y + 0) * _meshVariables.TileEdgeLength; 166 | _verticies[d] = new Vector3(x + 1, _maps.HeightMap[d], y + 1) * _meshVariables.TileEdgeLength; 167 | 168 | _triangleIndicies[threadIndex * 6 + 0] = a; 169 | _triangleIndicies[threadIndex * 6 + 1] = b; 170 | _triangleIndicies[threadIndex * 6 + 2] = c; 171 | _triangleIndicies[threadIndex * 6 + 3] = b; 172 | _triangleIndicies[threadIndex * 6 + 4] = d; 173 | _triangleIndicies[threadIndex * 6 + 5] = c; 174 | } 175 | 176 | public Mesh DisposeAndGetMesh() 177 | { 178 | // create and assign values to mesh 179 | var m = new Mesh(); 180 | 181 | m.SetVertices(_verticies); 182 | m.SetColors(_maps.ColorMap); 183 | m.triangles = _triangleIndicies.ToArray(); 184 | 185 | m.RecalculateNormals(); 186 | 187 | // Away with the memory hoarding!! (dispose the native arrays from memory) 188 | _verticies.Dispose(); 189 | _triangleIndicies.Dispose(); 190 | _maps.Dispose(); 191 | 192 | return m; 193 | } 194 | } 195 | 196 | [Serializable] 197 | public struct TerrainMeshVariables 198 | { 199 | [Range(1, 255)] 200 | public int terrainMeshDetail; 201 | public float terrainWidth; 202 | public float height; 203 | public int TotalVerts => (terrainMeshDetail + 1) * (terrainMeshDetail + 1); 204 | public int TotalTriangles => terrainMeshDetail * terrainMeshDetail * 6; 205 | public float TileEdgeLength => terrainWidth / terrainMeshDetail; 206 | } 207 | [Serializable] 208 | public struct TerrainHeightmapVariables 209 | { 210 | [Header("Noise")] 211 | public float noiseScale; 212 | [Range(0, 4)] 213 | public float frequency, lacunarity; 214 | [Range(1, 10)] 215 | public int octaves; 216 | public float weight; 217 | 218 | public float falloffSteepness, falloffOffset; 219 | [Header("Extras")] 220 | public float waterLevel; 221 | } 222 | 223 | public struct Maps 224 | { 225 | [NativeDisableParallelForRestriction] public NativeArray HeightMap; 226 | [NativeDisableParallelForRestriction] public NativeArray ColorMap; 227 | 228 | public Maps(NativeArray h, NativeArray c) 229 | { 230 | HeightMap = h; 231 | ColorMap = c; 232 | } 233 | 234 | public void Dispose() 235 | { 236 | ColorMap.Dispose(); 237 | HeightMap.Dispose(); 238 | } 239 | } 240 | -------------------------------------------------------------------------------- /Mesh Generators/TreeGenerator.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using UnityEngine; 3 | using B83.MeshHelper; 4 | 5 | public class TreeGenerator : MonoBehaviour 6 | { 7 | public float height, LeavesHeight; 8 | [Range(1, 10)] public int HeightSegments; 9 | 10 | public Color bark, Leaves; 11 | 12 | List verts = new List(); 13 | List Triangles = new List(); 14 | 15 | Color[] Colors; 16 | 17 | private void Start() 18 | { 19 | // Slow 20 | 21 | Vector3 pos = transform.position; 22 | 23 | for (int i = 0; i < HeightSegments; i++){ 24 | float x = 1 - BiasFunction((float)i / (float)HeightSegments, .2f); 25 | AddSquarePoints(x, Random.Range(-25, 25), new Vector3(pos.x, i * height + pos.y, pos.z), i); 26 | } 27 | 28 | float BiasFunction(float x, float Bias) => x * Mathf.Pow(1 - Bias, 3) / (x * Mathf.Pow(1 - Bias, 3) - x + 1); 29 | 30 | // Fast 31 | 32 | for (int i = 0; i < HeightSegments - 1; i++) 33 | { 34 | addquad(verts[i * 4], verts[i * 4 + 1], verts[(i + 1) * 4 + 1], verts[(i + 1) * 4]); 35 | addquad(verts[i * 4 + 1], verts[i * 4 + 2], verts[(i + 1) * 4 + 2], verts[(i + 1) * 4 + 1]); 36 | addquad(verts[i * 4 + 2], verts[i * 4 + 3], verts[(i + 1) * 4 + 3], verts[(i + 1) * 4 + 2]); 37 | addquad(verts[i * 4 + 3], verts[i * 4], verts[(i + 1) * 4], verts[(i + 1) * 4 + 3]); 38 | } 39 | 40 | // Slow 41 | 42 | int s = (HeightSegments - 1) * 4; 43 | 44 | addquad(verts[s], verts[s + 1], verts[s + 2], verts[s + 3]); 45 | 46 | // Fast 47 | 48 | float d = verts[(int)(verts.Count / 2) + (int)(verts.Count / 4)].y; 49 | AddTreeSquare(3, Random.Range(-5, 5), new Vector3(pos.x, pos.y + LeavesHeight + (d - (d / 2)), pos.z), true); 50 | AddTreeSquare(2, Random.Range(-5, 5), new Vector3(pos.x, pos.y + LeavesHeight + (d + (height) * 3), pos.z), false); 51 | 52 | // Slow 53 | 54 | int b = verts.Count - 8; 55 | 56 | // Fast 57 | 58 | addquad(verts[b], verts[b + 1], verts[b + 5], verts[b + 4]); 59 | addquad(verts[b + 1], verts[b + 2], verts[b + 6], verts[b + 5]); 60 | addquad(verts[b + 2], verts[b + 3], verts[b + 7], verts[b + 6]); 61 | addquad(verts[b], verts[b + 4], verts[b + 7], verts[b + 3]); 62 | 63 | // Slow 64 | 65 | GetComponent().mesh = new Mesh() { 66 | vertices = verts.ToArray(), 67 | triangles = Triangles.ToArray() 68 | }; 69 | 70 | // For End 71 | 72 | var welder = new MeshWelder(GetComponent().mesh); 73 | 74 | welder.Weld(); 75 | 76 | // Slow 77 | 78 | GetComponent().mesh.RecalculateNormals(); 79 | 80 | Colors = new Color[GetComponent().mesh.vertices.Length]; 81 | 82 | for (int i = 0; i < GetComponent().mesh.vertices.Length; i++) 83 | { 84 | if (i < GetComponent().mesh.vertices.Length - 8) 85 | Colors[i] = bark; 86 | else 87 | Colors[i] = Leaves; 88 | } 89 | 90 | GetComponent().mesh.colors = Colors; 91 | 92 | } 93 | 94 | 95 | // Slow 96 | 97 | void addquad(Vector3 a, Vector3 b, Vector3 c, Vector3 d) 98 | { 99 | int l = verts.Count; 100 | Triangles.AddRange(new List() { l, l + 1, l + 2, l, l + 2, l + 3 }); 101 | verts.AddRange(new List() { a, b, c, d }); 102 | } 103 | 104 | 105 | 106 | float Cos(float x) => Mathf.Cos(x); float Sin(float x) => Mathf.Sin(x); 107 | 108 | 109 | 110 | 111 | public void AddSquarePoints(float scale, float Rotation, Vector3 pos, int verticalposition) 112 | { 113 | scale = scale * 5; 114 | 115 | // Fast 116 | 117 | Vector3 a = new Vector3(pos.x - scale, pos.y, pos.z + scale) 118 | , b = new Vector3(pos.x + scale, pos.y, pos.z + scale) 119 | , c = new Vector3(pos.x + scale, pos.y, pos.z - scale) 120 | , d = new Vector3(pos.x - scale, pos.y, pos.z - scale); 121 | 122 | // Slow 123 | 124 | int l = verts.Count; 125 | 126 | if (verticalposition == 0) {Triangles.AddRange(new List() { l + 2, l + 1, l, l + 3, l + 2, l });} 127 | 128 | verts.AddRange(new List() { rot(pos, a, Rotation), rot(pos, b, Rotation), rot(pos, c, Rotation), rot(pos, d, Rotation) }); 129 | 130 | Vector3 rot(Vector3 center, Vector3 point, float Degrees) 131 | { 132 | Vector3 FinalPosition = Vector3.zero; 133 | 134 | float angle = (-Degrees) * (Mathf.PI / 180); 135 | FinalPosition.x = Cos(angle) * (point.x - center.x) - Sin(angle) * (point.z - center.z) + center.x; 136 | FinalPosition.z = Sin(angle) * (point.x - center.x) + Cos(angle) * (point.z - center.z) + center.z; 137 | 138 | return new Vector3(FinalPosition.x, pos.y, FinalPosition.z); 139 | } 140 | 141 | } 142 | 143 | public void AddTreeSquare(float scale, float Rotation, Vector3 pos, bool upordown) 144 | { 145 | scale = scale * 5; 146 | 147 | // Fast 148 | 149 | Vector3 a = new Vector3(pos.x - scale, pos.y, pos.z + scale) 150 | , b = new Vector3(pos.x + scale, pos.y, pos.z + scale) 151 | , c = new Vector3(pos.x + scale, pos.y, pos.z - scale) 152 | , d = new Vector3(pos.x - scale, pos.y, pos.z - scale); 153 | 154 | // Slow 155 | 156 | int l = verts.Count; 157 | 158 | if (upordown) 159 | { 160 | Triangles.AddRange(new List() { l + 2, l + 1, l, l + 3, l + 2, l }); 161 | } 162 | else 163 | { 164 | Triangles.AddRange(new List() { l, l + 1, l + 2, l, l + 2, l + 3 }); 165 | } 166 | 167 | // Fast 168 | 169 | verts.AddRange(new List() { rot(pos, a, Rotation), rot(pos, b, Rotation), rot(pos, c, Rotation), rot(pos, d, Rotation) }); 170 | 171 | 172 | Vector3 rot(Vector3 center, Vector3 point, float Degrees) 173 | { 174 | Vector3 FinalPosition = Vector3.zero; 175 | 176 | float angle = (-Degrees) * (Mathf.PI / 180); 177 | FinalPosition.x = Cos(angle) * (point.x - center.x) - Sin(angle) * (point.z - center.z) + center.x; 178 | FinalPosition.z = Sin(angle) * (point.x - center.x) + Cos(angle) * (point.z - center.z) + center.z; 179 | 180 | return new Vector3(FinalPosition.x, pos.y, FinalPosition.z); 181 | } 182 | } 183 | } 184 | -------------------------------------------------------------------------------- /Procedural Noise Islands/Island Texture With Customisable Octaves and Seed.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using Unity.Mathematics; 5 | using System; 6 | 7 | public class IslandTextureGeneration : MonoBehaviour 8 | { 9 | public int TextureSize; 10 | public float NoiseScale, IslandSize; 11 | [Range(1, 20)] public int NoiseOctaves; 12 | [Range(0, 99999999)] public int Seed; 13 | 14 | // Privates 15 | 16 | private Color[] col; 17 | private Texture2D tex; 18 | 19 | public Gradient ColorGradient; 20 | 21 | private void Start() 22 | { 23 | tex = new Texture2D(TextureSize, TextureSize); 24 | col = new Color[tex.height * tex.width]; 25 | 26 | Renderer rend = GetComponent(); 27 | rend.sharedMaterial.mainTexture = tex; 28 | 29 | Vector2 Org = new Vector2(Mathf.Sqrt(Seed), Mathf.Sqrt(Seed)); 30 | 31 | for (int x = 0, i = 0; x < TextureSize; x++){ 32 | for (int y = 0; y < TextureSize; y++, i++){ 33 | float a = Noisefunction(x, y, Org); 34 | col[i] = ColorGradient.Evaluate(Noisefunction((float)x, (float)y, Org)); 35 | } 36 | } 37 | tex.SetPixels(col); 38 | tex.Apply(); 39 | tex.wrapMode = TextureWrapMode.Clamp; 40 | } 41 | 42 | private float Noisefunction(float x, float y, Vector2 Origin) 43 | { 44 | float a = 0, noisesize = NoiseScale, opacity = 1; 45 | 46 | for (int octaves = 0; octaves < NoiseOctaves; octaves++) 47 | { 48 | float xVal = (x / (noisesize * TextureSize)) + Origin.x; 49 | float yVal = (y / (noisesize * TextureSize)) - Origin.y; 50 | float z = noise.snoise(new float2(xVal, yVal)); 51 | a += Mathf.InverseLerp(0, 1, z) / opacity; 52 | 53 | noisesize /= 2f; 54 | opacity *= 2f; 55 | } 56 | 57 | return a -= FallOffMap(x, y, TextureSize, IslandSize); 58 | } 59 | 60 | private float FallOffMap(float x, float y, int size, float islandSize) 61 | { 62 | float gradient = 1; 63 | 64 | gradient /= (x * y) / (size * size) * (1 - (x / size)) * (1 - (y / size)); 65 | gradient -= 16; 66 | gradient /= islandSize; 67 | 68 | 69 | return gradient; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Procedural Noise Islands/Noise Map Generation V1: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | 5 | public class NoiseMap : MonoBehaviour 6 | { 7 | public int size, scale; 8 | private Renderer Renderer; 9 | private Color[] Col; 10 | private Texture2D tex; 11 | public float FalloffDensity; 12 | private float[] fall, perlin; 13 | 14 | private void Start() 15 | { 16 | Renderer = this.gameObject.GetComponent(); 17 | Col = new Color[size * size]; 18 | fall = new float[size * size]; 19 | perlin = new float[size * size]; 20 | tex = new Texture2D(size, size); 21 | Renderer.material.mainTexture = tex; 22 | GenerateFallOffMap(); 23 | GeneratePerlinNoiseMap(); 24 | Invoke("GenerateFinalIsland", 1); 25 | 26 | 27 | } 28 | 29 | void GenerateFallOffMap() 30 | { 31 | Vector2 center = new Vector2(size / 2, size / 2); 32 | for (int x = 0; x < size; x++) 33 | { 34 | for (int y = 0; y < size; y++) 35 | { 36 | float dist = Vector2.Distance(new Vector2(x, y), center) / FalloffDensity; 37 | fall[x * size + y] = dist; 38 | //Col[x * size + y] = new Color(dist, dist, dist); 39 | } 40 | } 41 | //tex.SetPixels(Col); 42 | //tex.Apply(); 43 | } 44 | 45 | void GeneratePerlinNoiseMap() 46 | { 47 | float x = 0.0f; 48 | while(x < size) 49 | { 50 | float y = 0.0f; 51 | while(y < size) 52 | { 53 | float xCoord = x / size * scale; 54 | float yCoord = y / size * scale; 55 | float sample = Mathf.PerlinNoise(xCoord, yCoord); 56 | perlin[(int)x * size + (int)y] = sample; 57 | //Col[(int)x * size + (int)y] = new Color(sample, sample, sample); 58 | y++; 59 | } 60 | x++; 61 | } 62 | //tex.SetPixels(Col); 63 | //tex.Apply(); 64 | } 65 | 66 | void GenerateFinalIsland() 67 | { 68 | for (int x = 0; x < size; x++) 69 | { 70 | for (int y = 0; y < size; y++) 71 | { 72 | float sample = perlin[x * size + y] - fall[x * size + y]; 73 | Col[x * size + y] = new Color(sample, sample, sample) * UnityEngine.Random.ColorHSV(); 74 | } 75 | } 76 | tex.SetPixels(Col); 77 | tex.Apply(); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /Procedural Noise Islands/Noise Map Generation V2: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class PerlinNoise : MonoBehaviour 4 | { 5 | public int size, FallOff; 6 | public float scale; 7 | public Renderer rend; 8 | Color[] col; 9 | Texture2D tex; 10 | 11 | private void Start() 12 | { 13 | col = new Color[size * size]; 14 | tex = new Texture2D(size, size); 15 | rend.material.mainTexture = tex; 16 | int rand = UnityEngine.Random.Range(0, 100000); 17 | float y = 0.0f; 18 | while(y < size) 19 | { 20 | float x = 0.0f; 21 | while(x < size) 22 | { 23 | float xcoord = (x / size * scale) + rand; 24 | float ycoord = (y / size * scale) + rand; 25 | float sample = (Mathf.PerlinNoise(xcoord, ycoord) + Mathf.PerlinNoise(xcoord * 3f, ycoord * 3f) / 2 26 | + Mathf.PerlinNoise(xcoord * 6f, ycoord * 6f) / 5) 27 | - Vector2.Distance(new Vector2(y, x), new Vector2(size / 2, size / 2)) / FallOff; 28 | 29 | col[(int)y * size + (int)x] = new Color(sample, sample, sample); 30 | x++; 31 | } 32 | y++; 33 | } 34 | tex.SetPixels(col); 35 | tex.Apply(); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Procedural Noise Islands/Noise With Biomes/Biome Texture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flaroon/Video-Files/1a3c69e87e24bd7c8f15d1e27309d66b1a10a646/Procedural Noise Islands/Noise With Biomes/Biome Texture.png -------------------------------------------------------------------------------- /Procedural Noise Islands/Noise With Biomes/Noise Generator.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.IO; 3 | 4 | public class NoiseGenerationWithOctaves : MonoBehaviour 5 | { 6 | public int TextureSize; 7 | public float NoiseScale, IslandSize; 8 | [Range(1, 20)] public int NoiseOctaves; 9 | public Texture2D biomeTexture; 10 | public string Seed; 11 | 12 | private void Awake() 13 | { 14 | // Generate textures and apply texture 15 | Texture2D tex = new Texture2D(TextureSize, TextureSize); 16 | GetComponent().sharedMaterial.mainTexture = tex; 17 | 18 | // Calculate seed 19 | UnityEngine.Random.InitState(Seed.GetHashCode()); 20 | Vector2 Org = new Vector2(Random.Range(-9999, 9999), Random.Range(-9999, 9999)); 21 | 22 | float b = Time.realtimeSinceStartup; 23 | 24 | // Loop through all pixels in texture and assign a color 25 | for (int x = 0; x < TextureSize; x++) 26 | for (int y = 0; y < TextureSize; y++) 27 | tex.SetPixel(x, y, Noisefunction.FinalNoise(x, y, NoiseOctaves, Org, TextureSize, NoiseScale, IslandSize, biomeTexture)); 28 | tex.Apply(); 29 | 30 | print("Time since start of game : " + Time.realtimeSinceStartup.ToString()); 31 | print("Time for loop to run was : " + (Time.realtimeSinceStartup - b).ToString()); 32 | } 33 | 34 | } 35 | 36 | -------------------------------------------------------------------------------- /Procedural Noise Islands/Noise With Biomes/Noise function.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using Unity.Mathematics; 3 | 4 | public class Noisefunction 5 | { 6 | public static Color FinalNoise(float x, float y, int Octaves, Vector2 Origin, int Size, float NoiseScale, float IslandSize, Texture2D BiomeTex) 7 | { 8 | // Get rainfall and temperature noise 9 | float a = WarpedNoise(x, y, Octaves, Origin, Size, NoiseScale); 10 | float t = TempNoise(x + 100, y - 100, Octaves, Origin, Size, NoiseScale, IslandSize); 11 | float r = RainNoise(x - 100, y + 100, Octaves, Origin, Size, NoiseScale, IslandSize); 12 | // Biome Texture is 50 x 50 pixels so to calculate the pixel 13 | // correspondence we multiply the 0 to 1 nosie values to be between 0 and 50 and an int 14 | 15 | t = Mathf.RoundToInt(t * 50); 16 | r = Mathf.RoundToInt(r * 50); 17 | 18 | // Return color returned from the texture 19 | return terraincol((int)t, (int)r, BiomeTex); 20 | } 21 | 22 | static float TempNoise(float x, float y, int Octaves, Vector2 Origin, int Size, float NoiseScale, float IslandSize) 23 | { 24 | float a = WarpedNoise(x, y, Octaves, Origin, Size, NoiseScale); 25 | 26 | return a - FalloffMap(x - 100, y + 100, Size, IslandSize); 27 | } 28 | static float RainNoise(float x, float y, int Octaves, Vector2 Origin, int Size, float NoiseScale, float IslandSize) 29 | { 30 | float a = WarpedNoise(x, y, Octaves, Origin, Size, NoiseScale); 31 | 32 | return a - FalloffMap(x + 100, y - 100, Size, IslandSize); 33 | } 34 | public static float WarpedNoise(float x, float y, int Octaves, Vector2 Origin, int Size, float NoiseScale) 35 | { 36 | // warp the nosie by generating multiple instances of noise 37 | var q = Noise(x + 5.3f, y + 0.8f, Octaves, Origin, Size, NoiseScale); 38 | 39 | return Noise(x + 80.0f * q, y + 80.0f * q, Octaves, Origin, Size, NoiseScale); 40 | } 41 | 42 | public static float Noise(float x, float y, int Octaves, Vector2 Origin, int Size, float NoiseScale) 43 | { 44 | // Just generate the noise map 45 | 46 | float a = 0, Opacity = 1, MaxValue = 0; 47 | 48 | // Loop for octaves 49 | for (int octaves = 0; octaves < Octaves; octaves++) 50 | { 51 | // find sample position on xy axis 52 | float xVal = (x / (Size / NoiseScale)) + Origin.x; 53 | float yVal = (y / (Size / NoiseScale)) + Origin.y; 54 | float z = (noise.snoise(new float2(xVal, yVal)) + 1) * 0.5f; 55 | a += (z / Opacity); 56 | MaxValue += 1f / Opacity; 57 | 58 | // Change opacity and scale 59 | NoiseScale *= 2f; 60 | Opacity *= 2f; 61 | } 62 | 63 | // divide by max value to normalize the noise value between 0 and 1 64 | a /= MaxValue; 65 | 66 | return a; 67 | } 68 | 69 | private static float FalloffMap(float x, float y, float size, float islandSize) 70 | { 71 | // Generate a radial gradient to make island 72 | return (1 / ((x * y) / (size * size) * (1 - (x / size)) * (1 - (y / size))) - 16) / islandSize; 73 | } 74 | 75 | public static Color terraincol(int temperaturemap, int rainfallmap, Texture2D biomeTexture ) 76 | { 77 | // use the temp and rainfall maps to sample pixel color from biome texture 78 | return (biomeTexture.GetPixel(temperaturemap, rainfallmap)); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Flaroon Youtube Video's Code 2 | 3 | ![Banner Christmas Theme](https://user-images.githubusercontent.com/68732404/137118457-377d52ba-2c1d-43e7-905c-d951c3c6328e.png) 4 | 5 | This is where you will find all of the scripts that are made on the Flaroon Youtube channel. 6 | -------------------------------------------------------------------------------- /Shader Graph/Water Shaders.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flaroon/Video-Files/1a3c69e87e24bd7c8f15d1e27309d66b1a10a646/Shader Graph/Water Shaders.zip -------------------------------------------------------------------------------- /Texture Generation Misc/Voronoi Diagram Script: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class VoronoiDiagramTutorial : MonoBehaviour 4 | { 5 | public int Size, polygons; 6 | public float CircleSize; 7 | Renderer rend; 8 | Color[] col; 9 | Texture2D tex; 10 | Vector2[] randomPositions; 11 | 12 | private void Start() 13 | { 14 | rend = this.gameObject.GetComponent(); 15 | col = new Color[Size * Size]; 16 | randomPositions = new Vector2[Size * Size]; 17 | tex = new Texture2D(Size, Size); 18 | rend.material.mainTexture = tex; 19 | VoronoiDiagram(); 20 | } 21 | 22 | private void VoronoiDiagram() 23 | { 24 | for (int i = 0; i < polygons; i++) 25 | { 26 | randomPositions[i] = new Vector2(UnityEngine.Random.Range(0, Size), UnityEngine.Random.Range(0, Size)); 27 | } 28 | for (int x = 0; x < Size; x++) 29 | { 30 | for (int y = 0; y < Size; y++) 31 | { 32 | float[] distances = new float[polygons]; 33 | for (int p = 0; p < polygons; p++) 34 | { 35 | distances[p] = Vector2.Distance(new Vector2(x, y), randomPositions[p]); 36 | } 37 | float sample = Mathf.Min(distances) / CircleSize; 38 | col[x * Size + y] = new Color(sample, sample, sample); 39 | } 40 | } 41 | tex.SetPixels(col); 42 | tex.Apply(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /Voxel Mesh Generators/TexAtlas.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Flaroon/Video-Files/1a3c69e87e24bd7c8f15d1e27309d66b1a10a646/Voxel Mesh Generators/TexAtlas.png -------------------------------------------------------------------------------- /Voxel Mesh Generators/Voxel Mesh Episode 1.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using Unity.Mathematics; 5 | 6 | public class SimplifiedScript : MonoBehaviour 7 | { 8 | 9 | public Vector3Int Dimentions; 10 | public float NoiseScale; 11 | byte[,,] Voxels; 12 | 13 | private void Awake() 14 | { 15 | Voxels = new byte[Dimentions.x, Dimentions.y, Dimentions.z]; 16 | GenerateNoise(); 17 | GenerateMesh(); 18 | } 19 | 20 | private void GenerateNoise() 21 | { 22 | for (int x = 0; x < Dimentions.x; x++){ 23 | for (int y = 0; y < Dimentions.y; y++){ 24 | for (int z = 0; z < Dimentions.z; z++){ 25 | Voxels[x, y, z] = noise.snoise(new float3((float)x, (float)y, (float)z) / NoiseScale) >= 0 ? (byte)1 : (byte)0; 26 | } 27 | } 28 | } 29 | } 30 | 31 | private void GenerateMesh() 32 | { 33 | float at = Time.realtimeSinceStartup; 34 | List Triangles = new List(); 35 | List Verticies = new List(); 36 | List uv = new List(); 37 | 38 | for (int x = 1; x < Dimentions.x - 1; x++) 39 | for (int y = 1; y < Dimentions.y - 1; y++) 40 | for (int z = 1; z < Dimentions.z - 1; z++) 41 | { 42 | Vector3[] VertPos = new Vector3[8]{ 43 | new Vector3(-1, 1, -1), new Vector3(-1, 1, 1), 44 | new Vector3(1, 1, 1), new Vector3(1, 1, -1), 45 | new Vector3(-1, -1, -1), new Vector3(-1, -1, 1), 46 | new Vector3(1, -1, 1), new Vector3(1, -1, -1), 47 | }; 48 | 49 | int[,] Faces = new int[6, 9]{ 50 | {0, 1, 2, 3, 0, 1, 0, 0, 0}, //top 51 | {7, 6, 5, 4, 0, -1, 0, 1, 0}, //bottom 52 | {2, 1, 5, 6, 0, 0, 1, 1, 1}, //right 53 | {0, 3, 7, 4, 0, 0, -1, 1, 1}, //left 54 | {3, 2, 6, 7, 1, 0, 0, 1, 1}, //front 55 | {1, 0, 4, 5, -1, 0, 0, 1, 1} //back 56 | }; 57 | 58 | if (Voxels[x, y, z] == 1) 59 | for (int o = 0; o < 6; o++) 60 | if (Voxels[x + Faces[o, 4], y + Faces[o, 5], z + Faces[o, 6]] == 0) 61 | AddQuad(o, Verticies.Count); 62 | 63 | void AddQuad(int facenum, int v) 64 | { 65 | // Add Mesh 66 | for (int i = 0; i < 4; i++) Verticies.Add(new Vector3(x, y, z) + VertPos[Faces[facenum, i]] / 2f); 67 | Triangles.AddRange(new List() { v, v + 1, v + 2, v, v + 2, v + 3 }); 68 | 69 | // Add uvs 70 | Vector2 bottomleft = new Vector2(Faces[facenum, 7], Faces[facenum, 8]) / 2f; 71 | 72 | uv.AddRange(new List() { bottomleft + new Vector2(0, 0.5f), bottomleft + new Vector2(0.5f, 0.5f), bottomleft + new Vector2(0.5f, 0), bottomleft }); 73 | } 74 | } 75 | 76 | GetComponent().mesh = new Mesh() 77 | { 78 | vertices = Verticies.ToArray(), 79 | triangles = Triangles.ToArray(), 80 | uv = uv.ToArray() 81 | }; 82 | 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /Water Scripts/UnderWaterDetection.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using UnityEngine; 4 | using UnityEngine.Rendering; 5 | using UnityEngine.Rendering.Universal; 6 | 7 | public class UnderWaterDetection : MonoBehaviour 8 | { 9 | public GameObject BoundingBox, Player; 10 | public Volume Post; 11 | public Color UnderWaterColor; 12 | public bool UnderWater; 13 | 14 | // Effects 15 | 16 | private Vignette VG; 17 | private DepthOfField DOF; 18 | private ColorAdjustments CA; 19 | 20 | private void Start() 21 | { 22 | Post.profile.TryGet(out DOF); 23 | Post.profile.TryGet(out CA); 24 | Post.profile.TryGet(out VG); 25 | } 26 | 27 | private void FixedUpdate() 28 | { 29 | if (BoundingBox.GetComponent().bounds.Contains(Player.transform.position)) UnderWater = false; 30 | else UnderWater = true; 31 | 32 | if (!UnderWater) 33 | { 34 | VG.intensity.value = 0.35f; 35 | DOF.focusDistance.value = 0.1f; 36 | CA.colorFilter.value = UnderWaterColor; 37 | } 38 | else 39 | { 40 | VG.intensity.value = 0.292f; 41 | DOF.focusDistance.value = 5f; 42 | CA.colorFilter.value = Color.white; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /Water Scripts/Water.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class Water : MonoBehaviour 4 | { 5 | Vector3[] verticies; 6 | int[] triangles; 7 | private Vector2[] uvs; 8 | [Range(0, 256)] public int Size; 9 | [Range(1, 8)] public int octaves; 10 | public float height, speed, frequency; 11 | float off = .0f; 12 | 13 | private void Update() 14 | { 15 | verticies = new Vector3[(Size + 1) * (Size + 1)]; 16 | triangles = new int[Size * Size * 6]; 17 | uvs = new Vector2[(Size + 1) * (Size + 1)]; 18 | 19 | for (int x = 0, a = 0; x <= Size; x++) 20 | { 21 | for (int y = 0; y <= Size; y++, a++) 22 | { 23 | float z = 0.0f; 24 | for (int i = 0; i < octaves; i++) 25 | { 26 | z += Mathf.PerlinNoise(((float)x * frequency / 10 + off) * i, ((float)y * frequency / 10 + off) * i) * height * i; 27 | } 28 | verticies[a] = new Vector3(x, z, y); 29 | uvs[a] = new Vector2((float)x / (float)Size, (float)y / (float)Size); 30 | } 31 | } 32 | off += speed / 100; 33 | for (int z = 0, vert = 0, tris = 0; z < Size; z++, vert++) 34 | { 35 | for (int x = 0; x < Size; x++, vert++, tris+=6) 36 | { 37 | triangles[tris + 0] = vert + 0; 38 | triangles[tris + 1] = vert + 1 + Size; 39 | triangles[tris + 2] = vert + 1; 40 | triangles[tris + 3] = vert + 1; 41 | triangles[tris + 4] = vert + 1 + Size; 42 | triangles[tris + 5] = vert + 2 + Size; 43 | 44 | } 45 | } 46 | 47 | GetComponent().mesh = new Mesh() 48 | { 49 | vertices = verticies, 50 | triangles = triangles, 51 | uv = uvs 52 | }; 53 | } 54 | } 55 | --------------------------------------------------------------------------------