- Home /
Scripted mesh w/quad topology - material-per-submesh not working ?
I'm trying to create a mesh with submeshes in script using MeshTopology.Quads , before subsequently setting distinct materials per submesh via the materials array. Code is similar to that below, whereby I've simplified things just to be an example mesh with 2 submeshes and 2 materials.
Per the screenshot, it appears only one of the submeshes is correctly being rendered with a material - the other appears to have a 'neutral' appearance indicating that the material is not being applied to the second submesh, despite being correct.
Interestingly (and perhaps tellingly) if I set up a similar example uses the more 'standard' triangles topology, it works perfectly - so I'm wondering if material-per-submesh for quads is simply not supported by Unity?
GameObject myObj = new GameObject("Object");
MeshFilter meshFilter = (MeshFilter)myObj.AddComponent(typeof(MeshFilter));
Mesh mesh = new Mesh();
Vector3[] vertices = new Vector3[]
{
new Vector3( 1, 0, 1),
new Vector3( 1, 0, -1),
new Vector3(-1, 0, -1),
new Vector3(-1, 0, 1),
new Vector3( 1, 0, 3),
new Vector3( 1, 0, 1),
new Vector3(-1, 0, 1),
new Vector3(-1, 0, 3),
};
int[] indexes1 = new int[] {0, 1, 2, 3};
int[] indexes2 = new int[] { 4, 5, 6, 7 };
mesh.vertices = vertices;
mesh.subMeshCount = 2;
mesh.SetIndices(indexes1, MeshTopology.Quads, 0);
mesh.SetIndices(indexes2, MeshTopology.Quads, 1);
meshFilter.mesh = mesh;
MeshRenderer renderer = (MeshRenderer)myObj.AddComponent(typeof(MeshRenderer));
Material[] mat = new Material[2];
mat[0] = new Material(Shader.Find("Standard"));
mat[0].SetColor("_Color", Color.red);
mat[1] = new Material(Shader.Find("Standard"));
mat[1].SetColor("_Color", Color.blue);
renderer.materials = mat;
meshFilter.sharedMesh.RecalculateBounds();
meshFilter.sharedMesh.RecalculateNormals();
I have worked around this for the moment by converting quads to triangles (noting I'm working with scripted mesh creation from an established csv input file format which allows / prefers quad face specifications).
Nonetheless, I'm interested as to whether Unity 'should' support multiple materials for quads