繁体   English   中英

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

[英]How to place a custom object (.scn) in ARKit on tap?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM