일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- unity #mixamo #character #texture #material
- Android #AndroidStudio #안드로이드스튜디오 #안드로이드스튜디오설치 #안드로이드앱개발
- 게임개발
- 시리얼노션
- 시리얼강의후기
- Unity
- 전시진강의후기
- unity #animation #animator #mixamo #character
- Today
- Total
HB의 개발 블로그
[Unity] CombineMeshes를 활용한 최적화 본문
1. Mesh Combine
여러개의 Mesh를 하나로 합쳐서 Draw Call 수를 줄이는 기능을 제공합니다.
Draw Call 수를 줄이는 것은 프로젝트의 성능을 향상시키는 중요한 최적화 방법 중 하나입니다.
유니티에서는 Mesh.CombineMeshes() 메소드(Method)를 활용하여 Mesh를 결합할 수 있습니다.
아래 예제 코드를 통해 확인해봅시다.
2. 예제 적용
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CombineMeshes : MonoBehaviour
{
void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
Combine();
}
}
void Combine()
{
// 자식 오브젝트들의 MeshFilter 컴포넌트를 가져온다.
MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
// CombineInstance 배열 생성
CombineInstance[] combine = new CombineInstance[meshFilters.Length];
int vertexCount = 0;
for (int i = 0; i < meshFilters.Length; i++)
{
// MeshFilter가 null인 경우 continue
if (meshFilters[i].sharedMesh == null) continue;
// CombineInstance에 Mesh와 Transform 정보 저장
combine[i].mesh = meshFilters[i].sharedMesh;
combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
// GameObject 비활성화
meshFilters[i].gameObject.SetActive(false);
// 정점 수 추가
vertexCount += meshFilters[i].sharedMesh.vertexCount;
}
// MeshFilter 컴포넌트 가져오기
MeshFilter meshFilter = transform.GetComponent<MeshFilter>();
// Mesh 생성
meshFilter.mesh = new Mesh();
// 정점 수에 따라 IndexFormat 자동 선택
if (vertexCount > 65535)
{
meshFilter.mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32;
}
// CombineMeshes 함수를 사용하여 Mesh 결합
meshFilter.mesh.CombineMeshes(combine);
// MeshCollider에 Mesh 할당
GetComponent<MeshCollider>().sharedMesh = meshFilter.mesh;
// GameObject 활성화
transform.gameObject.SetActive(true);
// 회전과 위치 초기화
transform.rotation = Quaternion.identity;
transform.position = Vector3.zero;
}
}
주석을 통해 설명을 남겨두었지만 위 예제 코드는 하위 GameObject의 Mesh를 결합하여 이 GameObject에 새로운 Mesh를 생성하고, 이 Mesh를 렌더링합니다.
+ Mesh.CombineMeshes() 메서드는 65535개의 vertex(정점)까지만 지원합니다. 만약 정점 수가 이 값을 초과할 경우, ArgumentException 예외가 발생합니다. 이를 해결하기 위해서는 UInt32 IndexFormat을 사용하는 Mesh를 생성해야 합니다.
만약 자식오브젝트의 수가 너무 많아 처리해야할 vertex가 너무 많을 때 활용할 수 있습니다. 그렇지 않다면 굳이 작성하지 않으셔도 괜찮습니다.
스크립트 작성을 완료하였다면 에디터 내에서는 Parent Object에 다음과 같이 세팅을 합니다.
Mesh Renderer Material에는 하나로 합칠 Mesh를 넣어줍니다.
그런 다음 함수를 실행해보면 스탯 창에서 Betches와 SetPass Calls가 유의미하게 변하는 것을 확인할 수 있습니다!
주의사항
하나의 큰 Mesh 대신 여러 개의 작은 Mesh를 사용하는 것이 성능에 더 유리한 경우가 있습니다.
또한, Mesh Combine은 Mesh가 많을 때만 유용하며, Mesh의 수가 적을 때는 성능 향상 효과가 크지 않을 수 있습니다. 따라서 Mesh Combine의 사용은 상황에 따라 다르므로 적절한 판단이 필요합니다.
참고링크
Unity - Scripting API: Mesh.CombineMeshes
Combining Meshes is useful for performance optimization. If mergeSubMeshes is true, all the Meshes are combined together into a single sub-mesh. Otherwise, each Mesh is placed in a different sub-mesh. If all Meshes share the same Material, this property sh
docs.unity3d.com
'Unity' 카테고리의 다른 글
[Unity] 유니티로 프로젝트 시작하기 (0) | 2025.02.06 |
---|---|
[Unity]Error - Andoir Build - JDK directory is not set or invalid (0) | 2023.08.16 |
[Unity]Error - ProjectBrowser.OnGUI.repaint 무한 로딩 상태 (0) | 2023.06.02 |
[Unity] Class 상속, 접근제한자 (0) | 2023.04.06 |
[Unity] 프로젝트 최적화 방법들 (0) | 2023.03.29 |