Source: extern/objects.js

  1. import register from "../util/register.js";
  2. import m_geom_fact from "../intern/geometry.js";
  3. import m_obj_fact from "../intern/objects.js";
  4. import m_batch_fact from "../intern/batch.js";
  5. import m_obj_util_fact from "../intern/obj_util.js";
  6. import m_print_fact from "../intern/print.js";
  7. import m_scenes_fact from "../intern/scenes.js";
  8. import * as m_util from "../intern/util.js";
  9. /**
  10. * Objects API.
  11. * <p>Additional topics in the User Manual: {@link
  12. * https://www.blend4web.com/doc/en/objects.html#object-transform-api|Object Transform API},
  13. * {@link https://www.blend4web.com/doc/en/objects.html#get-object-api|Get
  14. * Object API}, {@link
  15. * https://www.blend4web.com/doc/en/objects.html#object-selection|Object
  16. * Selection}, {@link
  17. * https://www.blend4web.com/doc/en/objects.html#copying-objects-instancing|Copying
  18. * Objects (Instancing)}
  19. * @module objects
  20. * @local ObjectMetaTags
  21. * @local WindBendingParams
  22. */
  23. function Objects(ns, exports) {
  24. var m_geom = m_geom_fact(ns);
  25. var m_obj = m_obj_fact(ns);
  26. var m_batch = m_batch_fact(ns);
  27. var m_obj_util = m_obj_util_fact(ns);
  28. var m_print = m_print_fact(ns);
  29. var m_scenes = m_scenes_fact(ns);
  30. /**
  31. * @typedef {Object} ObjectMetaTags
  32. * @property {string} title The title meta tag.
  33. * @property {string} description The description meta tag.
  34. * @property {string} category The category meta tag.
  35. */
  36. /**
  37. * Wind bending params.
  38. * @typedef {Object} WindBendingParams
  39. * @property {number} angle Angle of main wind bending in degrees
  40. * @property {number} main_frequency Frequency of main wind bending
  41. * @property {number} detail_frequency Frequency of detail wind bending
  42. * @property {number} detail_amplitude Amplitude of detail wind bending
  43. * @property {number} branch_amplitude Amplitude of branches wind bending
  44. * @cc_externs angle main_frequency detail_frequency
  45. * @cc_externs detail_amplitude branch_amplitude
  46. */
  47. /**
  48. * Get the Blender-assigned meta tags from the object.
  49. * @method module:objects.get_meta_tags
  50. * @param {Object3D} obj Object 3D
  51. * @returns {ObjectMetaTags} Object meta tags
  52. * @cc_externs title description category
  53. */
  54. exports.get_meta_tags = function(obj) {
  55. if (obj)
  56. return m_obj.get_meta_tags(obj);
  57. }
  58. /**
  59. * Get the Blender-assigned custom property field from the object.
  60. * @method module:objects.get_custom_prop
  61. * @param {Object3D} obj Object 3D
  62. * @returns {*} Object custom property field
  63. */
  64. exports.get_custom_prop = function(obj) {
  65. if (obj)
  66. return m_obj.get_custom_prop(obj);
  67. }
  68. /**
  69. * Copy MESH object.
  70. * @method module:objects.copy
  71. * @param {Object3D} obj Object 3D
  72. * @param {string} name New unique object name
  73. * @param {boolean} [deep_copy=false] Copy WebGL buffers
  74. * @returns {Object3D} New object.
  75. */
  76. exports.copy = function(obj, name, deep_copy) {
  77. if (!m_obj_util.is_mesh(obj)) {
  78. m_print.error("object \"" + obj.name + "\" is not of type \"MESH\".");
  79. return false;
  80. }
  81. if (!m_obj_util.is_dynamic(obj)) {
  82. m_print.error("object \"" + obj.name + "\" is not dynamic.");
  83. return false;
  84. }
  85. if (!(m_geom.has_dyn_geom(obj) || m_geom.check_shape_keys(obj)) && deep_copy) {
  86. m_print.error("object \"" + obj.name + "\" has not dynamic "
  87. + "geometry for deep copying.");
  88. return false;
  89. }
  90. // HACK: a temporary (provisional) solution
  91. var objs = m_obj.get_scene_objs(m_scenes.get_active(), "MESH", m_obj.DATA_ID_ALL);
  92. if (objs.indexOf(obj) == - 1) {
  93. m_print.error("object \"" + obj.name + "\" does not belong to the "
  94. + "active scene.");
  95. return false;
  96. }
  97. name = name || "";
  98. return m_obj.copy(obj, name, deep_copy);
  99. }
  100. /**
  101. * Update object's boundings (box, cone, cylinder, ellipsoid, sphere, capsule).
  102. * @method module:objects.update_boundings
  103. * @param {Object3D} obj Object 3D
  104. */
  105. exports.update_boundings = function(obj) {
  106. if (!m_obj_util.is_mesh(obj)) {
  107. m_print.error("The type of the object \"" + obj.name +
  108. "\" is not \"MESH\".");
  109. return;
  110. }
  111. if (!(m_geom.has_dyn_geom(obj) || m_geom.check_shape_keys(obj))) {
  112. m_print.error("object \"" + obj.name + "\" has not dynamic "
  113. + "geometry.");
  114. return;
  115. }
  116. m_obj.update_boundings(obj);
  117. }
  118. /**
  119. * Get parent object.
  120. * @method module:objects.get_parent
  121. * @param {Object3D} obj Child object
  122. * @returns {?Object3D} Parent object
  123. */
  124. exports.get_parent = m_obj_util.get_parent;
  125. /**
  126. * Get DupliGroup parent object.
  127. * @method module:objects.get_dg_parent
  128. * @param {Object3D} obj Child object
  129. * @returns {?Object3D} Parent object
  130. */
  131. exports.get_dg_parent = m_obj_util.get_dg_parent;
  132. /**
  133. * Check if the object is a MESH.
  134. * @method module:objects.is_mesh
  135. * @param {Object3D} obj Object 3D
  136. * @returns {boolean} Checking result.
  137. */
  138. exports.is_mesh = m_obj_util.is_mesh;
  139. /**
  140. * Check if the object is an ARMATURE.
  141. * @method module:objects.is_armature
  142. * @param {Object3D} obj Object 3D
  143. * @returns {boolean} Checking result.
  144. */
  145. exports.is_armature = m_obj_util.is_armature;
  146. /**
  147. * Check if the object is a SPEAKER.
  148. * @method module:objects.is_speaker
  149. * @param {Object3D} obj Object 3D
  150. * @returns {boolean} Checking result.
  151. */
  152. exports.is_speaker = m_obj_util.is_speaker;
  153. /**
  154. * Check if the object is a CAMERA.
  155. * @method module:objects.is_camera
  156. * @param {Object3D} obj Object 3D
  157. * @returns {boolean} Checking result.
  158. */
  159. exports.is_camera = m_obj_util.is_camera;
  160. /**
  161. * Check if the object is a LAMP.
  162. * @method module:objects.is_lamp
  163. * @param {Object3D} obj Object 3D
  164. * @returns {boolean} Checking result.
  165. */
  166. exports.is_lamp = m_obj_util.is_lamp;
  167. /**
  168. * Check if the object is an EMPTY.
  169. * @method module:objects.is_empty
  170. * @param {Object3D} obj Object 3D
  171. * @returns {boolean} Checking result.
  172. */
  173. exports.is_empty = m_obj_util.is_empty;
  174. /**
  175. * Check if the object is a LINE.
  176. * @method module:objects.is_line
  177. * @param {Object3D} obj Object 3D
  178. * @returns {boolean} Checking result.
  179. */
  180. exports.is_line = m_obj_util.is_line;
  181. /**
  182. * Check if the object is a WORLD.
  183. * @method module:objects.is_world
  184. * @param {Object3D} obj Object 3D
  185. * @returns {boolean} Checking result.
  186. */
  187. exports.is_world = m_obj_util.is_world;
  188. /**
  189. * Get all scene selectable objects.
  190. * @method module:objects.get_selectable_objects
  191. * @returns {Object3D[]} Array with selectable objects.
  192. */
  193. exports.get_selectable_objects = function() {
  194. return m_obj.get_selectable_objects();
  195. }
  196. /**
  197. * Get all scene outlining objects.
  198. * @method module:objects.get_outlining_objects
  199. * @returns {Object3D[]} Array with outlining objects.
  200. */
  201. exports.get_outlining_objects = function() {
  202. return m_obj.get_outlining_objects();
  203. }
  204. /**
  205. * Check if object is dynamic.
  206. * @method module:objects.is_dynamic
  207. * @param {Object3D} obj Object 3D
  208. * @returns {boolean} Checking result.
  209. */
  210. exports.is_dynamic = m_obj_util.is_dynamic;
  211. /**
  212. * Set object's wind bending parameters. Object must be dynamic.
  213. * @param {Object3D} obj Object 3D
  214. * @param {WindBendingParams} wb_params Wind Bending parameters
  215. * @example
  216. * var m_obj = require("objects");
  217. * var wb_params =
  218. * {
  219. * angle: 45,
  220. * main_frequency: 0.25,
  221. * detail_frequency: 1,
  222. * detail_amplitude: 0.1,
  223. * branch_amplitude: 0.3
  224. * };
  225. * m_obj.set_wind_bending_params(obj, wb_params);
  226. */
  227. exports.set_wind_bending_params = function(obj, wb_params) {
  228. if (!m_obj_util.is_dynamic_mesh(obj)) {
  229. m_print.error("The type of the object \"" + obj.name +
  230. "\" is not \"MESH\" or it is not dynamic.");
  231. return;
  232. }
  233. var render = obj.render;
  234. if (!render.wind_bending) {
  235. m_print.error("The \"" + obj.name + "\" object " +
  236. "doesn't have wind bending parameters.");
  237. return;
  238. }
  239. if (typeof wb_params.angle == "number") {
  240. var amp = m_batch.wb_angle_to_amp(m_util.deg_to_rad(wb_params.angle),
  241. render.bb_original, render.world_tsr[3]);
  242. render.wind_bending_amp = amp;
  243. }
  244. if (typeof wb_params.main_frequency == "number")
  245. render.wind_bending_freq = wb_params.main_frequency;
  246. if (typeof wb_params.detail_frequency == "number")
  247. render.detail_bending_freq = wb_params.detail_frequency;
  248. if (typeof wb_params.detail_amplitude == "number")
  249. render.detail_bending_amp = wb_params.detail_amplitude;
  250. if (typeof wb_params.branch_amplitude == "number")
  251. render.branch_bending_amp = wb_params.branch_amplitude;
  252. m_obj.set_hair_particles_wind_bend_params(obj);
  253. }
  254. /**
  255. * Get object's wind bending parameters. Object must be dynamic.
  256. * @param {Object3D} obj Object 3D
  257. * @returns {WindBendingParams} Wind Bending parameters
  258. */
  259. exports.get_wind_bending_params = function(obj) {
  260. var render = obj.render;
  261. if (!render.wind_bending)
  262. return null;
  263. var wb_params = {};
  264. var angle = m_util.rad_to_deg(m_batch.wb_amp_to_angle(render.wind_bending_amp,
  265. render.bb_original, render.world_tsr[3]));
  266. wb_params.angle = angle;
  267. wb_params.main_frequency = render.wind_bending_freq;
  268. wb_params.detail_frequency = render.detail_bending_freq;
  269. wb_params.detail_amplitude = render.detail_bending_amp;
  270. wb_params.branch_amplitude = render.branch_bending_amp;
  271. return wb_params;
  272. }
  273. /**
  274. * Create line object
  275. * @param {string} name Line object name
  276. */
  277. exports.create_line = function(name) {
  278. return m_obj.create_line(name);
  279. }
  280. /**
  281. * Hide objects that have the given data_id.
  282. * @param {number} data_id ID of loaded data.
  283. * @example
  284. * var m_obj = require("objects");
  285. * m_obj.hide_all_by_data_id(0);
  286. */
  287. exports.hide_all_by_data_id = function(data_id) {
  288. var objs = m_obj.get_all_objects("ALL", data_id);
  289. for (var i = 0; i < objs.length; i++)
  290. m_scenes.change_visibility(objs[i], true);
  291. }
  292. /**
  293. * Show objects that have the given data_id.
  294. * @param {number} data_id ID of loaded data.
  295. * @example
  296. * var m_obj = require("objects");
  297. * m_obj.show_all_by_data_id(1);
  298. */
  299. exports.show_all_by_data_id = function(data_id) {
  300. var objs = m_obj.get_all_objects("ALL", data_id);
  301. for (var i = 0; i < objs.length; i++)
  302. m_scenes.change_visibility(objs[i], false);
  303. }
  304. }
  305. var objects_factory = register("objects", Objects);
  306. export default objects_factory;