【发布时间】:2020-03-18 14:19:22
【问题描述】:
我在寻找什么?
我的要求的简单解释是这样的
- 使用 ARKit,使用 iPhone 摄像头检测物体
- 在此虚拟空间中查找此对象的位置
- 使用 SceneKit 在这个虚拟空间上放置一个 3D 对象。 3D 对象应位于 标记。
一个例子是使用相机检测 3D 空间中的小图像/标记位置,在虚拟空间中的该标记后面放置另一个 3D 球模型(这样球将对用户隐藏,因为标记/图像在前)
到目前为止我能做什么?
- 我能够使用 ARKit 检测标记/图像
- 我可以在屏幕上放置一个球的 3D 模型。
我的问题是什么?
我无法将球定位在检测到的标记后面。
当球在标记前面时,球正确地隐藏了标记。您可以在侧视图中看到球在标记的前面。见下文
但是当球在标记后面时,不会发生相反的情况。球总是看到前面挡住了标记。我预计标记会隐藏球。所以场景不尊重球位置的 z 深度。见下文
代码
请同时查看 cmets
override func viewDidLoad() {
super.viewDidLoad()
sceneView.delegate = self
sceneView.autoenablesDefaultLighting = true
//This loads my 3d model.
let ballScene = SCNScene(named: "art.scnassets/ball.scn")
ballNode = ballScene?.rootNode
//The model I have is too big. Scaling it here.
ballNode?.scale = SCNVector3Make(0.1, 0.1, 0.1)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
//I am trying to detect a marker/image. So ImageTracking configuration is enough
let configuration = ARImageTrackingConfiguration()
//Load the image/marker and set it as tracking image
//There is only one image in this set
if let trackingImages = ARReferenceImage.referenceImages(inGroupNamed: "Markers",
bundle: Bundle.main) {
configuration.trackingImages = trackingImages
configuration.maximumNumberOfTrackedImages = 1
}
sceneView.session.run(configuration)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
sceneView.session.pause()
}
func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
let node = SCNNode()
if anchor is ARImageAnchor {
//my image is detected
if let ballNode = self.ballNode {
//for some reason changing the y position translate the ball in z direction
//Positive y value moves it towards the screen (infront the marker)
ballNode.position = SCNVector3(0.0, -0.02, 0.0)
//Negative y value moves it away from the screen (behind the marker)
ballNode.position = SCNVector3(0.0, -0.02, 0.0)
node.addChildNode(ballNode)
}
}
return node
}
如何让场景尊重z位置?或者换句话说,如何在使用 ARKit 框架检测到的图像/标记后面显示 3D 模型?
我正在使用 Xcode 10.3 针对 iOS 12 运行。让我知道是否需要任何其他信息。
【问题讨论】:
标签: ios swift scenekit augmented-reality arkit