Exemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonUp(0))
        {
            Debug.Log("Click");
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                GameObject go = hit.collider.gameObject;
                Debug.Log("Hit:" + go);
                if (AutoAdd)
                {
                    if (!mergedObjs.Contains(go))
                    {
                        if (mergedObjRoot == null)
                        {
                            mergedObjRoot      = new GameObject();
                            mergedObjRoot.name = "mergedObjRoot";
                        }

                        mergedObjs.Add(go);
                        go.transform.SetParent(mergedObjRoot.transform);

                        mergedObj = MeshCombineHelper.SimpleCombine(mergedObjRoot, mergedObj);
                        mergedObj.transform.SetParent(this.transform);
                    }
                }
                if (AutoRemove)
                {
                    MeshCombineHelper.RemveGo(go);
                }
            }
        }
    }
Exemplo n.º 2
0
    private IEnumerator CombineEx_Coroutine(int mode, Action <GameObject> callback)
    {
        DateTime start = DateTime.Now;

        for (int i = 0; i < sourceList.Count; i++)
        {
            GameObject source = (GameObject)sourceList[i];
            Debug.LogError(string.Format("CombineEx {0} ({1}/{2})", source, i + 1, sourceList.Count));
            if (source != null)
            {
                yield return(MeshCombineHelper.CombineEx_Coroutine(source, Setting.IsDestroySource, Setting.WaitCount, mode, callback));
            }
        }
        Setting.WriteLog("完成合并 用时:" + (DateTime.Now - start));
        yield return(null);
    }
Exemplo n.º 3
0
    private MeshPartInfo InnerDoCombine(MeshPartInfo info, int id)
    {
        List <MeshFilter> mfs = info.meshFilters;
        int count             = mfs.Count;

        if (count == 0)
        {
            Debug.LogError("meshFiltersToCombined.Count == 0");
            return(null);
        }
        CombineInstance[] combines = new CombineInstance[count];
        Material[]        mats     = null;
        if (mat == null)
        {
            mats = new Material[count];
        }
        Matrix4x4 matrix = source.worldToLocalMatrix;

        for (int i = 0; i < count; i++)
        {
            MeshFilter   mf = mfs[i];
            MeshRenderer mr = mf.GetComponent <MeshRenderer>();
            if (mr == null)
            {
                continue;
            }
            Mesh ms = mf.sharedMesh;
            if (ms.isReadable == false)
            {
                Debug.LogError("模型必须设置为 Read/Write Enabled = True !! :" + ms);
                continue;
            }
            combines[i].mesh      = ms;
            combines[i].transform = matrix * mf.transform.localToWorldMatrix;
            //combines[i].transform=mf.transform;
            mr.enabled = false;//不渲染

            if (mat == null)
            {
                mats[i] = mr.sharedMaterial;
            }

            MeshCombineHelper.AddGo(mf.gameObject, this);
        }
        //source.gameObject.SetActive(false);

        Mesh newMesh = new Mesh();

        newMesh.indexFormat = indexFormat;
        newMesh.name        = source.name + "_Combined" + id;
        if (mat != null)
        {
            newMesh.name = mat.name + "_Combined" + id;
        }
        bool mergeSubMeshes = mat != null;

        newMesh.CombineMeshes(combines, mergeSubMeshes);//核心 合并网格

        // MeshPartInfo info=new MeshPartInfo();
        info.mesh = newMesh;
        info.mats = mats;
        return(info);
    }
