2

我正在尝试将 SCNNode 拖动到拖动位置的当前场景中。

var currentBlock: SCNNode?

@objc func handlePan (gesture: UIPanGestureRecognizer) {
    // Location of Pan
    var location = gesture.location(in: self.sceneView)
        
    switch gesture.state {
    case .began:
        // Add Node Reference, add Node to Scene
        if redBlockUI?.frame.contains(location) == true {
            currentBlock = createBlock(color: .red, location: location)
        }
    case .changed:
        // Drag Node Here
        currentBlock?.position = SCNVector3(location.x, location.y, -1)
    case .ended:
        // Remove Node Reference (Don't delete from Scene)
        currentBlock = nil
    default: break
    }
}

func createBlock (color: UIColor, location: CGPoint) -> SCNNode {
    // Make Geometry
    let block: SCNGeometry = SCNBox(width: 10.0, height: 10.0, length: 10.0, chamferRadius: 1.0)
    block.materials.first?.diffuse.contents = color
    // Attach Geometry to SCNNode
    let blockNode = SCNNode(geometry: block)
    blockNode.position = SCNVector3(location.x, location.y, -1)
    // Add to scene
    sceneView.scene.rootNode.addChildNode(blockNode)
    return blockNode
}

但是,该对象不是在此位置创建的。我需要进行某种点转换吗?谢谢!

4

1 回答 1

0

SceneKit 为此包含 2 个函数:将 3D 位置转换为 2D 的 projectPoint 和执行反向转换的 unprojectPoint。 https://developer.apple.com/documentation/scenekit/scnscenerenderer/1524089-projectpoint https://developer.apple.com/documentation/scenekit/scnscenerenderer/1522631-unprojectpoint

于 2020-07-23T17:51:32.710 回答