tsr.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /**
  2. * Copyright (C) 2014-2016 Triumph LLC
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. "use strict";
  18. /**
  19. * {@link TSR} (translation, scale, rotation} utility routines.
  20. * @module tsr
  21. * @see https://www.blend4web.com/doc/en/objects.html#moving-via-tsr-vectors
  22. */
  23. b4w.module["tsr"] = function(exports, require) {
  24. var m_mat4 = require("__mat4");
  25. var m_print = require("__print");
  26. var m_tsr = require("__tsr");
  27. /**
  28. * Create a new identity TSR vector.
  29. * @method module:tsr.create
  30. * @returns {TSR} New TSR vector
  31. */
  32. exports.create = m_tsr.create;
  33. /**
  34. * Create a new TSR vector from given values.
  35. * @method module:tsr.from_values
  36. * @param {Number} x X translation.
  37. * @param {Number} y Y translation.
  38. * @param {Number} z Z translation.
  39. * @param {Number} s Scale.
  40. * @param {Number} qx X quaternion rotation.
  41. * @param {Number} qy Y quaternion rotation.
  42. * @param {Number} qz Z quaternion rotation.
  43. * @param {Number} qw W quaternion rotation.
  44. * @returns {TSR} New TSR vector
  45. */
  46. exports.from_values = m_tsr.from_values;
  47. /**
  48. * Copy one TSR vector to another.
  49. * @method module:tsr.copy
  50. * @param {TSR} tsr Source TSR vector
  51. * @param {TSR} tsr2 Destination TSR vector
  52. */
  53. exports.copy = m_tsr.copy;
  54. /**
  55. * Set TSR to identity.
  56. * @method module:tsr.identity
  57. * @param {TSR} tsr TSR vector
  58. */
  59. exports.identity = m_tsr.identity;
  60. /**
  61. * Create a new TSR from separate trans, scale and quat.
  62. * @method module:tsr.create_sep
  63. * @param {Vec3} trans Translation vector
  64. * @param {Number} scale Scale
  65. * @param {Quat} quat Rotation quaternion
  66. * @param {TSR} [dest] Destination TSR vector
  67. * @returns {TSR} dest Destination TSR vector
  68. * @deprecated use set_sep() instead
  69. */
  70. exports.create_sep = create_sep;
  71. function create_sep(trans, scale, quat, dest) {
  72. m_print.error_deprecated("create_sep", "set_sep");
  73. return set_sep(trans, scale, quat, dest);
  74. }
  75. /**
  76. * Set TSR from separate trans, scale and quat.
  77. * @method module:tsr.set_sep
  78. * @param {Vec3} trans Translation vector
  79. * @param {Number} scale Scale
  80. * @param {Quat} quat Rotation quaternion
  81. * @param {TSR} [dest] Destination TSR vector
  82. * @returns {TSR} dest Destination TSR vector
  83. */
  84. exports.set_sep = m_tsr.set_sep;
  85. function set_sep(trans, scale, quat, dest) {
  86. if (!dest)
  87. var dest = m_tsr.create();
  88. set_sep(trans, scale, quat, dest);
  89. return dest;
  90. }
  91. /**
  92. * Set TSR translation.
  93. * @method module:tsr.set_trans
  94. * @param {Vec3} trans Translation vector
  95. * @param {TSR} dest Destination TSR vector
  96. */
  97. exports.set_trans = m_tsr.set_trans;
  98. /**
  99. * Set TSR scale.
  100. * @method module:tsr.set_scale
  101. * @param {Number} scale Scale
  102. * @param {TSR} dest Destination TSR vector
  103. */
  104. exports.set_scale = m_tsr.set_scale;
  105. /**
  106. * Set TSR translation and scale from vec4.
  107. * @method module:tsr.set_transcale
  108. * @param {Vec4} transcale Translation+Scale vector
  109. * @param {TSR} dest Destination TSR vector
  110. */
  111. exports.set_transcale = m_tsr.set_transcale ;
  112. /**
  113. * Set TSR quaternion.
  114. * @method module:tsr.set_quat
  115. * @param {Quat} quat Rotation quaternion
  116. * @param {TSR} dest Destination TSR vector
  117. */
  118. exports.set_quat = m_tsr.set_quat;
  119. /**
  120. * Get ArrayBufferView from translation part of TSR.
  121. * @method module:tsr.get_trans_view
  122. * @param {TSR} tsr TSR vector
  123. * @returns {Vec3} Translation part of TSR
  124. */
  125. exports.get_trans_view = m_tsr.get_trans_view;
  126. /**
  127. * Get TSR scale.
  128. * @method module:tsr.get_scale
  129. * @returns {Number} Scale
  130. */
  131. exports.get_scale = m_tsr.get_scale;
  132. /**
  133. * Get ArrayBufferView from quaternion part of TSR.
  134. * @method module:tsr.get_quat_view
  135. * @returns {Quat} Quaternion part of TSR
  136. */
  137. exports.get_quat_view = m_tsr.get_quat_view;
  138. /**
  139. * Calculates the inverse of TSR.
  140. * @method module:tsr.invert
  141. * @param {TSR} tsr TSR vector
  142. * @param {TSR} dest Destination TSR vector
  143. * @returns {TSR} Destination TSR vector
  144. */
  145. exports.invert = m_tsr.invert;
  146. /**
  147. * Create mat4 from TSR.
  148. * Not optimized.
  149. * @method module:tsr.to_mat4
  150. * @param {TSR} tsr TSR vector.
  151. * @param {?Mat4} [dest=mat4.create()] Destination matrix.
  152. * @returns {Mat4} Destination matrix.
  153. */
  154. exports.to_mat4 = function(tsr, dest) {
  155. if (!dest)
  156. var dest = m_mat4.create();
  157. m_tsr.to_mat4(tsr, dest);
  158. return dest;
  159. }
  160. /**
  161. * Set TSR from mat4.
  162. * Not optimized.
  163. * @method module:tsr.from_mat4
  164. * @param {Mat4} mat Matrix.
  165. * @param {TSR} dest Destination TSR vector.
  166. * @returns {TSR} Destination TSR vector.
  167. */
  168. exports.from_mat4 = m_tsr.from_mat4;
  169. /**
  170. * Multiply two TSRs.
  171. * @method module:tsr.multiply
  172. * @param {TSR} tsr First TSR vector
  173. * @param {TSR} tsr2 Second TSR vector
  174. * @param {TSR} dest Destination TSR vector
  175. * @returns {TSR} Destination TSR vector
  176. */
  177. exports.multiply = m_tsr.multiply;
  178. /**
  179. * Transform vec3 by TSR.
  180. * @method module:tsr.transform_vec3
  181. * @param {Vec3} trans Vector to transform
  182. * @param {TSR} tsr TSR vector
  183. * @param {Vec3} dest Destination vector
  184. */
  185. exports.transform_vec3 = m_tsr.transform_vec3;
  186. /**
  187. * Transform vec3 by inverse TSR.
  188. * @method module:tsr.transform_vec3_inv
  189. * @param {Vec3} trans Vector to transform
  190. * @param {TSR} tsr TSR vector
  191. * @param {Vec3} dest Destination vector
  192. */
  193. exports.transform_vec3_inv = m_tsr.transform_vec3_inv;
  194. /**
  195. * Tranform vec3 vectors by TSR.
  196. * optional destination offset in values (not vectors, not bytes)
  197. * @method module:tsr.transform_vectors
  198. * @param {Float32Array} vectors Array of vectors to transform
  199. * @param {TSR} tsr TSR vector
  200. * @param {Float32Array} new_vectors Destination array of vectors
  201. * @param {Number} [dest_offset=0] Offset in new_vectors array
  202. * @returns {Float32Array} Destination array of vectors
  203. */
  204. exports.transform_vectors = m_tsr.transform_vectors;
  205. /**
  206. * Transform directional vec3 vectors by TSR.
  207. * optional destination offset in values (not vectors, not bytes)
  208. * @method module:tsr.transform_dir_vectors
  209. * @param {Float32Array} vectors Array of vectors to transform
  210. * @param {TSR} tsr TSR vector
  211. * @param {Float32Array} new_vectors Destination array of vectors
  212. * @param {Number} [dest_offset=0] Offset in new_vectors array
  213. * @returns {Float32Array} Destination array of vectors
  214. */
  215. exports.transform_dir_vectors = m_tsr.transform_dir_vectors;
  216. /**
  217. * Transform directional vec3 by TSR.
  218. * @method module:tsr.transform_dir_vec3
  219. * @param {Vec3} trans Vector to transform
  220. * @param {TSR} tsr TSR vector
  221. * @param {Vec3} dest Destination vector
  222. */
  223. exports.transform_dir_vec3 = m_tsr.transform_dir_vec3;
  224. /**
  225. * Tranform 4 comp tangent vectors by matrix.
  226. * optional destination offset in values (not vectors, not bytes)
  227. * @method module:tsr.transform_tangents
  228. * @param {Float32Array} vectors Array of vectors to transform
  229. * @param {TSR} tsr TSR vector
  230. * @param {Float32Array} new_vectors Destination array of vectors
  231. * @param {Number} [dest_offset=0] Offset in new_vectors array
  232. * @returns {Float32Array} Destination array of vectors
  233. */
  234. exports.transform_tangents = m_tsr.transform_tangents;
  235. /**
  236. * Perform TSR translation by given vec3.
  237. * @method module:tsr.translate
  238. * @param {TSR} tsr TSR vector
  239. * @param {Vec3} trans Translation vector
  240. * @param {TSR} dest Destination TSR vector
  241. * @returns {TSR} Destination TSR vector
  242. */
  243. exports.translate = m_tsr.translate;
  244. /**
  245. * Perform interpolation between two TSR vectors.
  246. * @method module:tsr.interpolate
  247. * @param {TSR} tsr First TSR vector
  248. * @param {TSR} tsr2 Second TSR vector
  249. * @param {Number} factor Interpolation factor
  250. * @param {TSR} dest Destination TSR vector
  251. * @returns {TSR} Destination TSR vector
  252. */
  253. exports.interpolate = function(tsr, tsr2, factor, dest) {
  254. if (!dest)
  255. var dest = m_tsr.create();
  256. m_tsr.interpolate(tsr, tsr2, factor, dest);
  257. return dest;
  258. }
  259. }