【问题标题】:SceneKit: z value of tap location?SceneKit:点击位置的z值?
【发布时间】:2020-11-29 10:56:33
【问题描述】:

我在我的项目中使用sceneKit,并想在用户点击人体3d模型的位置添加一个红色球体(作为标记)(见下图)。使用我目前的代码,将球体添加到正确的位置 - 但是,它没有添加到人体顶部,而是非常靠近相机(z 值关闭)。如何更改红色球体的 z 值,使其添加到人体顶部而不是相机前面?非常感谢:)

  import UIKit
  import QuartzCore
  import SceneKit
      
   class GameViewController: UIViewController {
        
    var selectedNode: SCNNode!
    var markerSphereNode: SCNNode!
    var bodyNode: SCNNode!
    @IBOutlet weak var sceneView: SCNView!
    override func viewDidLoad() {
        super.viewDidLoad()
        let scene = SCNScene(named: "art.scnassets/femaleBodySceneKit Scene.scn")
        markerSphereNode = scene?.rootNode.childNode(withName: "markerSphere", recursively: true)
        bodyNode = scene?.rootNode.childNode(withName: "Body_M_GeoRndr", recursively: true)
        
        sceneView.scene = scene
        sceneView.allowsCameraControl = true
        _ = sceneView.pointOfView
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first!
        let touchPoint = touch.location(in: sceneView)
        
        if sceneView.hitTest(touch.location(in: sceneView), options: nil).first != nil {
            markerSphereNode.position = sceneView.unprojectPoint(
                SCNVector3(x: Float(touchPoint.x),
                           y: Float(touchPoint.y),
                           z: 0.56182814))
        }
    }
}

当您点击身体的某个位置时,红色球体的显示方式

旋转相机时它离身体多远(如果仔细观察,它在背景中

红点应该如何出现

【问题讨论】:

    标签: swift xcode scenekit arkit skscene


    【解决方案1】:

    SCNHitTestResult 已经为您提供了localCoordinatesworldCoordinates。有必要使用unprojectPoint方法。

    【讨论】:

      猜你喜欢
      • 2019-06-21
      • 2015-06-09
      • 2017-02-07
      • 2016-10-13
      • 2022-07-07
      • 1970-01-01
      • 2018-11-01
      • 2014-11-24
      • 2017-12-03
      相关资源
      最近更新 更多