Untitled

 avatar
unknown
plain_text
5 months ago
9.0 kB
3
Indexable
 val bundle = intent.extras
        if (bundle != null) {
            latLongPoints = bundle?.getParcelableArrayList("route_points")!!

        } else {
            Log.e("ARActivity", "No bundle found!")
        }


        setContent {
            var currentLatLng by remember { mutableStateOf<LatLng?>(null) }

            // Get current location and update the state
//            LaunchedEffect(Unit) {
//                if(!locationgot){
//                    getCurrentLocation { location ->
//                        currentLatLng = location
//                        locationgot = true
//                    }
//                }
//
//            }

            Box(modifier = Modifier.fillMaxSize()) {
                // Only show AR scene once we have the location
                currentLatLng?.let { latLng ->
//        ofc latlong
                   destinationLatLng = LatLng(17.4373905,78.3865911)
//      hostel latlong
             //      destinationLatLng = LatLng(17.4479518,78.3829899)
                    latLongPoints.add(destinationLatLng!!)
                    latLongPoints.add(currentLatLng!!)
                    ARSceneContent(latLongPoints)
                }

                // Exit button at bottom-right corner
                Button(
                    onClick = {
                        // Exit current activity
//                        finish()
                        val intent: Intent = Intent(this@ArCore, MainActivity::class.java)
                        startActivity(intent)
                    },
                    modifier = Modifier
                        .align(Alignment.BottomEnd)
                        .padding(16.dp)
                ) {
                    Text(text = "Exit")
                }
            }
        }
 // Function to get the current location
    private fun getCurrentLocation(onLocationRetrieved: (LatLng) -> Unit) {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            fusedLocationClient.lastLocation.addOnSuccessListener { location: Location? ->
                location?.let {
                    val latLng = LatLng(it.latitude, it.longitude)
                    onLocationRetrieved(latLng)
//                    Log.d("tag", "getCurrentLocation: "+latLng)
                }
            }
        }
    }
    companion object {
        const val LOCATION_PERMISSION_REQUEST_CODE = 1001
    }
    val anchorNodeMap = mutableMapOf<LatLng, AnchorNode>()

    @Composable
    fun ARSceneContent(latlongpointslist: ArrayList<LatLng>) {
        var currentTargetIndex by remember { mutableStateOf(0) }
        val currentLatLng = remember { mutableStateOf<LatLng?>(null) }

        LaunchedEffect(Unit) {
            getCurrentLocation { location ->
                currentLatLng.value = location
            }
        }
        Box(modifier = Modifier.fillMaxSize()) {
            currentLatLng.value?.let { userLocation ->
                // Trigger recomposition when currentTargetIndex changes
                DisplaySequentialArrows(
                    userLocation = userLocation,
                    latLongPoints = latLongPoints,
                    currentTargetIndex = currentTargetIndex,
                    onNextTarget = { currentTargetIndex++ }
                )
            }
            val engine = rememberEngine()
            val modelLoader = rememberModelLoader(engine)
            val materialLoader = rememberMaterialLoader(engine)
            val cameraNode = rememberARCameraNode(engine)
            val childNodes = rememberNodes()
            val view = rememberView(engine)
            val collisionSystem = rememberCollisionSystem(view)

            var planeRenderer by remember { mutableStateOf(true) }

            var trackingFailureReason by remember {
                mutableStateOf<TrackingFailureReason?>(null)
            }
            var frame by remember { mutableStateOf<Frame?>(null) }
            var destinationAnchor: Anchor? = null
            var currentAnchor: Anchor? = null
            // Persistent list to hold all arrow nodes
            val arrowNodes = mutableListOf<AnchorNode>()
            var anchoradded = false
            ARScene(
                modifier = Modifier.fillMaxSize(),
                childNodes = childNodes,
                engine = engine,
                view = view,

                modelLoader = modelLoader,
                collisionSystem = collisionSystem,
                sessionConfiguration = { session, config ->
                    config.depthMode =
                        when (session.isDepthModeSupported(Config.DepthMode.AUTOMATIC)) {
                            true -> Config.DepthMode.AUTOMATIC
                            else -> Config.DepthMode.DISABLED
                        }
                    config.instantPlacementMode = Config.InstantPlacementMode.LOCAL_Y_UP
                    config.lightEstimationMode =
                        Config.LightEstimationMode.ENVIRONMENTAL_HDR
                    config.geospatialMode = Config.GeospatialMode.ENABLED
                },
                cameraNode = cameraNode,
                planeRenderer = planeRenderer,
                onTrackingFailureChanged = {
                    trackingFailureReason = it
                },
                onViewCreated = {

//                    val earth = session?.earth
//                    if (earth?.earthState == Earth.EarthState.ENABLED) {
//                        latlongpointslist.forEach { latLng ->
//
//                            val anchor = createDestinationAnchor(earth, latLng)
//                            if (anchor != null) {
//                                childNodes += createAnchorNode(
//                                    engine = engine,
//                                    modelLoader = modelLoader,
//                                    materialLoader = materialLoader,
//                                    anchor = anchor
//                                )
//                            }
//                            Log.d("ARCore", "Earth state: ${earth.earthState}")
//                            Log.d("ARCore", "Anchor created for LatLng: $latLng")
//                            Log.d("ARCore", "Node added for anchor: ${anchor!!.trackingState}")
//                        }
//                    }
                },

              onSessionUpdated = {session: Session, updatedFrame: Frame ->

                  frame = updatedFrame
                    val earth = session.earth
                    if (earth?.earthState == Earth.EarthState.ENABLED) {

                        latlongpointslist.forEach { latLng ->
//                            destinationAnchor?.detach()
//                            destinationAnchor = null
//                            if (destinationAnchor == null) {
                            Log.d("tag", "anchorNodeMap:= "+anchorNodeMap)
                            if (!anchorNodeMap.containsKey(latLng)) {
                             val anchorn =   createDestinationAnchor(earth, latLng)
                               if(anchorn != null){
                                   val newnode = createAnchorNode(
                                       engine = engine,
                                       modelLoader = modelLoader,
                                       materialLoader = materialLoader,
                                       anchor = anchorn
                                   )
                                   childNodes += newnode
                                   anchorNodeMap[latLng] = newnode
                               }else{
                                   Log.d("tag", "anchorn:null ")
                               }
                            }else{

                                Log.d("tag", "anchorNodeMap:same ")
                            }
                        }
                }
              },

              onSessionCreated = {session: Session ->

//         destinationAnchor?.detach()
//         destinationAnchor = null
//         val earth = session.earth
//         if (earth?.earthState == Earth.EarthState.ENABLED) {
//
//             if (destinationAnchor == null) {

//                 destinationAnchor =
//                     createDestinationAnchor(earth, destinationLatLng!!)?.also { anchor ->
//                         childNodes += createAnchorNode(
//                             engine = engine,
//                             modelLoader = modelLoader,
//                             materialLoader = materialLoader,
//                             anchor = anchor
//                         )
//                     }
//             }
//         }
     }
            )
        }
    }
Editor is loading...
Leave a Comment