我试图将SCNNode拖动到拖动位置的当前场景中。这是我的代码:
@objc func handlePan (gesture: UIPanGestureRecognizer) {
var location = gesture.location(in: self.sceneView)
switch gesture.state {
case .began:
if (redBlockUI?.frame.contains(location))! {
currentBlock = createBlock(color: .red, location: location)
}
break;
case .changed:
if currentBlock != nil {
let position = SCNVector3(location.x, location.y, -1)
currentBlock?.position = position
}
case .ended:
currentBlock = nil
default:
break;
}
}
func createBlock (color: UIColor, location: CGPoint) -> SCNNode? {
let block: SCNGeometry = SCNBox(width: 10.0, height: 10.0, length: 10.0, chamferRadius: 1.0)
block.materials.first?.diffuse.contents = color
let blockNode = SCNNode(geometry: block)
blockNode.position = SCNVector3(location.x, location.y, -1)
sceneView.scene.rootNode.addChildNode(blockNode)
return blockNode
}
但是,此位置不会创建对象。我需要进行某种点转换吗?谢谢!
答案 0 :(得分:0)
SceneKit为此提供了2个功能:projectPoint将3D位置转换为2D,unprojectPoint执行反向转换。 https://developer.apple.com/documentation/scenekit/scnscenerenderer/1524089-projectpoint https://developer.apple.com/documentation/scenekit/scnscenerenderer/1522631-unprojectpoint