Figuring out if Anchor is visible in current screen

柔情痞子 提交于 2019-12-22 14:02:11

问题


I'm using ARCore to build my android app, where I allowing users to place anchors. I need to be able to check if the Anchor is in the current frame. Any idea how can I do it? Thanks!


回答1:


If you're using ARCore, you're probably doing frustum culling where you don't render objects that aren't within the viewable space, an optimization used to stop you from making gl calls to render "unviewable" elements of your scene.

If you have access to the objects after the renderer calculates this, then you can use that value.

Another way you can do this, is by grabbing the Camera and getting the View and Projection matrices. Then you can project the anchor coordinates onto 2D screen coordinates and if the calculated coordinates are outside the screen (ie. x/y values are > or < the screen width/height). You'll have to account for objects that are behind the camera too (the dot product between the camera forward and vector from camera to anchor should be positive).

https://developers.google.com/ar/reference/java/com/google/ar/core/Camera.html.




回答2:


There is a quite simple way to do this. Let's say you have an AnchorNode attached to your anchor.

First, get the node world position:

val worldPosition = node.worldPosition

Second, use scene camera to transform world position into a screen point:

val screenPoint = arFragment.arSceneView.scene.camera.worldToScreenPoint(worldPosition)

Now just check whether the point is inside screen size bounds.




回答3:


I created a method based on camera.worldToScreenPoint(worldPosition). So I can check if a position is visible:

fun OriginalCamera.isWorldPositionVisible(worldPosition: Vector3): Boolean {
    val var2 = com.google.ar.sceneform.math.Matrix()
    com.google.ar.sceneform.math.Matrix.multiply(projectionMatrix, viewMatrix, var2)
    val var5: Float = worldPosition.x
    val var6: Float = worldPosition.y
    val var7: Float = worldPosition.z
    val var8 = var5 * var2.data[3] + var6 * var2.data[7] + var7 * var2.data[11] + 1.0f * var2.data[15]
    if (var8 < 0f) {
        return false
    }
    val var9 = Vector3()
    var9.x = var5 * var2.data[0] + var6 * var2.data[4] + var7 * var2.data[8] + 1.0f * var2.data[12]
    var9.x = var9.x / var8
    if (var9.x !in -1f..1f) {
        return false
    }

    var9.y = var5 * var2.data[1] + var6 * var2.data[5] + var7 * var2.data[9] + 1.0f * var2.data[13]
    var9.y = var9.y / var8
    return var9.y in -1f..1f
}

(And I fixed the problem that Anton Stukov said in the comments)



来源:https://stackoverflow.com/questions/49439991/figuring-out-if-anchor-is-visible-in-current-screen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!