gpt4 book ai didi

swift - 创建一个类似于 Apple 的焦点方 block 的对象放置指示器

转载 作者:行者123 更新时间:2023-11-28 07:46:21 26 4
gpt4 key购买 nike

在我的第一个 ARKit 项目中,我使用水平面检测将 3d 模型放置在水平面上。

我正在尝试使用焦点方 block ,但作为 Swift 新手,我不知道如何使用它。所以我想创建自己的静态指标。

我的方法是创建一个 SCNPlane 作为指标。但是,我的 SCNPlane 不像 Apple 的焦点方 block 那样快速移动,因此我需要代码方面的帮助。

这是我的代码:

func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
guard let planeAnchor = anchor as? ARPlaneAnchor else { return }

// Create a SceneKit plane to visualize the node using its position and extent.
let plane = SCNPlane(width: CGFloat(0.1), height: CGFloat(0.1))
let planeNode = SCNNode(geometry: plane)
let planeMaterial = SCNMaterial()
planeMaterial.diffuse.contents = UIColor.gray.withAlphaComponent(0.3)
plane.materials = [planeMaterial]
planeNode.position = SCNVector3Make(planeAnchor.center.x, 0, planeAnchor.center.z)

// SCNPlanes are vertically oriented in their local coordinate space.
// Rotate it to match the horizontal orientation of the ARPlaneAnchor.
planeNode.transform = SCNMatrix4MakeRotation(-Float.pi / 2, 1, 0, 0)

// ARKit owns the node corresponding to the anchor, so make the plane a child node.
node.addChildNode(planeNode)
}

func renderer(_ renderer: SCNSceneRenderer, didUpdate node: SCNNode, for anchor: ARAnchor) {
guard let planeAnchor = anchor as? ARPlaneAnchor,
let planeNode = node.childNodes.first,
let plane = planeNode.geometry as? SCNPlane
else { return }
let x = CGFloat(planeAnchor.center.x)
let y = CGFloat(planeAnchor.center.y)
let z = CGFloat(planeAnchor.center.z)
planeNode.position = SCNVector3(x, y, z)
}

不确定这是否是正确的方法?如果你们有正确的方法,请告诉我

最佳答案

我想它一定是这样工作的:

class ViewController: UIViewController {

@IBOutlet var sceneView: ARSCNView!
var focusSquare = FocusSquare() // custom class instantiation

// ............................

var session: ARSession {
return sceneView.session
}
override func viewDidLoad() {
super.viewDidLoad()
sceneView.delegate = self
sceneView.session.delegate = self

sceneView.scene.rootNode.addChildNode(focusSquare)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
resetTracking()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
session.pause()
}
func resetTracking() {
virtualObjectInteraction.selectedObject = nil
let configuration = ARWorldTrackingConfiguration()
configuration.planeDetection = [.horizontal, .vertical]
session.run(configuration, options: [.resetTracking, .removeExistingAnchors])
statusViewController.scheduleMessage("PLEASE, FIND A SURFACE", inSeconds: 5.0, messageType: .planeEstimation)
}
func updateFocusSquare(isObjectVisible: Bool) {
if isObjectVisible {
focusSquare.hide()
} else {
focusSquare.unhide()
statusViewController.scheduleMessage("MOVE LEFT-RIGHT", inSeconds: 5.0, messageType: .focusSquare)
}
if let camera = session.currentFrame?.camera, case .normal = camera.trackingState,
let result = self.sceneView.smartHitTest(screenCenter) {
updateQueue.async {
self.sceneView.scene.rootNode.addChildNode(self.focusSquare)
self.focusSquare.state = .detecting(hitTestResult: result, camera: camera)
}
addObjectButton.isHidden = false
statusViewController.cancelScheduledMessage(for: .focusSquare)
} else {
updateQueue.async {
self.focusSquare.state = .initializing
self.sceneView.pointOfView?.addChildNode(self.focusSquare)
}
addObjectButton.isHidden = true
}
}
}

您可以下载Apple 的示例项目here ,您可以在其中了解如何创建 FocusSquare() 类。

关于swift - 创建一个类似于 Apple 的焦点方 block 的对象放置指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50902014/

26 4 0
文章推荐: html - Firebug 显示的
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com