简体   繁体   English

在AS3中使用4x4 3DMatrix来扭曲图像

[英]Using a 4x4 3DMatrix in AS3 to skew images

I have an AS3 bitmap image that I would like to skew like so: 我有一个AS3位图图像,我想像这样倾斜:

I would only like to skew the bottom part of the image, and leave the top part alone. 我只想扭曲图像的底部,并让顶部单独留下。 Based on my research that I've done so far, it looks like I'd want to use the Matrix3D class, but I'm not quite sure how to effectively implement it. 基于我迄今为止所做的研究,看起来我想要使用Matrix3D类,但我不太确定如何有效地实现它。

Thanks to an answer below, this article is an excellent resource to understand the Matrix3D class with a 3x3 matrix: http://www.senocular.com/flash/tutorials/transformmatrix/ However as awesome as that tutorial is, it does not provide the capability to skew the above image. 感谢下面的答案,本文是一个很好的资源,可以用3x3矩阵来理解Matrix3D类: http//www.senocular.com/flash/tutorials/transformmatrix/然而,就像那个教程一样棒,它没有提供能够扭曲上面的图像。 What I'm looking for is a similar tutorial on how to use a 4x4 matrix. 我正在寻找的是关于如何使用4x4矩阵的类似教程。 All I'd like to know is where to put the numbers in the matrix, and I should be able to figure out everything else. 我想知道的是将数字放在矩阵中的位置,我应该能够找出其他所有内容。

Here's some example code on what I've got so for: 以下是我所得到的一些示例代码:

var tempLoader:Loader=new Loader();
var tempBitmap:Bitmap;
var fattenTheBottom:Number;
tempLoader.load(new URLRequest("image.png"));
tempLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,castBitmap);
function castBitmap(e:Event):void
{
    tempLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE,castBitmap);
    tempBitmap = Bitmap(tempLoader.content);
    // Make the image into a trapezoid here using fattenTheBottom
    addChild(tempBitmap);
}

Any advice would be wonderfully appreciated! 任何建议都会非常感激!

If you want a quick way using Matrix3D, here is an example of the kind of values you should use: 如果您想快速使用Matrix3D,以下是您应该使用的值类型的示例:

var m3d:Matrix3D = new Matrix3D(Vector.<Number>([
    1, 0,    0,    0, 
    0, 1.15, 0.25, 0.00005,
    0, 0,    1,    0,
    0, 0,    0,    1
]));
tempBitmap.transform.matrix3D = m3d;

But soon you will notice that this approach distorts the image in ways you don't want. 但很快你会注意到这种方法会以你不想要的方式扭曲图像。 For example, it will horizontally squeeze your image on one side (as you want), but the contents of the image will be stretched vertically as well. 例如,它会在一侧水平挤压您的图像(如您所愿),但图像的内容也将垂直拉伸。 Hard to explain, but once you try the above method, you will notice how the image is being vertically stretched. 很难解释,但是一旦你尝试了上面的方法,你会注意到图像是如何被垂直拉伸的。

The same kind of effect can be obtained by using rotationX combined with PerspectiveProjection , and scaleY . 使用rotationX结合PerspectiveProjectionscaleY可以获得相同的效果。

If you want a more elegant solution, both in code and image results, try out DistortImage . 如果您想在代码和图像结果中获得更优雅的解决方案,请尝试使用DistortImage The method there is not straight-forward (uses a mesh of subimages), but has great results. 那里的方法不直接(使用子图像网格),但效果很好。

I can only recommend you to read the best info only I ever found about this: 我只能建议您阅读我发现的最佳信息:
Understanding the Transformation Matrix 理解转换矩阵

Reading this, I think, you could really understand everything about transform matrix. 读到这个,我想,你可以真正理解变换矩阵的一切。 It's from Flash 8(AS2), but it's theoretical information will serve as well using AS3. 它来自Flash 8(AS2),但它的理论信息也可以使用AS3。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM