您好,登錄后才能下訂單哦!
在系列一中,我們已經(jīng)知道了問題所在,一個是優(yōu)化后我們模型位置改變了,另一個是如果是不同的材質(zhì)的物體一起優(yōu)化的時候,不同的材質(zhì)的對象會消失掉,我們在系列二中主要是解決這兩個問題:
接下來我們改進的思路是查找所有的MeshFilter,同時我們根據(jù)不同的材質(zhì)對我們需要優(yōu)化的對象進行分離。這就需要我們定義兩個鏈表:
ArrayList materials = new ArrayList(); ArrayList combineInstanceArrays = new ArrayList();
下面我們開始遍歷我們需要優(yōu)化對象的MeshFilter和MeshRenderer,因為材質(zhì)是與MeshRenderder相關(guān)聯(lián)的代碼如下:
foreach (GameObject obj in Objects) { if (!obj) continue; MeshFilter[] meshFilters = obj.GetComponentsInChildren<MeshFilter>(); foreach (MeshFilter meshFilter in meshFilters) { MeshRenderer meshRenderer = meshFilter.GetComponent<MeshRenderer>(); if (!meshRenderer) { Debug.LogError("MeshFilter does not have a coresponding MeshRenderer."); continue; } if (meshRenderer.materials.Length != meshFilter.sharedMesh.subMeshCount) { Debug.LogError("Mismatch between material count and submesh count. Is this the correct MeshRenderer?"); continue; } for (int s = 0; s < meshFilter.sharedMesh.subMeshCount; s++) { int materialArrayIndex = 0; for (materialArrayIndex = 0; materialArrayIndex < materials.Count; materialArrayIndex++) { if (materials[materialArrayIndex] == meshRenderer.sharedMaterials[s]) break; } if (materialArrayIndex == materials.Count) { materials.Add(meshRenderer.sharedMaterials[s]); combineInstanceArrays.Add(new ArrayList()); } CombineInstance combineInstance = new CombineInstance(); combineInstance.transform = meshRenderer.transform.localToWorldMatrix; combineInstance.subMeshIndex = s; combineInstance.mesh = meshFilter.sharedMesh; (combineInstanceArrays[materialArrayIndex] as ArrayList).Add(combineInstance); } } }
下面是針對MeshFilter的處理代碼:
{ MeshFilter meshFilterCombine = gameObject.GetComponent<MeshFilter>(); if (!meshFilterCombine) meshFilterCombine = gameObject.AddComponent<MeshFilter>(); Mesh[] meshes = new Mesh[materials.Count]; CombineInstance[] combineInstances = new CombineInstance[materials.Count]; for (int m = 0; m < materials.Count; m++) { CombineInstance[] combineInstanceArray = (combineInstanceArrays[m] as ArrayList).ToArray(typeof(CombineInstance)) as CombineInstance[]; meshes[m] = new Mesh(); meshes[m].CombineMeshes(combineInstanceArray, true, true); combineInstances[m] = new CombineInstance(); combineInstances[m].mesh = meshes[m]; combineInstances[m].subMeshIndex = 0; } meshFilterCombine.sharedMesh = new Mesh(); meshFilterCombine.sharedMesh.CombineMeshes(combineInstances, false, false); foreach (Mesh mesh in meshes) { mesh.Clear(); DestroyImmediate(mesh); } }
將其組合成一個Mesh,接下來也是最后一步創(chuàng)建Mesh Renderer并將材質(zhì)賦值給它
{ MeshRenderer meshRendererCombine = gameObject.GetComponent<MeshRenderer>(); if (!meshRendererCombine) meshRendererCombine = gameObject.AddComponent<MeshRenderer>(); Material[] materialsArray = materials.ToArray(typeof(Material)) as Material[]; meshRendererCombine.materials = materialsArray; } }
下面我們看一下優(yōu)化前后有啥變化,首先我們用四個Cube和一個Sphere,三種材質(zhì)。效果圖如下:
下面我們將我們優(yōu)化的圖展示一下:
對比上圖我們可以看到Saved by batching減少了,Used Textures 較少了,Render Textures使用的紋理大小也較少了,雖然相對來說Draw Call增加了,但是由于Render Textures少了,我們的優(yōu)化效果達到了,而且位置也沒變,多種材質(zhì)也沒問題了,但是還有個問題,就是我們的Cube有三個是相同的,但是我們看其材質(zhì)的效果圖:
相同的材質(zhì)寫了三次,這說明我們的程序還需要繼續(xù)去優(yōu)化,以上代碼只要將其組合在一起就可以使用了,我們將在講臺優(yōu)化系列三中繼續(xù)優(yōu)化我們的優(yōu)化程序。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。