Hello,
I followed a Brackeys tutorial to create a 3d mesh,
I ended up having a mesh that is 20x20(on the left)
Then I created a material and assigned in the sprite tile that is on the right.
It seems to only draw the edges from the material
It should look exactly like the one on the right side,
how can I fix that?
And second question, how can I assign each tile in my 3d mesh to look like the tile on the right, it seems the material covers the entirety of the mesh instead of each tile individually.
Any help would be appreciated, thank you
Here is my code
using UnityEngine;
[RequireComponent (typeof(MeshFilter))]
public class MeshGenerator : MonoBehaviour
{
Mesh mesh;
Vector3[] vertices;
int[] triangles;
public int xSize = 16;
public int zSize = 16;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
CreateShape();
UpdateMesh();
}
// Update is called once per frame
void Update()
{
}
void CreateShape()
{
vertices = new Vector3[(xSize + 1) * (zSize + 1)];
int i = 0; // Initialize the vertex index
// Generate vertices
for (int z = 0; z <= zSize; z++) // Iterate over z-axis
{
for (int x = 0; x <= xSize; x++) // Iterate over x-axis
{
vertices[i] = new Vector3(x, 0, z); // Assign vertex coordinates
i++; // Increment vertex index
}
}
// Generate triangles
triangles = new int[xSize * zSize * 6]; // Each square has two triangles (6 indices)
int vert = 0; // Current vertex index
int tris = 0; // Current triangle index
for (int z = 0; z < zSize; z++) // Iterate through rows of squares
{
for (int x = 0; x < xSize; x++) // Iterate through columns of squares
{
triangles[tris + 0] = vert + 0; // Bottom-left of the square
triangles[tris + 1] = vert + xSize + 1; // Top-left of the square
triangles[tris + 2] = vert + 1; // Bottom-right of the square
triangles[tris + 3] = vert + 1; // Bottom-right of the square
triangles[tris + 4] = vert + xSize + 1; // Top-left of the square
triangles[tris + 5] = vert + xSize + 2; // Top-right of the square
vert++; // Move to the next column
tris += 6; // Increment triangle indices by 6
}
vert++; // Skip the last vertex in each row
}
}
void UpdateMesh()
{
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
}
private void OnDrawGizmos()
{
if (vertices == null)
{
return;
}
for (int i = 0; i < vertices.Length; i++)
{
Gizmos.DrawSphere(vertices[i], 0.1f);
}
}
}

