使用Matrix3D进行无失真旋转

时间:2014-08-06 19:49:09

标签: actionscript-3 matrix rotation scale stage3d

如何为绕Z轴的Matrix3D旋转赋值。(li"旋转")?

正确计算位置和大小,但在添加旋转后,图像会被拉伸。

我尝试了两种旋转方法:

_positionMatrix.identity();
var v3:Vector.<Vector3D> = new Vector.<Vector3D>(3);
v3 = _positionMatrix.decompose();
v3[0].incrementBy(new Vector3D( x, y, 0 ));// x, y, z
v3[1].incrementBy(new Vector3D(0,0,-rotation*Math.PI/180)); // rotationX, rotationY, rotationZ
v3[2].incrementBy(new Vector3D(width,height,0)); // scaleX, scaleY, scaleZ
_positionMatrix.recompose(v3);

_positionMatrix.identity();
_positionMatrix.appendScale(width,height,1);
_positionMatrix.appendTranslation(x,y,0);
_positionMatrix.appendRotation(-rotation,Vector3D.Z_AXIS );

但效果是一样的:

enter image description here

我发现&#34; PerspectiveProjection&#34; class可以提供帮助,但无法理解如何在绕Z轴旋转时使用它。

2 个答案:

答案 0 :(得分:0)

首先,如果遇到这样的问题 - 尝试逐个消除矩阵变换,直到找到哪个矩阵是“坏”矩阵。例如,如果只存储一个转换 - 比例或平移或旋转,会发生什么? 在你的例子中我看不到任何罪犯,所以问题在于代码。我可以假设您的投影矩阵是错误的。你能提供吗?如何将最终矩阵应用于对象?

答案 1 :(得分:0)

谢谢,这是真的,我的观点投影矩阵是“糟糕的”。 这段代码完美无缺:

    private var _vertexData:Vector.<Number> = new <Number>[
        0,0,0, 0, 0, 0, 1,// x, y, z, r, g, b,a
        1, 0, 0, 0, 0, 0, 1,
        1, 1, 0, 0, 0, 0, 1,
        0, 1, 0,0, 0, 0,1
    ];



    var perspectiveProjectionMatrix:Matrix3D = new Matrix3D();
    var scaleX:Number = 2.0 / _stage.stageWidth;  // 2.0 / 500
    var scaleY:Number = -2.0 / _stage.stageHeight; // -2.0 / 375
    perspectiveProjectionMatrix.copyRawDataFrom(
            new <Number>[
                scaleX, 0.0, 0.0, 0.0,
                0.0, scaleY, 0.0, 0.0,
                0.0, 0.0, -1.0, 0.0,
                -1.0, 1.0, 0.0, 1.0
            ]
    );
    var modelViewMatrix:Matrix3D = trs(stage.stageWidth/2,stage.stageHeight/2, _rotation*Math.PI/180, _width*_globalScaleX, _height*_globalScaleY );
    var resultMatrix:Matrix3D = new Matrix3D();
    resultMatrix.prepend(perspectiveProjectionMatrix);
    resultMatrix.prepend(modelViewMatrix);
    _context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 0,resultMatrix , true);

“trs”在哪里:

public function trs( tx:Number, ty:Number, rotation:Number, xScale:Number, yScale:Number ):Matrix3D {

var data:Vector.<Number> = new <Number>[1, 0, 0, 0,  0, 1, 0, 0,  0, 0, 1, 0,  0, 0, 0, 1];

var sin:Number = Math.sin( rotation );
var cos:Number = Math.cos( rotation );
data[0] =  cos * xScale;
data[1] =  sin * xScale;
data[4] = -sin * yScale;
data[5] =  cos * yScale;
data[12] = tx;
data[13] = ty;


var matrix:Matrix3D = new Matrix3D();
matrix.copyRawDataFrom(data);
return matrix;

}