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