You are right – my first paragraph in my answer above is not correct. Apologies – I must blame sleep deprivation or perhaps just foolishness. I’m striking through that paragraph to avoid confusing others.
The key thing is that all matrix operations are always relative, not absolute – they rotate, translate, or scale etc relative to the previous state, not relative to the original frame of reference. For this reason, the only operations that can be moved around in sequence are ones which are commutative. For example, rotation is commutative in 2D and generally not in 3D:
Still, certain sets of operations are order invariant. Here are some examples:
order-independent (commutative)
// 2D translations
translate(x, y);
translate(x2, y2);
// 2D rotation
rotate(r);
rotate(r2);
// 3D translations
translate(x, y, z);
translate(x2, y2, z3);
However, most are not.
order-dependent (not commutative) examples
In general, mixing different operations from scale
rotate
or translate
is almost always order-dependent – unless one of the operation has no effect such as scale(1)
or rotate(TWO_PI)
. Also, 3D rotation is order-dependent.
// mixed translation and rotation
translate(x, y)
rotate(r);
// ...does not equal:
// rotate(r);
// translate(x, y)
// 3D mixed translation and rotation
rotateX(r);
translate(x, y, z)
// does not equal:
// translate(x, y, z)
// rotateX(r);
// 3D rotation
rotateX(r);
rotateY(r1);
rotateZ(r2);
// ...does not equal:
// rotateZ(r2);
// rotateY(r1);
// rotateX(r);