代码之家  ›  专栏  ›  技术社区  ›  SwiftyJD

如何将自定义对象(.scn)放置在ARKit中?

  •  0
  • SwiftyJD  · 技术社区  · 7 年前

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

    0 回复  |  直到 7 年前
        1
  •  0
  •   hng    6 年前

    您应该考虑将您的子节点添加到StaveVIEW中,而不是将其添加到ScNeNoDoE中。

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