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

Instant load mode mode shift

Open bpn-work opened this issue 1 year ago • 0 comments

Hi, I want to load a model instantly where ever the camera is shown. It shows at the required position then shifts towards center . I have added zaxis value to bring it forwards in the camera view. Here is my code flow:

val engine = rememberEngine()
                    val modelLoader = rememberModelLoader(engine)
                    val materialLoader = rememberMaterialLoader(engine)
                    val childNodes = rememberNodes()
                    val view = rememberView(engine)
                    val collisionSystem = rememberCollisionSystem(view)
                    // Store current view that can be used to capture image
                    var arSceneView by remember { mutableStateOf<ARSceneView?>(null) }
                    var trackingFailureReason by remember {
                        mutableStateOf<TrackingFailureReason?>(null)
                    }
                    var frame by remember { mutableStateOf<Frame?>(null) }
                    val scope = rememberCoroutineScope()
                    ARScene(
                        modifier = Modifier.fillMaxSize(),
                        childNodes = childNodes,
                        engine = engine,
                        view = view,
                        modelLoader = modelLoader,
                        collisionSystem = collisionSystem,
                        sessionConfiguration = { session, config ->
                            config.depthMode = Config.DepthMode.DISABLED
                            config.instantPlacementMode = Config.InstantPlacementMode.LOCAL_Y_UP
                            config.lightEstimationMode = Config.LightEstimationMode.DISABLED
                            config.planeFindingMode = Config.PlaneFindingMode.HORIZONTAL_AND_VERTICAL
                        },
                        planeRenderer = false, // Disable plane rendering
                        onTrackingFailureChanged = {
                            trackingFailureReason = it
                        },
                        onSessionUpdated = { session, updatedFrame ->
                            frame = updatedFrame
                            scope.launch {
                                if(frame?.camera?.isTracking == true){

                                    if (childNodes.isEmpty()) {
                                        val modelNode = ModelNode(
                                            modelInstance = modelLoader.createModelInstance(kModelFile),
                                        ).apply {
//                                    scale = Scale(0.7f/size.x)
                                            worldPosition = worldPosition.copy(z=-2f)
                                            // Set the initial position to the anchor's position
                                            // You can set the position if needed, but it defaults to (0, 0, 0) relative to the anchor
                                        }
                                        childNodes.add(modelNode)
                                    }
                                }
                            }
                        },
                    )
                    Text(
                        modifier = Modifier
                            .systemBarsPadding()
                            .fillMaxWidth()
                            .align(Alignment.TopCenter)
                            .padding(top = 16.dp, start = 32.dp, end = 32.dp),
                        textAlign = TextAlign.Center,
                        fontSize = 28.sp,
                        color = Color.White,
                        text = trackingFailureReason?.let {
                            it.getDescription(LocalContext.current)
                        } ?: "tap_anywhere_to_add_model"
                    )
                }
            }
        }
    }

Is there anything wrong here?

bpn-work avatar Aug 02 '24 03:08 bpn-work