在Flutter的X上旋转3D

Abd*_*ؤمن 5 transformation rotation flutter

我一直在进行Flutter旋转,

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

但是,问题是,我希望它类似于下图。Flutter如何在x轴上实现类似3D的旋转?

即使存在从3D到2D的映射,也有可能获得相同结果的替代方法。提前致谢。

rotation3d图

OpenCV中的示例图像:如何从旋转角度计算OpenCV的透视变换?

Abd*_*ؤمن 7

多亏了这次讨论这次回购,并且经过一天多的时间寻找答案,

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)
      ),
    );
Run Code Online (Sandbox Code Playgroud)

这是结果图像

移动颤动旋转3d透视

请阅读有关该主题的一些理论

  • `导入'dart:数学';` (2认同)

Rém*_*let 5

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

Transform下面是结合动画框架实现X、Y、Z方向旋转的例子。

在此输入图像描述

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,
            );
          },
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)