我正在使用 ArFragment,并且尝试以编程方式更改 TransformableNode(AnchorNode 的子级)的比例。我正在使用以下代码:
ar_scaleTest_button.setOnClickListener {
for(node in fragment!!.arSceneView.scene.children) {
if(node.children.size != 0) {
val transNode = node.children[0] as TransformableNode
if(transNode.isSelected) {
Toast.makeText(it.context,
"Scale was: "+transNode.localScale.toString(),
Toast.LENGTH_SHORT).show()
transNode.localScale = Vector3(1.0f, 1.0f, 1.0f)
}
}
}
}
调试后,我意识到正在执行的方法是 Node 类中的 setLocalScale(),但它接收的是空值而不是 Vector3(1.0f, 1.0f, 1.0f)。我通过在以下方法的第二行中放置一个断点来检查这一点,该断点永远不会到达:
public void setLocalScale(Vector3 var1) {
Preconditions.checkNotNull(var1, "Parameter \"scale\" was null.");
this.localScale.set(var1);
this.applyTransformUpdateRecursively();
}
我们设置的秤是否正确?还有其他方法可以做到这一点吗? 非常感谢!
在 ARCore 中,如果您无法使用
Vector(0.5f, 0.5f, 0.5f)
等命令将 Sceneform 节点的比例更改为其原始大小,则可以分 3 遍执行缩小比例操作。我已经尝试过很多次了——它有效。此代码完美适用于 ARCore 1.45 版本和 Sceneform 1.17.1 版本。要缩小球体,请在加载场景后点击屏幕。
class MainActivity : AppCompatActivity() {
private lateinit var arFragment: ArFragment
private lateinit var node: TransformableNode
val anchorNode = AnchorNode()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
val fragment = supportFragmentManager.findFragmentByTag("frag")
arFragment = fragment as ArFragment
this.appendSphereToScene(arFragment)
this.downScaleSphere(arFragment)
}
fun appendSphereToScene(fragment: ArFragment) {
fragment.arSceneView.scene.addChild(anchorNode)
MaterialFactory.makeOpaqueWithColor(fragment.context, Color(1.0f,0.5f,0.1f))
.thenAccept {
val sphere = ShapeFactory.makeSphere(0.25f, Vector3.zero(), it)
node = TransformableNode(fragment.transformationSystem)
node.renderable = sphere
node.worldPosition = Vector3(0f,0f,-2f)
node.localScale = Vector3(2f,2f,2f)
anchorNode.addChild(node)
}
}
fun downScaleSphere(fragment: ArFragment) {
fragment.arSceneView.setOnTouchListener { _, _ ->
val sf = 0.5f // scale factor
anchorNode.worldScale = Vector3(1.0f, sf, sf)
anchorNode.worldScale = Vector3(sf, 1.0f, sf)
anchorNode.worldScale = Vector3(sf, sf, 1.0f)
return@setOnTouchListener true
}
}
}