I'm using seceneview and ARSceneView to render an AR experience. As part of that, I want to render text and an image to an anchor. I'm trying the following code, adopted from one of their sample apps (which works when rendering a model file:
fun addAnchorNode(anchor: Anchor) {
sceneView.addChildNode(
AnchorNode(sceneView.engine, anchor)
.apply {
isEditable = true
lifecycleScope.launch {
val view = LayoutInflater.from(this@MainActivity).inflate(R.layout.marker, null)
val attachmentManager =
ViewAttachmentManager(this@MainActivity, sceneView)
attachmentManager.addView(view)
addChildNode(ViewNode(sceneView.engine, sceneView.modelLoader, attachmentManager))
}
anchorNode = this
}
)
}
}
Normal sceneview (the non-AR one) has a much easier looking to use ViewNode that takes a View as a parameter. Am I missing something like that here? Or am I maybe using the wrong ownerView for the ViewAttachementManager?
The code is compiling and running, but nothing is rendering. Replacing the view code with a model load does result in a rendering model.
Edit:
I'm now trying the much less verbose:
val attachmentManager =
ViewAttachmentManager(this@MainActivity, sceneView)
val node = ViewNode(sceneView.engine, sceneView.modelLoader, attachmentManager)
node.loadView(this@MainActivity, R.layout.marker, onLoaded = { instance, view ->
attachmentManager.addView(view)
})
I've also tried without the onLoad, and even tried making the main sceneView's ViewAttachmentManager public so I can use that directly. Still nothing rendering.
I found a solution:
fun addAnchorNode(anchor: Anchor) {
sceneView.addChildNode(
AnchorNode(sceneView.engine, anchor)
.apply {
isEditable = true
lifecycleScope.launch {
val attachmentManager =
ViewAttachmentManager(this@MainActivity, sceneView)
attachmentManager.onResume()
val node = ViewNode(sceneView.engine, sceneView.modelLoader, attachmentManager)
node.loadView(this@MainActivity, R.layout.marker, onLoaded = { instance, view ->
rootNode.addChildNode(node)
})
anchorNode = this
}
)
}
There are 2 big changes here. First is that you can't add the ViewNode until it has been loaded, or it won't render. The second is that you need to call attachmentManager.onResume, even if you use the attachmentManager used in the sceneView. Without both those changes it won't work.