sceneview-android icon indicating copy to clipboard operation
sceneview-android copied to clipboard

ViewNode not rendering

Open BorkoCvejic opened this issue 1 year ago • 12 comments

Hi guys,

I want to track an image, and show some text above it. There is an "ar-augmented-image" sample, so I changed the part of the code where a "rabbit" image is detected. Instead of adding ModelNode() I'm adding ViewNode, but for some reason my text is not rendering.

ViewRenderable.builder()
         .setView(sceneView.context, R.layout.view_text)
          .build(sceneView.engine)
          .thenAccept {
                 sceneView.addChildNode(ViewNode(
                        engine = sceneView.engine,
                        modelLoader = sceneView.modelLoader,
                        viewAttachmentManager = viewAttachmentManager
                   ).apply {
                         quaternion = dev.romainguy.kotlin.math.Quaternion.fromEuler(-90f, 0f, 0f)
                         it.view.findViewById<TextView>(R.id.tvContent).text = "TEST TEST TEST"
                         setRenderable(it)
                         isEditable = true
                         scale = Scale(5f, -5f, 5f)
                         Toast.makeText(requireContext(), "IMAGE DETECTED", Toast.LENGTH_SHORT).show()
                    })
           }

In onViewCreated:

sceneView = view.findViewById<ARSceneView>(R.id.sceneView)
viewAttachmentManager = ViewAttachmentManager(requireContext(), sceneView)

and view_text.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tvContent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_purple"
        android:text="DUGACAK TEkST"
        android:textColor="@android:color/black"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

I read in other issue that onViewLoaded might be needed but something like this didn't help as well:

onViewLoaded = { renderableInstance, view ->
     sceneView.addChildNode(this)
}

What am I doing wrong?

BorkoCvejic avatar Jan 11 '24 11:01 BorkoCvejic

augmentedImage.trackingState is in state PAUSED, and it triggers only once, even in sample project, that's unexpected.

BorkoCvejic avatar Jan 15 '24 17:01 BorkoCvejic

What worked in 0.9.5 no longer works in 2.0.2 regarding ViewNodes. In my case the ViewRenderable builder never completes.

ryust avatar Jan 15 '24 22:01 ryust

Seems like it behaves the same on my side with 0.9.5 sadly.

BorkoCvejic avatar Jan 15 '24 22:01 BorkoCvejic

I think there was some unnecessary code in the first post, but this cleaner version is still not working:

val node = ViewNode(
      engine = sceneView.engine,
      modelLoader = sceneView.modelLoader,
      viewAttachmentManager = viewAttachmentManager
 )
      node.apply {
              loadView(
                     requireContext(),
                     R.layout.view_text
              )
      quaternion = dev.romainguy.kotlin.math.Quaternion.fromEuler(-90f, 0f, 0f)
      isEnabled = true
      scale = Scale(5f)
      Toast.makeText(requireContext(), "IMAGE DETECTED", Toast.LENGTH_SHORT).show()
}
sceneView.addChildNode(node)

I noticed there is different ViewNode in ModelViewer sample, but ViewNode which is used with ARSceneView is not working.

BorkoCvejic avatar Jan 25 '24 18:01 BorkoCvejic

@BorkoCvejic Have you called the lifecycle methods on the viewAttachManager object? if not, then you need to add the following code in your activity or fragment's lifecycle methods

   override fun onResume() {
        super.onResume()
        viewAttachmentManager.onResume()
    }

    override fun onPause() {
        super.onPause()
        viewAttachmentManager.onPause()
    }

akhatriST avatar Jan 26 '24 13:01 akhatriST

Thanks for answering, but still no luck.

Here is a whole MainFragment if somebody wants to take a look, there are minimal changes from ar-augmented-image sample.

BorkoCvejic avatar Jan 26 '24 13:01 BorkoCvejic

Here is my ARContentNode implementation

import android.widget.TextView
import com.google.ar.sceneform.rendering.ViewAttachmentManager
import com.google.ar.sceneform.rendering.ViewRenderable
import io.github.sceneview.ar.ARSceneView
import io.github.sceneview.node.ViewNode

class ARContentNode(
    val arscene: ARSceneView,
    val viewAttManager: ViewAttachmentManager,
    val onLoaded: (node: ViewNode) -> Unit
) {

    init {
        renderContent()
    }

    private fun renderContent() {
        ViewRenderable.builder()
            .setView(arscene.context, R.layout.ar_layout)
            .build(arscene.engine).thenAccept { r: ViewRenderable? ->
                val viewNode = ViewNode(
                    engine = arscene.engine,
                    modelLoader = arscene.modelLoader,
                    viewAttachmentManager = viewAttManager
                ).apply {
                    if (r != null) {
                        setRenderable(r)
                        isEditable = true
                    }
                }
                onLoaded(viewNode)
            }
    }
}

and this is how I use the above class

ARContentNode(
                sceneView,
                viewAttachmentManager,
            ) { destNode ->
                rootNode.addChildNode(destNode)
            }

Hope this helps

akhatriST avatar Jan 26 '24 14:01 akhatriST

Thank you so much, it works! No idea why my code doesn't work.

When I try to create AnchorNode with anchor(centerPose) to be the rootNode for your solution it crashes, and when I set sceneView as parent, the view is a bit away and rotated but it renders! :) So, I'll need to figure out some more stuff but your solution renders.

BorkoCvejic avatar Jan 26 '24 14:01 BorkoCvejic

I partially got my issue resolved also. The key is previously I was adding the ViewNode to a ModelNode which was added to the AnchorNode. But if I add both the ViewNode and ModelNode to the AnchorNode, it works. So the issue may be that ViewNodes can't be added to other non-anchor nodes... which was possible before.

ryust avatar Jan 26 '24 15:01 ryust

@ryust How did you create AnchorNode, can you share the code?

BorkoCvejic avatar Jan 26 '24 15:01 BorkoCvejic

I am using the Geospatial API which will create a terrain anchor via "earth.resolveAnchorOnTerrainAsync". Then I create an AnchorNode via "AnchorNode(engine, terrainAnchor).

ryust avatar Jan 26 '24 16:01 ryust

Ok so you are not using centerPose and createAnchor which are crashing for me.

EDIT: From latest library version, the model is not rendering directly on tracked image but far away from it, but that's other, new issue.

BorkoCvejic avatar Jan 26 '24 16:01 BorkoCvejic

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

github-actions[bot] avatar Apr 26 '24 05:04 github-actions[bot]

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.

github-actions[bot] avatar May 04 '24 05:05 github-actions[bot]