sceneView.getSession()返回null

问题描述 投票:0回答:1

我有 sceneform 示例 hellosceneform 应用程序及其 arFragment,并且想要在自定义坐标上添加对象。为此,据我了解,我需要有一个会话来在其上添加一个锚点,然后创建一个节点等。但是当我运行

arFragment.getArSceneView().getSession()
来调用
createAnchor()
时,我得到
null
。如何获取not null会话?

附注 arFragment 是标准的 hellosceneform 的 ux 片段:

arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.ux_fragment)

android augmented-reality arcore sceneform
1个回答
0
投票

回答问题永远不会太晚,尤其是因为没有答案))。场景的组装非常简单:

arFragment
包含一个
view
,它保留了
Sceneform scene
。保存模型本身的ArAnchor附加到该场景的根点。这是代码:

class MainActivity : AppCompatActivity() {
    private lateinit var arFragment: ArFragment

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

        setContentView(R.layout.activity_main)
        val fragment = supportFragmentManager.findFragmentByTag("frag")
        this.arFragment = fragment as ArFragment
        addNodeToScene(arFragment)
    }

    private fun addNodeToScene(fragment: ArFragment) {
        MaterialFactory.makeOpaqueWithColor(
            arFragment.context,
            Color(1f,0f,0f)

        ).thenAccept { material ->
            val sphere = ShapeFactory.makeSphere(
                0.25f,
                Vector3(0f, 0f,-2f),
                material
            )
            val anchorNode = AnchorNode()
            val modelNode = TransformableNode(fragment.transformationSystem)
            modelNode.renderable = sphere
            modelNode.setParent(anchorNode)
            fragment.arSceneView.scene.addChild(anchorNode)
            modelNode.select()

            if (modelNode.isActive) {
                Toast.makeText(this@MainActivity,
                    "Model loaded",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.