I'm following a tutorial on YouTube about procedural generation of planets. I'm puzzled in some aspects of the code shown. If any of you could explain the following points, I would be very pleased.
First take a look at the algorithm, written in C# for Unity:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TerrainFace {
Mesh mesh;
int resolution;
Vector3 localUp;
Vector3 axisA;
Vector3 axisB;
public TerrainFace(Mesh mesh, int resolution, Vector3 localUp)
{
this.mesh = mesh;
this.resolution = resolution;
this.localUp = localUp;
axisA = new Vector3(localUp.y, localUp.z, localUp.x);
axisB = Vector3.Cross(localUp, axisA);
}
public void ConstructMesh()
{
Vector3[] vertices = new Vector3[resolution * resolution];
int[] triangles = new int[(resolution - 1) * (resolution - 1) * 6];
int triIndex = 0;
for (int y = 0; y < resolution; y++)
{
for (int x = 0; x < resolution; x++)
{
int i = x + y * resolution;
Vector2 percent = new Vector2(x, y) / (resolution - 1);
Vector3 pointOnUnitCube = localUp + (percent.x - .5f) * 2 * axisA + (percent.y - .5f) * 2 * axisB;
Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;
vertices[i] = pointOnUnitSphere;
if (x != resolution - 1 && y != resolution - 1)
{
triangles[triIndex] = i;
triangles[triIndex + 1] = i + resolution + 1;
triangles[triIndex + 2] = i + resolution;
triangles[triIndex + 3] = i;
triangles[triIndex + 4] = i + 1;
triangles[triIndex + 5] = i + resolution + 1;
triIndex += 6;
}
}
}
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
}
}
I will briefly explain the code:
The main objective is to generate 6 Meshes which will form a cube, and then by normalization of vectors generate a sphere. This algorithm takes a "Resolution" value, which will determine the how detailed the sphere will be. Each mesh is conformed by vertices (Vector) which will guide the program to make the triangles that will build the sphere.
My question regard in this part of the code:
Mesh mesh;
int resolution;
Vector3 localUp;
Vector3 axisA;
Vector3 axisB;
public TerrainFace(Mesh mesh, int resolution, Vector3 localUp)
{
this.mesh = mesh;
this.resolution = resolution;
this.localUp = localUp;
axisA = new Vector3(localUp.y, localUp.z, localUp.x);
axisB = Vector3.Cross(localUp, axisA);
}
I do not understand exactly what is localUp
and the pair of axes it generates from it.
So the questions would be the following:
- What are
localUp
,axisA
andaxisB
? - What are they doing?
- If they have values (coordinates or something like that) where are they getting those.?