Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by vade · Apr 10, 2012 at 08:36 PM · gameobjectchildrendrawmesh

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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 );
                 }
             }
         }
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges