我有一个屏幕与AR,此刻.usdz 3D模型存储在本地应用程序中,我们需要确保使用获取请求接收它们。
下面是要检查的URL:https://developer.apple.com/augmented-reality/quick-look/models/vintagerobot2k/robot_walk_idle.usdz
import SwiftUI
import RealityKit
import ARKit
import FocusEntity
struct ARScreen : View {
@StateObject var selectedVerb = globalVerb()
@State private var isPlacementEnabled = false
@State private var selectedModel = "sitdown.usdz"//: String?
@State private var modelComfiredForPlacement: String?
init() {
let navBarAppearance = UINavigationController()
navBarAppearance.navigationBar.largeTitleTextAttributes=[NSAttributedString.Key.foregroundColor:
UIColor.white]
UINavigationBar.appearance().tintColor = .black
}
var body: some View {
NavigationView {
ZStack(alignment: .bottom) {
ARViewContainer(modelConfirmedForPlacement: self.$modelComfiredForPlacement)
PlacementButtonView(isPlacementEnabled: self.$isPlacementEnabled, selectedModel: self.selectedModel, modelComfirmedForPlacement: self.$modelComfiredForPlacement)
}.background(Color("Yellow").ignoresSafeArea())
}.navigationBarTitle("", displayMode: .inline)
}
struct ARViewContainer: UIViewRepresentable {
@Binding var modelConfirmedForPlacement: String?
func makeUIView(context: Context) -> ARView {
let arView = CustomARView(frame: .zero)//ARView(frame: .zero)
return arView
}
func updateUIView(_ uiView: ARView, context: Context) {
if let modelName = self.modelConfirmedForPlacement {
print("Debug: adding model to scene")
let fileName = modelName + ".usdz"
let modelEntity = try! ModelEntity.load(named: "\(UserDefaults().value(forKey: "verb")!).usdz")
let anchorEntity = AnchorEntity(plane: .any)
anchorEntity.addChild(modelEntity)
uiView.scene.addAnchor(anchorEntity)
modelEntity.playAnimation(modelEntity.availableAnimations[0].repeat(),
transitionDuration: 0.5,
startsPaused: false)
DispatchQueue.main.async {
self.modelConfirmedForPlacement = nil
}
}
}
}
class CustomARView: ARView, FEDelegate {
let focusSquare = FESquare()
required init(frame frameRect: CGRect) {
super.init(frame: frameRect)
focusSquare.viewDelegate = self
focusSquare.delegate = self
focusSquare.setAutoUpdate(to: true)
self.setupARView()
}
func setupARView() {
let config = ARWorldTrackingConfiguration()
config.planeDetection = [.horizontal, .vertical]
config.environmentTexturing = .automatic
if ARWorldTrackingConfiguration.supportsSceneReconstruction(.mesh) {
config.sceneReconstruction = .mesh
}
self.session.run(config)
}
@MainActor required dynamic init?(coder decoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
}
struct PlacementButtonView: View {
@Binding var isPlacementEnabled: Bool
var selectedModel: String?
@Binding var modelComfirmedForPlacement: String?
var body: some View {
Button(action: {
print("Debug: Confirm model")
self.modelComfirmedForPlacement = self.selectedModel
}) {
Image(systemName: "checkmark")
.frame(width: 60, height: 60)
.font(.title)
.background(Color.white.opacity(0.75))
.cornerRadius(30)
.padding(20)
}
}
}
#if DEBUG
struct ContentView_Previews : PreviewProvider {
static var previews: some View {
ARScreen()
}
}
#endif
我试图用RealityKit – Load ModelEntity from web URL resource解决这个问题,但没有任何结果
1条答案
按热度按时间zbdgwd5y1#
我有一个swift软件包可以帮你做到这一点,几分钟前我刚刚发布了它的一个版本:
https://github.com/maxxfrazer/RealityToolkit
方法RealityToolkit.loadEntity是异步的,所以你要么使用异步方法,要么将它 Package 在一个Task中,如下所示:
但它应该为您工作,请留下一个问题的回购,如果有任何问题使用它!
正在使用的可以在这里找到。
我可能会把功能移到RealityUI上,而不是将来成为一个独立的Repo。