Reputation: 65
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!
Upvotes: 3
Views: 2638
Reputation: 71
I created a method based on camera.worldToScreenPoint(worldPosition)
. So I can check if a position is visible:
fun com.google.ar.sceneform.Camera.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)
Upvotes: 3
Reputation: 1335
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.
Upvotes: 1
Reputation: 1527
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.
Upvotes: 0