【SwiftUI、RealityKit】2.3 创建您的第一个 AR 程序

程序效果:

 程序代码:(您需要创建两个文件)

文件1(MyApp):

import SwiftUI
@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            Reality()
        }
    }
}

文件2(Reality):

import SwiftUI
import RealityKit
struct Reality: View {
    var body: some View {
        ARViewContainer().edgesIgnoringSafeArea(.all)
    }
}
struct ARViewContainer: UIViewRepresentable {
    func makeUIView(context: Context) -> ARView {
        let arView = ARView(frame: .zero, cameraMode: .ar, automaticallyConfigureSession: true)
        let anchorEntity = AnchorEntity(plane: .horizontal)//水平类场景
        let box = MeshResource.generateBox(size: 0.2/2, cornerRadius: 0.05/5)//形状
        let material = SimpleMaterial(color: .blue, isMetallic: true)//颜色
        let ARentity = ModelEntity(mesh: box, materials: [material])//实体(加载模型)
        anchorEntity.addChild(ARentity)//锚点
        arView.scene.anchors.append(anchorEntity)//场景
        return arView
    }
    func updateUIView(_ uiView: ARView, context: Context) {
    }
}

备注:本节生成了一个具有金属属性的正方体,然后展现它。

目录:RealityKit AR教程

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值