美文网首页
css3 Matrix

css3 Matrix

作者: hibryce | 来源:发表于2018-07-15 13:17 被阅读0次

Matrix

今天终于搞明白的Matrix在css3 transform中是怎么回事了。

在网上查了半天,也都是讲讲怎么用Matrix,也没找到为什么要这样用。

最后找到了opengl的文档,才弄明白。
http://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/

2D转换都是33矩阵。3D是44的矩阵。
而实际一个二维空间向量只有两个维度,三维只有三个维度。为什么转换的是否会多了一个,这个问题就让我很困惑。

Until then, we only considered 3D vertices as a (x,y,z) triplet. Let’s introduce w. We will now have (x,y,z,w) vectors.

This will be more clear soon, but for now, just remember this :

If w == 1, then the vector (x,y,z,1) is a position in space.
If w == 0, then the vector (x,y,z,0) is a direction.

可以从上面的描述看出多出的一个维度的作用是指示这个矩阵代表的是一个方向还是一个点。

下面对平面向量 [a b c](c为0或者1,含义同上)

translate(x,y): [1 0 x, 0 1 y, 0 0 1][a b c]=[a+x b+y c]

rotate(deg): [cos(deg) -sin(deg) 0, sin(deg) cos(deg) 0, 0 0 1][a b c]=[acos(deg)-bsin(deg) asin(deg)+bcos(deg) c]

不妨令 a=rcos(w),b=rsin(w), 则上式=[rcos(w+deg) rsin(w+deg) c]

scale(sx,sy): [sx 0 0, 0 sy 0, 0 0 1][a b c]=[sxa syb c]

相信通过上面的几个简单的矩阵变换,可以很明显的看出Matrix在其中的作用。

最后还有一个比较重要的问题,就是合并多个变换的时候,需要注意顺序,并不是完全按照下列公式的顺序。

TransformedVector = TranslationMatrix * RotationMatrix * ScaleMatrix * OriginalVector;

解释如下:

As a matter of fact, the order above is what you will usually need for game characters and other items : Scale it first if needed; then set its direction, then translate it. For instance, given a ship model (rotations have been removed for simplification) :

The wrong way :

  • You translate the ship by (10,0,0). Its center is now at 10 units of the origin.
  • You scale your ship by 2. Every coordinate is multiplied by 2 relative to the origin, which is far away… So you end up with a big ship, but centered at 2*10 = 20. Which you don’t want.

The right way :

  • You scale your ship by 2. You get a big ship, centered on the origin.
  • You translate your ship. It’s still the same size, and at the right distance.

相关文章

  • 最酷炫的动画函数(matrix)详细全解

    css3 matrix()矩阵 参数matrix()有六个参数:matrix(a,b,c,d,e,f); 这六个参...

  • css3 Matrix

    Matrix 今天终于搞明白的Matrix在css3 transform中是怎么回事了。 在网上查了半天,也都是讲...

  • CSS3 transform中的Matrix(矩阵)

    1.CSS3中的矩阵 CSS3中的矩阵式一个方法,matrix() 和 matrix3d() 前者为2D平面的移动...

  • matrix矩阵

    原文-理解CSS3 transform中的Matrix(矩阵) by张鑫旭 transform中将用到这个变化矩...

  • CSS3 transform Matrix

    之前大家在CSS3 动画里用到比较多的transform属性,实际上transform属性的好多方法都是可以通过m...

  • CSS3 matrix - matrix3d介绍

    上一篇介绍了transform变形属性,其实一系列变形函数的本质都是matrix矩阵运算,本篇就来看看究竟是怎么运...

  • CSS3神奇的matrix

    关键词:matrix css最为人称道的优点之一就是增加了transform功能,用它来配黑transition可...

  • Android 自定义View缩小图片

    Matrix matrix =new Matrix(); //Matrix绘图 Matrix提供了transla...

  • CSS3 transform 中的 matrix

    CSS的transforn详解 位移,旋转,偏移,缩放分别使用translate/rotate/skew/scal...

  • CSS3 transform 中的 matrix

    CSS的transforn详解 位移,旋转,偏移,缩放分别使用translate/rotate/skew/scal...

网友评论

      本文标题:css3 Matrix

      本文链接:https://www.haomeiwen.com/subject/nnlppftx.html