在Flutter中的X上旋转3D

时间:2018-05-21 23:30:19

标签: rotation flutter transformation

我一直在使用Flutter轮换,

new Matrix4.identity() ..rotateX(degrees * 3.1415927 / 180),

但问题是,我希望它与下图类似。 我可以使用Flutter在x轴上实现类似3D的旋转吗?

即使存在从3D到2D的映射,或者存在替代方案 这会得到相同的结果。 提前谢谢。

rotate3d diagram

OpenCV中的示例图片:How to calculate perspective transform for OpenCV from rotation angles?

2 个答案:

答案 0 :(得分:3)

感谢this discussionthis repo,经过一天多的时间寻求答案,

static Matrix4 _pmat(num pv) {
    return new Matrix4(
      1.0, 0.0, 0.0, 0.0, //
      0.0, 1.0, 0.0, 0.0, //
      0.0, 0.0, 1.0, pv * 0.001, //
      0.0, 0.0, 0.0, 1.0,
    );
}

Matrix4 perspective = _pmat(1.0);


// then use it

new Center(
      child: new Transform(
        child: new FittedBox(
          fit: BoxFit.fill,
          child: LogoWidget(),
        ),
        alignment: FractionalOffset.center,
        transform: perspective.scaled(1.0, 1.0, 1.0)
          ..rotateX(math.pi - degrees * math.pi / 180)
          ..rotateY(0.0)
          ..rotateZ(0.0)
      ),
    );

这是结果图像

mobile flutter rotation 3d perspective

请阅读有关此主题的little theory

答案 1 :(得分:1)

您可以使用Transform窗口小部件将矩阵应用到其子项上。

这是一个将Transform与动画框架相结合以在X,Y和Z方向上旋转的示例。

enter image description here

import 'dart:math' as math;
import 'package:flutter/material.dart';

void main() {
  runApp(
    new MaterialApp(
      home: new Home(),
    ),
  );
}

class Home extends StatefulWidget {
  @override
  _HomeState createState() => new _HomeState();
}

class _HomeState extends State<Home> with SingleTickerProviderStateMixin {
  AnimationController animationController;
  Animation<double> rotateX;
  Animation<double> rotateY;
  Animation<double> rotateZ;

  @override
  initState() {
    super.initState();
    animationController = new AnimationController(
      duration: const Duration(seconds: 2),
      vsync: this,
    )..repeat();
    print('bar');
    rotateX = new Tween<double>(
      begin: .0,
      end: 1.0,
    ).animate(new CurvedAnimation(
      parent: animationController,
      curve: new Interval(.0, 1 / 3),
    ));
    rotateY = new Tween<double>(
      begin: .0,
      end: 1.0,
    ).animate(new CurvedAnimation(
      parent: animationController,
      curve: new Interval(1 / 3, 2 / 3),
    ));
    rotateZ = new Tween<double>(
      begin: .0,
      end: .5,
    ).animate(new CurvedAnimation(
      parent: animationController,
      curve: new Interval(2 / 3, 1.0),
    ));
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Center(
        child: new AnimatedBuilder(
          animation: animationController,
          builder: (context, child) {
            final card = new SizedBox(
              width: 42.0,
              height: 42.0,
              child: new Card(
                color:
                    animationController.value >= 1 / 6 && animationController.value <= 3 / 6 ? Colors.blue : Colors.red,
              ),
            );

            return new Transform(
              transform: new Matrix4.rotationX(rotateX.value * math.pi)
                ..multiply(new Matrix4.rotationY(rotateY.value * math.pi))
                ..multiply(new Matrix4.rotationZ(rotateZ.value * math.pi)),
              alignment: Alignment.center,
              child: card,
            );
          },
        ),
      ),
    );
  }
}