Unity Grid Merge

Source: Internet
Author: User

From an optimization perspective, the mesh needs to be merged.

From the angle of the dress up (here refers to change the shape, change the composition of the parts of the dress, rather than the hanging point type of dress up), all need mesh merging, material merging. If it is a person's dress up, then you need to merge skinnedmeshrenderer and brush the corresponding list of bones. The rendering of the model after the merging of meshes can reduce the load on the GPU, but after testing, although the mesh is merged but because the sub-object mesh material is different and does not reduce the drawcall, if the material is changed to the same, unity itself will be batch merge, so the Drawcall will be reduced! In MMO games, grid dynamic merging is also very necessary!

Grid Merge Code:

     Private voidCombinemesh () {//obtains all meshrenderer components in itself and all sub-objectsmeshrenderer[] Meshrenderers = getcomponentsinchildren<meshrenderer>(); //Material Sphere Arraymaterial[] Materials =NewMaterial[meshrenderers.length];  for(inti =0; i < meshrenderers.length; i++) {Materials[i]=meshrenderers[i].sharedmaterial; }        //Merging Mesh//all msehfilter components in the back and sub-objectsmeshfilter[] Meshfilters = getcomponentsinchildren<meshfilter>(); Combineinstance[] Combines=NewCombineinstance[meshrenderers.length];  for(inti =0; i < meshfilters.length; i++) {Combines[i].mesh=Meshfilters[i].sharedmesh; //The point of the Matrix's own spatial coordinates into world space coordinatesCombines[i].transform =Meshfilters[i].transform.localtoworldmatrix; Meshfilters[i].gameobject.setactive (false); }        //Regenerate MeshMeshfilter Meshfilter = transform. Getcomponent<meshfilter>(); if(Meshfilter = =NULL) {Meshfilter= gameobject.addcomponent<meshfilter>(); }        //mesh assignment to the Meshfilter componentMeshfilter.mesh =NewMesh (); //Merge Mesh, the second parameter is false, which means that it is not merged into a grid, but a self-grid listTransform. Getcomponent<meshfilter> (). Mesh.combinemeshes (Combines,false); Transform.gameObject.SetActive (true); //assign a material to a merged new mesh//transform. Getcomponent<meshrenderer> (). sharedmaterials = materials;Meshrenderer Meshrender= transform. Getcomponent<meshrenderer>(); if(Meshrender = =NULL) {Meshrender= gameobject.addcomponent<meshrenderer>(); } meshrender.sharedmaterials=materials; }

Before running:

After merging:

After merging, the material of three sub-objects is found on the parent object, and the material is merged into a meshrender.

Project address: [Email protected]:wuzhang/unitycombinemeshrender.git

Unity Grid Merge

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.