diff --git a/Assets/PCG/Planet Procedural Generation/Scripts/Planet.cs b/Assets/PCG/Planet Procedural Generation/Scripts/Planet.cs index 419531423..fd2ef75f4 100644 --- a/Assets/PCG/Planet Procedural Generation/Scripts/Planet.cs +++ b/Assets/PCG/Planet Procedural Generation/Scripts/Planet.cs @@ -4,15 +4,41 @@ public class Planet : MonoBehaviour { - // Start is called before the first frame update - void Start() - { - + [Range(2,256)] + public int resolution = 10; + + [SerializeField, HideInInspector] + MeshFilter[] meshFilters; + TerrainFace[] terrainFaces; + + private void OnValidate() { + Initialize(); + GenerateMesh(); + } + + void Initialize() { + if(meshFilters == null || meshFilters.Length == 0) { + meshFilters = new MeshFilter[6]; + } + terrainFaces = new TerrainFace[6]; + + Vector3[] directions = {Vector3.up,Vector3.down,Vector3.left,Vector3,right,Vector3.forward,Vector3.back}; + + for(int i = 0; i < 6; i++) { + GameObject meshObj = new GameObject("mesh"); + meshObj.transform.parent = transform; + + meshObj.AddComponent().sharedMaterial = new Material(Shader.Find("Standard")); + meshFilters[i] = meshObj.AddComponent(); + meshFilters[i].sharedMesh = new Mesh(); + + terrainFaces[i] = new TerrainFace(meshFilters[i].sharedMesh, resolution, directions[i]); + } } - // Update is called once per frame - void Update() - { - + void GenerateMesh() { + foreach (TerrainFace face in terrainFaces) { + face.ConstructMesh(); + } } } diff --git a/Assets/PCG/Planet Procedural Generation/Scripts/TerrainFace.cs b/Assets/PCG/Planet Procedural Generation/Scripts/TerrainFace.cs index cb129607c..8f8b19fc0 100644 --- a/Assets/PCG/Planet Procedural Generation/Scripts/TerrainFace.cs +++ b/Assets/PCG/Planet Procedural Generation/Scripts/TerrainFace.cs @@ -21,6 +21,7 @@ public TerrainFace(Mesh mesh, int resolution, Vector3 localup) { public ConstructMesh() { Vector3[] vertices = new Vector3[resolution * resolution]; // Resolution = number of verices across a single face int[] triangles = new int[(resolution-1)*(resolution-1)*2*3]; + int triIndex = 0; // We can create triangles as long as the current vertex is not along the right or bottom edge for(int y = 0; y < resolution; y++) { for(int x = 0; x < resolution; x++) { @@ -28,7 +29,22 @@ public ConstructMesh() { Vector2 percent = new Vector2(x, y) / (resolution - 1); // When x is at highest point, it will be equal to 1% (lowest point 0%) # how close to complete each loop is - where should the vertex be on each face? Vector3 pointOnUnitCube = localUp + (percent.x-.5f)*2*axisA + (percent.y - .5f) * 2 * axisB;// ^How far along the axis we are vertices[i] = pointOnUnitCube; + + // The two triangles of the square in the array + if(x != resolution - 1 && y != resolution - 1) { + triangles[triIndex] = i; + triangles[triIndex+1] = i+resolution+1; + triangles[triIndex+2] = i+resolution; // End of first triangle + + triangles[triIndex+3] = i; + triangles[triIndex+4] = i + 1; + triangles[triIndex+5] = i + resolution+1; + triIndex += 6; + } } } + mesh.vertices = vertices; + mesh.triangles = triangles; + mesh.RecalculateNormals(); } }