unity 3d 之合并网格和贴图(combine mesh and texture)
https://www.cnblogs.com/eangulee/p/3877824.html
unity 3d 之合并网格和贴图(combine mesh and texture)
本人是个小白,但是有个做技术的理想。
关于合并网格和贴图这个问题困扰了我好久,问群友,逛论坛,翻帖子,或者说是我的愚笨吧,不过经过努力还是我解决了,测试通过,一个8drawcall的模型,合并后降到2drawcall,当然现在移动设备的性能都比较高了,不必过多纠结于drawcall,如果没这需要请路过吧。。。
好的,废话不多说,我把代码贴出来。

1 using UnityEngine;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.IO;
5
6 public class CombineMesher : MonoBehaviour
7 {
8 // Use this for initialization
9 void Start()
10 {
11 Combine(transform);
12 }
13
14 // Update is called once per frame
15 void Update()
16 {
17
18 }
19
20
21 public Transform Combine(Transform root)
22 {
23 float startTime = Time.realtimeSinceStartup;
24
25 // The SkinnedMeshRenderers that will make up a character will be
26 // combined into one SkinnedMeshRenderers using one material.
27 // This will speed up rendering the resulting character.
28 // note:each SkinnedMeshRenderer must share a same material
29 List<CombineInstance> combineInstances = new List<CombineInstance>();
30 List<Material> materials = new List<Material>();
31 Material material = null;
32 List<Transform> bones = new List<Transform>();
33 Transform[] transforms = root.GetComponentsInChildren<Transform>();
34 List<Texture2D> textures = new List<Texture2D>();
35 int width = 0;
36 int height = 0;
37
38 int uvCount = 0;
39
40 List<Vector2[]> uvList = new List<Vector2[]>();
41
42 foreach (SkinnedMeshRenderer smr in root.GetComponentsInChildren<SkinnedMeshRenderer>())
43 {
44 if (material == null)
45 material = Instantiate(smr.sharedMaterial) as Material;
46 for (int sub = 0; sub < smr.sharedMesh.subMeshCount; sub++)
47 {
48 CombineInstance ci = new CombineInstance();
49 ci.mesh = smr.sharedMesh;
50 ci.subMeshIndex = sub;
51 combineInstances.Add(ci);
52 }
53
54 uvList.Add(smr.sharedMesh.uv);
55 uvCount += smr.sharedMesh.uv.Length;
56
57 if (smr.material.mainTexture != null)
58 {
59 textures.Add(smr.renderer.material.mainTexture as Texture2D);
60 width += smr.renderer.material.mainTexture.width;
61 height += smr.renderer.material.mainTexture.height;
62 }
63
64 // we need to recollect references to the bones we are using
65 foreach (Transform bone in smr.bones)
66 {
67 foreach (Transform transform in transforms)
68 {
69 if (transform.name != bone.name) continue;
70 bones.Add(transform);
71 break;
72 }
73 }
74 Object.Destroy(smr.gameObject);
75 }
76
77 // Obtain and configure the SkinnedMeshRenderer attached to
78 // the character base.
79 SkinnedMeshRenderer r = root.gameObject.GetComponent<SkinnedMeshRenderer>();
80 if (!r)
81 r = root.gameObject.AddComponent<SkinnedMeshRenderer>();
82
83 r.sharedMesh = new Mesh();
84
85 //only set mergeSubMeshes true will combine meshs into single submesh
86 r.sharedMesh.CombineMeshes(combineInstances.ToArray(), true, false);
87 r.bones = bones.ToArray();
88 r.material = material;
89
90 Texture2D skinnedMeshAtlas = new Texture2D(1024, 512);
91 Rect[] packingResult = skinnedMeshAtlas.PackTextures(textures.ToArray(), 0);
92 Vector2[] atlasUVs = new Vector2[uvCount];
93
94 //as combine textures into single texture,so need recalculate uvs
95
96 int j = 0;
97 for (int i = 0; i < uvList.Count; i++)
98 {
99 foreach (Vector2 uv in uvList[i])
100 {
101 atlasUVs[j].x = Mathf.Lerp(packingResult[i].xMin, packingResult[i].xMax, uv.x);
102 atlasUVs[j].y = Mathf.Lerp(packingResult[i].yMin, packingResult[i].yMax, uv.y);
103 j++;
104 }
105 }
106
107 r.material.mainTexture = skinnedMeshAtlas;
108 r.sharedMesh.uv = atlasUVs;
109
110 Debug.Log("combine meshes takes : " + (Time.realtimeSinceStartup - startTime) * 1000 + " ms");
111 return root;
112 }
113 }

