basis.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. /*************************************************************************/
  2. /* basis.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "basis.h"
  31. #include "core/math/math_funcs.h"
  32. #include "core/string/print_string.h"
  33. #define cofac(row1, col1, row2, col2) \
  34. (rows[row1][col1] * rows[row2][col2] - rows[row1][col2] * rows[row2][col1])
  35. void Basis::from_z(const Vector3 &p_z) {
  36. if (Math::abs(p_z.z) > (real_t)Math_SQRT12) {
  37. // choose p in y-z plane
  38. real_t a = p_z[1] * p_z[1] + p_z[2] * p_z[2];
  39. real_t k = 1.0f / Math::sqrt(a);
  40. rows[0] = Vector3(0, -p_z[2] * k, p_z[1] * k);
  41. rows[1] = Vector3(a * k, -p_z[0] * rows[0][2], p_z[0] * rows[0][1]);
  42. } else {
  43. // choose p in x-y plane
  44. real_t a = p_z.x * p_z.x + p_z.y * p_z.y;
  45. real_t k = 1.0f / Math::sqrt(a);
  46. rows[0] = Vector3(-p_z.y * k, p_z.x * k, 0);
  47. rows[1] = Vector3(-p_z.z * rows[0].y, p_z.z * rows[0].x, a * k);
  48. }
  49. rows[2] = p_z;
  50. }
  51. void Basis::invert() {
  52. real_t co[3] = {
  53. cofac(1, 1, 2, 2), cofac(1, 2, 2, 0), cofac(1, 0, 2, 1)
  54. };
  55. real_t det = rows[0][0] * co[0] +
  56. rows[0][1] * co[1] +
  57. rows[0][2] * co[2];
  58. #ifdef MATH_CHECKS
  59. ERR_FAIL_COND(det == 0);
  60. #endif
  61. real_t s = 1.0f / det;
  62. set(co[0] * s, cofac(0, 2, 2, 1) * s, cofac(0, 1, 1, 2) * s,
  63. co[1] * s, cofac(0, 0, 2, 2) * s, cofac(0, 2, 1, 0) * s,
  64. co[2] * s, cofac(0, 1, 2, 0) * s, cofac(0, 0, 1, 1) * s);
  65. }
  66. void Basis::orthonormalize() {
  67. // Gram-Schmidt Process
  68. Vector3 x = get_column(0);
  69. Vector3 y = get_column(1);
  70. Vector3 z = get_column(2);
  71. x.normalize();
  72. y = (y - x * (x.dot(y)));
  73. y.normalize();
  74. z = (z - x * (x.dot(z)) - y * (y.dot(z)));
  75. z.normalize();
  76. set_column(0, x);
  77. set_column(1, y);
  78. set_column(2, z);
  79. }
  80. Basis Basis::orthonormalized() const {
  81. Basis c = *this;
  82. c.orthonormalize();
  83. return c;
  84. }
  85. void Basis::orthogonalize() {
  86. Vector3 scl = get_scale();
  87. orthonormalize();
  88. scale_local(scl);
  89. }
  90. Basis Basis::orthogonalized() const {
  91. Basis c = *this;
  92. c.orthogonalize();
  93. return c;
  94. }
  95. bool Basis::is_orthogonal() const {
  96. Basis identity;
  97. Basis m = (*this) * transposed();
  98. return m.is_equal_approx(identity);
  99. }
  100. bool Basis::is_diagonal() const {
  101. return (
  102. Math::is_zero_approx(rows[0][1]) && Math::is_zero_approx(rows[0][2]) &&
  103. Math::is_zero_approx(rows[1][0]) && Math::is_zero_approx(rows[1][2]) &&
  104. Math::is_zero_approx(rows[2][0]) && Math::is_zero_approx(rows[2][1]));
  105. }
  106. bool Basis::is_rotation() const {
  107. return Math::is_equal_approx(determinant(), 1, (real_t)UNIT_EPSILON) && is_orthogonal();
  108. }
  109. #ifdef MATH_CHECKS
  110. // This method is only used once, in diagonalize. If it's desired elsewhere, feel free to remove the #ifdef.
  111. bool Basis::is_symmetric() const {
  112. if (!Math::is_equal_approx(rows[0][1], rows[1][0])) {
  113. return false;
  114. }
  115. if (!Math::is_equal_approx(rows[0][2], rows[2][0])) {
  116. return false;
  117. }
  118. if (!Math::is_equal_approx(rows[1][2], rows[2][1])) {
  119. return false;
  120. }
  121. return true;
  122. }
  123. #endif
  124. Basis Basis::diagonalize() {
  125. //NOTE: only implemented for symmetric matrices
  126. //with the Jacobi iterative method
  127. #ifdef MATH_CHECKS
  128. ERR_FAIL_COND_V(!is_symmetric(), Basis());
  129. #endif
  130. const int ite_max = 1024;
  131. real_t off_matrix_norm_2 = rows[0][1] * rows[0][1] + rows[0][2] * rows[0][2] + rows[1][2] * rows[1][2];
  132. int ite = 0;
  133. Basis acc_rot;
  134. while (off_matrix_norm_2 > (real_t)CMP_EPSILON2 && ite++ < ite_max) {
  135. real_t el01_2 = rows[0][1] * rows[0][1];
  136. real_t el02_2 = rows[0][2] * rows[0][2];
  137. real_t el12_2 = rows[1][2] * rows[1][2];
  138. // Find the pivot element
  139. int i, j;
  140. if (el01_2 > el02_2) {
  141. if (el12_2 > el01_2) {
  142. i = 1;
  143. j = 2;
  144. } else {
  145. i = 0;
  146. j = 1;
  147. }
  148. } else {
  149. if (el12_2 > el02_2) {
  150. i = 1;
  151. j = 2;
  152. } else {
  153. i = 0;
  154. j = 2;
  155. }
  156. }
  157. // Compute the rotation angle
  158. real_t angle;
  159. if (Math::is_equal_approx(rows[j][j], rows[i][i])) {
  160. angle = Math_PI / 4;
  161. } else {
  162. angle = 0.5f * Math::atan(2 * rows[i][j] / (rows[j][j] - rows[i][i]));
  163. }
  164. // Compute the rotation matrix
  165. Basis rot;
  166. rot.rows[i][i] = rot.rows[j][j] = Math::cos(angle);
  167. rot.rows[i][j] = -(rot.rows[j][i] = Math::sin(angle));
  168. // Update the off matrix norm
  169. off_matrix_norm_2 -= rows[i][j] * rows[i][j];
  170. // Apply the rotation
  171. *this = rot * *this * rot.transposed();
  172. acc_rot = rot * acc_rot;
  173. }
  174. return acc_rot;
  175. }
  176. Basis Basis::inverse() const {
  177. Basis inv = *this;
  178. inv.invert();
  179. return inv;
  180. }
  181. void Basis::transpose() {
  182. SWAP(rows[0][1], rows[1][0]);
  183. SWAP(rows[0][2], rows[2][0]);
  184. SWAP(rows[1][2], rows[2][1]);
  185. }
  186. Basis Basis::transposed() const {
  187. Basis tr = *this;
  188. tr.transpose();
  189. return tr;
  190. }
  191. Basis Basis::from_scale(const Vector3 &p_scale) {
  192. return Basis(p_scale.x, 0, 0, 0, p_scale.y, 0, 0, 0, p_scale.z);
  193. }
  194. // Multiplies the matrix from left by the scaling matrix: M -> S.M
  195. // See the comment for Basis::rotated for further explanation.
  196. void Basis::scale(const Vector3 &p_scale) {
  197. rows[0][0] *= p_scale.x;
  198. rows[0][1] *= p_scale.x;
  199. rows[0][2] *= p_scale.x;
  200. rows[1][0] *= p_scale.y;
  201. rows[1][1] *= p_scale.y;
  202. rows[1][2] *= p_scale.y;
  203. rows[2][0] *= p_scale.z;
  204. rows[2][1] *= p_scale.z;
  205. rows[2][2] *= p_scale.z;
  206. }
  207. Basis Basis::scaled(const Vector3 &p_scale) const {
  208. Basis m = *this;
  209. m.scale(p_scale);
  210. return m;
  211. }
  212. void Basis::scale_local(const Vector3 &p_scale) {
  213. // performs a scaling in object-local coordinate system:
  214. // M -> (M.S.Minv).M = M.S.
  215. *this = scaled_local(p_scale);
  216. }
  217. void Basis::scale_orthogonal(const Vector3 &p_scale) {
  218. *this = scaled_orthogonal(p_scale);
  219. }
  220. Basis Basis::scaled_orthogonal(const Vector3 &p_scale) const {
  221. Basis m = *this;
  222. Vector3 s = Vector3(-1, -1, -1) + p_scale;
  223. Vector3 dots;
  224. Basis b;
  225. for (int i = 0; i < 3; i++) {
  226. for (int j = 0; j < 3; j++) {
  227. dots[j] += s[i] * abs(m.get_column(i).normalized().dot(b.get_column(j)));
  228. }
  229. }
  230. m.scale_local(Vector3(1, 1, 1) + dots);
  231. return m;
  232. }
  233. float Basis::get_uniform_scale() const {
  234. return (rows[0].length() + rows[1].length() + rows[2].length()) / 3.0f;
  235. }
  236. void Basis::make_scale_uniform() {
  237. float l = (rows[0].length() + rows[1].length() + rows[2].length()) / 3.0f;
  238. for (int i = 0; i < 3; i++) {
  239. rows[i].normalize();
  240. rows[i] *= l;
  241. }
  242. }
  243. Basis Basis::scaled_local(const Vector3 &p_scale) const {
  244. return (*this) * Basis::from_scale(p_scale);
  245. }
  246. Vector3 Basis::get_scale_abs() const {
  247. return Vector3(
  248. Vector3(rows[0][0], rows[1][0], rows[2][0]).length(),
  249. Vector3(rows[0][1], rows[1][1], rows[2][1]).length(),
  250. Vector3(rows[0][2], rows[1][2], rows[2][2]).length());
  251. }
  252. Vector3 Basis::get_scale_local() const {
  253. real_t det_sign = SIGN(determinant());
  254. return det_sign * Vector3(rows[0].length(), rows[1].length(), rows[2].length());
  255. }
  256. // get_scale works with get_rotation, use get_scale_abs if you need to enforce positive signature.
  257. Vector3 Basis::get_scale() const {
  258. // FIXME: We are assuming M = R.S (R is rotation and S is scaling), and use polar decomposition to extract R and S.
  259. // A polar decomposition is M = O.P, where O is an orthogonal matrix (meaning rotation and reflection) and
  260. // P is a positive semi-definite matrix (meaning it contains absolute values of scaling along its diagonal).
  261. //
  262. // Despite being different from what we want to achieve, we can nevertheless make use of polar decomposition
  263. // here as follows. We can split O into a rotation and a reflection as O = R.Q, and obtain M = R.S where
  264. // we defined S = Q.P. Now, R is a proper rotation matrix and S is a (signed) scaling matrix,
  265. // which can involve negative scalings. However, there is a catch: unlike the polar decomposition of M = O.P,
  266. // the decomposition of O into a rotation and reflection matrix as O = R.Q is not unique.
  267. // Therefore, we are going to do this decomposition by sticking to a particular convention.
  268. // This may lead to confusion for some users though.
  269. //
  270. // The convention we use here is to absorb the sign flip into the scaling matrix.
  271. // The same convention is also used in other similar functions such as get_rotation_axis_angle, get_rotation, ...
  272. //
  273. // A proper way to get rid of this issue would be to store the scaling values (or at least their signs)
  274. // as a part of Basis. However, if we go that path, we need to disable direct (write) access to the
  275. // matrix elements.
  276. //
  277. // The rotation part of this decomposition is returned by get_rotation* functions.
  278. real_t det_sign = SIGN(determinant());
  279. return det_sign * get_scale_abs();
  280. }
  281. // Decomposes a Basis into a rotation-reflection matrix (an element of the group O(3)) and a positive scaling matrix as B = O.S.
  282. // Returns the rotation-reflection matrix via reference argument, and scaling information is returned as a Vector3.
  283. // This (internal) function is too specific and named too ugly to expose to users, and probably there's no need to do so.
  284. Vector3 Basis::rotref_posscale_decomposition(Basis &rotref) const {
  285. #ifdef MATH_CHECKS
  286. ERR_FAIL_COND_V(determinant() == 0, Vector3());
  287. Basis m = transposed() * (*this);
  288. ERR_FAIL_COND_V(!m.is_diagonal(), Vector3());
  289. #endif
  290. Vector3 scale = get_scale();
  291. Basis inv_scale = Basis().scaled(scale.inverse()); // this will also absorb the sign of scale
  292. rotref = (*this) * inv_scale;
  293. #ifdef MATH_CHECKS
  294. ERR_FAIL_COND_V(!rotref.is_orthogonal(), Vector3());
  295. #endif
  296. return scale.abs();
  297. }
  298. // Multiplies the matrix from left by the rotation matrix: M -> R.M
  299. // Note that this does *not* rotate the matrix itself.
  300. //
  301. // The main use of Basis is as Transform.basis, which is used by the transformation matrix
  302. // of 3D object. Rotate here refers to rotation of the object (which is R * (*this)),
  303. // not the matrix itself (which is R * (*this) * R.transposed()).
  304. Basis Basis::rotated(const Vector3 &p_axis, real_t p_angle) const {
  305. return Basis(p_axis, p_angle) * (*this);
  306. }
  307. void Basis::rotate(const Vector3 &p_axis, real_t p_angle) {
  308. *this = rotated(p_axis, p_angle);
  309. }
  310. void Basis::rotate_local(const Vector3 &p_axis, real_t p_angle) {
  311. // performs a rotation in object-local coordinate system:
  312. // M -> (M.R.Minv).M = M.R.
  313. *this = rotated_local(p_axis, p_angle);
  314. }
  315. Basis Basis::rotated_local(const Vector3 &p_axis, real_t p_angle) const {
  316. return (*this) * Basis(p_axis, p_angle);
  317. }
  318. Basis Basis::rotated(const Vector3 &p_euler, EulerOrder p_order) const {
  319. return Basis::from_euler(p_euler, p_order) * (*this);
  320. }
  321. void Basis::rotate(const Vector3 &p_euler, EulerOrder p_order) {
  322. *this = rotated(p_euler, p_order);
  323. }
  324. Basis Basis::rotated(const Quaternion &p_quaternion) const {
  325. return Basis(p_quaternion) * (*this);
  326. }
  327. void Basis::rotate(const Quaternion &p_quaternion) {
  328. *this = rotated(p_quaternion);
  329. }
  330. Vector3 Basis::get_euler_normalized(EulerOrder p_order) const {
  331. // Assumes that the matrix can be decomposed into a proper rotation and scaling matrix as M = R.S,
  332. // and returns the Euler angles corresponding to the rotation part, complementing get_scale().
  333. // See the comment in get_scale() for further information.
  334. Basis m = orthonormalized();
  335. real_t det = m.determinant();
  336. if (det < 0) {
  337. // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles.
  338. m.scale(Vector3(-1, -1, -1));
  339. }
  340. return m.get_euler(p_order);
  341. }
  342. Quaternion Basis::get_rotation_quaternion() const {
  343. // Assumes that the matrix can be decomposed into a proper rotation and scaling matrix as M = R.S,
  344. // and returns the Euler angles corresponding to the rotation part, complementing get_scale().
  345. // See the comment in get_scale() for further information.
  346. Basis m = orthonormalized();
  347. real_t det = m.determinant();
  348. if (det < 0) {
  349. // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles.
  350. m.scale(Vector3(-1, -1, -1));
  351. }
  352. return m.get_quaternion();
  353. }
  354. void Basis::rotate_to_align(Vector3 p_start_direction, Vector3 p_end_direction) {
  355. // Takes two vectors and rotates the basis from the first vector to the second vector.
  356. // Adopted from: https://gist.github.com/kevinmoran/b45980723e53edeb8a5a43c49f134724
  357. const Vector3 axis = p_start_direction.cross(p_end_direction).normalized();
  358. if (axis.length_squared() != 0) {
  359. real_t dot = p_start_direction.dot(p_end_direction);
  360. dot = CLAMP(dot, -1.0f, 1.0f);
  361. const real_t angle_rads = Math::acos(dot);
  362. set_axis_angle(axis, angle_rads);
  363. }
  364. }
  365. void Basis::get_rotation_axis_angle(Vector3 &p_axis, real_t &p_angle) const {
  366. // Assumes that the matrix can be decomposed into a proper rotation and scaling matrix as M = R.S,
  367. // and returns the Euler angles corresponding to the rotation part, complementing get_scale().
  368. // See the comment in get_scale() for further information.
  369. Basis m = orthonormalized();
  370. real_t det = m.determinant();
  371. if (det < 0) {
  372. // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles.
  373. m.scale(Vector3(-1, -1, -1));
  374. }
  375. m.get_axis_angle(p_axis, p_angle);
  376. }
  377. void Basis::get_rotation_axis_angle_local(Vector3 &p_axis, real_t &p_angle) const {
  378. // Assumes that the matrix can be decomposed into a proper rotation and scaling matrix as M = R.S,
  379. // and returns the Euler angles corresponding to the rotation part, complementing get_scale().
  380. // See the comment in get_scale() for further information.
  381. Basis m = transposed();
  382. m.orthonormalize();
  383. real_t det = m.determinant();
  384. if (det < 0) {
  385. // Ensure that the determinant is 1, such that result is a proper rotation matrix which can be represented by Euler angles.
  386. m.scale(Vector3(-1, -1, -1));
  387. }
  388. m.get_axis_angle(p_axis, p_angle);
  389. p_angle = -p_angle;
  390. }
  391. Vector3 Basis::get_euler(EulerOrder p_order) const {
  392. switch (p_order) {
  393. case EULER_ORDER_XYZ: {
  394. // Euler angles in XYZ convention.
  395. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
  396. //
  397. // rot = cy*cz -cy*sz sy
  398. // cz*sx*sy+cx*sz cx*cz-sx*sy*sz -cy*sx
  399. // -cx*cz*sy+sx*sz cz*sx+cx*sy*sz cx*cy
  400. Vector3 euler;
  401. real_t sy = rows[0][2];
  402. if (sy < (1.0f - (real_t)CMP_EPSILON)) {
  403. if (sy > -(1.0f - (real_t)CMP_EPSILON)) {
  404. // is this a pure Y rotation?
  405. if (rows[1][0] == 0 && rows[0][1] == 0 && rows[1][2] == 0 && rows[2][1] == 0 && rows[1][1] == 1) {
  406. // return the simplest form (human friendlier in editor and scripts)
  407. euler.x = 0;
  408. euler.y = atan2(rows[0][2], rows[0][0]);
  409. euler.z = 0;
  410. } else {
  411. euler.x = Math::atan2(-rows[1][2], rows[2][2]);
  412. euler.y = Math::asin(sy);
  413. euler.z = Math::atan2(-rows[0][1], rows[0][0]);
  414. }
  415. } else {
  416. euler.x = Math::atan2(rows[2][1], rows[1][1]);
  417. euler.y = -Math_PI / 2.0f;
  418. euler.z = 0.0f;
  419. }
  420. } else {
  421. euler.x = Math::atan2(rows[2][1], rows[1][1]);
  422. euler.y = Math_PI / 2.0f;
  423. euler.z = 0.0f;
  424. }
  425. return euler;
  426. } break;
  427. case EULER_ORDER_XZY: {
  428. // Euler angles in XZY convention.
  429. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
  430. //
  431. // rot = cz*cy -sz cz*sy
  432. // sx*sy+cx*cy*sz cx*cz cx*sz*sy-cy*sx
  433. // cy*sx*sz cz*sx cx*cy+sx*sz*sy
  434. Vector3 euler;
  435. real_t sz = rows[0][1];
  436. if (sz < (1.0f - (real_t)CMP_EPSILON)) {
  437. if (sz > -(1.0f - (real_t)CMP_EPSILON)) {
  438. euler.x = Math::atan2(rows[2][1], rows[1][1]);
  439. euler.y = Math::atan2(rows[0][2], rows[0][0]);
  440. euler.z = Math::asin(-sz);
  441. } else {
  442. // It's -1
  443. euler.x = -Math::atan2(rows[1][2], rows[2][2]);
  444. euler.y = 0.0f;
  445. euler.z = Math_PI / 2.0f;
  446. }
  447. } else {
  448. // It's 1
  449. euler.x = -Math::atan2(rows[1][2], rows[2][2]);
  450. euler.y = 0.0f;
  451. euler.z = -Math_PI / 2.0f;
  452. }
  453. return euler;
  454. } break;
  455. case EULER_ORDER_YXZ: {
  456. // Euler angles in YXZ convention.
  457. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
  458. //
  459. // rot = cy*cz+sy*sx*sz cz*sy*sx-cy*sz cx*sy
  460. // cx*sz cx*cz -sx
  461. // cy*sx*sz-cz*sy cy*cz*sx+sy*sz cy*cx
  462. Vector3 euler;
  463. real_t m12 = rows[1][2];
  464. if (m12 < (1 - (real_t)CMP_EPSILON)) {
  465. if (m12 > -(1 - (real_t)CMP_EPSILON)) {
  466. // is this a pure X rotation?
  467. if (rows[1][0] == 0 && rows[0][1] == 0 && rows[0][2] == 0 && rows[2][0] == 0 && rows[0][0] == 1) {
  468. // return the simplest form (human friendlier in editor and scripts)
  469. euler.x = atan2(-m12, rows[1][1]);
  470. euler.y = 0;
  471. euler.z = 0;
  472. } else {
  473. euler.x = asin(-m12);
  474. euler.y = atan2(rows[0][2], rows[2][2]);
  475. euler.z = atan2(rows[1][0], rows[1][1]);
  476. }
  477. } else { // m12 == -1
  478. euler.x = Math_PI * 0.5f;
  479. euler.y = atan2(rows[0][1], rows[0][0]);
  480. euler.z = 0;
  481. }
  482. } else { // m12 == 1
  483. euler.x = -Math_PI * 0.5f;
  484. euler.y = -atan2(rows[0][1], rows[0][0]);
  485. euler.z = 0;
  486. }
  487. return euler;
  488. } break;
  489. case EULER_ORDER_YZX: {
  490. // Euler angles in YZX convention.
  491. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
  492. //
  493. // rot = cy*cz sy*sx-cy*cx*sz cx*sy+cy*sz*sx
  494. // sz cz*cx -cz*sx
  495. // -cz*sy cy*sx+cx*sy*sz cy*cx-sy*sz*sx
  496. Vector3 euler;
  497. real_t sz = rows[1][0];
  498. if (sz < (1.0f - (real_t)CMP_EPSILON)) {
  499. if (sz > -(1.0f - (real_t)CMP_EPSILON)) {
  500. euler.x = Math::atan2(-rows[1][2], rows[1][1]);
  501. euler.y = Math::atan2(-rows[2][0], rows[0][0]);
  502. euler.z = Math::asin(sz);
  503. } else {
  504. // It's -1
  505. euler.x = Math::atan2(rows[2][1], rows[2][2]);
  506. euler.y = 0.0f;
  507. euler.z = -Math_PI / 2.0f;
  508. }
  509. } else {
  510. // It's 1
  511. euler.x = Math::atan2(rows[2][1], rows[2][2]);
  512. euler.y = 0.0f;
  513. euler.z = Math_PI / 2.0f;
  514. }
  515. return euler;
  516. } break;
  517. case EULER_ORDER_ZXY: {
  518. // Euler angles in ZXY convention.
  519. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
  520. //
  521. // rot = cz*cy-sz*sx*sy -cx*sz cz*sy+cy*sz*sx
  522. // cy*sz+cz*sx*sy cz*cx sz*sy-cz*cy*sx
  523. // -cx*sy sx cx*cy
  524. Vector3 euler;
  525. real_t sx = rows[2][1];
  526. if (sx < (1.0f - (real_t)CMP_EPSILON)) {
  527. if (sx > -(1.0f - (real_t)CMP_EPSILON)) {
  528. euler.x = Math::asin(sx);
  529. euler.y = Math::atan2(-rows[2][0], rows[2][2]);
  530. euler.z = Math::atan2(-rows[0][1], rows[1][1]);
  531. } else {
  532. // It's -1
  533. euler.x = -Math_PI / 2.0f;
  534. euler.y = Math::atan2(rows[0][2], rows[0][0]);
  535. euler.z = 0;
  536. }
  537. } else {
  538. // It's 1
  539. euler.x = Math_PI / 2.0f;
  540. euler.y = Math::atan2(rows[0][2], rows[0][0]);
  541. euler.z = 0;
  542. }
  543. return euler;
  544. } break;
  545. case EULER_ORDER_ZYX: {
  546. // Euler angles in ZYX convention.
  547. // See https://en.wikipedia.org/wiki/Euler_angles#Rotation_matrix
  548. //
  549. // rot = cz*cy cz*sy*sx-cx*sz sz*sx+cz*cx*cy
  550. // cy*sz cz*cx+sz*sy*sx cx*sz*sy-cz*sx
  551. // -sy cy*sx cy*cx
  552. Vector3 euler;
  553. real_t sy = rows[2][0];
  554. if (sy < (1.0f - (real_t)CMP_EPSILON)) {
  555. if (sy > -(1.0f - (real_t)CMP_EPSILON)) {
  556. euler.x = Math::atan2(rows[2][1], rows[2][2]);
  557. euler.y = Math::asin(-sy);
  558. euler.z = Math::atan2(rows[1][0], rows[0][0]);
  559. } else {
  560. // It's -1
  561. euler.x = 0;
  562. euler.y = Math_PI / 2.0f;
  563. euler.z = -Math::atan2(rows[0][1], rows[1][1]);
  564. }
  565. } else {
  566. // It's 1
  567. euler.x = 0;
  568. euler.y = -Math_PI / 2.0f;
  569. euler.z = -Math::atan2(rows[0][1], rows[1][1]);
  570. }
  571. return euler;
  572. } break;
  573. default: {
  574. ERR_FAIL_V_MSG(Vector3(), "Invalid parameter for get_euler(order)");
  575. }
  576. }
  577. return Vector3();
  578. }
  579. void Basis::set_euler(const Vector3 &p_euler, EulerOrder p_order) {
  580. real_t c, s;
  581. c = Math::cos(p_euler.x);
  582. s = Math::sin(p_euler.x);
  583. Basis xmat(1, 0, 0, 0, c, -s, 0, s, c);
  584. c = Math::cos(p_euler.y);
  585. s = Math::sin(p_euler.y);
  586. Basis ymat(c, 0, s, 0, 1, 0, -s, 0, c);
  587. c = Math::cos(p_euler.z);
  588. s = Math::sin(p_euler.z);
  589. Basis zmat(c, -s, 0, s, c, 0, 0, 0, 1);
  590. switch (p_order) {
  591. case EULER_ORDER_XYZ: {
  592. *this = xmat * (ymat * zmat);
  593. } break;
  594. case EULER_ORDER_XZY: {
  595. *this = xmat * zmat * ymat;
  596. } break;
  597. case EULER_ORDER_YXZ: {
  598. *this = ymat * xmat * zmat;
  599. } break;
  600. case EULER_ORDER_YZX: {
  601. *this = ymat * zmat * xmat;
  602. } break;
  603. case EULER_ORDER_ZXY: {
  604. *this = zmat * xmat * ymat;
  605. } break;
  606. case EULER_ORDER_ZYX: {
  607. *this = zmat * ymat * xmat;
  608. } break;
  609. default: {
  610. ERR_FAIL_MSG("Invalid order parameter for set_euler(vec3,order)");
  611. }
  612. }
  613. }
  614. bool Basis::is_equal_approx(const Basis &p_basis) const {
  615. return rows[0].is_equal_approx(p_basis.rows[0]) && rows[1].is_equal_approx(p_basis.rows[1]) && rows[2].is_equal_approx(p_basis.rows[2]);
  616. }
  617. bool Basis::operator==(const Basis &p_matrix) const {
  618. for (int i = 0; i < 3; i++) {
  619. for (int j = 0; j < 3; j++) {
  620. if (rows[i][j] != p_matrix.rows[i][j]) {
  621. return false;
  622. }
  623. }
  624. }
  625. return true;
  626. }
  627. bool Basis::operator!=(const Basis &p_matrix) const {
  628. return (!(*this == p_matrix));
  629. }
  630. Basis::operator String() const {
  631. return "[X: " + get_column(0).operator String() +
  632. ", Y: " + get_column(1).operator String() +
  633. ", Z: " + get_column(2).operator String() + "]";
  634. }
  635. Quaternion Basis::get_quaternion() const {
  636. #ifdef MATH_CHECKS
  637. ERR_FAIL_COND_V_MSG(!is_rotation(), Quaternion(), "Basis must be normalized in order to be casted to a Quaternion. Use get_rotation_quaternion() or call orthonormalized() if the Basis contains linearly independent vectors.");
  638. #endif
  639. /* Allow getting a quaternion from an unnormalized transform */
  640. Basis m = *this;
  641. real_t trace = m.rows[0][0] + m.rows[1][1] + m.rows[2][2];
  642. real_t temp[4];
  643. if (trace > 0.0f) {
  644. real_t s = Math::sqrt(trace + 1.0f);
  645. temp[3] = (s * 0.5f);
  646. s = 0.5f / s;
  647. temp[0] = ((m.rows[2][1] - m.rows[1][2]) * s);
  648. temp[1] = ((m.rows[0][2] - m.rows[2][0]) * s);
  649. temp[2] = ((m.rows[1][0] - m.rows[0][1]) * s);
  650. } else {
  651. int i = m.rows[0][0] < m.rows[1][1]
  652. ? (m.rows[1][1] < m.rows[2][2] ? 2 : 1)
  653. : (m.rows[0][0] < m.rows[2][2] ? 2 : 0);
  654. int j = (i + 1) % 3;
  655. int k = (i + 2) % 3;
  656. real_t s = Math::sqrt(m.rows[i][i] - m.rows[j][j] - m.rows[k][k] + 1.0f);
  657. temp[i] = s * 0.5f;
  658. s = 0.5f / s;
  659. temp[3] = (m.rows[k][j] - m.rows[j][k]) * s;
  660. temp[j] = (m.rows[j][i] + m.rows[i][j]) * s;
  661. temp[k] = (m.rows[k][i] + m.rows[i][k]) * s;
  662. }
  663. return Quaternion(temp[0], temp[1], temp[2], temp[3]);
  664. }
  665. void Basis::get_axis_angle(Vector3 &r_axis, real_t &r_angle) const {
  666. /* checking this is a bad idea, because obtaining from scaled transform is a valid use case
  667. #ifdef MATH_CHECKS
  668. ERR_FAIL_COND(!is_rotation());
  669. #endif
  670. */
  671. real_t angle, x, y, z; // variables for result
  672. real_t angle_epsilon = 0.1; // margin to distinguish between 0 and 180 degrees
  673. if ((Math::abs(rows[1][0] - rows[0][1]) < CMP_EPSILON) && (Math::abs(rows[2][0] - rows[0][2]) < CMP_EPSILON) && (Math::abs(rows[2][1] - rows[1][2]) < CMP_EPSILON)) {
  674. // singularity found
  675. // first check for identity matrix which must have +1 for all terms
  676. // in leading diagonal and zero in other terms
  677. if ((Math::abs(rows[1][0] + rows[0][1]) < angle_epsilon) && (Math::abs(rows[2][0] + rows[0][2]) < angle_epsilon) && (Math::abs(rows[2][1] + rows[1][2]) < angle_epsilon) && (Math::abs(rows[0][0] + rows[1][1] + rows[2][2] - 3) < angle_epsilon)) {
  678. // this singularity is identity matrix so angle = 0
  679. r_axis = Vector3(0, 1, 0);
  680. r_angle = 0;
  681. return;
  682. }
  683. // otherwise this singularity is angle = 180
  684. angle = Math_PI;
  685. real_t xx = (rows[0][0] + 1) / 2;
  686. real_t yy = (rows[1][1] + 1) / 2;
  687. real_t zz = (rows[2][2] + 1) / 2;
  688. real_t xy = (rows[1][0] + rows[0][1]) / 4;
  689. real_t xz = (rows[2][0] + rows[0][2]) / 4;
  690. real_t yz = (rows[2][1] + rows[1][2]) / 4;
  691. if ((xx > yy) && (xx > zz)) { // rows[0][0] is the largest diagonal term
  692. if (xx < CMP_EPSILON) {
  693. x = 0;
  694. y = Math_SQRT12;
  695. z = Math_SQRT12;
  696. } else {
  697. x = Math::sqrt(xx);
  698. y = xy / x;
  699. z = xz / x;
  700. }
  701. } else if (yy > zz) { // rows[1][1] is the largest diagonal term
  702. if (yy < CMP_EPSILON) {
  703. x = Math_SQRT12;
  704. y = 0;
  705. z = Math_SQRT12;
  706. } else {
  707. y = Math::sqrt(yy);
  708. x = xy / y;
  709. z = yz / y;
  710. }
  711. } else { // rows[2][2] is the largest diagonal term so base result on this
  712. if (zz < CMP_EPSILON) {
  713. x = Math_SQRT12;
  714. y = Math_SQRT12;
  715. z = 0;
  716. } else {
  717. z = Math::sqrt(zz);
  718. x = xz / z;
  719. y = yz / z;
  720. }
  721. }
  722. r_axis = Vector3(x, y, z);
  723. r_angle = angle;
  724. return;
  725. }
  726. // as we have reached here there are no singularities so we can handle normally
  727. real_t s = Math::sqrt((rows[1][2] - rows[2][1]) * (rows[1][2] - rows[2][1]) + (rows[2][0] - rows[0][2]) * (rows[2][0] - rows[0][2]) + (rows[0][1] - rows[1][0]) * (rows[0][1] - rows[1][0])); // s=|axis||sin(angle)|, used to normalise
  728. angle = Math::acos((rows[0][0] + rows[1][1] + rows[2][2] - 1) / 2);
  729. if (angle < 0) {
  730. s = -s;
  731. }
  732. x = (rows[2][1] - rows[1][2]) / s;
  733. y = (rows[0][2] - rows[2][0]) / s;
  734. z = (rows[1][0] - rows[0][1]) / s;
  735. r_axis = Vector3(x, y, z);
  736. r_angle = angle;
  737. }
  738. void Basis::set_quaternion(const Quaternion &p_quaternion) {
  739. real_t d = p_quaternion.length_squared();
  740. real_t s = 2.0f / d;
  741. real_t xs = p_quaternion.x * s, ys = p_quaternion.y * s, zs = p_quaternion.z * s;
  742. real_t wx = p_quaternion.w * xs, wy = p_quaternion.w * ys, wz = p_quaternion.w * zs;
  743. real_t xx = p_quaternion.x * xs, xy = p_quaternion.x * ys, xz = p_quaternion.x * zs;
  744. real_t yy = p_quaternion.y * ys, yz = p_quaternion.y * zs, zz = p_quaternion.z * zs;
  745. set(1.0f - (yy + zz), xy - wz, xz + wy,
  746. xy + wz, 1.0f - (xx + zz), yz - wx,
  747. xz - wy, yz + wx, 1.0f - (xx + yy));
  748. }
  749. void Basis::set_axis_angle(const Vector3 &p_axis, real_t p_angle) {
  750. // Rotation matrix from axis and angle, see https://en.wikipedia.org/wiki/Rotation_matrix#Rotation_matrix_from_axis_angle
  751. #ifdef MATH_CHECKS
  752. ERR_FAIL_COND_MSG(!p_axis.is_normalized(), "The axis Vector3 must be normalized.");
  753. #endif
  754. Vector3 axis_sq(p_axis.x * p_axis.x, p_axis.y * p_axis.y, p_axis.z * p_axis.z);
  755. real_t cosine = Math::cos(p_angle);
  756. rows[0][0] = axis_sq.x + cosine * (1.0f - axis_sq.x);
  757. rows[1][1] = axis_sq.y + cosine * (1.0f - axis_sq.y);
  758. rows[2][2] = axis_sq.z + cosine * (1.0f - axis_sq.z);
  759. real_t sine = Math::sin(p_angle);
  760. real_t t = 1 - cosine;
  761. real_t xyzt = p_axis.x * p_axis.y * t;
  762. real_t zyxs = p_axis.z * sine;
  763. rows[0][1] = xyzt - zyxs;
  764. rows[1][0] = xyzt + zyxs;
  765. xyzt = p_axis.x * p_axis.z * t;
  766. zyxs = p_axis.y * sine;
  767. rows[0][2] = xyzt + zyxs;
  768. rows[2][0] = xyzt - zyxs;
  769. xyzt = p_axis.y * p_axis.z * t;
  770. zyxs = p_axis.x * sine;
  771. rows[1][2] = xyzt - zyxs;
  772. rows[2][1] = xyzt + zyxs;
  773. }
  774. void Basis::set_axis_angle_scale(const Vector3 &p_axis, real_t p_angle, const Vector3 &p_scale) {
  775. _set_diagonal(p_scale);
  776. rotate(p_axis, p_angle);
  777. }
  778. void Basis::set_euler_scale(const Vector3 &p_euler, const Vector3 &p_scale, EulerOrder p_order) {
  779. _set_diagonal(p_scale);
  780. rotate(p_euler, p_order);
  781. }
  782. void Basis::set_quaternion_scale(const Quaternion &p_quaternion, const Vector3 &p_scale) {
  783. _set_diagonal(p_scale);
  784. rotate(p_quaternion);
  785. }
  786. // This also sets the non-diagonal elements to 0, which is misleading from the
  787. // name, so we want this method to be private. Use `from_scale` externally.
  788. void Basis::_set_diagonal(const Vector3 &p_diag) {
  789. rows[0][0] = p_diag.x;
  790. rows[0][1] = 0;
  791. rows[0][2] = 0;
  792. rows[1][0] = 0;
  793. rows[1][1] = p_diag.y;
  794. rows[1][2] = 0;
  795. rows[2][0] = 0;
  796. rows[2][1] = 0;
  797. rows[2][2] = p_diag.z;
  798. }
  799. Basis Basis::lerp(const Basis &p_to, const real_t &p_weight) const {
  800. Basis b;
  801. b.rows[0] = rows[0].lerp(p_to.rows[0], p_weight);
  802. b.rows[1] = rows[1].lerp(p_to.rows[1], p_weight);
  803. b.rows[2] = rows[2].lerp(p_to.rows[2], p_weight);
  804. return b;
  805. }
  806. Basis Basis::slerp(const Basis &p_to, const real_t &p_weight) const {
  807. //consider scale
  808. Quaternion from(*this);
  809. Quaternion to(p_to);
  810. Basis b(from.slerp(to, p_weight));
  811. b.rows[0] *= Math::lerp(rows[0].length(), p_to.rows[0].length(), p_weight);
  812. b.rows[1] *= Math::lerp(rows[1].length(), p_to.rows[1].length(), p_weight);
  813. b.rows[2] *= Math::lerp(rows[2].length(), p_to.rows[2].length(), p_weight);
  814. return b;
  815. }
  816. void Basis::rotate_sh(real_t *p_values) {
  817. // code by John Hable
  818. // http://filmicworlds.com/blog/simple-and-fast-spherical-harmonic-rotation/
  819. // this code is Public Domain
  820. const static real_t s_c3 = 0.94617469575; // (3*sqrt(5))/(4*sqrt(pi))
  821. const static real_t s_c4 = -0.31539156525; // (-sqrt(5))/(4*sqrt(pi))
  822. const static real_t s_c5 = 0.54627421529; // (sqrt(15))/(4*sqrt(pi))
  823. const static real_t s_c_scale = 1.0 / 0.91529123286551084;
  824. const static real_t s_c_scale_inv = 0.91529123286551084;
  825. const static real_t s_rc2 = 1.5853309190550713 * s_c_scale;
  826. const static real_t s_c4_div_c3 = s_c4 / s_c3;
  827. const static real_t s_c4_div_c3_x2 = (s_c4 / s_c3) * 2.0;
  828. const static real_t s_scale_dst2 = s_c3 * s_c_scale_inv;
  829. const static real_t s_scale_dst4 = s_c5 * s_c_scale_inv;
  830. const real_t src[9] = { p_values[0], p_values[1], p_values[2], p_values[3], p_values[4], p_values[5], p_values[6], p_values[7], p_values[8] };
  831. real_t m00 = rows[0][0];
  832. real_t m01 = rows[0][1];
  833. real_t m02 = rows[0][2];
  834. real_t m10 = rows[1][0];
  835. real_t m11 = rows[1][1];
  836. real_t m12 = rows[1][2];
  837. real_t m20 = rows[2][0];
  838. real_t m21 = rows[2][1];
  839. real_t m22 = rows[2][2];
  840. p_values[0] = src[0];
  841. p_values[1] = m11 * src[1] - m12 * src[2] + m10 * src[3];
  842. p_values[2] = -m21 * src[1] + m22 * src[2] - m20 * src[3];
  843. p_values[3] = m01 * src[1] - m02 * src[2] + m00 * src[3];
  844. real_t sh0 = src[7] + src[8] + src[8] - src[5];
  845. real_t sh1 = src[4] + s_rc2 * src[6] + src[7] + src[8];
  846. real_t sh2 = src[4];
  847. real_t sh3 = -src[7];
  848. real_t sh4 = -src[5];
  849. // Rotations. R0 and R1 just use the raw matrix columns
  850. real_t r2x = m00 + m01;
  851. real_t r2y = m10 + m11;
  852. real_t r2z = m20 + m21;
  853. real_t r3x = m00 + m02;
  854. real_t r3y = m10 + m12;
  855. real_t r3z = m20 + m22;
  856. real_t r4x = m01 + m02;
  857. real_t r4y = m11 + m12;
  858. real_t r4z = m21 + m22;
  859. // dense matrix multiplication one column at a time
  860. // column 0
  861. real_t sh0_x = sh0 * m00;
  862. real_t sh0_y = sh0 * m10;
  863. real_t d0 = sh0_x * m10;
  864. real_t d1 = sh0_y * m20;
  865. real_t d2 = sh0 * (m20 * m20 + s_c4_div_c3);
  866. real_t d3 = sh0_x * m20;
  867. real_t d4 = sh0_x * m00 - sh0_y * m10;
  868. // column 1
  869. real_t sh1_x = sh1 * m02;
  870. real_t sh1_y = sh1 * m12;
  871. d0 += sh1_x * m12;
  872. d1 += sh1_y * m22;
  873. d2 += sh1 * (m22 * m22 + s_c4_div_c3);
  874. d3 += sh1_x * m22;
  875. d4 += sh1_x * m02 - sh1_y * m12;
  876. // column 2
  877. real_t sh2_x = sh2 * r2x;
  878. real_t sh2_y = sh2 * r2y;
  879. d0 += sh2_x * r2y;
  880. d1 += sh2_y * r2z;
  881. d2 += sh2 * (r2z * r2z + s_c4_div_c3_x2);
  882. d3 += sh2_x * r2z;
  883. d4 += sh2_x * r2x - sh2_y * r2y;
  884. // column 3
  885. real_t sh3_x = sh3 * r3x;
  886. real_t sh3_y = sh3 * r3y;
  887. d0 += sh3_x * r3y;
  888. d1 += sh3_y * r3z;
  889. d2 += sh3 * (r3z * r3z + s_c4_div_c3_x2);
  890. d3 += sh3_x * r3z;
  891. d4 += sh3_x * r3x - sh3_y * r3y;
  892. // column 4
  893. real_t sh4_x = sh4 * r4x;
  894. real_t sh4_y = sh4 * r4y;
  895. d0 += sh4_x * r4y;
  896. d1 += sh4_y * r4z;
  897. d2 += sh4 * (r4z * r4z + s_c4_div_c3_x2);
  898. d3 += sh4_x * r4z;
  899. d4 += sh4_x * r4x - sh4_y * r4y;
  900. // extra multipliers
  901. p_values[4] = d0;
  902. p_values[5] = -d1;
  903. p_values[6] = d2 * s_scale_dst2;
  904. p_values[7] = -d3;
  905. p_values[8] = d4 * s_scale_dst4;
  906. }
  907. Basis Basis::looking_at(const Vector3 &p_target, const Vector3 &p_up) {
  908. #ifdef MATH_CHECKS
  909. ERR_FAIL_COND_V_MSG(p_target.is_equal_approx(Vector3()), Basis(), "The target vector can't be zero.");
  910. ERR_FAIL_COND_V_MSG(p_up.is_equal_approx(Vector3()), Basis(), "The up vector can't be zero.");
  911. #endif
  912. Vector3 v_z = -p_target.normalized();
  913. Vector3 v_x = p_up.cross(v_z);
  914. #ifdef MATH_CHECKS
  915. ERR_FAIL_COND_V_MSG(v_x.is_equal_approx(Vector3()), Basis(), "The target vector and up vector can't be parallel to each other.");
  916. #endif
  917. v_x.normalize();
  918. Vector3 v_y = v_z.cross(v_x);
  919. Basis basis;
  920. basis.set_columns(v_x, v_y, v_z);
  921. return basis;
  922. }