我怎么能ARNode指向一个ARAnchor?
我想art.scnassets/ship.scn在屏幕中央使用显示并指向我刚刚放置在场景中的对象.
/// ViewController Class
func placeObject() {
let screenCentre : CGPoint = CGPoint(x: self.sceneView.bounds.midX, y: self.sceneView.bounds.midY)
guard let hitTestResult = sceneView.hitTest(screenCentre, types: [.featurePoint]).first else { return }
// Place an anchor for a virtual character.
let anchor = ARAnchor(name: identifierString, transform: hitTestResult.worldTransform)
sceneView.session.add(anchor: anchor)
// add to item model
ItemModel.shared.anchors.append((identifierString, anchor)
}
func showDirection(of object: ARAnchor) { // object: saved anchor
if !Guide.shared.isExist {
let startPoint = SCNVector3(0, 0 , -1)
let targetPoint = SCNVector3(object.transform.columns.3.x, object.transform.columns.3.y, object.transform.columns.3.z)
let guideNode = Guide.shared.setPosition(from: startPoint, to: targetPoint)
// add the ship in the center of the view
sceneView.pointOfView?.addChildNode(guideNode)
}
}
/// Guide Class
func setPosition(from start: SCNVector3, to target: SCNVector3) -> SCNNode {
isExist = true
guideNode.position = start
targetPosition = target
// create target node from saved anchor
let desNode = SCNNode()
desNode.position = targetPosition
let lookAtConstraints = SCNLookAtConstraint(target: desNode)
guideNode.constraints = [lookAtConstraints]
return guideNode
}
// MARK: - ARSCNViewDelegate
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
if let name = anchor.name, name.hasPrefix(identifierString) {
// Create 3D Text
let textNode: SCNNode = createNewBubbleParentNode(identifierString)
node.addChildNode(textNode)
}
}
Run Code Online (Sandbox Code Playgroud)
我试过SCNLookAtConstraint但它没有按预期工作,有什么建议吗?
我注意到SCNLookAtConstraint当我的手机方向与地平线平行时,该功能有效。(屏幕朝上)
这行代码发挥了魔力!guideNode.pivot = SCNMatrix4Rotate(guideNode.pivot, Float.pi, 0, 1, 1)
如果您有兴趣,请参阅这里。
public func showDirection(of object: arItemList) {
guard let pointOfView = sceneView.pointOfView else { return }
// remove previous instruction
for node in pointOfView.childNodes {
node.removeFromParentNode()
}
// target
let desNode = SCNNode()
let targetPoint = SCNVector3.positionFromTransform(object.1.transform)
desNode.worldPosition = targetPoint
// guide
let startPoint = SCNVector3(0, 0 , -1.0)
let guideNode = loadObject()
guideNode.scale = SCNVector3(0.7, 0.7, 0.7)
guideNode.position = startPoint
let lookAtConstraints = SCNLookAtConstraint(target: desNode)
lookAtConstraints.isGimbalLockEnabled = true
// Here's the magic
guideNode.pivot = SCNMatrix4Rotate(guideNode.pivot, Float.pi, 0, 1, 1)
guideNode.constraints = [lookAtConstraints]
pointOfView.addChildNode(guideNode)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
250 次 |
| 最近记录: |