I've following a tutorial on youtube about procedural generation of planets. I have already watched the complete video and I'm puzzled in some aspects of the code the guy in the video is showing. If any of you could explain the following points I would be very pleased.
First take a look to the code:
Before doing so, there is some things to take into account.
- It's working on unity using C#
- This is just an algorithm
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 of this is to generate 6 Mesh which will form a cube, and then by normalization of vectors generate an sphere. In a nutshell 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 axis.
So the questions would be the following:
- What are localUp, axisA and AxisB?
- What are they doing?
- If they have values (coordinates or something like that) where are they getting those.?
Thansk everyone in advance!.
P.D: If you are interested in this please check out the original author's video https://www.youtube.com/watch?v=QN39W020LqU