使用 Sceneform 生态系统有问题地旋转 3D 模型

Problematically rotate 3D model using Sceneform ecosystem

我在 Android 项目中使用 Sceneform SDK

我的项目中有 sfbsfa 个对象,我希望对象的初始旋转旋转 90 度。 我怎样才能实现它?

我在这些文件中找到了下一个代码并更改了比例。

但是我没有找到轮换的方法。

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

不确定这是否是您要找的东西,但试试这个它对我来说就像噩梦,虽然它有效我已经试过了 在底部页面的某处,他将向您解释如何旋转 3dobject 查找此标题 "Bonus: Make the heart rotate!"

how to do rotatation animation in sceneform

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

//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)
}

和上面提到的例子很相似。希望对您有所帮助!

我已经使用 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

但基本上最后一个参数是以度为单位的角度。在本例中为 90deg -> 90f。通过矢量指定旋转方向。在示例中,我在 x 方向 (x,y,z) -> (1f, 0, 0) 上旋转。希望对你有帮助。

为避免Gimbal Lock使用四元数旋转。要围绕 Zclock wise 旋转 3D 对象,请遵循以下 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)
    }
}

希望对您有所帮助。