Implementing Depth API for Collision Detection in Google ARCore Furniture App

38 views Asked by At

I'm developing a furniture app using Google ARCore's ARFragment for model placement. Seeking guidance on using the Depth API to dynamically blur areas where 3D models intersect with real-world objects. Code snippet provided below. How can I integrate the Depth API for collision detection? Specifically, I want to blur parts of the model that collide with the environment. Using Google ARCore with ARFragment. Any help with code snippets or alternative approaches appreciated.

class ChairModelActivity : AppCompatActivity() {
    private lateinit var  binding : ActivityChairModelBinding
    private var arFragment: ArFragment? = null
    private var currentModel: TransformableNode? = null
    private var selectedFilterView: CardView? = null
    private val modelRenderables = mutableMapOf<Int, ModelRenderable>()
    private val modelNodes = ArrayList<TransformableNode>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityChairModelBinding.inflate(layoutInflater)
        setContentView(binding.root)

        arFragment = supportFragmentManager.findFragmentById(R.id.sceneform_fragment2) as ArFragment
        val modelPaths = listOf(
            "chair.glb",
            "chair1.glb",
        )

        for ((index, path) in modelPaths.withIndex()) {
            loadModelRenderable(path) { renderable ->
                modelRenderables[index] = renderable
            }
        }
        initOnClickListeners()
    }
    private fun loadModelRenderable(modelPath: String, onComplete: (ModelRenderable) -> Unit) {
        ModelRenderable.builder()
            .setSource(
                this,
                RenderableSource.builder()
                    .setSource(this, Uri.parse(modelPath), RenderableSource.SourceType.GLB)
                    .setRecenterMode(RenderableSource.RecenterMode.ROOT)
                    .build()
            )
            .build()
            .thenAccept { renderable -> onComplete(renderable) }
            .exceptionally { throwable ->
                AlertDialog.Builder(this)
                    .setMessage("Something is not right: ${throwable.message}")
                    .show()
                null
            }

    private fun onTapArPlaneListener(modelRenderable: ModelRenderable?) {
        arFragment?.setOnTapArPlaneListener { hitResult, _, _ ->
            val anchor = hitResult.createAnchor()
            modelRenderable?.let {
                if (anchor != null) {
                    addModel(anchor, it)
                }
            }
        }
    }
    private fun addModel(anchor: Anchor, modelRenderable: ModelRenderable) : TransformableNode {
        currentModel?.let {
            it.setParent(null)
            currentModel = null
        }
        val anchorNode = AnchorNode(anchor)
        anchorNode.setParent(arFragment!!.arSceneView.scene)        
val model = TransformableNode(arFragment!!.transformationSystem)
        model.localPosition = Vector3.zero()
        model.scaleController.maxScale = 1f
        model.scaleController.minScale = 0.5f
        model.setParent(anchorNode)
        model.renderable = modelRenderable
        model.select()
        currentModel = model
        modelNodes.add(model)
        model.setOnTapListener { _, _ ->
            updateScaleInfo(model)
        }
        return model
    }
}```


Seeking guidance on implementing a solution for AR model collisions. I want to use the Depth API to dynamically blur specific areas where 3D models intersect with real-world objects. Currently using Google AR fragment in Google AR Core library in android. Need help with code snippets, addressing errors, and optimizing the blurring process
0

There are 0 answers