Search code examples
androidarcoresceneform

How to fit 3D models in device screen when display it in 3d view


In my demo project(3D view). When model is display, Some models display large sized(out of screen) and some models are too small sized. What is best scale ratio to fit model in the device screen.

My code is..

     private fun createRenderAble() {
        ModelRenderable.builder().setSource(
            this, RenderableSource.builder()
                .setSource(this, Uri.parse(model?.modelUri), RenderableSource.SourceType.GLB)
                .setRecenterMode(RenderableSource.RecenterMode.CENTER)
                .build()
        ).setRegistryId(model?.modelUri)
         .build()
         .thenAccept { renderable ->
                hideProgress()
                addNodeToScene(renderable)
            }
            .exceptionally {
                showToast(it.localizedMessage)
                hideProgress()
                null
            }
}

 private fun addNodeToScenee(renderable: ModelRenderable?) {
    val tempNode = Node()
    tempNode.renderable = renderable

    val collisionShape: Box = tempNode.collisionShape as Box
   // var radius = 1f

  //  if (collisionShape.size.x > 2.0) {
 //       radius = 3f
  //  }
  //  if (collisionShape.size.y > 1.3) {
   //     radius = 2f
   // }

     radius=collisionShape.size.x
    val node = DragTransformableNode(radius, transformationSystem).apply {
        setParent(sceneView.scene)
        this.renderable = renderable
        select()
    }

    sceneView.scene.addChild(node)
}

enter image description here


Solution

  • You can control this by checking the size of your Renderable. Below is the code that will help you resolve the issue. After setting renderable to node do the below.

            val collisionShape: Box = node.collisionShape as Box
            var radius = 1f
            if (collisionShape.size.x > 2.0) {
                radius = 3f
            }
            if (collisionShape.size.y > 1.3) {
                radius = 2f
            }
    

    or

    val collisionShape: Box = node.collisionShape as Box
     radius=collisionShape.size.x
    

    Explanation: What I'm doing here is checking if renderable has width more than 2.0 meter I'm setting the radius of a camera to 3f. This will help us to accommodate the complete model into the screen. Same goes for the Height(collisionShape.size.y).