유니티에서 CombineMeshes 함수를 이용하여 배칭하기

유니티에서 CombineMeshes 함수를 사용하여 드로우콜 수치 낮추기

CombineMeshes 함수를 이용하여 다중 메시를 단일 메시로 결합할수있다.

1)프로젝트의 동적배칭 및 정적 배칭을 비활성화하고 아래와 같이 씬을 구성하였다. 

 

2)씬을 구성하는 오브젝트들은 같은 재질을 공유하여 오브젝트의 수와 동일한 수치의 batches를 표시하고있다.

 

3)새로운 게임오브젝트를 생성하고(parent) 개별적으로 존재하는 오브젝트들을 parent의 하위에 위치시킨다(child)

 

4)parent 게임오브젝트에 mesh filter, mesh renderer 컴포넌트를 추가한다. 결합한 메시와 재질을 할당하는데 사용된다.

meshrenderer에 child 게임오브젝트와 같은 재질을 할당한다.

 

5)스크립트를 작성하여 parent 게임오브젝트에 추가한다.

개별적으로 존재하는 child의 메시들을 저장하고(CombineInstance[]) 메시들의 위치를 로컬 공간에서 월드 공간으로 변환한(localToWorldMatrix) 트랜스폼을 할당한다. CombineInstance[]의 메시들을 결합하고(Mesh.CombineMeshes) 이것을 단일 메시로 사용한다.

docs.unity3d.com/ScriptReference/Mesh.CombineMeshes.html

 

6)게임을 실행하면 결합한 단일 메시를 parent 게임오브젝트의 mesh filter에서 확인할수있다.

 

7)batches의 수치가 감소한것을 확인할수있다.

결합 이전

8)오브젝트가 개별적으로 존재하기때문에 화면 바깥에 있는 메시들은 카메라 프러스텀(frustum)에 의해 컬링된다. 

 

결합 이후

9)결합된 단일 메시를 사용하기때문에 화면에 오브젝트의 일부만 존재하여도 전체 메시를 렌더링한다.

 

10)씬에서 카메라 뷰의 batches를 확인한다.

11)단일 메시로 결합할 오브젝트들이 동일한 재질을 사용하고있다. 

 

단일 메시를 사용할 게임오브젝트를 구성하고(mesh filter, mesh renderer) 스크립트를 추가한다(Mesh.CombineMeshes)

 

12)콘솔 윈도우에서 cannot combine mesh that does not allow access 메시지를 표시하고있다.

 

13)모델의 임포트세팅에서 meshs의 read/write enabled에 체크하고 적용한다.

 

14)Mesh.CombineMeshes 함수가 정상적으로 메시를 결합하며 batches의 수치가 감소한것을 확인할수있다.

 

MeshFilter.mesh

docs.unity3d.com/2021.1/Documentation/ScriptReference/MeshFilter-mesh.html?_ga=2.191856361.2138003264.1612430650-247112712.1600397187

 

Unity - Scripting API: MeshFilter.mesh

If no mesh is assigned to the mesh filter a new mesh will be created and assigned. If a mesh is assigned to the mesh filter already, then first query of mesh property will create a duplicate of it, and this copy will be returned. Further queries of mesh pr

docs.unity3d.com

MeshFilter.sharedMesh

docs.unity3d.com/ScriptReference/MeshFilter-sharedMesh.html

 

Unity - Scripting API: MeshFilter.sharedMesh

It is recommended to use this function only for reading mesh data and not for writing, since you might modify imported assets and all objects that use this mesh will be affected. Also, be aware that is not possible to undo the changes done to this mesh.

docs.unity3d.com

 

댓글

Designed by JB FACTORY