我正在尝试在场景中放置一个新对象,如何获取已放置在场景中的对象列表及其坐标,以便我放置的新对象不会放置在场景中的现有对象上AR 核心?
包含可渲染的每个节点、每个锚点及其子节点以及每个光源都附加到 Sceneform 场景的根节点。为每个节点命名并创建一个类型为
val scene = arFragment.arSceneView.scene
anchorNode.name = "anchor" // Anchor with Model A child node
modelNode.name = "model" // Model B node without anchor
lightNode.name = "light" // Light node
scene.addChild(anchorNode)
scene.addChild(modelNode)
scene.addChild(lightNode)
var nodes = emptyList<Map<String, Vector3>>()
for (i in 0..scene.children.size-1) {
val name: String = scene.children[i].name
val coords: Vector3 = scene.children[i].worldPosition
val dictionary = mutableMapOf<String, Vector3>()
dictionary.put(name, coords)
nodes += dictionary
// down the hierarchy (anchor -> model)
if (scene.children[i].name == "anchor" && !scene.children[i].children.isEmpty()) {
val nameR: String = scene.children[i].children[0].name
val coordsR: Vector3 = scene.children[i].children[0].worldPosition
val dictionaryR = mutableMapOf<String, Vector3>()
dictionaryR.put(nameR, coordsR)
nodes += dictionaryR
}
}
Log.d("Nodes:", "$nodes")
结果,您将获得三个字典的列表,其中包含节点名称及其坐标:
//* [ {anchor=[x,y,z]}, {model=[x,y,z]}, {light=[x,y,z]} ] *//