I am looking to basically blit a Game Object and its children on screen, via DrawMesh.
My GameObjects have children, some with multiple sub meshes and multiple materials, and I am attempting to iterate over my Game Object and its children, like so:
Vector3 scale = new Vector3(size, size, size);
Vector3 pos = new Vector3(posX, posY, posZ);
Quaternion rot = Quaternion.AngleAxis(Random.Range(0.0f, 360.0f), Vector3.up);
Matrix4x4 matrix = Matrix4x4.TRS(pos, rot, scale);
MeshFilter meshFilter = toInstance.GetComponent<MeshFilter>();
Mesh mesh = meshFilter.mesh;
Material mat = toInstance.GetComponent<Renderer>().sharedMaterial;
Graphics.DrawMesh(mesh, matrix, mat, 0);
// Eventually iterate over children and draw all meshes.
MeshFilter[] children = toInstance.GetComponentsInChildren<MeshFilter>();
foreach (MeshFilter child in children)
{
Mesh m = child.mesh;
int subMeshCount = m.subMeshCount;
for(int i = 0; i < subMeshCount; i++ )
{
Material[] materials = child.GetComponent<Renderer>().materials;
Graphics.DrawMesh(m, matrix, materials*, 0, null, i);*
}*
}*
However, I am unable to get all submeshes/materials to draw. Any pointers in dispatching a set of DrawMesh calls from a game object, to draw the object and all children? Thank you.
I was trying something similar, draw multiple copies of a mesh (phantoms ). Probably you have the arguments of Graphics.DrawMesh(…) in wrong order, and for getting the materials of the sub meshes, I found out that you can just access them over renderer.materials…