Source: extern/transform.js

  1. import register from "../util/register.js";
  2. import * as m_bounds from "../intern/boundings.js";
  3. import m_obj_util_fact from "../intern/obj_util.js";
  4. import m_phy_fact from "../intern/physics.js";
  5. import m_print_fact from "../intern/print.js";
  6. import * as m_quat from "../libs/gl_matrix/quat.js";
  7. import m_trans_fact from "../intern/transform.js";
  8. import * as m_tsr from "../intern/tsr.js";
  9. import * as m_util from "../intern/util.js";
  10. /**
  11. * Object transformations API.
  12. * With some exceptions specified below, make sure that the objects are dynamic.
  13. * @module transform
  14. */
  15. function Transform(ns, exports) {
  16. var m_obj_util = m_obj_util_fact(ns);
  17. var m_phy = m_phy_fact(ns);
  18. var m_print = m_print_fact(ns);
  19. var m_trans = m_trans_fact(ns);
  20. var _tsr_tmp = m_tsr.create();
  21. var _vec3_tmp = new Float32Array(3);
  22. var _quat4_tmp = new Float32Array(4);
  23. /**
  24. * Set the object translation.
  25. * @method module:transform.set_translation
  26. * @param {Object3D} obj Object 3D
  27. * @param {number} x X coord
  28. * @param {number} y Y coord
  29. * @param {number} z Z coord
  30. */
  31. exports.set_translation = function(obj, x, y, z) {
  32. if (m_obj_util.is_dynamic(obj)) {
  33. _vec3_tmp[0] = x;
  34. _vec3_tmp[1] = y;
  35. _vec3_tmp[2] = z;
  36. m_trans.set_translation(obj, _vec3_tmp);
  37. m_trans.update_transform(obj);
  38. m_phy.sync_transform(obj);
  39. } else
  40. m_print.error("Wrong object: \"" + obj.name + "\" is not dynamic.");
  41. }
  42. /**
  43. * Set the object translation (in the coordinate space of its parent).
  44. * @method module:transform.set_translation_rel
  45. * @param {Object3D} obj Object 3D
  46. * @param {number} x X coord
  47. * @param {number} y Y coord
  48. * @param {number} z Z coord
  49. */
  50. exports.set_translation_rel = function(obj, x, y, z) {
  51. if (m_obj_util.is_dynamic(obj)) {
  52. _vec3_tmp[0] = x;
  53. _vec3_tmp[1] = y;
  54. _vec3_tmp[2] = z;
  55. m_trans.set_translation_rel(obj, _vec3_tmp);
  56. m_trans.update_transform(obj);
  57. m_phy.sync_transform(obj);
  58. } else
  59. m_print.error("Wrong object: \"" + obj.name + "\" is not dynamic.");
  60. }
  61. /**
  62. * Set the object translation (vector form).
  63. * @method module:transform.set_translation_v
  64. * @param {Object3D} obj Object 3D
  65. * @param {Vec3} trans Translation vector
  66. */
  67. exports.set_translation_v = function(obj, trans) {
  68. if (m_obj_util.is_dynamic(obj)) {
  69. m_trans.set_translation(obj, trans);
  70. m_trans.update_transform(obj);
  71. m_phy.sync_transform(obj);
  72. } else
  73. m_print.error("Wrong object: \"" + obj.name + "\" is not dynamic.");
  74. }
  75. /**
  76. * Set the object translation in vector form
  77. * (in the coordinate space of its parent).
  78. * @method module:transform.set_translation_rel_v
  79. * @param {Object3D} obj Object 3D
  80. * @param {Vec3} trans Translation vector
  81. */
  82. exports.set_translation_rel_v = function(obj, trans) {
  83. if (m_obj_util.is_dynamic(obj)) {
  84. m_trans.set_translation_rel(obj, trans);
  85. m_trans.update_transform(obj);
  86. m_phy.sync_transform(obj);
  87. } else
  88. m_print.error("Wrong object: \"" + obj.name + "\" is not dynamic.");
  89. }
  90. /**
  91. * Set the object translation relatively to another object.
  92. * @method module:transform.set_translation_obj_rel
  93. * @param {Object3D} obj Transformed object
  94. * @param {number} x X coord
  95. * @param {number} y Y coord
  96. * @param {number} z Z coord
  97. * @param {Object3D} obj_ref Reference object
  98. */
  99. exports.set_translation_obj_rel = function(obj, x, y, z, obj_ref) {
  100. if (m_obj_util.is_dynamic(obj)) {
  101. _vec3_tmp[0] = x;
  102. _vec3_tmp[1] = y;
  103. _vec3_tmp[2] = z;
  104. var trans = m_tsr.get_trans_view(obj_ref.render.world_tsr);
  105. var quat = m_tsr.get_quat_view(obj_ref.render.world_tsr);
  106. m_util.transform_vec3(_vec3_tmp, 1, quat, trans, _vec3_tmp);
  107. m_trans.set_translation(obj, _vec3_tmp);
  108. m_trans.update_transform(obj);
  109. m_phy.sync_transform(obj);
  110. } else
  111. m_print.error("Wrong object: \"" + obj.name + "\" is not dynamic.");
  112. }
  113. /**
  114. * Get the object's translation vector.
  115. * @method module:transform.get_translation
  116. * @param {Object3D} obj Object 3D
  117. * @param {Vec3} [dest] Destination vector
  118. * @returns {Vec3} Destination vector
  119. * @example
  120. * var m_scenes = require("scenes");
  121. * var m_trans = require("transform");
  122. * var m_vec3 = require("vec3");
  123. * // precache 3D vector
  124. * var _vec3_tmp = m_vec3.create();
  125. * // ...
  126. * var cube = m_scenes.get_object_by_name("Cube");
  127. * var translation = m_trans.get_translation(cube, _vec3_tmp);
  128. */
  129. exports.get_translation = function(obj, dest) {
  130. if (!dest)
  131. dest = new Float32Array(3);
  132. m_trans.get_translation(obj, dest);
  133. return dest;
  134. }
  135. /**
  136. * Get the object's translation vector (in the coordinate space of its parent).
  137. * @method module:transform.get_translation_rel
  138. * @param {Object3D} obj Object 3D
  139. * @param {Vec3} [dest] Destination vector
  140. * @returns {Vec3} Destination vector
  141. */
  142. exports.get_translation_rel = function(obj, dest) {
  143. if (!dest)
  144. dest = new Float32Array(3);
  145. m_trans.get_translation_rel(obj, dest);
  146. return dest;
  147. }
  148. /**
  149. * Set the object's rotation quaternion.
  150. * @method module:transform.set_rotation
  151. * @param {Object3D} obj Object 3D
  152. * @param {number} x X part of quaternion
  153. * @param {number} y Y part of quaternion
  154. * @param {number} z Z part of quaternion
  155. * @param {number} w W part of quaternion
  156. */
  157. exports.set_rotation = function(obj, x, y, z, w) {
  158. if (m_obj_util.is_dynamic(obj)) {
  159. _quat4_tmp[0] = x;
  160. _quat4_tmp[1] = y;
  161. _quat4_tmp[2] = z;
  162. _quat4_tmp[3] = w;
  163. m_trans.set_rotation(obj, _quat4_tmp);
  164. m_trans.update_transform(obj);
  165. m_phy.sync_transform(obj);
  166. } else
  167. m_print.error("Wrong object: \"" + obj.name + "\" is not dynamic.");
  168. }
  169. /**
  170. * Set the object's rotation quaternion (in the coordinate space of its parent).
  171. * @method module:transform.set_rotation_rel
  172. * @param {Object3D} obj Object 3D
  173. * @param {number} x X part of quaternion
  174. * @param {number} y Y part of quaternion
  175. * @param {number} z Z part of quaternion
  176. * @param {number} w W part of quaternion
  177. */
  178. exports.set_rotation_rel = function(obj, x, y, z, w) {
  179. if (m_obj_util.is_dynamic(obj)) {
  180. _quat4_tmp[0] = x;
  181. _quat4_tmp[1] = y;
  182. _quat4_tmp[2] = z;
  183. _quat4_tmp[3] = w;
  184. m_trans.set_rotation_rel(obj, _quat4_tmp);
  185. m_trans.update_transform(obj);
  186. m_phy.sync_transform(obj);
  187. } else
  188. m_print.error("Wrong object: \"" + obj.name + "\" is not dynamic.");
  189. }
  190. /**
  191. * Set the object's rotation in vector form.
  192. * @method module:transform.set_rotation_v
  193. * @param {Object3D} obj Object 3D
  194. * @param {Quat} quat Quaternion vector
  195. */
  196. exports.set_rotation_v = function(obj, quat) {
  197. if (m_obj_util.is_dynamic(obj)) {
  198. m_trans.set_rotation(obj, quat);
  199. m_trans.update_transform(obj);
  200. m_phy.sync_transform(obj);
  201. } else
  202. m_print.error("Wrong object: \"" + obj.name + "\" is not dynamic.");
  203. }
  204. /**
  205. * Set the object's rotation in vector form
  206. * (in the coordinate space of its parent).
  207. * @method module:transform.set_rotation_rel_v
  208. * @param {Object3D} obj Object 3D
  209. * @param {Quat} quat Quaternion vector
  210. */
  211. exports.set_rotation_rel_v = function(obj, quat) {
  212. if (m_obj_util.is_dynamic(obj)) {
  213. m_trans.set_rotation_rel(obj, quat);
  214. m_trans.update_transform(obj);
  215. m_phy.sync_transform(obj);
  216. } else
  217. m_print.error("Wrong object: \"" + obj.name + "\" is not dynamic.");
  218. }
  219. /**
  220. * Get the object's rotation in vector form
  221. * (in the coordinate space of its parent).
  222. * @method module:transform.set_rotation_rel_v
  223. * @param {Object3D} obj Object 3D
  224. * @param {Quat} quat Quaternion vector
  225. * @returns {Quat} Destination vector
  226. * @example
  227. * var m_scenes = require("scenes");
  228. * var m_trans = require("transform");
  229. * var m_quat = require("quat");
  230. * // precache quaternion
  231. * var _quat_tmp = m_quat.create();
  232. * // ...
  233. * var cube = m_scenes.get_object_by_name("Cube");
  234. * var rot_quat = m_trans.get_rotation_rel_v(cube, _quat_tmp);
  235. */
  236. exports.get_rotation_rel_v = function(obj, quat) {
  237. if (m_obj_util.is_dynamic(obj)) {
  238. m_trans.get_rotation_rel(obj, quat);
  239. return quat;
  240. } else
  241. m_print.error("Wrong object: \"" + obj.name + "\" is not dynamic.");
  242. }
  243. /**
  244. * Get the object's rotation quaternion.
  245. * @method module:transform.get_rotation
  246. * @param {Object3D} obj Object 3D
  247. * @param {Quat} [opt_dest] Destination vector
  248. * @returns {Quat} Destination vector
  249. * @example
  250. * var m_scenes = require("scenes");
  251. * var m_trans = require("transform");
  252. * var m_quat = require("quat");
  253. * // precache quaternion
  254. * var _quat_tmp = m_quat.create();
  255. * // ...
  256. * var cube = m_scenes.get_object_by_name("Cube");
  257. * var rot_quat = m_trans.get_rotation(cube, _quat_tmp);
  258. */
  259. exports.get_rotation = function(obj, opt_dest) {
  260. if (!opt_dest)
  261. opt_dest = new Float32Array(4);
  262. m_trans.get_rotation(obj, opt_dest);
  263. return opt_dest;
  264. }
  265. /**
  266. * Get the object's rotation quaternion
  267. * (in the coordinate space of its parent).
  268. * @method module:transform.get_rotation_rel
  269. * @param {Object3D} obj Object 3D
  270. * @param {Quat} [opt_dest] Destination vector
  271. * @returns {Quat} Destination vector
  272. */
  273. exports.get_rotation_rel = function(obj, opt_dest) {
  274. if (!opt_dest)
  275. opt_dest = new Float32Array(4);
  276. m_trans.get_rotation_rel(obj, opt_dest);
  277. return opt_dest;
  278. }
  279. /**
  280. * Set euler rotation in the ZYX intrinsic system.
  281. * Using euler angles is discouraged, use quaternion instead.
  282. * @method module:transform.set_rotation_euler
  283. * @param {Object3D} obj Object 3D
  284. * @param {number} x Angle X in radians
  285. * @param {number} y Angle Y in radians
  286. * @param {number} z Angle Z in radians
  287. */
  288. exports.set_rotation_euler = function(obj, x, y, z) {
  289. if (m_obj_util.is_dynamic(obj)) {
  290. _vec3_tmp[0] = x;
  291. _vec3_tmp[1] = y;
  292. _vec3_tmp[2] = z;
  293. m_trans.set_rotation_euler(obj, _vec3_tmp);
  294. m_trans.update_transform(obj);
  295. m_phy.sync_transform(obj);
  296. } else
  297. m_print.error("Wrong object: \"" + obj.name + "\" is not dynamic.");
  298. }
  299. /**
  300. * Get Euler rotation of object in the ZYX intrinsic system.
  301. * Using euler angles is discouraged, use quaternion instead.
  302. * @method module:transform.get_rotation_euler
  303. * @param {Object3D} obj Object 3D
  304. * @param {Euler} [dest = new Float32Array(3)] Destination vector
  305. * @returns {Euler} Vector with Euler angles
  306. */
  307. exports.get_rotation_euler = function(obj, dest) {
  308. if (!dest)
  309. dest = new Float32Array(3);
  310. m_trans.get_rotation_euler(obj, dest);
  311. return dest;
  312. }
  313. /**
  314. * Get Euler rotation of object in the ZYX intrinsic system
  315. * (in the coordinate space of its parent).
  316. * Using euler angles is discouraged, use quaternion instead.
  317. * @method module:transform.get_rotation_euler
  318. * @param {Object3D} obj Object 3D
  319. * @param {Euler} [dest = new Float32Array(3)] Destination vector
  320. * @returns {Euler} Vector with Euler angles
  321. */
  322. exports.get_rotation_euler_rel = function(obj, dest) {
  323. if (!dest)
  324. dest = new Float32Array(3);
  325. m_trans.get_rotation_euler_rel(obj, dest);
  326. return dest;
  327. }
  328. /**
  329. * Set euler rotation in the ZYX intrinsic system
  330. * (in the coordinate space of its parent).
  331. * Using euler angles is discouraged, use quaternion instead.
  332. * @method module:transform.set_rotation_euler_rel
  333. * @param {Object3D} obj Object 3D
  334. * @param {number} x Angle X in radians
  335. * @param {number} y Angle Y in radians
  336. * @param {number} z Angle Z in radians
  337. */
  338. exports.set_rotation_euler_rel = function(obj, x, y, z) {
  339. if (m_obj_util.is_dynamic(obj)) {
  340. _vec3_tmp[0] = x;
  341. _vec3_tmp[1] = y;
  342. _vec3_tmp[2] = z;
  343. m_trans.set_rotation_euler_rel(obj, _vec3_tmp);
  344. m_trans.update_transform(obj);
  345. m_phy.sync_transform(obj);
  346. } else
  347. m_print.error("Wrong object: \"" + obj.name + "\" is not dynamic.");
  348. }
  349. /**
  350. * Set euler rotation in vector form.
  351. * Using euler angles is discouraged, use quaternion instead.
  352. * @method module:transform.set_rotation_euler_v
  353. * @param {Object3D} obj Object 3D
  354. * @param {Euler} euler Vector with euler angles
  355. */
  356. exports.set_rotation_euler_v = function(obj, euler) {
  357. if (m_obj_util.is_dynamic(obj)) {
  358. m_trans.set_rotation_euler(obj, euler);
  359. m_trans.update_transform(obj);
  360. m_phy.sync_transform(obj);
  361. } else
  362. m_print.error("Wrong object: \"" + obj.name + "\" is not dynamic.");
  363. }
  364. /**
  365. * Set euler rotation in vector form relatively to its parent
  366. * (in the coordinate space of its parent).
  367. * Using euler angles is discouraged, use quaternion instead.
  368. * @method module:transform.set_rotation_euler_rel_v
  369. * @param {Object3D} obj Object 3D
  370. * @param {Euler} euler Vector with euler angles
  371. */
  372. exports.set_rotation_euler_rel_v = function(obj, euler) {
  373. if (m_obj_util.is_dynamic(obj)) {
  374. m_trans.set_rotation_euler_rel(obj, euler);
  375. m_trans.update_transform(obj);
  376. m_phy.sync_transform(obj);
  377. } else
  378. m_print.error("Wrong object: \"" + obj.name + "\" is not dynamic.");
  379. }
  380. /**
  381. * Set the object scale.
  382. * @method module:transform.set_scale
  383. * @param {Object3D} obj Object 3D
  384. * @param {number} scale Object scale
  385. */
  386. exports.set_scale = function(obj, scale) {
  387. if (m_obj_util.is_dynamic(obj)) {
  388. m_trans.set_scale(obj, scale);
  389. m_trans.update_transform(obj);
  390. } else
  391. m_print.error("Wrong object: \"" + obj.name + "\" is not dynamic.");
  392. }
  393. /**
  394. * Set the object's scale
  395. * (in the coordinate space of its parent).
  396. * @method module:transform.set_scale_rel
  397. * @param {Object3D} obj Object 3D
  398. * @param {number} scale Object scale
  399. */
  400. exports.set_scale_rel = function(obj, scale) {
  401. if (m_obj_util.is_dynamic(obj)) {
  402. m_trans.set_scale_rel(obj, scale);
  403. m_trans.update_transform(obj);
  404. } else
  405. m_print.error("Wrong object: \"" + obj.name + "\" is not dynamic.");
  406. }
  407. /**
  408. * Get the object scale.
  409. * @method module:transform.get_scale
  410. * @param {Object3D} obj Object 3D
  411. * @returns {number} scale
  412. */
  413. exports.get_scale = function(obj) {
  414. return m_trans.get_scale(obj);
  415. }
  416. /**
  417. * Get the object scale
  418. * (in the coordinate space of its parent).
  419. * @method module:transform.get_scale_rel
  420. * @param {Object3D} obj Object 3D
  421. * @returns {number} scale
  422. */
  423. exports.get_scale_rel = function(obj) {
  424. return m_trans.get_scale_rel(obj);
  425. }
  426. /**
  427. * Reset EMPTY's transform to allow child objects behave in the absolute (world) space.
  428. * @method module:transform.empty_reset_transform
  429. * @param {Object3D} obj Object 3D
  430. */
  431. exports.empty_reset_transform = function(obj) {
  432. if (obj.type != "EMPTY") {
  433. m_print.error("Wrong object: " + obj.name);
  434. return;
  435. }
  436. for (var i = 0; i < obj.cons_descends.length; i++)
  437. if (!m_obj_util.is_dynamic(obj.cons_descends[i])) {
  438. m_print.error("Wrong object: \"" + obj.cons_descends[i].name
  439. + "\" is not dynamic.");
  440. return;
  441. }
  442. m_trans.set_translation(obj, [0, 0, 0]);
  443. m_trans.set_rotation(obj, [0, 0, 0, 1]);
  444. m_trans.set_scale(obj, 1);
  445. m_trans.update_transform(obj);
  446. m_phy.sync_transform(obj);
  447. }
  448. /**
  449. * Get object size (maximum radius, calculated from bounding box).
  450. * @method module:transform.get_object_size
  451. * @param {Object3D} obj Object 3D
  452. * @returns {number} Object size
  453. */
  454. exports.get_object_size = function(obj) {
  455. if (!m_obj_util.is_mesh(obj)) {
  456. m_print.error("Wrong object: " + obj.name);
  457. return 0;
  458. }
  459. return m_trans.get_object_size(obj);
  460. }
  461. /**
  462. * Get the object center in the world space.
  463. * Works for dynamic and static objects.
  464. * @method module:transform.get_object_center
  465. * @param {Object3D} obj Object 3D
  466. * @param {boolean} calc_bs_center Use the object's bounding sphere to
  467. * calculate center, otherwise use the bounding box.
  468. * @param {Vec3} [dest] Destination vector
  469. * @returns {Vec3} Destination vector
  470. */
  471. exports.get_object_center = function(obj, calc_bs_center, dest) {
  472. if (!m_obj_util.is_mesh(obj)) {
  473. m_print.error("Wrong object: " + obj.name);
  474. return null;
  475. }
  476. if (!dest)
  477. dest = new Float32Array(3);
  478. return m_trans.get_object_center(obj, calc_bs_center, dest);
  479. }
  480. /**
  481. * Perform incremental object translation in the local space.
  482. * @method module:transform.move_local
  483. * @param {Object3D} obj Object 3D
  484. * @param {number} dx Translation offset along X axis
  485. * @param {number} dy Translation offset along Y axis
  486. * @param {number} dz Translation offset along Z axis
  487. */
  488. exports.move_local = function(obj, dx, dy, dz) {
  489. if (m_obj_util.is_dynamic(obj)) {
  490. m_trans.move_local(obj, dx, dy, dz);
  491. m_trans.update_transform(obj);
  492. m_phy.sync_transform(obj);
  493. } else
  494. m_print.error("Wrong object: \"" + obj.name + "\" is not dynamic.");
  495. }
  496. /**
  497. * Perform incremental rotation around X axis in the local space.
  498. * @method module:transform.rotate_x_local
  499. * @param {Object3D} obj Object 3D
  500. * @param {number} angle Angle in radians
  501. */
  502. exports.rotate_x_local = function(obj, angle) {
  503. if (m_obj_util.is_dynamic(obj)) {
  504. var quat = m_quat.setAxisAngle(m_util.AXIS_X, angle, _quat4_tmp);
  505. m_trans.rotate_local(obj, quat);
  506. m_trans.update_transform(obj);
  507. m_phy.sync_transform(obj);
  508. } else
  509. m_print.error("Wrong object: \"" + obj.name + "\" is not dynamic.");
  510. }
  511. /**
  512. * Perform incremental rotation around Y axis in the local space.
  513. * @method module:transform.rotate_y_local
  514. * @param {Object3D} obj Object 3D
  515. * @param {number} angle Angle in radians
  516. */
  517. exports.rotate_y_local = function(obj, angle) {
  518. if (m_obj_util.is_dynamic(obj)) {
  519. var quat = m_quat.setAxisAngle(m_util.AXIS_MY, angle, _quat4_tmp);
  520. m_trans.rotate_local(obj, quat);
  521. m_trans.update_transform(obj);
  522. m_phy.sync_transform(obj);
  523. } else
  524. m_print.error("Wrong object: \"" + obj.name + "\" is not dynamic.");
  525. }
  526. /**
  527. * Perform incremental rotation around Z axis in the local space.
  528. * @method module:transform.rotate_z_local
  529. * @param {Object3D} obj Object 3D
  530. * @param {number} angle Angle in radians
  531. */
  532. exports.rotate_z_local = function(obj, angle) {
  533. if (m_obj_util.is_dynamic(obj)) {
  534. var quat = m_quat.setAxisAngle(m_util.AXIS_Z, angle, _quat4_tmp);
  535. m_trans.rotate_local(obj, quat);
  536. m_trans.update_transform(obj);
  537. m_phy.sync_transform(obj);
  538. } else
  539. m_print.error("Wrong object: \"" + obj.name + "\" is not dynamic.");
  540. }
  541. /**
  542. * Get object bounding box.
  543. * @method module:transform.get_object_bounding_box
  544. * @param {Object3D} obj Object 3D
  545. * @returns {BoundingBox} Bounding box
  546. * @cc_externs max_x min_x max_y min_y max_z min_z
  547. */
  548. exports.get_object_bounding_box = function(obj) {
  549. return m_bounds.clone_bb(obj.render.bb_world);
  550. }
  551. /**
  552. * Set the object's TSR vector.
  553. * @method module:transform.set_tsr
  554. * @param {Object3D} obj Object 3D
  555. * @param {TSR} tsr vector
  556. */
  557. exports.set_tsr = function(obj, tsr) {
  558. if (m_obj_util.is_dynamic(obj)) {
  559. m_trans.set_tsr(obj, tsr);
  560. m_trans.update_transform(obj);
  561. m_phy.sync_transform(obj);
  562. } else
  563. m_print.error("Wrong object: \"" + obj.name + "\" is not dynamic.");
  564. }
  565. /**
  566. * Set the object's TSR vector
  567. * (in the coordinate space of its parent).
  568. * @method module:transform.set_tsr_rel
  569. * @param {Object3D} obj Object 3D
  570. * @param {TSR} tsr vector
  571. */
  572. exports.set_tsr_rel = function(obj, tsr) {
  573. if (m_obj_util.is_dynamic(obj)) {
  574. m_trans.set_tsr_rel(obj, tsr);
  575. m_trans.update_transform(obj);
  576. m_phy.sync_transform(obj);
  577. } else
  578. m_print.error("Wrong object: \"" + obj.name + "\" is not dynamic.");
  579. }
  580. /**
  581. * Return an object's transformation in TSR form.
  582. * @method module:transform.get_tsr
  583. * @param {Object3D} obj Object 3D
  584. * @param {TSR} [dest] Destination vector.
  585. * @returns {TSR} Destination vector.
  586. */
  587. exports.get_tsr = function(obj, dest) {
  588. if (!dest)
  589. dest = new Float32Array(8);
  590. m_trans.get_tsr(obj, dest);
  591. return dest;
  592. }
  593. /**
  594. * Return the object transformation in TSR form.
  595. * (in the coordinate space of its parent).
  596. * @method module:transform.get_tsr_rel
  597. * @param {Object3D} obj Object 3D
  598. * @param {TSR} [dest] Destination vector.
  599. * @returns {TSR} Destination vector.
  600. */
  601. exports.get_tsr_rel = function(obj, dest) {
  602. if (!dest)
  603. dest = new Float32Array(8);
  604. m_trans.get_tsr_rel(obj, dest);
  605. return dest;
  606. }
  607. /**
  608. * Get distance between the two objects.
  609. * @method module:transform.distance
  610. * @param {Object3D} obj1 The first object.
  611. * @param {Object3D} obj2 The second object.
  612. * @returns {number} Distance
  613. */
  614. exports.distance = function(obj1, obj2) {
  615. return m_trans.distance(obj1, obj2);
  616. }
  617. /**
  618. * Set the object's transformation matrix.
  619. * It's better to use TSR form.
  620. * @method module:transform.set_matrix
  621. * @param {Object3D} obj Object 3D
  622. * @param {Mat4} mat Matrix
  623. */
  624. exports.set_matrix = function(obj, mat) {
  625. if (m_obj_util.is_dynamic(obj)) {
  626. m_tsr.from_mat4(mat, _tsr_tmp);
  627. m_trans.set_tsr(obj, _tsr_tmp);
  628. m_trans.update_transform(obj);
  629. m_phy.sync_transform(obj);
  630. } else
  631. m_print.error("Wrong object: \"" + obj.name + "\" is not dynamic.");
  632. }
  633. /**
  634. * Set the object's transformation matrix
  635. * (in the coordinate space of its parent).
  636. * It's better to use TSR form.
  637. * @method module:transform.set_matrix_rel
  638. * @param {Object3D} obj Object 3D
  639. * @param {Mat4} mat Matrix
  640. */
  641. exports.set_matrix_rel = function(obj, mat) {
  642. if (m_obj_util.is_dynamic(obj)) {
  643. m_tsr.from_mat4(mat, _tsr_tmp);
  644. m_trans.set_tsr_rel(obj, _tsr_tmp);
  645. m_trans.update_transform(obj);
  646. m_phy.sync_transform(obj);
  647. } else
  648. m_print.error("Wrong object: \"" + obj.name + "\" is not dynamic.");
  649. }
  650. /**
  651. * Return the object's transformation matrix.
  652. * It's better to use TSR form.
  653. * @method module:transform.get_matrix
  654. * @param {Object3D} obj Object 3D
  655. * @param {Mat4} [dest] Destination matrix.
  656. * @returns {Mat4} Destination matrix.
  657. */
  658. exports.get_matrix = function(obj, dest) {
  659. if (!dest)
  660. dest = new Float32Array(16);
  661. m_trans.get_tsr(obj, _tsr_tmp);
  662. m_tsr.to_mat4(_tsr_tmp, dest);
  663. return dest;
  664. }
  665. /**
  666. * Return the object's transformation matrix
  667. * (in the coordinate space of its parent).
  668. * It's better to use TSR form.
  669. * @method module:transform.get_matrix_rel
  670. * @param {Object3D} obj Object 3D
  671. * @param {Mat4} [dest] Destination matrix.
  672. * @returns {Mat4} Destination matrix.
  673. */
  674. exports.get_matrix_rel = function(obj, dest) {
  675. if (!dest)
  676. dest = new Float32Array(16);
  677. m_trans.get_tsr_rel(obj, _tsr_tmp);
  678. m_tsr.to_mat4(_tsr_tmp, dest);
  679. return dest;
  680. }
  681. }
  682. var transform_factory = register("transform", Transform);
  683. export default transform_factory;