您好,登錄后才能下訂單哦!
在系列一中,我們已經知道了問題所在,一個是優化后我們模型位置改變了,另一個是如果是不同的材質的物體一起優化的時候,不同的材質的對象會消失掉,我們在系列二中主要是解決這兩個問題:
接下來我們改進的思路是查找所有的MeshFilter,同時我們根據不同的材質對我們需要優化的對象進行分離。這就需要我們定義兩個鏈表:
ArrayList materials = new ArrayList(); ArrayList combineInstanceArrays = new ArrayList();
下面我們開始遍歷我們需要優化對象的MeshFilter和MeshRenderer,因為材質是與MeshRenderder相關聯的代碼如下:
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,接下來也是最后一步創建Mesh Renderer并將材質賦值給它
{ MeshRenderer meshRendererCombine = gameObject.GetComponent<MeshRenderer>(); if (!meshRendererCombine) meshRendererCombine = gameObject.AddComponent<MeshRenderer>(); Material[] materialsArray = materials.ToArray(typeof(Material)) as Material[]; meshRendererCombine.materials = materialsArray; } }
下面我們看一下優化前后有啥變化,首先我們用四個Cube和一個Sphere,三種材質。效果圖如下:
下面我們將我們優化的圖展示一下:
對比上圖我們可以看到Saved by batching減少了,Used Textures 較少了,Render Textures使用的紋理大小也較少了,雖然相對來說Draw Call增加了,但是由于Render Textures少了,我們的優化效果達到了,而且位置也沒變,多種材質也沒問題了,但是還有個問題,就是我們的Cube有三個是相同的,但是我們看其材質的效果圖:
相同的材質寫了三次,這說明我們的程序還需要繼續去優化,以上代碼只要將其組合在一起就可以使用了,我們將在講臺優化系列三中繼續優化我們的優化程序。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。