ARKit随机放置现实世界中的模型(ARKit place models in the real world randomly)

系统教程 行业动态 更新时间:2024-06-14 16:58:30
ARKit随机放置现实世界中的模型(ARKit place models in the real world randomly)

我正在尝试使用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) }

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

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

I'm experimenting with ARKit and I'm trying to place some models around the user. So what I want is that when the app starts it just places some models around the user so He needs to find them.

When he moves like for example 10 meters I want to add some random models again. I thought I could do it this way:

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) }

The problem is sometimes it doesn't find any anchors and then I don't know what to do. And when it finds some anchors it is randomly placed behind me not in front of me but behind me. I don't know why because never turn the camera so it can't find any anchors.

Is there a better way to place random models in the real world?

最满意答案

为了使它你需要使用session(_:didUpdate :)委托方法:

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 */ }

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

如果您提供自己的显示以呈现AR体验,请实施此方法。 提供的ARFrame对象包含从设备摄像头捕获的最新图像,您可以将其渲染为场景背景,以及有关摄像机参数和锚点变换的信息,可用于在摄像机图像上渲染虚拟内容。

这里非常重要 ,你随机选择了hitTest方法的点,这一点总是在镜头前。

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

标准化图像坐标空间中的一个点。 (点(0,0)表示图像的左上角,点(1,1)表示右下角。)

如果你想每10米放置一个物体,你可以保存摄像机位置(在session(_:didUpdate:)方法)并检查x+z坐标是否已经变得足够远,以放置新物体。

注意:

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

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

To make it you'll need use session(_:didUpdate:) delegate method:

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 */ }

It'll allows you place object whenever camera position updates:

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.

Really important here, that you're randomly choosing point for hitTest method, and this point always will be in front of camera.

Don't forget to use from 0 to 1.0 coordinate system for CGPoint in hitTest method:

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.)

If you want to place object each 10 meters, you can save camera position (in session(_:didUpdate:) method) and check that x+z coordinates was changed for far enough, to place new object.

NOTE:

I'm assuming that you're using world tracking session:

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

更多推荐

本文发布于:2023-04-15 03:32:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/9a10d068532657c823731d972550d52e.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   现实   世界   ARKit   place

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!