使用Sceneform生态系统旋转3D模型存在问题

11

我在Android项目中使用Sceneform SDK

我的项目中有sfbsfa对象,我想将我的对象的初始旋转角度旋转90度。如何实现?

我在这些文件中找到了下面的代码并更改了缩放比例。

但是我没有找到旋转的方法。

model: {
  attributes: [
     "Position",
     "TexCoord",
     "Orientation",
  ],
  collision: {},
  file: "sampledata/models/redmixer.obj",
  name: "redmixer",
  scale: 0.010015,
},

你想要展示它已经旋转了还是在运行时旋转它? - Fixus
@Fixus 已经旋转。 - David
1
你可能想在 https://github.com/google-ar/sceneform-android-sdk/issues 上提交一个功能请求,因为在 SFA 文件中添加类似的内容是有意义的(我们已经有了缩放...那么为什么不加旋转呢?) - Steven Mohr
4个回答

12

我已经使用 setLocalRotation 方法在程序中成功将对象旋转了90度。

      // Create the Anchor.
      Anchor anchor = hitResult.createAnchor();
      AnchorNode anchorNode = new AnchorNode(anchor);
      anchorNode.setParent(arFragment.getArSceneView().getScene());

      // Create the transformable andy and add it to the anchor.
      TransformableNode node = new TransformableNode(arFragment.getTransformationSystem());

      //set rotation in direction (x,y,z) in degrees 90
      node.setLocalRotation(Quaternion.axisAngle(new Vector3(1f, 0, 0), 90f));

      node.setParent(anchorNode);
      node.setRenderable(renderable);
      node.select();
如果您对四元数有更多了解的话,我建议您访问以下链接:https://proandroiddev.com/arcore-cupcakes-4-understanding-quaternion-rotations-f90703f3966e。但基本上,最后一个参数是以度为单位的角度。在这个例子中,90度 -> 90f。通过向量指定旋转的方向。在这个例子中,我沿着x轴方向旋转(x,y,z) -> (1f, 0, 0)。希望这能有所帮助。

它会旋转,但带有选择锚点(白色圆圈),如何防止它? - walkmn

1

我不确定这是否是您要寻找的,但请尝试一下。虽然它看起来很麻烦,但它确实有效,我已经尝试过了。在页面底部的某个地方,他会解释如何旋转3D对象,请查找标题“奖励:使心脏旋转!”

如何在Sceneform中进行旋转动画


0

我认为我可以帮助你(或者至少指出一个有用的方向)。尝试一下:

//Assuming you have created an anchor through hitResult or some other method and have 
//an ArFragment variable "fragment"

ModelRenderable.builder().setSource(context, Uri.parse("your-model.sfb").thenAccept{
   addModel(it, anchor)
   }

fun addModel(model: ModelRenderable, anchor: Anchor){
val aNode = AnchorNode(createAnchor)
val tNode = TransformableNode(fragment.transformationSystem)

//set rotation properties here
tNode.rotationController...
tNode.localRotation...

tNode.setParent(aNode)
fragment.ArSceneView.scene.addChild(aNode)
}

这与上述示例非常相似。希望能对您有所帮助!


0
为了避免万向节锁定,请使用四元数旋转。要使3D对象绕Z轴“顺时针”旋转,请按照以下Kotlin代码进行操作:
override fun onRight(value: Float) {

    objectNode.apply {

        Log.d("Clock Wise", value.toString())

        // XYZ is rotation direction, W component is angle size in degrees 
        rotation = Quaternion.axisAngle(Vector3(0.0f, 0.0f, 1.0f), -45.0f)
    }
}

希望这能有所帮助。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接