如何将SCNFloor放置在真实地板上?

问题描述 投票:0回答:1

我想在实际地板所在的位置添加一个scnfloor,但是它出现在上方,并且在添加childNode时,该对象不会躺在地板上,而是看起来像在飞翔。

[记录添加的节点或平面锚的位置时,无论扫描地板还是桌子,我总是得到y = 0

我想念的是什么?还是不可能更改地板的y?但是在文档中写成

“ ...其局部坐标空间”

,所以我认为有可能。

谢谢

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
    guard let planeAnchor = anchor as? ARPlaneAnchor else {return}
    let anchorX = planeAnchor.center.x
    let anchorY = planeAnchor.center.y
    let anchorZ = planeAnchor.center.z

    print("planeAnchor.center %@", planeAnchor.center) //y always 0
    print("node.position %@", node.position) //y always 0
    print("convert %@", node.convertPosition(SCNVector3(anchorX, anchorY, anchorZ), to: nil)) //y always 0

    let floor = SCNFloor()
    let floorNode = SCNNode(geometry: floor)
    floorNode.position = SCNVector3(anchorX, anchorY, anchorZ)
    let floorShape = SCNPhysicsShape(geometry: floor, options: nil)
    floorNode.physicsBody = SCNPhysicsBody(type: .static, shape: floorShape)
    node.addChildNode(floorNode)
    let childNode = createChildNode()
    floorNode.addChildNode(childNode)
}
scenekit arkit scnnode aranchor
1个回答
0
投票

锚的center.y值将始终为零,此处有更多信息:https://developer.apple.com/documentation/arkit/arplaneanchor/2882056-center

基本上,锚转换将改为提供所检测平面的正确位置。因此,您只需将SCNFloor实例添加为传入节点的子代,它将呈现在正确的位置,因此无需像上面所做的那样显式设置位置。

当ARKit细化平面的边界时,随着ARPlaneAnchor范围的变化,您将需要更新几何尺寸。一些更多信息在这里:https://developer.apple.com/documentation/arkit/tracking_and_visualizing_planes

例如,下面的代码对我有用,以最初检测到的平面大小渲染SCNFloor,并渲染一个恰好位于其顶部的框。平面准确地放置在地板上(或您检测到的任何水平表面):

  func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
    guard let planeAnchor = anchor as? ARPlaneAnchor else {
      return
    }

    let floor = SCNFloor()
    let floorNode = SCNNode(geometry: floor)
    floor.length = CGFloat(planeAnchor.extent.z)
    floor.width = CGFloat(planeAnchor.extent.x)
    node.addChildNode(floorNode)

    let box = SCNBox(width: 0.05, height: 0.05, length: 0.05, chamferRadius: 0)
    box.materials[0].diffuse.contents = UIColor.red
    let boxNode = SCNNode(geometry: box)
    boxNode.position = SCNVector3(0, 0.025, 0)

    node.addChildNode(boxNode)
  }
© www.soinside.com 2019 - 2024. All rights reserved.