当用户点击屏幕时,我试图在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?

最佳答案

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

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

关于ios - 如何在水龙头上的ARKit中放置自定义对象(.scn)?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57341126/

10-10 04:23