unity 3d 之合并网格和贴图(combine mesh and texture)的更多相关文章
- Unity 3D 游戏上线之后的流水总结
原地址:http://tieba.baidu.com/p/2817057297?pn=1 首先.unity 灯光烘焙 :Unity 3D FBX模型导入.选项Model 不导入资源球.Rig 不导入骨 ...
- C#程序员整理的Unity 3D笔记(十五):Unity 3D UI控件至尊–NGUI
目前,UGUI问世不过半年(其随着Unity 4.6发布问世),而市面上商用的产品,UI控件的至尊为NGUI:影响力和广度(可搜索公司招聘Unity 3D,常常能看到对NGUI关键词). NGUI虽然 ...
- 再议Unity 3D
一年前,偶发冲动,翻译了<[译] Unity3D游戏和facebook绑定(1:简介)>系列文章. 现在看有2个明显的好处, 一:给这个不温不火的博客带了top 3的人气: 二:我个人由此 ...
- [Unity 3D] Unity 3D 性能优化 (一)
听到过很多用Unity 3D开发游戏的程序员抱怨引擎效率太低,资源占用太高,包括我自己在以往项目的开发中也头疼过.最近终于有了空闲,可以仔细的研究一下该如何优化Unity 3D下的游戏性能.其实国外有 ...
- Unity 3D 建立开发环境
之后的基本方向 ios游戏开发,基础教程http://www.devdiv.com/unity_d_-thread-128068-1-1.html,学习Unity 3D游戏开发. 应该昨天表示,读了一 ...
- Unity 3D使用GameObject创建一个简单的可移动物体
于Unity 3D游戏的开发.游戏脚本需要3D模拟组合,该脚本将被写入阻力3D为了达到效果对象. 以下是一个小实例,使用Unity 3D实现一个可控制移动的小人.小人能够向前.向后.向左和向右移动. ...
- Unity 3D Framework Designing(1)—— MVVM 模式的设计和实施(Part 1)
初识 MVVM 谈起 MVVM 设计模式,可能第一映像你会想到 WPF/Sliverlight,他们提供了的数据绑定(Data Binding),命令(Command)等功能,这让 MVVM 模式得到 ...
- Unity 3D Framework Designing(3)——构建View和ViewModel的生命周期
> 对于一个View而言,本质上是一个MonoBehaviour.它本身就具备生命周期这个概念,比如,Awake,Start,Update,OnDestory等.这些是非常好的方法,可以让开发者 ...
- Unity 3D Framework Designing(9)——构建统一的 Repository
谈到 『Repository』 仓储模式,第一映像就是封装了对数据的访问和持久化.Repository 模式的理念核心是定义了一个规范,即接口『Interface』,在这个规范里面定义了访问以及持久化 ...
随机推荐
- Cannot find class [org.apache.commons.dbcp.BasicDataSource] for bean with name 'dataSource' defined in class path resource [applicationContext.xml]
Cannot find class [org.apache.commons.dbcp.BasicDataSource] for bean with name 'dataSource' defined ...
- 英语发音规则---ir字母组合发音规律
英语发音规则---ir字母组合发音规律 一.总结 一句话总结: 这个字母组合通常在单词中读[ɜː]:girl /gɜːl/ n. :shirt /ʃɜːt/ n. girl /gɜːl/ n. 女孩 ...
- SpringMVC注解示例
1.web.xml <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-cla ...
- C++(六)— 输入方式
1.输入包含空格的字符串 使用 getline(cin, str)读取一行字符串,遇到换行符停止:cin>>str,是遇到空格就停止. 实现:输入两个字符,在第一个字符中删除第二个字符中出 ...
- python-多线程趣味(锁)
接上一篇,程序员在敲代码的时候觉得无聊,无聊的时候,会想到去吃零食,那么假如一个函数: #! /usr/bin/env python #coding=utf-8 ''' ''' import time ...
- BEC listen and translation exercise 47
Site One was unpopular because of traffic and parking problems.场地一由于交通和停车问题而不受欢迎. The bombs killed a ...
- codeforces 680D D. Bear and Tower of Cubes(dfs+贪心)
题目链接: D. Bear and Tower of Cubes time limit per test 2 seconds memory limit per test 256 megabytes i ...
- 文件系统(node.js学习笔记)
根据nodejs菜鸟教程整理. 官方API文档:nodeJS文件系统API 其他整理:nodejs File System 文件系统操作函数分类 1.引用: 导入文件系统模块(fs)语句:var fs ...
- [转]七个对我最好的职业建议(精简版)--Nicholas C. Zakas
一.不要别人点什么,就做什么 我的第一份工作,只干了8个月,那家公司就倒闭了.我问经理,接下来我该怎么办,他说: "小伙子,千万不要当一个被人点菜的厨师,别人点什么,你就烧什么.不要接受那样 ...
- Trilead,SSH2的Java调用
最近项目要部署10台设备,如果每台设备都手动进行部署想想也是醉了. 因为之前一直使用SecurityFX以及SecurityCRT,所以考虑是否可以使用基于SSH2的类库来实现文件拷贝以及远程命令调用 ...