gpt4 book ai didi

ios - ARKit 将模型随机放置在现实世界中

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:54:00 26 4
gpt4 key购买 nike

我正在试验 ARKit,并尝试在用户周围放置一些模型。所以我想要的是,当应用程序启动时,它只是在用户周围放置一些模型,以便他需要找到它们。

例如,当他移动 10 米时,我想再次添加一些随机模型。我以为我可以这样做:

 let cameraTransform = self.sceneView.session.currentFrame?.camera.transform
let cameraCoordinates = MDLTransform(matrix: cameraTransform!)

let camX = CGFloat(cameraCoordinates.translation.x)
let camY = CGFloat(cameraCoordinates.translation.y)
let cameraPosition = CGPoint(x: camX, y: camY)
let anchors = self.sceneView.hitTest(cameraPosition, types: [.featurePoint, .estimatedHorizontalPlane])

if let hit = anchors.first {
let hitTransform = SCNMatrix4(hit.worldTransform)
let hitPosition = SCNVector3Make(hitTransform.m41, hitTransform.m42, hitTransform.m43)
self.sceneView.session.add(anchor: ARAnchor(transform: hit.worldTransform))
return Coordinate(hitPosition.x, hitPosition.y, hitPosition.z)
}

return Coordinate(0, 0, 0)
}

问题是有时找不到任何 anchor ,然后我不知道该怎么做。当它找到一些 anchor 时,它会随机放在我身后,而不是在我面前,而是在我身后。我不知道为什么,因为从不转动相机,所以它找不到任何 anchor 。

有没有更好的方法在现实世界中放置随机模型?

最佳答案

要做到这一点,您需要使用 session(_:didUpdate:)委托(delegate)方法:

func session(_ session: ARSession, didUpdate frame: ARFrame) {
guard let cameraTransform = session.currentFrame?.camera.transform else { return }
let cameraPosition = SCNVector3(
/* At this moment you could be sure, that camera properly oriented in world coordinates */
cameraTransform.columns.3.x,
cameraTransform.columns.3.y,
cameraTransform.columns.3.z
)
/* Now you have cameraPosition with x,y,z coordinates and you can calculate distance between those to points */
let randomPoint = CGPoint(
/* Here you can make random point for hitTest. */
x: CGFloat(arc4random()) / CGFloat(UInt32.max),
y: CGFloat(arc4random()) / CGFloat(UInt32.max)
)
guard let testResult = frame.hitTest(randomPoint, types: .featurePoint).first else { return }
let objectPoint = SCNVector3(
/* Converting 4x4 matrix into x,y,z point */
testResult.worldTransform.columns.3.x,
testResult.worldTransform.columns.3.y,
testResult.worldTransform.columns.3.z
)
/* do whatever you need with this object point */
}

它允许您在相机位置更新时放置对象:

Implement this method if you provide your own display for rendering an AR experience. The provided ARFrame object contains the latest image captured from the device camera, which you can render as a scene background, as well as information about camera parameters and anchor transforms you can use for rendering virtual content on top of the camera image.

这里非常重要,您要为hitTest 方法随机选择点,并且该点始终在镜头前。

不要忘记在 hitTest method 中为 CGPoint 使用从 0 到 1.0 的坐标系:

A point in normalized image coordinate space. (The point (0,0) represents the top left corner of the image, and the point (1,1) represents the bottom right corner.)

如果你想每 10 米放置一个物体,你可以保存相机位置(在 session(_:didUpdate:) 方法中)并检查 x+z 坐标已更改足够远,以放置新对象。

注意:

我假设您正在使用世界跟踪 session :

let configuration = ARWorldTrackingSessionConfiguration()
session.run(configuration, options: [.resetTracking, .removeExistingAnchors])

关于ios - ARKit 将模型随机放置在现实世界中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45193078/

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