如何在ARKit中自定义对象(.scn)放置在水龙头上?

时间:2019-08-03 18:31:53

标签: ios swift scenekit arkit

当用户点击屏幕时,我试图在ARKit中放置自定义对象。我想出了如何通过在代码中创建对象来放置对象的方法,但是我想放置一个已导入到项目中的对象(例如ship.scn)。这是我的代码,用于将对象放入代码及其工作中:

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touch = touches.first else {return}
        let results = sceneView.hitTest(touch.location(in: sceneView), types: [ARHitTestResult.ResultType.featurePoint])
        guard let hitFeature = results.last  else { return}
        let hitTransform = SCNMatrix4.init(hitFeature.worldTransform)

        let hitPosition = SCNVector3Make(hitTransform.m41, hitTransform.m42, hitTransform.m43)
      createBall(hitPosition: hitPosition)
    }


  func createBall(hitPosition: SCNVector3) {
        let newBall = SCNSphere(radius: 0.05)
        let newBallNode = SCNNode(geometry: newBall)
        newBallNode.position = hitPosition
        self.sceneView.scene.rootNode.addChildNode(newBallNode) 
    }

我尝试创建此功能来放置导入的.scn文件,但是当我点击屏幕时,什么都没有显示:

func createCustomScene(objectScene: SCNScene?, hitPosition: SCNVector3) {
    guard let scene = objectScene else {
        print("Could not load scene")
        return
    }

    let childNodes = scene.rootNode.childNodes

    for childNode in childNodes {
        sceneNode.addChildNode(childNode)
    }
}

这样称呼:createCustomScene(objectScene: SCNScene(named: "art.scnassets/ship.scn"), hitPosition: hitPosition)

有没有人知道为什么在点击屏幕时为什么不显示.scn?

1 个答案:

答案 0 :(得分:0)

您应该考虑将childNode添加到sceneView中,而不是将其添加到sceneNode中。

for childNode in childNodes {
      childNode.position = hitPosition
      self.sceneView.scene.rootNode.addChildNode(childNode)
}