【SwiftUI、RealityKit】4.1 导入 reality/usdz 文件

一、效果展示:

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

【文件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)//水平类场景
        guard let ARentity = try? ModelEntity.load(named: "U3aQM13") else {
            fatalError("U3aQM13 model is not!")
        }//导入reality/usdz文件
        anchorEntity.addChild(ARentity)//锚点
        arView.scene.anchors.append(anchorEntity)//场景
        return arView
    }
    func updateUIView(_ uiView: ARView, context: Context) {
    }
}

备注:制作一个 reality 或 usdz 文件,将文件导入程序。
目录:RealityKit AR教程

3 个赞

小球能够沿着轨道圆周运动吗?如果能,是模型自带的还是代码写动画?

您好,关于这个问题我是这样回答的:是模型自带动画。您直接使用模型的绑定动画是一个比较简单的问题,而通过代码设置路径,则需要您更加了解编程知识,而且RealityKit 对这方面的支持还不够友好,参考资料少。

1 个赞