gpt4 book ai didi

ios - 确定 SKNode 是否在相机 View 前面 (ARKit Spritekit)

转载 作者:行者123 更新时间:2023-11-28 11:44:09 29 4
gpt4 key购买 nike

我正在尝试检测相机何时正对着我放置在 ARSKView 中的对象。这是代码:

    override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
guard let sceneView = self.view as? ARSKView else {
return
}

if let currentFrame = sceneView.session.currentFrame {
//let cameraZ = currentFrame.camera.transform.columns.3.z
for anchor in currentFrame.anchors {
if let spriteNode = sceneView.node(for: anchor), spriteNode.name == "token", intersects(spriteNode) {
// token is within the camera view
let distance = simd_distance(anchor.transform.columns.3,
currentFrame.camera.transform.columns.3)
//print("DISTANCE BETWEEN CAMERA AND TOKEN: \(distance)")
if distance <= captureDistance {
// token is within the camera view and within capture distance
print("token is within the camera view and within capture distance")
}
}
}
}
}

问题在于,当对象直接位于相机前面和您的正后方时,intersects 方法都会返回 true。我如何更新此代码,以便它仅检测 spriteNode 何时位于当前相机取景器中?顺便说一下,我使用的是 SpriteKit,而不是 SceneKit。

这是我用来实际创建 anchor 的代码:

        self.captureDistance = captureDistance
guard let sceneView = self.view as? ARSKView else {
return
}

// Create anchor using the camera's current position
if sceneView.session.currentFrame != nil {

print("token dropped at \(distance) meters and bearing: \(bearing)")

// Add a new anchor to the session
let transform = getTransformGiven(bearing: bearing, distance: distance)
let anchor = ARAnchor(transform: transform)
sceneView.session.add(anchor: anchor)
}

func getTransformGiven(bearing: Float, distance: Float) -> matrix_float4x4 {
let origin = MatrixHelper.translate(x: 0, y: 0, z: Float(distance * -1))
let bearingTransform = MatrixHelper.rotateMatrixAroundY(degrees: bearing * -1, matrix: origin)
return bearingTransform
}

最佳答案

我花了一段时间看这个,得出的结论是试图获得 currentFrame.cameraanchor 之间的距离是行不通的仅仅是因为无论 anchor 是在相机前面还是后面,它都会返回相似的值。我的意思是,如果我们假设我们的 anchor 位于 x 点,并且我们向前移动 1 米或向后移动 1 米,那么相机和 anchor 之间的距离仍然是 1 米。

因此,经过一些实验后,我相信我们需要查看以下变量函数,以帮助我们检测我们的SKNode是否在前面相机:

(a) SpriteNode 的 zPosition 指的是:

The z-order of the node (used for ordering). Negative z is "into" the screen, Positive z is "out" of the screen

(b) open func intersects(_ node: SKNode) -> Bool 其中:

Returns true if the bounds of this node intersects with the transformed bounds of the other node, otherwise false.

因此,以下内容似乎完全符合您的需要:

override func update(_ currentTime: TimeInterval) {

//1. Get The Current ARSKView & Current Frame
guard let sceneView = self.view as? ARSKView, let currentFrame = sceneView.session.currentFrame else { return }

//3. Iterate Through Our Anchors & Check For Our Token Node
for anchor in currentFrame.anchors {

if let spriteNode = sceneView.node(for: anchor), spriteNode.name == "token"{

/*
If The ZPosition Of The SpriteNode Is Negative It Can Be Seen As Into The Screen Whereas Positive Is Out Of The Screen
However We Also Need To Know Whether The Actual Frostrum (SKScene) Intersects Our Object
If Our ZPosition Is Negative & The SKScene Doesnt Intersect Our Node Then We Can Assume It Isnt Visible
*/

if spriteNode.zPosition <= 0 && intersects(spriteNode){
print("Infront Of Camera")
}else{
print("Not InFront Of Camera")
}

}
}
}

希望对你有帮助

关于ios - 确定 SKNode 是否在相机 View 前面 (ARKit Spritekit),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52824981/

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