我有 sceneform 示例 hellosceneform 应用程序及其 arFragment,并且想要在自定义坐标上添加对象。为此,据我了解,我需要有一个会话来在其上添加一个锚点,然后创建一个节点等。但是当我运行
arFragment.getArSceneView().getSession()
来调用 createAnchor()
时,我得到 null
。如何获取not null会话?
附注 arFragment 是标准的 hellosceneform 的 ux 片段:
arFragment = (ArFragment) getSupportFragmentManager().findFragmentById(R.id.ux_fragment)
回答问题永远不会太晚,尤其是因为没有答案))。场景的组装非常简单:
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()
}
}
}
}