Source: util/b4w.js

  1. /**
  2. * Object in 3D space.
  3. * You should always use the engine's API in order to manipulate your 3D object.
  4. * Never access it directly by it's properties.
  5. * @typedef {Object} Object3D
  6. */
  7. /**
  8. * Sensor object.
  9. * @typedef {Object} Sensor
  10. * @see {@link module:controls}
  11. */
  12. /**
  13. * Typed two-dimensional vector.
  14. * @typedef {Float32Array} Vec2
  15. */
  16. /**
  17. * Typed three-dimensional vector. Can be created with {@link module:vec3.create}.
  18. * @typedef {Float32Array} Vec3
  19. */
  20. /**
  21. * Constant value matching character state (flying, walking etc).
  22. * @typedef {number} CharacterState
  23. */
  24. /**
  25. * Line set in parametric form
  26. * @typedef {Float32Array(6)} ParametricLine
  27. */
  28. /**
  29. * Typed four-dimensional vector. Can be created with {@link module:vec4.create}.
  30. * @typedef {Float32Array} Vec4
  31. */
  32. /**
  33. * Quaternion vector representing rotation.
  34. * Quaternion is a four-dimensional vector which has the following format:
  35. * [X, Y, Z, W]. Can be created with {@link module:quat.create}.
  36. * @typedef {Float32Array} Quat
  37. */
  38. /**
  39. * Euler vector representing rotation (in radians).
  40. * <!--
  41. * <p>The euler angles specified in intrinsic form (rotating space) and have the following meaning:
  42. * <ul>
  43. * TODO: check it!!!
  44. * <li>euler[0]: heading, around Y
  45. * <li>euler[1]: attitude, around new Z
  46. * <li>euler[2]: bank, around new X
  47. * </ul>
  48. * -->
  49. * <p>Using euler angles is discouraged, use quaternions instead.
  50. * @typedef {Float32Array} Euler
  51. */
  52. /**
  53. * TSR vector representing 3D object's transformations (Translation, Scale, Rotation).
  54. * TSR is an eight-dimensional vector which has the following format:
  55. * [X, Y, Z, SCALE, QUAT_X, QUAT_Y, QUAT_Z, QUAT_W].
  56. * Can be created with {@link module:tsr.create}.
  57. * @typedef {Float32Array} TSR
  58. */
  59. /**
  60. * 3x3 matrix.
  61. * The elements of matrix are placed in typed array in column-major order.
  62. * Can be created with {@link module:mat3.create}.
  63. * @typedef {Float32Array} Mat3
  64. */
  65. /**
  66. * 4x4 matrix.
  67. * The elements of matrix are placed in typed array in column-major order.
  68. * Can be created with {@link module:mat4.create}.
  69. * @typedef {Float32Array} Mat4
  70. */
  71. /**
  72. * Typed three-dimensional vector representing color.
  73. * Each component must be in range 0-1.
  74. * Can be created with {@link module:rgb.create}.
  75. * @typedef {Float32Array} RGB
  76. */
  77. /**
  78. * Typed four-dimensional vector representing color and alpha.
  79. * Each component must be in range 0-1.
  80. * Can be created with {@link module:rgba.create}.
  81. * @typedef {Float32Array} RGBA
  82. */
  83. /**
  84. * The JavaScript Date object.
  85. * @typedef {Object} Date
  86. */
  87. /**
  88. * Camera movement style enum. One of MS_*.
  89. * @typedef {number} CameraMoveStyle
  90. * @see {@link module:camera}
  91. */
  92. /**
  93. * Generic callback function with no parameters.
  94. * @callback GenericCallback
  95. */
  96. /**
  97. * Blend4Web global object.
  98. * @namespace
  99. * @suppress {duplicate}
  100. */
  101. // HACK: get b4w from global object
  102. var b4w;
  103. var is_worker = false;
  104. try {
  105. b4w = window.b4w;
  106. } catch (e) {
  107. b4w = self.b4w;
  108. is_worker = true
  109. }
  110. if (!b4w) {
  111. var b4w = {};
  112. // TODO elaborate b4w access from the console
  113. var DEBUG = true;
  114. if (DEBUG) {
  115. if (is_worker) {
  116. self.b4w = b4w;
  117. } else {
  118. window.b4w = b4w;
  119. }
  120. }
  121. var _module = {};
  122. var _n_module = {};
  123. b4w.module = _module;
  124. b4w._n_module = _n_module;
  125. // require functions per namespace
  126. var _ns_requires = {};
  127. b4w.cleanup = function (module_id, ns) {
  128. ns = ns || "__b4w_default";
  129. var mod = _module[module_id];
  130. if (mod)
  131. mod._compiled = null;
  132. _ns_requires[ns] = null;
  133. }
  134. /**
  135. * Local (module internal) require function.
  136. * This function is passed to the module implementation function and can be used
  137. * to import additional modules from the same namespace. If you need to import
  138. * a module from the different namespace use {@link b4w.require}.
  139. * @typedef {Function} b4w~RequireFunction
  140. * @param {string} module_id Module ID
  141. */
  142. /**
  143. * Module implementation function.
  144. * @callback b4w~ModuleFunction
  145. * @param {Object} exports Object with exported symbols
  146. * @param {b4w~RequireFunction} require Local (module internal) require function
  147. */
  148. /**
  149. * Register the module.
  150. * @method b4w.register
  151. * @param {string} module_id Module ID
  152. * @param {b4w~ModuleFunction} fun Function implementing the module
  153. */
  154. b4w.register = function (module_id, fun) {
  155. if (_module[module_id])
  156. return;
  157. //throw new Error("Module \"" + module_id + "\" already registered");
  158. _module[module_id] = fun;
  159. }
  160. /**
  161. * Prepare and return the registered module.
  162. * @method b4w.require
  163. * @param {string} module_id Module ID
  164. * @param {string} [ns="__b4w_default"] Namespace for processed modules
  165. * @returns {Object3D} Module object
  166. */
  167. b4w.require = function (module_id, ns) {
  168. if (!_module[module_id] && !_n_module[module_id])
  169. throw new Error("Module \"" + module_id + "\" not found");
  170. ns = ns || "__b4w_default";
  171. if (_n_module[module_id] !== undefined) {
  172. return _n_module[module_id](ns);
  173. } else if (!_ns_requires[ns]) {
  174. _ns_requires[ns] = (function (ns) {
  175. return function (module_id) {
  176. if (!_module[module_id] && !_n_module[module_id]) {
  177. throw new Error("Module \"" + module_id + "\" not found");
  178. }
  179. if (_n_module[module_id] !== undefined) {
  180. return _n_module[module_id](ns);
  181. }
  182. var mod = _module[module_id];
  183. if (!mod._compiled)
  184. mod._compiled = {};
  185. if (!mod._compiled[ns]) {
  186. mod._compiled[ns] = {};
  187. mod(mod._compiled[ns], _ns_requires[ns]);
  188. }
  189. return mod._compiled[ns];
  190. }
  191. })(ns);
  192. }
  193. return _ns_requires[ns](module_id);
  194. }
  195. /**
  196. * Check if the module was registered.
  197. * @method b4w.module_check
  198. * @param {string} module_id Module ID
  199. * @returns {boolean} Check result
  200. */
  201. b4w.module_check = function (module_id) {
  202. return _module[module_id] !== undefined ||
  203. _n_module[module_id] !== undefined;
  204. }
  205. /**
  206. * Get a namespace of the current module by it's require function.
  207. * @method b4w.get_namespace
  208. * @param {b4w~RequireFunction} mod_ns_require Local require function
  209. * @returns {string} Namespace.
  210. */
  211. b4w.get_namespace = function (mod_ns_require) {
  212. // TODO: fix for ES6 modules
  213. for (var ns in _ns_requires)
  214. if (_ns_requires[ns] == mod_ns_require)
  215. return ns;
  216. return "";
  217. }
  218. /**
  219. * Global vars for proper worker fallback operations.
  220. * @ignore
  221. */
  222. b4w.worker_listeners = [];
  223. b4w.worker_namespaces = [];
  224. }
  225. export default b4w;