| 1605 | return new Quaternion(c, axis.mul(s)); |
| 1606 | } |
| 1607 | static fromMatrix3(matrix) { |
| 1608 | const trace = matrix.trace(); |
| 1609 | if (trace >= 0) { |
| 1610 | let s = Math.sqrt(1 + trace); |
| 1611 | const w10 = 0.5 * s; |
| 1612 | s = 0.5 / s; |
| 1613 | const x = (matrix[1][2] - matrix[2][1]) * s; |
| 1614 | const y = (matrix[2][0] - matrix[0][2]) * s; |
| 1615 | const z = (matrix[0][1] - matrix[1][0]) * s; |
| 1616 | return new Quaternion(w10, new Vector3(x, y, z)); |
| 1617 | } |
| 1618 | if (matrix[0][0] > matrix[1][1] && matrix[0][0] > matrix[2][2]) { |
| 1619 | let s = Math.sqrt(matrix[0][0] - matrix[1][1] - matrix[2][2] + 1); |
| 1620 | const x = 0.5 * s; |
| 1621 | s = 0.5 / s; |
| 1622 | const y = (matrix[1][0] + matrix[0][1]) * s; |
| 1623 | const z = (matrix[0][2] + matrix[2][0]) * s; |
| 1624 | const w11 = (matrix[1][2] - matrix[2][1]) * s; |
| 1625 | return new Quaternion(w11, new Vector3(x, y, z)); |
| 1626 | } |
| 1627 | if (matrix[1][1] > matrix[2][2]) { |
| 1628 | let s = Math.sqrt(matrix[1][1] - matrix[0][0] - matrix[2][2] + 1); |
| 1629 | const y = 0.5 * s; |
| 1630 | s = 0.5 / s; |
| 1631 | const z = (matrix[2][1] + matrix[1][2]) * s; |
| 1632 | const x = (matrix[1][0] + matrix[0][1]) * s; |
| 1633 | const w12 = (matrix[2][0] - matrix[0][2]) * s; |
| 1634 | return new Quaternion(w12, new Vector3(x, y, z)); |
| 1635 | } |
| 1636 | let s = Math.sqrt(matrix[2][2] - matrix[0][0] - matrix[1][1] + 1); |
| 1637 | const z = 0.5 * s; |
| 1638 | s = 0.5 / s; |
| 1639 | const x = (matrix[0][2] + matrix[2][0]) * s; |
| 1640 | const y = (matrix[2][1] + matrix[1][2]) * s; |
| 1641 | const w13 = (matrix[0][1] - matrix[1][0]) * s; |
| 1642 | return new Quaternion(w13, new Vector3(x, y, z)); |
| 1643 | } |
| 1644 | static lookAt(dir, up) { |
| 1645 | return Quaternion.fromMatrix3(Matrix3.lookToLh(dir, up)); |
| 1646 | } |