Exemplo n.º 4
0
    private void CombineEx(int mode)
    {
        InitSetting();

        Debug.LogError("Start CombineEx mode:" + mode + "|" + gameObject);
        resultList = new List <GameObject>();
        string sourceName = "";

        // if((sourceList.Count==0 || CombineMode == 0)&&sourceRoot!=null){
        //     sourceList=new List<GameObject>();
        //     if(CombineMode==0){
        //         sourceList.Add(sourceRoot);
        //         sourceName+=sourceRoot.name;
        //     }
        //     else{
        //         for(int i=0;i<sourceRoot.transform.childCount;i++){
        //             GameObject child=sourceRoot.transform.GetChild(i).gameObject;
        //             sourceList.Add(child);
        //             sourceName+=child.name+";";
        //         }
        //     }
        // }

        if (CombineMode == 1 && sourceRoot != null)
        {
            sourceList = new List <GameObject>();
            for (int i = 0; i < sourceRoot.transform.childCount; i++)
            {
                GameObject child = sourceRoot.transform.GetChild(i).gameObject;
                sourceList.Add(child);
                sourceName += child.name + ";";
            }
        }
        else if (CombineMode == 0 && sourceRoot != null)
        {
            sourceList = new List <GameObject>();
            if (CombineMode == 0)
            {
                sourceList.Add(sourceRoot);
                sourceName += sourceRoot.name;
            }
        }

        Debug.LogError("sourceList:" + sourceName);
        // foreach(var source in sourceList){
        //     if(source==null)continue;

        //     if(CombineBySub){
        //         MeshCombiner subCombiner=gameObject.AddComponent<MeshCombiner>();
        //         subCombiner.Auto=true;
        //         //subCombiner.IsCoroutine=this.IsCoroutine;
        //         subCombiner.CombineBySub=false;
        //         //subCombiner.WaitCount=this.WaitCount;
        //         //subCombiner.IsDestroySource=false;
        //         subCombiner.sourceList.Add(source);
        //         SubCombiners.Add(subCombiner);
        //     }
        //     else{
        //         if(Setting.IsCoroutine){
        //             StartCoroutine(MeshCombineHelper.CombineEx_Coroutine(source,Setting.IsDestroySource,mode,Setting.WaitCount));
        //         }
        //         else{
        //             GameObject target=MeshCombineHelper.CombineEx(source,mode);
        //             resultList.Add(target);
        //             Debug.Log("Combine:"+source+"->"+target);
        //             if(Setting.IsDestroySource){
        //                 GameObject.Destroy(source);
        //             }
        //         }

        //     }
        // }

        if (Setting.IsCoroutine)
        {
            //合并
            StartCoroutine(CombineEx_Coroutine(mode, t => {
                Debug.Log("Result:" + t);
                if (IsSaveToScene)
                {
                    SaveToScene(t);
                }
            }));
        }
        else
        {
            for (int i = 0; i < sourceList.Count; i++)
            {
                GameObject source = sourceList[i];
                Debug.LogWarning(string.Format("CombineEx {0} ({1}/{2})", source, i + 1, sourceList.Count));
                if (source == null)
                {
                    continue;
                }

                if (CombineBySub)
                {
                    MeshCombiner subCombiner = gameObject.AddComponent <MeshCombiner>();
                    subCombiner.Auto = true;
                    //subCombiner.IsCoroutine=this.IsCoroutine;
                    subCombiner.CombineBySub = false;
                    //subCombiner.WaitCount=this.WaitCount;
                    //subCombiner.IsDestroySource=false;
                    subCombiner.sourceList.Add(source);
                    SubCombiners.Add(subCombiner);
                }
                else
                {
                    // if(Setting.IsCoroutine){
                    //     StartCoroutine(MeshCombineHelper.CombineEx_Coroutine(source,Setting.IsDestroySource,mode,Setting.WaitCount));
                    // }
                    // else
                    {
                        GameObject target = MeshCombineHelper.CombineEx(source, mode);//合并
                        resultList.Add(target);
                        Debug.Log("Combine:" + source + "->" + target);
                        if (Setting.IsDestroySource)
                        {
                            GameObject.Destroy(source);
                        }
                        // Scene scene=SceneManager.CreateScene(target.name);
                        // SceneManager.MoveGameObjectToScene(target, scene);
                        if (IsSaveToScene)
                        {
                            SaveToScene(target);
                        }
                    }
                }
            }
        }
    }