#Procedural Submeshes not Rendering

1 messages · Page 1 of 1 (latest)

still summit
#

Hi,

I am currently working on A plugin to support A custom model format, but when I apply the triangles to sub meshes, the model disappears. The mesh says the triangles exist, but the faces just completely disappear. They do not render nor appear in the wireframe. Any ideas on what's going on here?

Here is the relevant code:

mesh.vertices = verts.ToArray();
mesh.triangles = tris.ToArray();
mesh.uv = uvs.ToArray();
mesh.normals = normals.ToArray();
mesh.subMeshCount = materials.Count;

// Remove this section and everything works perfectly fine, minus the fact that the mesh only supports one material
for (int i = 0; i < mesh.subMeshCount; i++)
{
    List<int> targetTris = new List<int>();
    for (int x = 0; x < submeshes.Count; x++)
    {
        if (submeshes[x] == i)
        { targetTris.Add(submeshes[x]); }
    }
    mesh.SetTriangles(targetTris, i);
}

//mesh.RecalculateNormals();
mesh.RecalculateTangents();
mesh.RecalculateBounds();

mesh.name = new DirectoryInfo(ctx.assetPath).Name;
mesh.name = mesh.name.Substring(0, mesh.name.Length - 4);

ctx.AddObjectToAsset("mesh", mesh);

foreach (Material mat in materials)
{ ctx.AddObjectToAsset(mat.name, mat); }

GameObject meshObj = new GameObject();
meshObj.AddComponent<MeshFilter>().sharedMesh = mesh;
meshObj.AddComponent<MeshRenderer>().SetSharedMaterials(materials);

ctx.AddObjectToAsset("main", meshObj);
ctx.SetMainObject(meshObj);
dull vault
#

it looks a lot like whatever way you are coming up with the submeshes list, it's wrong

still summit
#

for additional context, the mesh looks like this

#

which looks about right

still summit
# dull vault it looks a lot like whatever way you are coming up with the submeshes list, it's...

Hi, for additional context, the submeshes list contains a list of what material (submesh) to assign to for each triangle. Here is the code:

if (i.Length == 1)
{
    if (!matNames.Contains(i[0]))
    {
        Material mat = new Material(Shader.Find("Standard"));
        mat.name = i[0];
        materials.Add(mat);
        matNames.Add(i[0]);
        currentMatIndex = matNames.Count - 1;
    }
    else { currentMatIndex = matNames.FindIndex(x => x == i[0]); }

    continue;
}

Vector3 vert = new Vector3(float.Parse(i[1]), float.Parse(i[2]), float.Parse(i[3]));
Vector3 normal = new Vector3(float.Parse(i[4]), float.Parse(i[5]), float.Parse(i[6]));
Vector2 uv = new Vector2(float.Parse(i[7]), float.Parse(i[8]));

if (verts.Contains(vert))
{
    int vertIndex = verts.FindIndex(x => x == vert);
    if (uvs[vertIndex] == uv && normals[vertIndex] == normal)
    { tris.Add(vertIndex); submeshes.Add(currentMatIndex); continue; }
}

verts.Add(vert);
tris.Add(verts.Count - 1);
uvs.Add(uv);
normals.Add(normal);
submeshes.Add(currentMatIndex);