使用 Sceneform Android 在 3D 模型上绘制可渲染形状

2023-12-02

我在用场景窗体 Android SDK在Android APP中渲染3D模型。

为了显示 3D 模型,我使用下面的代码,它也支持 3D 模型中的旋转和缩放功能

 private fun renderLocalObject(position: Int) {

    ModelRenderable.builder()
            .setSource(appCompatActivity, Uri.parse(models.get(position)))
            .setRegistryId(models.get(position))
            .build()
            .thenAccept { modelRenderable: ModelRenderable ->
                addNodeToScene(modelRenderable)
            }
            .exceptionally { throwable: Throwable? ->
                var message: String?
                message = if (throwable is CompletionException) {
                    Logger.DebugLog("ERROR LOADING 3D", throwable.message)
                    "Internet is not working"
                } else {
                    Logger.DebugLog("ERROR LOADING 3D", "FAILED TO LOAD")
                    "Can't load Model"

                }
                null
            }
}

private fun addNodeToScene(model: ModelRenderable) {

    if (sceneView != null) {

        val transformationSystem = makeTransformationSystem()
        val dragTransformableNode = DragTransformableNode(5f, transformationSystem)
        dragTransformableNode.renderable = model
        sceneView!!.scene.addChild(dragTransformableNode)
        dragTransformableNode.select()


        sceneView!!.getScene()
                .addOnPeekTouchListener { hitTestResult: HitTestResult?, motionEvent: MotionEvent? ->
                    transformationSystem.onTouch(
                            hitTestResult,
                            motionEvent
                    )

                    when (motionEvent?.action) {
                        MotionEvent.ACTION_UP ->
                            if (hitTestResult != null) {
                                addDot(hitTestResult)
                            }

                    }


                }
    }
}

我想直接在 3d 模型上绘制一个球体可渲染形状,但节点没有卡住模型。

以下是所需的输出

Expected Output

下面是我的代码的最终输出

Final Output

这是我添加节点的代码

  private fun addDot(hitTestResult: HitTestResult){
    val color = Color(.8f, 0f, 0f)
    MaterialFactory.makeOpaqueWithColor(appCompatActivity, color)
            .thenAccept { material->
                // The sphere is in local coordinate space, so make the center 0,0,0
                val sphere = ShapeFactory.makeSphere(0.05f, Vector3.zero(),
                        material)
                val indicatorModel = Node()
                indicatorModel.setParent(hitTestResult.node)
                indicatorModel.worldPosition = hitTestResult.point
              //  indicatorModel.localPosition = Vector3(0f, 0f, -1f)
                indicatorModel.renderable = sphere
               // sceneView!!.getScene().addChild(indicatorModel)


            }
}

有没有办法直接在 3d 模型上绘制形状。


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Sceneform Android 在 3D 模型上绘制可渲染形状 的相关文章

随机推荐