我在寻找什么?
我的要求的简单解释是这样的
- 使用 ARKit,使用 iPhone 相机检测对象
- 找到这个物体在这个虚拟空间上的位置
- 使用 SceneKit 将 3D 对象放置在该虚拟空间上。 3D 对象应该位于
标记。
一个示例是使用相机检测 3D 空间中的小图像/标记位置,在虚拟空间中的该标记后面放置另一个 3D 球模型(因此球将对用户隐藏,因为标记/图像位于前面)
到目前为止我能做什么?
- 我能够使用 ARKit 检测标记/图像
- 我能够在屏幕上放置球 3D 模型。
我的问题是什么?
我无法将球放置在检测到的标记后面。
当球位于标记前面时,球会正确隐藏标记。您可以在侧视图中看到球位于标记的前面。见下文
但当球位于标记后面时,则不会发生相反的情况。球总是在前方阻挡标记。我预计记分员会隐藏球。所以场景不考虑球位置的 z 深度。见下文
Code
也请看一下评论
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 运行。如果需要任何其他信息,请告诉我。
为此,您需要在 3D 场景中创建一个遮挡物。自从ARReferenceImage
has a physicalSize
当场景中添加几何体时,应该很简单ARImageAnchor
被建造。
几何形状将是SCNPlane
with a SCNMaterial
适合封堵器。我会选择一个SCNLightingModelConstant
照明模型(这是最便宜的,我们实际上不会绘制飞机)colorBufferWriteMask
等于SCNColorMaskNone
。该对象应该是透明的,但仍然写入深度缓冲区(这就是它充当遮挡器的方式)。
最后,通过设置遮挡物来确保遮挡物在任何增强对象之前渲染renderingOrder
to -1
(如果应用程序已经使用渲染顺序,则可以使用更低的值)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)