I am trying to generate a mesh that will be a rock and I would like to texture it. I looked around, but could not figure how to do this. I use this code to generate a mesh, it’s kinda ugly, but works for now:
void CreateRock()
{
GameObject rockObject = new GameObject();
rockObject.transform.position = new Vector3 ( 0, 2, 0 );
MeshFilter mf = rockObject.AddComponent < MeshFilter >();
MeshRenderer mr = rockObject.AddComponent < MeshRenderer > ();
Vector3[] verts = new Vector3[ 12 ];
int[] tri = new int[10 * 3 * 2];
// bottom
float size = 0.75f;
float height = 0;
verts[0] = new Vector3( size, height, -size );
verts[1] = new Vector3( size, height, size );
verts[2] = new Vector3( -size, height, size );
verts[3] = new Vector3( -size, height, -size );
// center
size = 1.25f;
height = 3;
verts[4] = new Vector3( size, height, -size );
verts[5] = new Vector3( size, height, size );
verts[6] = new Vector3( -size, height, size );
verts[7] = new Vector3( -size, height, -size );
// top
size = 0.5f;
height = 4;
verts[ 8] = new Vector3( size, height, -size );
verts[ 9] = new Vector3( size, height, size );
verts[10] = new Vector3( -size, height, size );
verts[11] = new Vector3( -size, height, -size );
//triangles (clockwise)
// bottom
// 0, 1, 2
// 0, 2, 3
tri[0] = 0;
tri[1] = 1;
tri[2] = 2;
tri[3] = 0;
tri[4] = 2;
tri[5] = 3;
// bottom right
// 0, 5, 1
// 0, 4, 5
tri[6] = 0;
tri[7] = 5;
tri[8] = 1;
tri[9] = 0;
tri[10] = 4;
tri[11] = 5;
// bottom front
// 0, 7, 4
// 0, 3, 7
tri[12] = 0;
tri[13] = 7;
tri[14] = 4;
tri[15] = 0;
tri[16] = 3;
tri[17] = 7;
// bottom left
// 3, 2, 6
// 3, 6, 7
tri[18] = 3;
tri[19] = 2;
tri[20] = 6;
tri[21] = 3;
tri[22] = 6;
tri[23] = 7;
// bottom back
// 2, 1, 5
// 2, 5, 6
tri[24] = 2;
tri[25] = 1;
tri[26] = 5;
tri[27] = 2;
tri[28] = 5;
tri[29] = 6;
// top right
// 4, 8, 9
// 4, 9, 5
tri[30] = 4;
tri[31] = 8;
tri[32] = 9;
tri[33] = 4;
tri[34] = 9;
tri[35] = 5;
// top front
// 7, 11, 8
// 7, 8, 4
tri[36] = 7;
tri[37] = 11;
tri[38] = 8;
tri[39] = 7;
tri[40] = 8;
tri[41] = 4;
// top left
// 11, 6, 10
// 11, 7, 6
tri[42] = 11;
tri[43] = 6;
tri[44] = 10;
tri[45] = 11;
tri[46] = 7;
tri[47] = 6;
// top back
// 6, 5, 9
// 6, 9, 10
tri[48] = 6;
tri[49] = 5;
tri[50] = 9;
tri[51] = 6;
tri[52] = 9;
tri[53] = 10;
// top
// 10, 9, 8
// 11, 10, 8
tri[54] = 10;
tri[55] = 9;
tri[56] = 8;
tri[57] = 11;
tri[58] = 10;
tri[59] = 8;
Mesh mesh = new Mesh();
mesh.name = "Rock";
mesh.vertices = verts;
mesh.triangles = tri;
mesh.RecalculateNormals();
mf.mesh = mesh;
mr.material = this.defaultRockMaterial;
}
This works, and I see this:
I would like to apply this kind of texture to it. I would like the top to be generally green and bottom to be black. Maybe have some sort of wave effect where the color changes. So, here is a sample texture:
I guess I should be calculating UVs for the mesh, but not sure where to start. Halp please!