import UIKit
import RealityKit
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
let anchor = AnchorEntity(plane: .horizontal)
arview.scene.addAnchor(anchor)
let flyer = try Entity.loadModel(named: "flyer")
anchor.addChild(flyer)
}
}
Systems and Framework basic
Reality File
클래스
ARView
Anchor
Entity의 root가 됨
맞는 Anchor가 발견되면, 거기에 entity가 붙음
Scene
Entity
AR Object를 만드는 building block
트리 구조로 되어 있어서, 이를 기반으로 scene을 만들거나, 계층을 변경하기 좋다.
Entity와 Component
let entity = Entity()
// component 추가/변경
entity.components[SpotLightComponent.self] = SpotLightComponent(color: .white)
entity.components[SpotLightComponent.Shadow.self] = SpotLightComponent.Shadow()
// 컴포넌트 제거
entity.components[CollisionComponent.self] = nil
// 자식 entity 추가
entity.addChild(childEntity)
// 로컬 포지션 설정
entity.position = [1.0, 0.0, 0.5]
// 전역 포지션 설정
entity.setPosition([0.5, 0.2, 1.5], relativeTo: nil)
AnchorEntity
let tableAnchor = AnchorEntity(plane: .horizontal,
classification: .table,
minimumBounds: [0.5, 0.5])
scene.addAnchor(tableAnchor) // 시스템이 적절한 물체를 찾으면 자동으로 활성화 된다.
let imageAnchor = AnchorEntity(.image(group: "Posters", name: "WorldsFair"))
scene.addAnchor(imageAnchor)
ModelEntity
let anchor = AnchorEntity(plane: .horizontal)
scene.addAnchor(tableAnchor)
// 동기적으로 모델을 로딩하기 때문에 무거운 작업이다.
// 비동기 버전도 존재한다.
let robot = try ModelEntity.loadModel(named: "robot")
anchor.addChild(robot)
let anchor = AnchorEntity(plane: .horizontal)
scene.addAnchor(tableAnchor)
let box = MeshResource.generateBox(size: 1.0, cornerRadius: 0.1)
let metal = SimpleMaterial(color: .gray, isMetalic: true)
let model = try ModelEntity(mesh: box, materials: [metal])
anchor.addChild(model)
let controller = entity.playAnimation(named: "dance")
controller.pause()
if controller.isPaused {
controller.resume()
}
controller.stop()
let destination = Transform(translation: [0.0, 0.0, 5.0]) // 단위: meter
let controller = entity.move(to: destination,
relativeTo: nil,
duration: 1.0,
easing: .easeInOut)
AmbientLight
SpotLight
DirectionalLight
PerspectiveCamera
TriggerVolume
BodyTrackedEntity
Reality Composor