in the code below planet.init calls the tile.init().
Context: The Planet class is fed a data object containing information about the planet to spawn, the main one being the radius (how many tiles deep it goes). The same amount of circular strips are then created by spawning a number of tiles depending on the circumference per strip (the farther out it goes, the more tiles are needed to not make the sprites seem stretched), and for every tile, a new mesh is created as shown in the video. It’s just a quad-strip where the vertices are snapped to a certain radius from the center of the planet. The sprites are just used as textures on top of that quad-strip by letting the uv-coordinates run from (0,0) to (1,1) for every tile
planet.init() :
public void Init(Level level, Pickup[] pickupPrefabs)
{
float planetScale = (level.RowCount + YOffset - 1) * 2 + 0.5f;
_planetSphere.localScale = new Vector3(planetScale, planetScale, 1);
_planetSphereBackground.localScale = new Vector3(planetScale, planetScale, 1);
float totalRadius = level.TotalRadius;//.RowCount + YOffset - 1;
Tiles = new List<Tile>();
for (int i = 0; i < level.RowCount; i++)
{
float rowRadius = i + YOffset;
float circumference = 2 * Mathf.PI * rowRadius;
int rowTileCount = Mathf.RoundToInt(circumference / TileWidth);
Tile tilePrefab = i == level.RowCount - 1 ? _grassPrefab : _dirtPrefab;
for (int j = 0; j < rowTileCount; j++)
{
Tile tile = Instantiate(tilePrefab, transform);
tile.Init(j, rowRadius, rowTileCount, 1f, planetBar);
Tiles.Add(tile);
}
}
Tile.init() :
public void Init(int xIndex, float rowY, int rowTileCount, float rowHeight, Bar planetBar)
{
this.planetBar = planetBar;
mesh = new Mesh();
List<Vector3> vertices = new List<Vector3>();
List<int> indices = new List<int>();
List<Vector2> uvs = new List<Vector2>();
float minX = (float)xIndex / rowTileCount;
float maxX = (xIndex + 1f) / rowTileCount;
float minY = rowY;
float maxY = minY + rowHeight;
for(int i = 0; i < HorizontalVertexCount; i++)
{
float t = (float)i / (HorizontalVertexCount - 1);
float x = Mathf.Lerp(minX, maxX, t);
Vector3 vertTop = new Vector3(maxY * Mathf.Cos(x * Mathf.PI * 2f), maxY * Mathf.Sin(x * Mathf.PI * 2f)) - transform.position;
vertices.Add(vertTop);
uvs.Add(new Vector2(t, 1f));
center += vertTop;
Vector3 vertBottom = new Vector3(minY * Mathf.Cos(x * Mathf.PI * 2f), minY * Mathf.Sin(x * Mathf.PI * 2f)) - transform.position;
vertices.Add(vertBottom);
uvs.Add(new Vector2(t, 0f));
center += vertBottom;
if(i > 0)
{
int vertexCount = vertices.Count - 1;
indices.Add(vertexCount - 3);
indices.Add(vertexCount - 2);
indices.Add(vertexCount - 1);
indices.Add(vertexCount - 1);
indices.Add(vertexCount - 2);
indices.Add(vertexCount);
}
}
center /= vertices.Count;
mesh.vertices = vertices.ToArray();
mesh.triangles = indices.ToArray();
mesh.uv = uvs.ToArray();
up = center.normalized;
_textureFilter.mesh = mesh;
_colorMeshFilter.mesh = mesh;
_backgroundMeshFilter.mesh = mesh;
}
MORE SPECIFICALLY, i want to understand how vertTop and vertBottom are created in Tile.init method ??