IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 游戏开发 -> Unity 合并Mesh 将多个小的物体合并成一个大物体,同批次渲染 -> 正文阅读

[游戏开发]Unity 合并Mesh 将多个小的物体合并成一个大物体,同批次渲染

可直接调用的MergeUtils类
MergeUtils.MergeMesh会自动判断顶点是否超出合并最大顶点 然后分批
MergeUtils.MergeMesh3是直接合并

using System.Collections.Generic;

using UnityEditor;

using UnityEngine;
using UnityEngine.Rendering;


namespace OrcFramework.Game
{
    public class MergeUtils
    {

        /// <summary>
        /// 合并网格
        /// </summary>
        private static void MergeMesh2(GameObject parent, bool mergeSubMeshes = false)
        {
            MeshRenderer[] meshRenderers = parent.GetComponentsInChildren<MeshRenderer>();
            Material[] materials = new Material[meshRenderers.Length];
            for (int i = 0; i < meshRenderers.Length; i++)
            {
                materials[i] = meshRenderers[i].sharedMaterial;
            }

            MeshFilter[] meshFilters = parent.GetComponentsInChildren<MeshFilter>();   //获取 所有子物体的网格


            CombineInstance[] combineInstances = new CombineInstance[meshFilters.Length]; //新建一个合并组,长度与 meshfilters一致
            for (int i = 0; i < meshFilters.Length; i++)                                  //遍历
            {
                combineInstances[i].mesh = meshFilters[i].sharedMesh;                   //将共享mesh,赋值
                combineInstances[i].transform = meshFilters[i].transform.localToWorldMatrix; //本地坐标转矩阵,赋值
                GameObject.DestroyImmediate(meshFilters[i].gameObject);
            }
            Mesh newMesh = new Mesh();                                  //声明一个新网格对象
            newMesh.CombineMeshes(combineInstances, mergeSubMeshes, true);                    //将combineInstances数组传入函数
            parent.AddComponent<MeshFilter>().sharedMesh = newMesh; //给当前空物体,添加网格组件;将合并后的网格,给到自身网格

            parent.AddComponent<MeshRenderer>().sharedMaterials = materials;
        }



        public static GameObject[] MergeMesh(GameObject parent, bool mergeAll = true, bool deleteChildren = false, bool mergeSubMeshes = false)
        {
            List<GameObject> list = new List<GameObject>();
            int verts = 0;
            //存放要合并的父物体  
            Dictionary<int, GameObject> NewParent = new Dictionary<int, GameObject>();
            //获取所有网格过滤器;
            MeshFilter[] meshFilters = parent.GetComponentsInChildren<MeshFilter>();
            for (int i = 0; i < meshFilters.Length; i++)
            {
                verts += meshFilters[i].mesh.vertexCount;

                if (NewParent.ContainsKey((verts / 65536) + 1))
                {
                    meshFilters[i].transform.parent = NewParent[(verts / 65536) + 1].transform;
                }
                else
                {
                    GameObject wx = new GameObject("Combine");
                    wx.transform.parent = parent.transform;
                    NewParent.Add((verts / 65536) + 1, wx);
                    meshFilters[i].transform.parent = NewParent[(verts / 65536) + 1].transform;
                }


            }
            foreach (var item in NewParent)
            {
                list.Add(MergeUtils.MergeMesh3(item.Value, true, true));
            }

            if (deleteChildren)
            {

                //合并完成后删除子项
                for (int i = parent.transform.childCount - 1; i >= 0; i--)
                {
                    if (parent.transform.GetChild(i).name != "Combine")
                    {
                        GameObject.Destroy(parent.transform.GetChild(i).gameObject);
                    }
                }

            }

            return list.ToArray();
        }

        public static GameObject MergeMesh3(GameObject parent, bool mergeAll = true, bool deleteChildren = false, bool mergeSubMeshes = false)
        {
            //获取所有网格过滤器;
            MeshFilter[] meshFilters = parent.GetComponentsInChildren<MeshFilter>();
            //存放不同的材质球,相同的就存一个;
            Dictionary<string, Material> materials = new Dictionary<string, Material>();

            //存放要合并的网格对象;
            Dictionary<string, List<CombineInstance>> combines = new Dictionary<string, List<CombineInstance>>();
            for (int i = 0; i < meshFilters.Length; i++)
            {
                //构造一个网格合并结构体;
                CombineInstance combine = new CombineInstance();

                //给结构体的mesh赋值;
                combine.mesh = meshFilters[i].sharedMesh;
                combine.transform = meshFilters[i].transform.localToWorldMatrix;

                MeshRenderer renderer = meshFilters[i].GetComponent<MeshRenderer>();
                if (renderer == null)
                {
                    continue;
                }
                Material mat = renderer.sharedMaterial;

                if (!materials.ContainsKey(mat.name))
                {
                    materials.Add(mat.name, mat);
                }
                if (combines.ContainsKey(mat.name))
                {
                    combines[mat.name].Add(combine);
                }
                else
                {
                    List<CombineInstance> coms = new List<CombineInstance>();
                    coms.Add(combine);
                    combines[mat.name] = coms;
                }

                //Destroy(meshFilters[i].gameObject);
            }
            GameObject combineObj = new GameObject("Combine");
            combineObj.transform.parent = parent.transform;
            foreach (KeyValuePair<string, Material> mater in materials)
            {
                GameObject obj = new GameObject(mater.Key);
                obj.transform.parent = combineObj.transform;
                MeshFilter combineMeshFilter = obj.AddComponent<MeshFilter>();
                combineMeshFilter.mesh = new Mesh();

                //将引用相同材质球的网格合并;
                combineMeshFilter.sharedMesh.CombineMeshes(combines[mater.Key].ToArray(), true, true);
                Debug.LogError("网格定点数" + combineMeshFilter.sharedMesh.vertices);
                MeshRenderer rend = obj.AddComponent<MeshRenderer>();

                //指定材质球;
                rend.sharedMaterial = mater.Value;

                rend.shadowCastingMode = ShadowCastingMode.Off;
                rend.receiveShadows = true;
            }

            if (mergeAll)
            {
                MergeMesh2(combineObj);
                //Combine(combineObj)
            }


            if (deleteChildren)
            {

                //合并完成后删除子项
                for (int i = parent.transform.childCount - 1; i >= 0; i--)
                {
                    if (parent.transform.GetChild(i).name != "Combine")
                    {
                        GameObject.Destroy(parent.transform.GetChild(i).gameObject);
                    }
                }

            }

            return combineObj;
        }
        public static GameObject MergeMesh4(GameObject parent, bool mergeAll = true, bool deleteChildren = false, bool mergeSubMeshes = false)
        {

            //获取所有网格过滤器;
            MeshFilter[] meshFilters = parent.GetComponentsInChildren<MeshFilter>();
            //存放不同的材质球,相同的就存一个;
            Dictionary<string, Material> materials = new Dictionary<string, Material>();

            //存放要合并的网格对象;
            Dictionary<string, List<CombineInstance>> combines = new Dictionary<string, List<CombineInstance>>();
            for (int i = 0; i < meshFilters.Length; i++)
            {
                //构造一个网格合并结构体;
                CombineInstance combine = new CombineInstance();

                //给结构体的mesh赋值;
                combine.mesh = meshFilters[i].sharedMesh;
                combine.transform = meshFilters[i].transform.localToWorldMatrix;

                MeshRenderer renderer = meshFilters[i].GetComponent<MeshRenderer>();
                if (renderer == null)
                {
                    continue;
                }
                Material mat = renderer.sharedMaterial;

                if (!materials.ContainsKey(mat.name))
                {
                    materials.Add(mat.name, mat);
                }
                if (combines.ContainsKey(mat.name))
                {
                    combines[mat.name].Add(combine);
                }
                else
                {
                    List<CombineInstance> coms = new List<CombineInstance>();
                    coms.Add(combine);
                    combines[mat.name] = coms;
                }

                //Destroy(meshFilters[i].gameObject);
            }
            GameObject combineObj = new GameObject("Combine");
            combineObj.transform.parent = parent.transform;
            foreach (KeyValuePair<string, Material> mater in materials)
            {
                GameObject obj = new GameObject(mater.Key);
                obj.transform.parent = combineObj.transform;
                MeshFilter combineMeshFilter = obj.AddComponent<MeshFilter>();
                combineMeshFilter.mesh = new Mesh();

                //将引用相同材质球的网格合并;
                combineMeshFilter.sharedMesh.CombineMeshes(combines[mater.Key].ToArray(), true, true);
                Debug.LogError("网格定点数" + combineMeshFilter.sharedMesh.vertices);
                MeshRenderer rend = obj.AddComponent<MeshRenderer>();

                //指定材质球;
                rend.sharedMaterial = mater.Value;

                rend.shadowCastingMode = ShadowCastingMode.Off;
                rend.receiveShadows = true;
            }

            if (mergeAll)
            {
                MergeMesh2(combineObj);
                //Combine(combineObj)
            }


            if (deleteChildren)
            {

                //合并完成后删除子项
                for (int i = parent.transform.childCount - 1; i >= 0; i--)
                {
                    if (parent.transform.GetChild(i).name != "Combine")
                    {
                        GameObject.Destroy(parent.transform.GetChild(i).gameObject);
                    }
                }

            }

            return combineObj;
        }

    }
}
  游戏开发 最新文章
6、英飞凌-AURIX-TC3XX: PWM实验之使用 GT
泛型自动装箱
CubeMax添加Rtthread操作系统 组件STM32F10
python多线程编程:如何优雅地关闭线程
数据类型隐式转换导致的阻塞
WebAPi实现多文件上传,并附带参数
from origin ‘null‘ has been blocked by
UE4 蓝图调用C++函数(附带项目工程)
Unity学习笔记(一)结构体的简单理解与应用
【Memory As a Programming Concept in C a
上一篇文章      下一篇文章      查看所有文章
加:2022-02-26 12:03:55  更:2022-02-26 12:05:15 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/27 16:27:01-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码