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』,在这个规范里面定义了访问以及持久化 ...
随机推荐
- 分享知识-快乐自己:MyBtis内置缓存机制
在实际的项目开发中,通常对数据库的查询性能要求很高,而mybatis提供了查询缓存来缓存数据,从而达到提高查询性能的要求. mybatis的查询缓存分为一级缓存和二级缓存,一级缓存是SqlSessio ...
- Mysql存储过程及调用
存储过程: 存储过程是SQL 语句和可选控制流语句的预编译集合,以一个名称存储并作为一个单元处理.存储过程存储在数据库内,可由应用程序通过一个调用执行,而且允许用户声明变量.有条件执行以及其它强大的 ...
- 如何在node.js中使用neo4j
本章中你将会学到如何在node.js中使用neo4j图形数据库. 当你想存储或者查询和数据紧密关联的数据的时候,图形数据库很有用. neo4j是一个可有效存储,处理和查询你数据模型中紧密相连的元素的数 ...
- 2018.6.21 HOLTEK HT49R70A-1 Source Code analysis
Cange note: “Reading TMR1H will latch the contents of TMR1H and TMR1L counter to the destination”? F ...
- 机器学习 Support Vector Machines 3
Optimal margin classifiers 前面我们讲过,对如下的原始的优化问题我们希望找到一个优化的边界分类器. minγ,w,bs.t.12∥w∥2y(i)(wTx(i)+b)⩾1,i= ...
- noip前打板子 qwq
在某咕上打了一晚上的模板 感觉还好... #include<bits/stdc++.h> #define LL long long using namespace std; inline ...
- BZOJ3700: 发展城市
BZOJ3700: 发展城市 https://lydsy.com/JudgeOnline/problem.php?id=3700 分析: 枚举两个人,先求链交,求到两个端点的时间. 链交求法:求两两\ ...
- ACM学习历程—HDU1041 Computer Transformation(递推 && 大数)
Description A sequence consisting of one digit, the number 1 is initially written into a computer. A ...
- 查找图像中椭圆轮廓的快速随机hough变换
查找图像中椭圆轮廓的快速随机hough变换 图像中椭圆轮廓的查找在视频监控等领域有着广泛的应用,经典hough变换给我们提供了一种查找各种图形轮廓的方法,特别是在直线查找方面具有非常高的精确度.但是由 ...
- jQuery对象和DOM对象的互换
Dom 对象:指的是普通的 JavaScript 对象 jQuery对象:是包装 Dom 对象后产生的对象. 一:JQuery 对象和 Dom 对象 在使用 JQuery 过程中,我们一般(也是绝大多 ...