RealityKit - 기본 머터리얼과 텍스쳐
What is Material??
머터리얼은 프로토콜이다.
Material | Apple Developer Documentation
A type that describes the material aspects of a mesh, like color and texture.
developer.apple.com
Apple에서 제공하는 머터리얼의 종류
CustomMaterial
→ 커스텀 메탈 셰이더 함수로 사용자가 만들 수 있는 머티리얼OcclusionMaterial
→ 렌더링된 오브젝트를 숨기는 보이지 않는 머티리얼PhysicallyBasedMaterial
→ 실제 오브젝트의 모양을 시뮬레이션하는 머티리얼PortalMaterial
→ 순간이동에 최적화된 머터리얼ShaderGraphMaterial
→ 유니티로 만드는 머터리얼이라는데 VisionOS와 함께 새로 출시한 APISimpleMaterial
→ 단일 색으로 칠하는 머터리얼UnlitMaterial
→ 조명효과, 그림자 효과 없이 만드는 머터리얼VideoMaterial
→ 동영상(AVPlayer)을 띄울 수 있는 머터리얼
CustomMaterial | Apple Developer Documentation
A material that works with custom Metal shader functions.
developer.apple.com
Unlit 머터리얼
UnlitMaterial | Apple Developer Documentation
A simple material that doesn’t respond to lights in the scene.
developer.apple.com
씬의 조명(현실 세계의 조명과 다양한 명암 등...)에 반응하지 않는 단순한 머티리얼입니다.
PhysicallyBasedMaterial
실제 오브젝트의 모양을 시뮬레이션하는 머티리얼입니다.
- RealityKit 머티리얼은 렌더링된 3D 오브젝트 표면 속성을 정의하는 오브젝트
- 물리 기반 렌더링(PBR) 머티리얼은 실제 오브젝트에서 빛이 반사되는 방식에 가깝게 근사화한 머티리얼
- 물리 기반 머티리얼을 사용하면 AR 씬에 매우 사실적인 오브젝트를 만들 수 있다.
PhysicallyBasedMaterial의 많은 프로퍼티는 두 가지 이상의 데이터 유형을 사용하여 해당 프로퍼티를 지정할 수 있는 옵션을 제공 - 전체 머티리얼의 특정 색상을 사용해 단일 baseColor를 설정, 엔티티에 UV 매핑되는 이미지를 사용가능
- PhysicallyBasedMaterial에는 USDZ에서 지원하는 모든 머티리얼 프로퍼티가 포함됩니다.
- iOS 15 이상 RealityKit USDZ 파일에서 엔티티를 임포트, PhysicallyBasedMaterial을 자동으로 사용
💡 현실세계 물리(주변광, 직접광, 그림자 등)에 동적인 반응을 하는 머터리얼 다른 머터리얼 객체에 속성에 PhysicallyBasedMaterial 하위 객체 타입을 사용하는 경우가 있다.
예) public var color: SimpleMaterial.BaseColor ← SimpleMaterial 하위 속성 color의 타입
Occlusion 머터리얼
OcclusionMaterial | Apple Developer Documentation
An invisible material that hides objects rendered behind it.
developer.apple.com
Occlusion에 대한 직접적인 이해
⇒ 가상의 Object를 투명하게 보여주는 기술이다.
💡 모델에 Occlusion 머터리얼을 씌워 다른 모델과 겹치게 하는 방법을 사용한다. (코드상 계층구조 X)
Video 머터리얼
VideoMaterial | Apple Developer Documentation
A material that supports animated textures.
developer.apple.com
AVFoundation의 AVPlayer 클래스 인스턴스를 넣을 수 있는 머터리얼
비디오 무한 재생하기
- NotificationCenter 기본 사용하기
- NotificationCenter closure 사용하기
- NotificationCenter의 combine 사용하기
self.videoCancel = NotificationCenter.default
.publisher(for: .AVPlayerItemDidPlayToEndTime,object: player.currentItem)
.receive(on: DispatchQueue.main)
.sink { completion in
switch completion{
case .finished: print("성공적이다!!")
case .failure(let err): print(err)
}
print("completion called self.subscription?.cancel()")
} receiveValue: {[weak self] output in
print("다시 돌아가기")
guard let playerItem = output.object as? AVPlayerItem else { return }
print(playerItem.status)
playerItem.seek(to: CMTime.zero, completionHandler: nil)
self?.player.play()
}
머터리얼에 이미지 텍스처 넣기
💡 png는 기본적으로 이름을 안넣어줘도 되지만 jpg는 확장자를 넣어줘야한다.
- Resource 가져오기
TextureResource.loadAsync(named: "purple_flower")
- BaseColor (PhysicallyBasedMaterial.BaseColor)에 Texture넣기
mat.color = .init(tint:.white,texture: .init(resource))
상자 모델에 면마다 다른 머터리얼 적용하기
let mesh = MeshResource.generateBox(width: 0.2, height: 0.2, depth: 0.2,splitFaces: true)
⇒ splitFaces가 면 마자의 정점을 분리해 각각에 텍스쳐가 적용 가능하게 만든다.