Question by
khizer_awan · Jan 06, 2019 at 10:54 AM ·
proceduralprocedural meshprocedural-terrainprocedural texturinguvmapping
Apply Material (with custom texture) on procedural mesh
Hello guys, I am very new to procedural mesh generation + i am not a 3d modeler at all.
The problem i have is i can't seem to find a way to properly apply my material (having a custom texture) to my procedurally generated simple plane mesh.
Using this code to generate mesh
public class MeshGen : MonoBehaviour
{
public int xSize;
public int zSize;
Vector3[] vertices;
int[] triangles;
Mesh mesh;
void Start()
{
mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
CreateMesh();
}
void CreateMesh()
{
vertices = new Vector3[(xSize + 1) * (zSize + 1)];
for (int i = 0, z = 0; z <= zSize; z++)
{
for (int x = 0; x <= xSize; x++)
{
vertices[i] = new Vector3(x, 0, z);
i++;
}
}
triangles = new int[xSize * zSize * 6];
int vert = 0;
int tris = 0;
for (int z = 0; z < zSize; z++)
{
for (int x = 0; x < xSize; x++)
{
triangles[tris + 0] = vert + 0;
triangles[tris + 1] = vert + xSize + 1;
triangles[tris + 2] = vert + 1;
triangles[tris + 3] = vert + 1;
triangles[tris + 4] = vert + xSize + 1;
triangles[tris + 5] = vert + xSize + 2;
vert++;
tris += 6;
}
vert++;
}
}
void UpdateMesh()
{
mesh.Clear();
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.RecalculateNormals();
}
}
left one is simple plane in unity, and right one is my generated mesh (both having the same material applied). P.S i want my procedural mesh to look like plane (having the material & texture properly applied)
the material i am using
scene.png
(67.8 kB)
material.png
(57.7 kB)
Comment