使用Reality Kit在相机和墙之间显示掩码

发布于 2025-01-26 14:19:44 字数 1244 浏览 4 评论 0原文

我制作了一个视频,以生成一个平面图,如果用户离墙壁太近,或者如果在相机和墙/地板之间出现任何物体,则需要在某个位置捕捉墙壁和地板,然后需要显示<强>太近在该对象上掩盖

我尝试在session(_ session:arsession,didupdate框架:arframe)中使用rycast方法,但我在AR方面非常新,不知道我们需要使用哪种方法。

func session(_ session: ARSession, didUpdate frame: ARFrame) {
        
            
        guard let query = self.arView?.makeRaycastQuery(from: self.arView?.center ?? CGPoint.zero,
                                                  allowing: .estimatedPlane,
                                                  alignment: .any)
        else { return }
        
        guard let raycastResult = self.arView?.session.raycast(query).first
        else { return }
        
        let currentPositionOfCamera = raycastResult.worldTransform.getPosition()
        if currentPositionOfCamera != .zero {
            let distanceFromCamera = frame.camera.transform.getPosition().distanceFrom(position: currentPositionOfCamera)
            print("Distance from raycast:",distanceFromCamera)
            if (distance < 0.5) {
                 print("Too Close")
            }
        }

    } 

I made a video for generating a floor plan in which I need to capture the wall and floor together at a certain position if a user is too near to the wall or if any object come between the camera and wall/floor then need to show Too Close mask on that object something like display in this video.

I try to use rycast in session(_ session: ARSession, didUpdate frame: ARFrame) method but I am very new in AR and not know which method we need to use.

func session(_ session: ARSession, didUpdate frame: ARFrame) {
        
            
        guard let query = self.arView?.makeRaycastQuery(from: self.arView?.center ?? CGPoint.zero,
                                                  allowing: .estimatedPlane,
                                                  alignment: .any)
        else { return }
        
        guard let raycastResult = self.arView?.session.raycast(query).first
        else { return }
        
        let currentPositionOfCamera = raycastResult.worldTransform.getPosition()
        if currentPositionOfCamera != .zero {
            let distanceFromCamera = frame.camera.transform.getPosition().distanceFrom(position: currentPositionOfCamera)
            print("Distance from raycast:",distanceFromCamera)
            if (distance < 0.5) {
                 print("Too Close")
            }
        }

    } 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

双马尾 2025-02-02 14:19:44

I am just learning ARKit and RealityKit as well, but wouldn't your code be:

let currentPositionOfCamera = self.arView.cameraTransform.translation

if currentPositionOfCamera != .zero {

    // distance is defined in simd as the distance between 2 points
    let distanceFromCamera = distance(raycastResult.worldTransform.position, currentPositionOfCamera)
    print("Distance from raycast:",distanceFromCamera)
    if (distanceFromCamera < 0.5) {
        print("Too Close")

        let rayDirection = normalize(raycastResult.worldTransform.position - self.arView.cameraTransform.translation)
        // This pulls the text back toward the camera from the plane
        let textPositionInWorldCoordinates = result.worldTransform.position - (rayDirection * 0.1)

        let textEntity = self.model(for: classification)
        // This scales the text so it is of a consistent size
        textEntity.scale = .one * raycastDistance

        var textPositionWithCameraOrientation = self.arView.cameraTransform
        textPositionWithCameraOrientation.translation = textPositionInWorldCoordinates
        // self.textAnchor is defined somewhere in the class as an optional
        self.textAnchor = AnchorEntity(world: textPositionWithCameraOrientation.matrix)
        textAnchor.addChild(textEntity)
        self.arView.scene.addAnchor(textAnchor)
    } else {
        guard let textAnchor = self.textAnchor else { return }
        self.removeAnchor(textAnchor)
    }
}

// Creates a text ModelEntity 
func tooCloseModel() -> ModelEntity {
        let lineHeight: CGFloat = 0.05
        let font = MeshResource.Font.systemFont(ofSize: lineHeight)
        let textMesh = MeshResource.generateText("Too Close", extrusionDepth: Float(lineHeight * 0.1), font: font)
        let textMaterial = SimpleMaterial(color: classification.color, isMetallic: true)
        let model = ModelEntity(mesh: textMesh, materials: [textMaterial])
        // Center the text
        model.position.x -= model.visualBounds(relativeTo: nil).extents.x / 2
        return model
}

This code is adapted from

I am just learning ARKit and RealityKit as well, but wouldn't your code be:

let currentPositionOfCamera = self.arView.cameraTransform.translation

if currentPositionOfCamera != .zero {

    // distance is defined in simd as the distance between 2 points
    let distanceFromCamera = distance(raycastResult.worldTransform.position, currentPositionOfCamera)
    print("Distance from raycast:",distanceFromCamera)
    if (distanceFromCamera < 0.5) {
        print("Too Close")

        let rayDirection = normalize(raycastResult.worldTransform.position - self.arView.cameraTransform.translation)
        // This pulls the text back toward the camera from the plane
        let textPositionInWorldCoordinates = result.worldTransform.position - (rayDirection * 0.1)

        let textEntity = self.model(for: classification)
        // This scales the text so it is of a consistent size
        textEntity.scale = .one * raycastDistance

        var textPositionWithCameraOrientation = self.arView.cameraTransform
        textPositionWithCameraOrientation.translation = textPositionInWorldCoordinates
        // self.textAnchor is defined somewhere in the class as an optional
        self.textAnchor = AnchorEntity(world: textPositionWithCameraOrientation.matrix)
        textAnchor.addChild(textEntity)
        self.arView.scene.addAnchor(textAnchor)
    } else {
        guard let textAnchor = self.textAnchor else { return }
        self.removeAnchor(textAnchor)
    }
}

// Creates a text ModelEntity 
func tooCloseModel() -> ModelEntity {
        let lineHeight: CGFloat = 0.05
        let font = MeshResource.Font.systemFont(ofSize: lineHeight)
        let textMesh = MeshResource.generateText("Too Close", extrusionDepth: Float(lineHeight * 0.1), font: font)
        let textMaterial = SimpleMaterial(color: classification.color, isMetallic: true)
        let model = ModelEntity(mesh: textMesh, materials: [textMaterial])
        // Center the text
        model.position.x -= model.visualBounds(relativeTo: nil).extents.x / 2
        return model
}

This code is adapted from Apple's Visualizing Scene Semantics.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文