UNITY 网格合并

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour {

    public Avatar a;

	// Use this for initialization
	void Start () {


        MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();

        Debug.Log("meshFilters:" + meshFilters.Length);

        CombineInstance[] combine = new CombineInstance[meshFilters.Length];



        MeshRenderer[] meshRendererList = GetComponentsInChildren<MeshRenderer>();  //获取自身和所有子物体中所有MeshRenderer组件
        Material[] mats = new Material[meshRendererList.Length];                    //新建材质球数组


        Texture2D[] texList = new Texture2D[meshRendererList.Length];


        int i = 0;
        while (i < meshFilters.Length)
        {

            //mats[meshFilters.Length-i-1] = meshRendererList[i].sharedMaterial;                           //获取材质球列表

            mats[i] = meshRendererList[i].sharedMaterial;

            //

            Color c = mats[i].color;

            /*
             * */

            Texture2D tx = mats[i].GetTexture("_MainTex") as Texture2D;
            Texture2D tx2D = new Texture2D(tx.width, tx.height, TextureFormat.ARGB32, false);
            tx2D.SetPixels(tx.GetPixels(0, 0, tx.width, tx.height));
            tx2D.Apply();

            //
            texList[i] = tx2D;
            
            combine[i].mesh = meshFilters[i].sharedMesh;
            //combine[i].transform = meshFilters[i].transform.localToWorldMatrix;

            combine[i].transform = transform.worldToLocalMatrix * meshFilters[i].transform.localToWorldMatrix;

            meshFilters[i].gameObject.SetActive(false);
            i++;
        }

        //
        string shName = mats[0].shader.name;


        Material matNew = new Material(mats[0].shader);
        matNew.CopyPropertiesFromMaterial(mats[0]);


        Texture2D texNew = new Texture2D(1024, 1024);
        matNew.SetTexture("_MainTex", texNew);
        Rect[] rects = texNew.PackTextures(texList, 0);//, 2048);

        //
        GameObject quad = GameObject.Find("Quad");
        quad.GetComponent<MeshRenderer>().material.mainTexture = texNew;


        Mesh meshNew = new Mesh();

       
        //
        i = 0;
        while (i < meshFilters.Length)
        {
            int uvLen = combine[i].mesh.uv.Length;
            Vector2[] uvData = combine[i].mesh.uv;


            for(int j = 0;j< uvLen;j++)
            {
                uvData[j].x = uvData[j].x * rects[i].width + rects[i].x;
                uvData[j].y = uvData[j].y * rects[i].height + rects[i].y;


            }

            //直接设不行
            //combine[i].mesh.uv = uvData;

            meshFilters[i].mesh.uv = uvData;
            combine[i].mesh = meshFilters[i].mesh;

            i++;
        }

        

        transform.GetComponent<MeshFilter>().mesh = meshNew;

        transform.GetComponent<MeshFilter>().name = "xxx";

        transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine, true);//,truefalse);////

        //为合并后的GameObject指定材质
        //transform.GetComponent<MeshRenderer>().sharedMaterials =mats;// materialNew;//          
        transform.GetComponent<MeshRenderer>().sharedMaterial = matNew;


        transform.gameObject.SetActive(true);


    }

    // Update is called once per frame
    void Update () {
		
	}
}

  

posted on 2017-04-16 10:17  jiahuafu  阅读(1066)  评论(0编辑  收藏  举报

导航