gpt4 book ai didi

ios - ARKit - 点击屏幕后物体粘在相机上

转载 作者:行者123 更新时间:2023-11-30 11:17:19 28 4
gpt4 key购买 nike

我从选择 ARKit 项目时获得的模板项目开始。当您运行该应用程序时,您可以看到船舶并从任何角度查看它。

但是,一旦我允许相机控制并点击屏幕或通过平移放大船舶,船舶就会卡在相机上。现在,无论我带着相机走到哪里,船都会粘在屏幕上。

我浏览了Apple Guide并且似乎并不真正认为这是意外行为,因为这种行为没有任何内容。

缩放或触摸屏幕后如何保持船舶位置固定?

最佳答案

好吧,看起来allowsCameraControl根本不是答案。它适用于 SceneKit,但不适用于 ARKit(也许它适用于 AR 中的某些东西,但我还没有意识到)。

为了放大 View ,需要 UIPinchGestureRecognizer。

    // 1. Find the touch location
// 2. Perform a hit test
// 3. From the results take the first result
// 4. Take the node from that first result and change the scale
@objc private func handlePan(recognizer: UIPinchGestureRecognizer) {
if recognizer.state == .changed {
// 1.
let location = recognizer.location(in: sceneView)

// 2.
let hitTestResults = sceneView.hitTest(location, options: nil)

// 3.
if let hitTest = hitTestResults.first {
let shipNode = hitTest.node

let newScaleX = Float(recognizer.scale) * shipNode.scale.x
let newScaleY = Float(recognizer.scale) * shipNode.scale.y
let newScaleZ = Float(recognizer.scale) * shipNode.scale.z

// 4.
shipNode.scale = SCNVector3(newScaleX, newScaleY, newScaleZ)

recognizer.scale = 1
}
}

关于#2。我对另一个名为hitTest(_:types:)hitTest方法感到有点困惑

文档注释

This method searches for AR anchors and real-world objects detected by the AR session, not SceneKit content displayed in the view. To search for SceneKit objects, use the view's hitTest(_:options:) method instead.

因此,如果您想要缩放作为 SceneKit 内容的 node ,则无法使用该方法

关于ios - ARKit - 点击屏幕后物体粘在相机上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51626770/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com