- Home /
How to apply a texture to a submesh
I created a cube mesh because I need to apply two materials to it : one on 5 faces (basic colored material) and one on the remaining face (textured with an image)
Here's my code to create the submesh and apply the materials.
public class CubeMesh : MonoBehaviour
{
public float size;
public Material[] materials;
// Start is called before the first frame update
void Start()
{
createCube();
}
// Update is called once per frame
void createCube()
{
Vector3[] vertices = {
new Vector3(0, 0, 0)*size,
new Vector3(1, 0, 0)*size,
new Vector3(1, 1, 0)*size,
new Vector3(0, 1, 0)*size,
new Vector3(0, 1, 1)*size,
new Vector3(1, 1, 1)*size,
new Vector3(1, 0, 1)*size,
new Vector3(0, 0, 1)*size,
};
int[] triangles = {
0, 2, 1, //face front
0, 3, 2,
2, 3, 4, //face top
2, 4, 5,
1, 2, 5, //face right
1, 5, 6,
0, 7, 4, //face left
0, 4, 3,
5, 4, 7, //face back
5, 7, 6,
0, 6, 7, //face bottom
0, 1, 6
};
print(triangles.Length);
Mesh mesh = new Mesh();
GetComponent<MeshFilter>().mesh = mesh;
mesh.vertices = vertices;
mesh.triangles = triangles;
mesh.subMeshCount = 2;
int[] subTriangles = new int[6]; // Front face submesh
Array.Copy(triangles, 0, subTriangles, 0, 6);
mesh.SetTriangles(subTriangles, 0);
subTriangles = new int[30];
Array.Copy(triangles, 6, subTriangles, 0, 30);
mesh.SetTriangles(subTriangles, 1);
mesh.RecalculateNormals();
Renderer r = GetComponent<Renderer>();
r.materials = materials;
}
}
I made the textured material by dropping the image in the Albedo grey square. It's working on regular cubes. However, after assigning the script to a GameObject and setting the materials, I cannot see the image on the face of the cube. I only get a white texture (the image is a logo which is mostly white)
Is there something wrong in what I'm doing?
Answer by Bunny83 · Jul 26, 2019 at 01:02 PM
You haven't created any UV coordinates for your vertices. So there's no texture mapping taking place. By default all vertices would have a UV coordinate of 0,0 so all faces would be mapped to the lower left corner of your image.
Also your mesh doesn't represent a cube. It's more like a lowpoly sphere since you have shared vertices everywhere. The vertices at the corners need to be split into 3, one for each face that meets at that corner. That's why a cube mesh needs 24 vertices instead of 8. You have 6 faces and each face needs 4 seperate vertices. In order to have a cube look like a cube the 4 vertices of a face need to have all the same vertex normal. However if you only have 8 vertices in total, you can not assign 3 different normals to the same vertex. Likewise the UV coordinates. To map a single face to a certain portion of a texture you need to specify a specific UV coordinate at each of the 4 corners. However if you share vertices between two or more faces you can only specify one UV coordinate which would be shared between all faces. This won't work.
Creating a UV mapped cube mesh procedurally is of course possible, but it's a bit more work as what you've done here.
The code example I've posted over here doesn't really create a cube but the frustom of a camera which is just a deformed cube. Though the concept would be similar. I also started with the initial 8 vertices but have created that "m_VertOrder" array which creates the 24 required vertices out of the initial 8. Though I do not generate any UVs in this example since there is not only one valid way how to unwrap a cube.
If you want to create an unwrapped cube (with UV coordinates) it might be simpler to just create each face one by one.
Hi, thank you for all the information and the help, it's been very useful. I followed your advice and made a 24 vertices cube and generated the UVs but the texture is split in 4 and the four parts of the image are respectively in the opposite corner it should be.
Here is how I generate UVs
Vector2[] uvs = new Vector2[vertices.Length];
for (int i = 0; i < uvs.Length; i++) {
uvs[i] = new Vector2(vertices[i].x, vertices[i].y);
}
mesh.uv = uvs;
I tried a few combinations of vertices and triangle order but can't fix it.
Your answer
Follow this Question
Related Questions
Layered Textures/Materials 0 Answers
Mesh wont accept material? 2 Answers
Applying a material to mesh parts 1 Answer
Smoothing mesh texture around edges 0 Answers