通过链式操作简化变换矩阵的创建

Contents
[ ]

假设,有一个TransformBuilder实例结核病,以及连锁经营:

C#

 // Change the (x, y, z) into (x + 1, y, z)

var a = tb.Translate(1, 0, 0)

// Rotate alone with the Y axis with 180 deg will change the (x, y, z) into (-x, y, -z)

.RotateEulerDegree(0, 180, 0)

// Scale by 2 will change the (x, y, z) into (2x, 2y, 2z)

.Scale(2)

// change the (x, y, z) into (z, y, x)

.Rearrange(Axis.ZAxis, Axis.YAxis, Axis.XAxis)

.Matrix;



public enum ComposeOrder

{

   /// <summary>

   /// Append the new transform to the chain

   /// </summary>

   Append,

   /// <summary>

   /// Prepend the new transform to the chain

   /// </summary>

   Prepend

}

如果此实例的合成顺序为Prepend,则从左到右计算最终矩阵,这意味着最终转换矩阵将执行以下任务:

  1. 将 (x,y,z) 改为 (x 1,y,z)
  2. 以180度的y轴单独旋转会将 (x,Y,z) 变为 (-x,y,-z)
  3. 缩放2将把 (x,y,z) 变成 (2x,2y,2z)
  4. 将 (x,y,z) 改为 (z,y,x)

但是,如果附加了撰写顺序,则顺序将反转,例如:

  1. 将 (x,y,z) 改为 (z,y,x)
  2. 缩放2将把 (x,y,z) 变成 (2x,2y,2z)
  3. 以180度的y轴单独旋转会将 (x,Y,z) 变为 (-x,y,-z)
  4. 将 (x,y,z) 改为 (x 1,y,z)

C#

 //use prepend order so the calculation is performed from left to right:

var m = (new TransformBuilder(ComposeOrder.Prepend))

   //Change the (x, y, z) into (x + 1, y, z)

   .Translate(1, 0, 0)

   // Rotate alone with the Y axis with 180deg will change the (x, y, z) into (-x, y, -z)

   .RotateEulerDegree(0, 180, 0)

   //Scale by 2 will change the (x, y, z) into (2x, 2y, 2z)

   .Scale(2)

   //change the (x, y, z) into (z, y, x)

   .Rearrange(Axis.ZAxis, Axis.YAxis, Axis.XAxis)

   .Matrix;

 //Apply this matrix on a (0, 0, 0) vector, then we get the right result (0, 0, -2)

 var t = m * Vector3.Origin;