Untitled

 avatar
unknown
plain_text
5 months ago
3.2 kB
3
Indexable
 onSessionCreated = { session ->
                    val earth = session.earth
                    if (earth?.earthState == Earth.EarthState.ENABLED) {
                        // Iterate over your predefined lat-long list and create anchors
                        latlongpointslist.forEach { point ->
                            val latLng = LatLng(point.latitude, point.longitude)
                            
                                val anchor = createDestinationAnchor(earth, latLng)
                                anchor?.let {
                                    val anchorNode = createAnchorNode(engine, modelLoader, materialLoader, it)
                                    childNodes += anchorNode
                                    arrowNodes.add(anchorNode)
                                    Log.d("ARScene", "Arrow added at Lat=${point.latitude}, Lng=${point.longitude}")
                                }
                        }
                    } else {
                        Log.e("ARScene", "Earth state is not enabled. Cannot create anchors.")
                    }
                },
 fun createDestinationAnchor(earth: Earth?, destinationLatLng: LatLng): Anchor? {
        if (earth?.earthState != Earth.EarthState.ENABLED) {
            Log.e("Anchor", "Earth is not enabled")
            return null
        }
        val latitude = destinationLatLng.latitude
        val longitude = destinationLatLng.longitude
        val altitude = earth.cameraGeospatialPose.altitude - 1.5 // Adjust altitude for indoor/outdoor

        return earth.createAnchor(
            longitude,
            altitude,
            latitude,
            0.0f, 0.0f, 0.0f, 1.0f // Orientation quaternion (default facing)
        ).also {
            Log.d("Anchor", "Created anchor at Lat=$latitude, Lng=$longitude, Alt=$altitude")
        }
    }

// Placing arrow on the anchor position
    fun createAnchorNode(
        engine: Engine,
        modelLoader: ModelLoader,
        materialLoader: MaterialLoader,
        anchor: Anchor
    ): AnchorNode {

        val anchorNode = AnchorNode(engine = engine, anchor = anchor)
        val modelNode = ModelNode(
            modelInstance = modelLoader.createModelInstance(kModelFile),
            // Scale to fit in a 0.5 meters cube
            scaleToUnits = 0.3f
        ).apply {
            // Model Node needs to be editable for independent rotation from the anchor rotation
            isEditable = true
            editableScaleRange = 0.2f..0.75f
        }
        val boundingBoxNode = CubeNode(
            engine,
            size = modelNode.extents,
            center = modelNode.center,
            materialInstance = materialLoader.createColorInstance(Color.White.copy(alpha = 0.5f))
        ).apply {
            isVisible = false
        }
        modelNode.addChildNode(boundingBoxNode)
        anchorNode.addChildNode(modelNode)

        listOf(modelNode, anchorNode).forEach {
            it.onEditingChanged = { editingTransforms ->
                boundingBoxNode.isVisible = editingTransforms.isNotEmpty()
            }
        }
        return anchorNode
    }
Editor is loading...
Leave a Comment