bkw*_*ero 4 swift swiftui realitykit visionos
举例来说,我想在用户头顶上锚定一朵云。
我知道它AnchorEntity存在,并且您可以使用 来获取对用户头部的引用AnchorEntity(.head)。但我该如何实际使用它呢?使用这段代码我什么也看不到。
import SwiftUI
import RealityKit
struct CloudSpace: View {
let headAnchor = AnchorEntity(.head)
var body: some View {
RealityView { content in
async let cloud = ModelEntity(named: "Cloud")
do {
content.add(headAnchor)
let cloud = try await cloud
headAnchor.addChild(cloud)
} catch {
print(error)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
要查看AnchoringComponent.Target.head实际效果,您可以使用真实的 Vision Pro 设备或 Xcode 15 VisionOS 模拟器。要激活头部的目标(.camera在 iOS 中也称为目标,或DeviceAnchor在 ARKit for VisionOS 中称为对象),请尝试以下代码。但请考虑以下事实:head默认情况下,锚点会自动更新每一帧 \xe2\x80\x93 使用.once跟踪模式来更改它。
import SwiftUI\nimport RealityKit\nimport RealityKitContent\n\nstruct ContentView: View {\n var body: some View {\n VStack {\n RealityView { content in\n\n if let cloud = try? await Entity(named: "Scene",\n in: realityKitContentBundle) {\n\n let anchor = AnchorEntity(.head)\n anchor.anchoring.trackingMode = .once\n cloud.setParent(anchor)\n content.add(anchor)\n \n // Z offset is important\n cloud.transform.translation.z = -1.0 \n cloud.transform.translation.y = 0.25\n anchor.name = "Head Anchor"\n print(content)\n }\n }\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n
| 归档时间: |
|
| 查看次数: |
1461 次 |
| 最近记录: |