【问题标题】:Flutter - How to rotate a 3D object along Y axis?Flutter - 如何沿 Y 轴旋转 3D 对象?
【发布时间】:2021-09-23 06:28:08
【问题描述】:

所以它是一个 360 度旋转的 3D 对象。 我想要的是它应该沿 Y 轴旋转,而不是沿 X 轴旋转。此外,当前锚点位于屏幕中心(宇航员的脚)。我想把它改成对象的中心。

class _HomePageState extends State<HomePage>
with SingleTickerProviderStateMixin {
late Object astronaut;
late final AnimationController _controller;
@override
void initState() {
_controller = AnimationController(
  vsync: this,
  duration: Duration(seconds: 10),
)..repeat();
astronaut = Object(fileName: 'assets/mercedes/Astronaut.obj');
super.initState();
}

void dispose() {
 _controller.dispose();

 super.dispose();
}

@override
Widget build(BuildContext context) {
 return Scaffold(
  backgroundColor: Colors.black,
  appBar: AppBar(
    title: Text('3D Demo'),
    backgroundColor: Colors.black,
  ),
  // body:

  body: Center(
    child: AnimatedBuilder(
      animation: _controller,
      builder: (_, child) {
        return Transform.rotate(
          angle: _controller.value * 2 * math.pi,
          child: child,
        );
      },
      child: Cube(
        onSceneCreated: (Scene scene) {
          scene.world.add(astronaut);
          scene.camera.zoom = 10;
         },
       ),
     ),
   ),
 );
 }
}

【问题讨论】:

    标签: flutter animation 3d


    【解决方案1】:

    以下是旋转 x 轴、y 轴和 z 轴的方法。

    return Transform(
                transform: Matrix4.identity()
                  ..setEntry(3, 2, 0.01)
                  ..rotateX(
                    math.pi * 0 * _Controller.value, //change 0 to any value to rotate x Axis
                  )
                  ..rotateY(
                    math.pi * 2 * _Controller.value,
                  )
                  ..rotateZ(
                    math.pi * 0 * _Controller.value, //change 0 to any value to rotate z Axis
                  ),
                child: child,
                
              );
    

    【讨论】:

    • 这并没有旋转它,它给了它另一个动画,一种从左上角出现并在右下角消失的对象。
    • 我相信你需要旋转z轴,而不是y轴。要获得您正在寻找的结果,请将 rotateZ 中的 0 更改为 2(或您想要的任何其他值),然后将 rotateY 中的 2 替换为 0。
    • 我让它沿 Y 轴旋转,但问题是,它像 2D 对象一样旋转。它已经是一个 3D 对象。我猜这段代码是用于旋转 2D 的东西。顺便说一句,对齐:您的代码中缺少 Alignment.center。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    • 2017-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多