Last active
April 13, 2019 22:46
-
-
Save AyoubGharbi/e4f9f1108f8b3a2a593fce6ef7d38281 to your computer and use it in GitHub Desktop.
A simple procedural generated mesh using async tasks for a little jucy animation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Unless otherwise noted, the files in this repository are | |
//Copyright(c) 2019 Ayoub Gharbi. | |
using System.Linq; | |
using System.Threading.Tasks; | |
using UnityEngine; | |
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))] | |
public class Island : MonoBehaviour | |
{ | |
public int XIslandSize; | |
public int ZIslandSize; | |
private bool _isDoneInitializing = false; | |
private Triangle[] _triangles; | |
private Vertex[] _vertices; | |
private Mesh _mesh; | |
private MeshFilter _meshFilter; | |
private void Awake() | |
{ | |
_meshFilter = base.GetComponent<MeshFilter>(); | |
_meshFilter.sharedMesh = _mesh = new Mesh(); | |
_vertices = new Vertex[(XIslandSize + 1) * (ZIslandSize + 1)]; | |
_triangles = new Triangle[_vertices.Length / 3]; | |
} | |
private async Task Start() | |
{ | |
await Setup(); | |
} | |
public async Task Setup() | |
{ | |
//Vertices work | |
for (int z = 0, index = 0; z <= ZIslandSize; z++) | |
{ | |
for (int x = 0; x <= XIslandSize; x++, index++) | |
{ | |
_vertices[index] = new Vertex(new Vector3(x, 0.1f, z)); | |
} | |
} | |
_mesh.vertices = _vertices.Select(p => p.Position).ToArray(); | |
_isDoneInitializing = true; | |
//Triangles work | |
_triangles = new Triangle[6 / 3 * XIslandSize * ZIslandSize]; | |
for (int z = 0, index = 0, comp = 0; z < ZIslandSize; comp++, z++) | |
{ | |
for (int x = 0; x < XIslandSize; x++, index += 2, comp++) | |
{ | |
_triangles[index] = new Triangle(new int[3] | |
{ | |
comp, | |
comp + XIslandSize + 1, | |
comp + 1 | |
}); | |
_triangles[index + 1] = new Triangle(new int[3] | |
{ | |
comp + 1, | |
comp + XIslandSize + 1, | |
comp + XIslandSize + 2 | |
}); | |
await Task.Delay(100); | |
_mesh.triangles = _triangles.Where(el => el != null && el.VerticesIndices != null) | |
.Select(el => el.VerticesIndices) | |
.SelectMany(el => el).ToArray(); | |
} | |
} | |
} | |
private void OnDrawGizmos() | |
{ | |
if (!_isDoneInitializing) | |
{ | |
return; | |
} | |
Gizmos.color = Color.black; | |
for (int z = 0, index = 0; z <= ZIslandSize; z++) | |
{ | |
for (int x = 0; x <= XIslandSize; x++, index++) | |
{ | |
Gizmos.DrawSphere(_vertices[index].Position, 0.1f); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Unless otherwise noted, the files in this repository are | |
//Copyright(c) 2019 Ayoub Gharbi. | |
using System; | |
[Serializable] | |
public class Triangle | |
{ | |
public int[] VerticesIndices = new int[3]; | |
public Triangle(int[] indices) | |
{ | |
VerticesIndices = indices ?? throw new System.ArgumentNullException(nameof(indices)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Unless otherwise noted, the files in this repository are | |
//Copyright(c) 2019 Ayoub Gharbi. | |
using UnityEngine; | |
using System; | |
[Serializable] | |
public class Vertex | |
{ | |
public Vector3 Position { get; private set; } | |
public Vertex(Vector3 position) | |
{ | |
Position = position; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment