当前位置: 首页 > news >正文

Unity性能优化---动态网格组合(二)

在上一篇中,组合的是同一个材质球的网格,如果其中有不一样的材质球会发生什么?如下图:

将场景中的一个物体替换为不同的材质球

运行之后,就变成了相同的材质。

 要实现组合不同材质的网格步骤如下:

 在父物体上添加不同的材质球,

然后在组合时,先将相同材质的物体组合,再将不同材质的物体组合,但是组合为一个mesh后,因为是两个材质球,会调用绘制两次。

 

 

代码如下

public class MeshCombine2 : MonoBehaviour
{public GameObject[] obj1;public GameObject[] obj2;public void Start(){this.MeshCombine();}void MeshCombine(){MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();List<CombineInstance> combine1 = new List<CombineInstance>(obj1.Length);List<CombineInstance> combine2 = new List<CombineInstance>(obj2.Length);for (int j = 0; j < meshFilters.Length; j++){MeshFilter meshFilter = meshFilters[j];CombineInstance l_combine = new CombineInstance();MeshRenderer meshRender = meshFilter.GetComponent<MeshRenderer>();string materialName = meshRender.material.name.Replace(" (Instance)", "");if (materialName == "Grass"){l_combine.mesh = meshFilter.mesh;l_combine.transform = meshFilter.transform.localToWorldMatrix;combine2.Add(l_combine);}else{l_combine.mesh = meshFilter.mesh;l_combine.transform = meshFilter.transform.localToWorldMatrix;combine1.Add(l_combine);}meshFilter.gameObject.SetActive(false);}Mesh obj1Mesh = new Mesh();obj1Mesh.CombineMeshes(combine1.ToArray());Mesh obj2Mesh = new Mesh();obj2Mesh.CombineMeshes(combine2.ToArray());CombineInstance[] combine = new CombineInstance[2];combine[0].mesh = obj1Mesh;combine[0].transform = this.transform.localToWorldMatrix;combine[1].mesh = obj2Mesh;combine[1].transform = this.transform.localToWorldMatrix;Mesh mesh = new Mesh();mesh.CombineMeshes(combine,false);transform.GetComponent<MeshFilter>().sharedMesh = mesh;transform.gameObject.SetActive(true);}
}

参考链接:

Unity 与 C# 中的动态网格组合 - 组合不同颜色的网格 |哈布拉多 (habrador.com)

http://www.lryc.cn/news/497901.html

相关文章:

  • JVM学习《垃圾回收算法和垃圾回收器》
  • GPS模块/SATES-ST91Z8LR:电路搭建;直接用电脑的USB转串口进行通讯;模组上报定位数据转换地图识别的坐标手动查询地图位置
  • 什么是TCP的三次握手
  • 《Clustering Propagation for Universal Medical Image Segmentation》CVPR2024
  • Linux ifconfig ip 命令详解
  • Vue3 对于echarts使用 v-show,导致显示不全,宽度仅100px,无法重新渲染的问题
  • C++实现俄罗斯方块
  • 鸿蒙分享:添加模块,修改app名称图标
  • 扫描IP段内的使用的IP
  • 【专题】虚拟存储器
  • Python之爬虫入门--示例(2)
  • 5G CPE终端功能及性能评测(四)
  • 人工智能驱动的骗局会模仿熟悉的声音
  • 电子病历静态数据脱敏路径探索
  • 混合云策略在安全领域受到青睐
  • Echarts使用平面方法绘制三维立体柱状图表
  • java-判断语句
  • 11.14【JAVA EXP3】【DEBUG】
  • UE5 和 UE4 中常用的控制台命令总结
  • MR30分布式IO模块赋能喷水织机
  • C++中的封装性
  • PyTorch 深度学习框架简介:灵活、高效的 AI 开发工具
  • leetcode-22.括号生成
  • devops-Dockerfile+Jenkinsfile方式部署Java前后端应用
  • 【Apache Paimon】-- 4 -- Flink 消费 kafka 数据,然后写入 paimon
  • 【成功解决】:VS2019(Visual Studio 2019)遇到E2870问题:此配置中不支持 128 位浮点类型
  • 什么是TCP的三次握手?
  • SQL教程(2):SQL基础语法及用途
  • 在Ubuntu22.04 jammy下用qemu模型riscv32环境装鸿蒙(待续)
  • C++:基本-union是没有构造函数和析构函数的