ogl
Version:
WebGL Library
61 lines (59 loc) • 2.09 kB
JavaScript
// assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
export function fromRotationMatrix(out, m, order = 'YXZ') {
if (order === 'XYZ') {
out[1] = Math.asin(Math.min(Math.max(m[8], -1), 1));
if (Math.abs(m[8]) < 0.99999) {
out[0] = Math.atan2(-m[9], m[10]);
out[2] = Math.atan2(-m[4], m[0]);
} else {
out[0] = Math.atan2(m[6], m[5]);
out[2] = 0;
}
} else if (order === 'YXZ') {
out[0] = Math.asin(-Math.min(Math.max(m[9], -1), 1));
if (Math.abs(m[9]) < 0.99999) {
out[1] = Math.atan2(m[8], m[10]);
out[2] = Math.atan2(m[1], m[5]);
} else {
out[1] = Math.atan2(-m[2], m[0]);
out[2] = 0;
}
} else if (order === 'ZXY') {
out[0] = Math.asin(Math.min(Math.max(m[6], -1), 1));
if (Math.abs(m[6]) < 0.99999) {
out[1] = Math.atan2(-m[2], m[10]);
out[2] = Math.atan2(-m[4], m[5]);
} else {
out[1] = 0;
out[2] = Math.atan2(m[1], m[0]);
}
} else if (order === 'ZYX') {
out[1] = Math.asin(-Math.min(Math.max(m[2], -1), 1));
if (Math.abs(m[2]) < 0.99999) {
out[0] = Math.atan2(m[6], m[10]);
out[2] = Math.atan2(m[1], m[0]);
} else {
out[0] = 0;
out[2] = Math.atan2(-m[4], m[5]);
}
} else if (order === 'YZX') {
out[2] = Math.asin(Math.min(Math.max(m[1], -1), 1));
if (Math.abs(m[1]) < 0.99999) {
out[0] = Math.atan2(-m[9], m[5]);
out[1] = Math.atan2(-m[2], m[0]);
} else {
out[0] = 0;
out[1] = Math.atan2(m[8], m[10]);
}
} else if (order === 'XZY') {
out[2] = Math.asin(-Math.min(Math.max(m[4], -1), 1));
if (Math.abs(m[4]) < 0.99999) {
out[0] = Math.atan2(m[6], m[5]);
out[1] = Math.atan2(m[8], m[0]);
} else {
out[0] = Math.atan2(-m[9], m[10]);
out[1] = 0;
}
}
return out;
}