- Home /
using DrawMesh to draw all sub meshes and materials for a game object and children
Hello
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[i], 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.
Answer by 1rel · Feb 28, 2014 at 05:26 AM
Hi
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...
Hope this helps!
public int m_iNumberOfPhantomRenders = 4;
private MeshFilter[] m_aoMeshFilter;
void Awake()
{
m_aoMeshFilter = GetComponentsInChildren<MeshFilter>();
}
void Update()
{
RenderPhantoms( m_iNumberOfPhantomRenders );
}
private void RenderPhantoms( int iCount )
{
float fDist = 80.0f; // tmp
Vector3 oTrans = Vector3.forward * fDist;
Vector3 oAccumTranslate = Vector3.zero;
for( int i=0; i<=iCount; ++i )
{
Matrix4x4 oMTranslate = Matrix4x4.identity;
if( i == 0 )
{
// draw one clone behind the player
oMTranslate *= Matrix4x4.TRS( -oTrans, Quaternion.identity, Vector3.one );
}
else
{
oAccumTranslate += oTrans;
oMTranslate *= Matrix4x4.TRS( oAccumTranslate,
//Quaternion.Euler( 0.0f, 0.0f, Mathf.Sin( Time.time ) * 10.0f ),
Quaternion.identity,
Vector3.one );
}
foreach( MeshFilter oMF in m_aoMeshFilter )
{
if( !oMF.renderer.enabled
|| !oMF.gameObject.activeInHierarchy // new!
)
{
//print( oMF.gameObject.name );
continue;
}
int iSubMeshCount = oMF.mesh.subMeshCount;
Matrix4x4 oM = oMTranslate * oMF.transform.localToWorldMatrix;
for( int j=0; j<iSubMeshCount; ++j )
{
Graphics.DrawMesh(
oMF.mesh,
oM,
oMF.renderer.materials[j],
oMF.gameObject.layer,
null,
j,
null,
true,
true );
}
}
}
}
Your answer
Follow this Question
Related Questions
Adding ChildObject 2 Answers
How to check parent value instead name value(more details in post) 3 Answers
Destroy Child Object(s) OnValidate? 6 Answers
How to attach a mesh created with Graphics.DrawMesh to a GameObject? 1 Answer
Is it possible to FindWithTag only within children of a certain gameObject? 1 Answer