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

SceneView with compose

Open melkonyan-n opened this issue 1 year ago • 0 comments

Hi! Thank you for library. I`m now crearting an ar app with compose and need to add node with custom compose view.

I tried with ViewNode2:

private fun **createViewNode**(context: Context, engine: Engine): ViewNode2 {
    val composeView = ComposeView(context).apply { setContent { RenderableLayout() }}
    return ViewNode2(
        engine = engine,
        windowManager = ViewNode2.WindowManager(context),
        materialLoader = MaterialLoader(engine, context),
        view = composeView
    )
}

my composable:

@Composable
fun RenderableLayout(text: String) {
    MaterialTheme {
        Card(
            modifier = Modifier.size(100.dp),
            shape = MaterialTheme.shapes.medium,
            colors = CardColors(
                containerColor = Color.White,
                contentColor = Color.White,
                disabledContainerColor = Color.Yellow,
                disabledContentColor = Color.Green
            )
        ) {
            Text(
                modifier = Modifier
                    .padding(2.dp)
                    .align(Alignment.CenterHorizontally),
                text = text,
                style = MaterialTheme.typography.bodyLarge.copy(color = Color.Black),
            )
        }
    }
}

used:

...
onGestureListener = rememberOnGestureListener(onSingleTapConfirmed = { motionEvent, node ->
                if (node == null) {
                   frame?.hitTest(motionEvent)?.firstOrNull { hitResult ->
                        AnchorNode(
                            engine = engine,
                            anchor = hitResult.createAnchor()
                        ).also { anchorNode ->
                            childNodes += anchorNode
                            **createViewNode**(context, engine).apply { parent = anchorNode }
                        }
                        true
                   }
                } else {
                    node.parent = null
                }
            })

The best I could achieve:

  • user taps on the screen
  • view is placed. But the view is plain black (I suppose it is not rendered). Can you help me to understand, where is problem and how to fix it?

melkonyan-n avatar Sep 26 '24 14:09 melkonyan-n