一、程序效果:

 

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

【文件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 文件,将文件导入程序。

目录:https://docs.qq.com/mind/DTE5ScEFsRW5yQklw

Logo

GitCode AI社区是一款由 GitCode 团队打造的智能助手,AI大模型社区、提供国内外头部大模型及数据集服务。

更多推荐