Skip to content

Commit

Permalink
#3 UD1-1 GSCA-9 GenerateMesh TerrainFace
Browse files Browse the repository at this point in the history
  • Loading branch information
Gizmotronn committed Oct 30, 2021
1 parent c02bcf6 commit 849a011
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
42 changes: 34 additions & 8 deletions Assets/PCG/Planet Procedural Generation/Scripts/Planet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MeshRenderer>().sharedMaterial = new Material(Shader.Find("Standard"));
meshFilters[i] = meshObj.AddComponent<meshFilter>();
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();
}
}
}
16 changes: 16 additions & 0 deletions Assets/PCG/Planet Procedural Generation/Scripts/TerrainFace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,30 @@ 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++) {
int i = x + y * resolution; // Number of iterations of the inner loop + outer loop
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();
}
}

0 comments on commit 849a011

Please sign in to comment.