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

如何在ARKit中添加多个对象?

  •  -1
  • Sachin  · 技术社区  · 7 年前

    在苹果的AR示例项目中,有一个在房间中放置椅子的选项。在代码中放置多张椅子需要做什么? 一个简单的append函数会起作用吗?

    当我点击椅子选项时,我需要将第一把椅子放置在平面中。如果我再次点击该选项,则应再次放置椅子。我知道我也需要一个删除函数。那么,如何检测用户的长时间点击?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Clay    7 年前

    一个基本的轻触功能,可在每次轻触显示屏时添加一个球。

     @objc func handleTap(_ gesture: UITapGestureRecognizer) {
    
     let results = self.sceneView.hitTest(gesture.location(in: gesture.view), types: ARHitTestResult.ResultType.featurePoint)
    guard let result: ARHitTestResult = results.first else {
        return
    }
    
    // create a simple ball
    let sphereNode = SCNNode(geometry: SCNSphere(radius: 0.2)
    
    // create position of ball based on tap result
    let position = SCNVector3Make(result.worldTransform.columns.3.x, result.worldTransform.columns.3.y, result.worldTransform.columns.3.z)
    
    // set position of ball before adding to scene
    sphereNode?.position = position
    
     // each tap adds a new instance of the ball.
       self.sceneView.scene.rootNode.addChildNode(sphereNode!)
    
      }
    

    如果您需要完整的swift代码才能开始。。。看看这篇早期的帖子 adds a cube.scn from a remote url

    您可以使用

    @objc func longPress(_ gesture: UILongPressGestureRecognizer) {
    
    
    } 
    

    但最好只是检测到你已经点击了你想要移除的现有球体。您可以向上述函数中添加类似的内容。

    let tappedNode = self.sceneView.hitTest(gesture.location(in: gesture.view), options: [:])
    
    if !tappedNode.isEmpty {
      let node = tappedNode[0].node 
      node.removeFromParent()
      } else { 
     // add my new node
    }