无法将节点放在ARKit的原点

时间:2018-08-25 20:27:24

标签: sprite-kit arkit

当我尝试添加相对于摄像机当前帧或单位矩阵的节点时,它们会出现在场景中:

if let currentFrame = sceneView.session.currentFrame {
  let identity = "1️⃣"
  let identityTransform = matrix_identity_float4x4 // i.e. simd_float4x4([[1.0, 0.0, 0.0, 0.0)], [0.0, 1.0, 0.0, 0.0)], [0.0, 0.0, 1.0, 0.0)], [0.0, 0.0, 0.0, 1.0)]])
  sceneView.session.add(anchor: LabelAnchor(string: identity, transform: identityTransform))

  let camera = ""
  let cameraTransform = currentFrame.camera.transform // e.g. simd_float4x4([[0.333257, -0.936176, -0.111876, 0.0)], [-0.265663, 0.0206128, -0.963846, 0.0)], [0.904635, 0.350929, -0.241838, 0.0)], [0.19599, 0.0331948, -0.0420802, 1.0)]])
  sceneView.session.add(anchor: LabelAnchor(string: camera, transform: cameraTransform))
}

当我删除标识节点(以防它明显地阻塞其他节点)并在原点添加一个节点时,它不会出现在场景中:

  let origin = ""
  let originTransform = matrix_float4x4() // i.e. simd_float4x4([[0.0, 0.0, 0.0, 0.0)], [0.0, 0.0, 0.0, 0.0)], [0.0, 0.0, 0.0, 0.0)], [0.0, 0.0, 0.0, 0.0)]])
  sceneView.session.add(anchor: LabelAnchor(string: origin, transform: originTransform))

请注意,它在测试中仅出现10次以上。

如何在场景的原点添加节点并使它可见?

(注意:一个猜测是原点可能实际上是matrix_identity_float4x4,但是在哪里可以找到解释此问题的文档?)


这是我的项目设置和其他代码:

我从使用SpriteKit的ARKit的默认Xcode模板开始。

我将配置更改为:

configuration.worldAlignment = .gravityAndHeading

我创建了一个简单的LabelNode来显示不同节点的不同字符串:

class LabelAnchor: ARAnchor {
  let string: String

  init(string: String, transform: matrix_float4x4) {
    self.string = string
    super.init(transform: transform)
  }

  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

  var skLabelNode: SKLabelNode {
    let node = SKLabelNode(text: string)
    node.horizontalAlignmentMode = .center
    node.verticalAlignmentMode = .center
    return node
  }
}

并调整视图(_ :, nodeFor:):

  func view(_ view: ARSKView, nodeFor anchor: ARAnchor) -> SKNode? {
    // Create and configure a node for the anchor added to the view's session.

    switch anchor {
    case let anchor as LabelAnchor:
      return anchor.skLabelNode
    default:
      let labelNode = SKLabelNode(text: "")
      labelNode.horizontalAlignmentMode = .center
      labelNode.verticalAlignmentMode = .center
      return labelNode;
    }
  }

0 个答案:

没有答案