Source: extern/camera.js

  1. import register from "../util/register.js";
  2. import m_cam_fact from "../intern/camera.js";
  3. import m_cont_fact from "../intern/container.js";
  4. import * as m_math from "../intern/math.js";
  5. import m_obj_util_fact from "../intern/obj_util.js";
  6. import m_phy_fact from "../intern/physics.js";
  7. import m_print_fact from "../intern/print.js";
  8. import m_scs_fact from "../intern/scenes.js";
  9. import m_trans_fact from "../intern/transform.js";
  10. import * as m_tsr from "../intern/tsr.js";
  11. import * as m_util from "../intern/util.js";
  12. import * as m_vec3 from "../libs/gl_matrix/vec3.js";
  13. import * as m_vec4 from "../libs/gl_matrix/vec4.js";
  14. import * as m_quat from "../libs/gl_matrix/quat.js";
  15. /**
  16. * API for controlling the camera within the bounds of the current camera model.
  17. * Use {@link module:transform|Transform API} for low-level actions.<br>
  18. * All functions require a valid camera object reference. Use
  19. * {@link module:scenes.get_object_by_name|scenes.get_object_by_name()} or
  20. * {@link module:scenes.get_active_camera|scenes.get_active_camera()} to obtain it.
  21. * The result of applying various transforms to a camera can be overridden by the
  22. * present camera limits.
  23. * <p>
  24. * <b>API examples for this module</b>:
  25. * {@link https://www.blend4web.com/doc/en/camera.html#camera-controls-api|English},
  26. * {@link https://www.blend4web.com/doc/zh/camera.html#camera-controls-api|中文},
  27. * {@link https://www.blend4web.com/doc/ru/camera.html#camera-controls-api|Русский}.
  28. * </p>
  29. * @module camera
  30. * @local DistanceLimits
  31. * @local VerticalRotationLimits
  32. * @local HorizontalRotationLimits
  33. * @local HoverAngleLimits
  34. * @local VerticalTranslationLimits
  35. * @local HorizontalTranslationLimits
  36. * @local HoverCameraParams
  37. * @local TargetCameraParams
  38. * @local EyeCameraParams
  39. * @local StaticCameraParams
  40. * @local PivotLimits
  41. * @local VelocityParams
  42. * @local FrustumPlanes
  43. */
  44. function Camera(ns, exports) {
  45. var m_cam = m_cam_fact(ns);
  46. var m_cont = m_cont_fact(ns);
  47. var m_obj_util = m_obj_util_fact(ns);
  48. var m_phy = m_phy_fact(ns);
  49. var m_print = m_print_fact(ns);
  50. var m_scs = m_scs_fact(ns);
  51. var m_trans = m_trans_fact(ns);
  52. var _vec2_tmp = new Float32Array(2);
  53. var _vec3_tmp = new Float32Array(3);
  54. var _vec3_tmp2 = new Float32Array(3);
  55. var _vec4_tmp = new Float32Array(4);
  56. var _quat_tmp = m_quat.create();
  57. var _limits_tmp = {};
  58. var _limits_tmp2 = {};
  59. /**
  60. * An object that defines distance limits for the HOVER/TARGET camera.
  61. * The "min" value must be less or equal than the "max" value.
  62. * @typedef {Object} DistanceLimits
  63. * @property {number} min The minimum distance to the pivot.
  64. * @property {number} max The maximum distance to the pivot.
  65. * @cc_externs min max
  66. */
  67. /**
  68. * An object that defines limits for rotations in a vertical plane for the TARGET/EYE camera.
  69. * The limits are converted by engine into the range [-Pi, Pi] when set via API.
  70. * @see https://www.blend4web.com/doc/en/camera.html#camera-spherical-coordinates
  71. * @typedef {Object} VerticalRotationLimits
  72. * @property {number} down The elevation angle in radians that restricts a downward rotation.
  73. * @property {number} up The elevation angle in radians that restricts an upward rotation.
  74. * @property {boolean} [camera_space=false] Define limits relative to the current
  75. * camera position/orientation, otherwise - in world space (by default).
  76. * @cc_externs down up camera_space
  77. */
  78. /**
  79. * An object that defines limits for rotations in a horizontal plane for the TARGET/EYE camera.
  80. * The limits are converted by engine into the range [0, 2Pi] when set via API.
  81. * @see https://www.blend4web.com/doc/en/camera.html#camera-spherical-coordinates
  82. * @typedef {Object} HorizontalRotationLimits
  83. * @property {number} left The azimuth angle in radians that restricts a leftward rotation.
  84. * @property {number} right The azimuth angle in radians that restricts a rightward rotation.
  85. * @property {boolean} [camera_space=false] Define limits relative to the current
  86. * camera position/orientation, otherwise - in world space (by default).
  87. * @cc_externs left right camera_space
  88. */
  89. /**
  90. * An object that defines limits for rotations in a vertical plane for the HOVER camera.
  91. * The limits are converted by engine into the range [-Pi, Pi] and then clamped
  92. * to be in range [-Pi/2, 0] when set via API.
  93. * The "down" value must be greater or equal than the "up" value.
  94. * @see https://www.blend4web.com/doc/en/camera.html#camera-spherical-coordinates
  95. * @typedef {Object} HoverAngleLimits
  96. * @property {number} down The elevation angle in radians that restricts a downward rotation.
  97. * @property {number} up The elevation angle in radians that restricts an upward rotation.
  98. * @cc_externs down up
  99. */
  100. /**
  101. * An object that defines limits for translations along the Z axis for the HOVER camera.
  102. * The "min" value must be less or equal than the "max" value.
  103. * @see https://www.blend4web.com/doc/en/camera.html#hover-translation-limits
  104. * @typedef {Object} VerticalTranslationLimits
  105. * @property {number} min The minimum value that restricts camera translation along the Z axis.
  106. * @property {number} max The maximum value that restricts camera translation along the Z axis.
  107. * @cc_externs min max
  108. */
  109. /**
  110. * An object that defines limits for translations along the X axis for the HOVER camera.
  111. * The "min" value must be less or equal than the "max" value.
  112. * @see https://www.blend4web.com/doc/en/camera.html#hover-translation-limits
  113. * @typedef {Object} HorizontalTranslationLimits
  114. * @property {number} min The minimum value that restricts camera translation along the X axis.
  115. * @property {number} max The maximum value that restricts camera translation along the X axis.
  116. * @cc_externs min max
  117. */
  118. /**
  119. * An object that defines limits for translations along the Y axis for the
  120. * camera pivot point.
  121. * @typedef {Object} PivotLimits
  122. * @property {number} min_z The minimum value that restricts pivot translation
  123. * along the Z axis.
  124. * @property {number} max_z The maximum value that restricts pivot translation
  125. * along the Z axis.
  126. * @cc_externs min_z max_z
  127. */
  128. /**
  129. * An object that defines velocity of the camera movement.
  130. * @typedef {Object} VelocityParams
  131. * @property {number} [trans=Current velocity value] Translation velocity ([0,Infinity]).
  132. * @property {number} [rot=Current velocity value] Rotation velocity ([0,Infinity]).
  133. * @property {number} [zoom=Current velocity value] Zoom velocity ([0,1]).
  134. * @cc_externs trans rot zoom
  135. */
  136. /**
  137. * An object that defines the STATIC camera parameters.
  138. * @typedef {Object} StaticCameraParams
  139. * @property {?Vec3} [pos=null] Position of the camera. Set to null to keep the current
  140. * camera position.
  141. * @property {?Vec3} [look_at=null] Point the camera is looking at. Set to null to
  142. * keep the existing camera orientation.
  143. * @cc_externs pos look_at
  144. */
  145. /**
  146. * An object that defines the EYE camera parameters.
  147. * @typedef {Object} EyeCameraParams
  148. * @property {?Vec3} [pos=null] Position of the camera. Set to null to keep the
  149. * current camera position.
  150. * @property {?Vec3} [look_at=null] Point the camera is looking at. Set to null
  151. * to keep the existing camera orientation.
  152. * @property {?HorizontalRotationLimits} [horiz_rot_lim=null] Horizontal rotation
  153. * limits. Set to null to disable the limits.
  154. * @property {?VerticalRotationLimits} [vert_rot_lim=null] Vertical rotation limits.
  155. * Set to null to disable the limits.
  156. * @cc_externs pos look_at horiz_rot_lim vert_rot_lim
  157. */
  158. /**
  159. * An object that defines the TARGET camera parameters.
  160. * @typedef {Object} TargetCameraParams
  161. * @property {?Vec3} [pos=null] Position of the camera. Set to null to keep the
  162. * current camera position.
  163. * @property {Vec3} [pivot=null] Camera pivot point. If set to null, when the
  164. * current view vector of unit length will be used to define the pivot point.
  165. * @property {?HorizontalRotationLimits} [horiz_rot_lim=null] Horizontal
  166. * rotation limits. Set to null to disable the limits.
  167. * @property {?VerticalRotationLimits} [vert_rot_lim=null] Vertical rotation
  168. * limits. Set to null to disable the limits.
  169. * @property {?DistanceLimits} [dist_lim=null] Distance limits. Set to null to
  170. * disable the limits.
  171. * @property {?PivotLimits} [pivot_lim=null] Pivot limits. Set to
  172. * null to disable the limits.
  173. * @property {boolean} [use_panning=false] Use panning mode.
  174. * @cc_externs pos pivot horiz_rot_lim vert_rot_lim dist_lim use_panning
  175. */
  176. /**
  177. * An object that defines the HOVER camera parameters.
  178. * @typedef {Object} HoverCameraParams
  179. * @property {?Vec3} [pos=null] Position of the camera. Set to null to keep the
  180. * current camera position.
  181. * @property {Vec3} pivot Camera pivot point.
  182. * @property {?DistanceLimits} [dist_lim=null] Distance limits. Set to null to
  183. * define the limits as fixed distance to the pivot which depends on the
  184. * given pivot and camera positions.
  185. * @property {?HoverAngleLimits} [hover_angle_lim=null] Hover angle limits.
  186. * Set to null to define the limits as fixed angle which depends on the given
  187. * pivot and camera positions.
  188. * @property {?HorizontalTranslationLimits} [horiz_trans_lim=null] Horizontal
  189. * translation limits. Set to null to disable the limits.
  190. * @property {?VerticalTranslationLimits} [vert_trans_lim=null] Vertical
  191. * translation limits. Set to null to disable the limits.
  192. * @property {boolean} [enable_horiz_rot=false] Enable horizontal rotation.
  193. * @cc_externs pos pivot dist_lim hover_angle_lim horiz_trans_lim vert_trans_lim
  194. * @cc_externs enable_horiz_rot
  195. */
  196. /**
  197. * An object that defines the HOVER camera parameters.
  198. * @typedef {Object} HoverCameraParamsRel
  199. * @property {?Vec3} [pos=null] Position of the camera. Set to null to keep the
  200. * current camera position.
  201. * @property {Vec3} pivot Camera pivot point.
  202. * @property {number} [dist_interval=0] A distance variation around the established
  203. * distance to the pivot: distance ± dist_interval/2. The resulted distance
  204. * limits are clamped to be in range [0, +∞].
  205. * @property {number} [angle_interval=0] A hover angle variation (in radians)
  206. * around the established hover angle: hover angle ± angle_interval/2. The
  207. * resulted hover angle limits are clamped to be in range [-PI/2, 0].
  208. * @property {number} [t=0.5] An optional parameter which lies in range [0, 1]
  209. * and defines the disposition of the given angle and distance intervals around
  210. * the established values, for example:
  211. * 0 - the given camera position is the nearest zoomed-in point,
  212. * 1 - the given camera position is the farthest zoomed-out point,
  213. * 0.5 (by default) - the given camera position is in the middle of the given
  214. * intervals and can be zoomed equally in both directions.
  215. * @cc_externs pos pivot dist_interval angle_interval t
  216. */
  217. /**
  218. * Camera frustum planes object.
  219. * @typedef {Object} FrustumPlanes
  220. * @property {Plane} Left frustum plane.
  221. * @property {Plane} Right frustum plane.
  222. * @property {Plane} Top frustum plane.
  223. * @property {Plane} Bottom frustum plane.
  224. * @property {Plane} Near frustum plane.
  225. * @property {Plane} Far frustum plane.
  226. * @cc_externs left right top bottom near far
  227. */
  228. /**
  229. * The camera's movement style: STATIC (non-interactive).
  230. * @const {CameraMoveStyle} module:camera.MS_STATIC
  231. */
  232. exports.MS_STATIC = m_cam.MS_STATIC;
  233. /**
  234. * The camera's movement style: TARGET.
  235. * @const {CameraMoveStyle} module:camera.MS_TARGET_CONTROLS
  236. */
  237. exports.MS_TARGET_CONTROLS = m_cam.MS_TARGET_CONTROLS;
  238. /**
  239. * The camera's movement style: EYE.
  240. * @const {CameraMoveStyle} module:camera.MS_EYE_CONTROLS
  241. */
  242. exports.MS_EYE_CONTROLS = m_cam.MS_EYE_CONTROLS;
  243. /**
  244. * The camera's movement style: HOVER.
  245. * @const {CameraMoveStyle} module:camera.MS_HOVER_CONTROLS
  246. */
  247. exports.MS_HOVER_CONTROLS = m_cam.MS_HOVER_CONTROLS;
  248. /**
  249. * Setup the STATIC camera.
  250. * @method module:camera.static_setup
  251. * @param {Object3D} camobj Camera 3D-object.
  252. * @param {?StaticCameraParams} [params=null] The parameters of the STATIC camera.
  253. */
  254. exports.static_setup = function(camobj, params) {
  255. if (!m_obj_util.is_camera(camobj)) {
  256. m_print.error("static_setup(): Wrong camera object");
  257. return;
  258. }
  259. m_cam.wipe_move_style(camobj);
  260. camobj.render.move_style = m_cam.MS_STATIC;
  261. if (params) {
  262. var pos = params.pos || m_tsr.get_trans_view(camobj.render.world_tsr);
  263. if (params.look_at)
  264. m_cam.set_look_at(camobj, pos, params.look_at);
  265. else
  266. m_trans.set_translation(camobj, pos);
  267. }
  268. m_trans.update_transform(camobj);
  269. m_phy.sync_transform(camobj);
  270. // init ortho properties after the camera was updated
  271. m_cam.init_ortho_props(camobj);
  272. }
  273. /**
  274. * Set position and orientation for the STATIC camera.
  275. * @method module:camera.static_set_look_at
  276. * @param {Object3D} camobj Camera 3D-object.
  277. * @param {?Vec3} [pos=null] Position of the camera. Pass null to keep the current
  278. * camera position.
  279. * @param {?Vec3} [look_at=null] Point the camera is looking at. Pass null to
  280. * keep the existing camera orientation.
  281. */
  282. exports.static_set_look_at = function(camobj, pos, look_at) {
  283. if (!m_cam.is_static_camera(camobj)) {
  284. m_print.error("static_set_look_at(): Wrong camera object or camera move style");
  285. return;
  286. }
  287. pos = pos || m_tsr.get_trans_view(camobj.render.world_tsr);
  288. if (look_at)
  289. m_cam.set_look_at(camobj, pos, look_at);
  290. else
  291. m_trans.set_translation(camobj, pos);
  292. m_trans.update_transform(camobj);
  293. m_phy.sync_transform(camobj);
  294. };
  295. /**
  296. * Set the STATIC camera's rotation from quaternion.
  297. * @method module:camera.static_set_rotation
  298. * @param {Object3D} camobj Camera 3D-object.
  299. * @param {Quat} quat Quaternion vector.
  300. */
  301. exports.static_set_rotation = function(camobj, quat) {
  302. if (!m_cam.is_static_camera(camobj)) {
  303. m_print.error("static_set_rotation(): Wrong camera object or camera move style");
  304. return;
  305. }
  306. m_trans.set_rotation(camobj, quat);
  307. m_trans.update_transform(camobj);
  308. m_phy.sync_transform(camobj);
  309. };
  310. /**
  311. * Get the STATIC camera's rotation quaternion.
  312. * @method module:camera.static_get_rotation
  313. * @param {Object3D} camobj Camera 3D-object.
  314. * @param {?Quat} [dest=new Float32Array(4);] Destination vector.
  315. * @returns {?Quat} Destination vector.
  316. */
  317. exports.static_get_rotation = function(camobj, dest) {
  318. if (!m_cam.is_static_camera(camobj)) {
  319. m_print.error("static_get_rotation(): Wrong camera object or camera move style");
  320. return null;
  321. }
  322. dest = dest || new Float32Array(4);
  323. return m_trans.get_rotation(camobj, dest);
  324. };
  325. /**
  326. * Setup the EYE camera.
  327. * @method module:camera.eye_setup
  328. * @param {Object3D} camobj Camera 3D-object.
  329. * @param {?EyeCameraParams} [params=null] The parameters of the EYE camera.
  330. */
  331. exports.eye_setup = function(camobj, params) {
  332. if (!m_obj_util.is_camera(camobj)) {
  333. m_print.error("eye_setup(): Wrong camera object");
  334. return;
  335. }
  336. if (params && params.horiz_rot_lim)
  337. if (typeof params.horiz_rot_lim.left != "number"
  338. || typeof params.horiz_rot_lim.right != "number") {
  339. m_print.error("eye_setup(): Wrong horizontal limits object.");
  340. return;
  341. }
  342. if (params && params.vert_rot_lim)
  343. if (typeof params.vert_rot_lim.down != "number"
  344. || typeof params.vert_rot_lim.up != "number") {
  345. m_print.error("eye_setup(): Wrong vertical limits object.");
  346. return;
  347. }
  348. m_cam.wipe_move_style(camobj);
  349. camobj.render.move_style = m_cam.MS_EYE_CONTROLS;
  350. if (params) {
  351. var pos = params.pos || m_tsr.get_trans_view(camobj.render.world_tsr);
  352. m_cam.setup_eye_model(camobj, pos, params.look_at, params.horiz_rot_lim,
  353. params.vert_rot_lim);
  354. }
  355. m_trans.update_transform(camobj);
  356. m_phy.sync_transform(camobj);
  357. // init ortho after the camera was updated
  358. m_cam.init_ortho_props(camobj);
  359. }
  360. /**
  361. * Set position and orientation for the EYE camera.
  362. * @method module:camera.eye_set_look_at
  363. * @param {Object3D} camobj Camera 3D-object.
  364. * @param {?Vec3} [pos=null] Position of the camera. Pass null to keep the current
  365. * camera position.
  366. * @param {?Vec3} [look_at=null] Point the camera is looking at. Pass null to
  367. * keep the existing camera orientation.
  368. */
  369. exports.eye_set_look_at = function(camobj, pos, look_at) {
  370. if (!m_cam.is_eye_camera(camobj)) {
  371. m_print.error("eye_set_look_at(): Wrong camera object or camera move style");
  372. return;
  373. }
  374. pos = pos || m_tsr.get_trans_view(camobj.render.world_tsr);
  375. if (look_at)
  376. m_cam.set_look_at_corrected(camobj, pos, look_at);
  377. else
  378. m_trans.set_translation(camobj, pos);
  379. m_cam.correct_up(camobj, camobj.render.vertical_axis, true);
  380. m_trans.update_transform(camobj);
  381. m_phy.sync_transform(camobj);
  382. };
  383. /**
  384. * Rotate the EYE camera counterclockwise (CCW) around its origin by the given angles.
  385. * Performs delta rotation or sets the camera's absolute rotation depending on
  386. * the "is_abs" parameter.
  387. * @see https://www.blend4web.com/doc/en/camera.html#camera-spherical-coordinates
  388. * @method module:camera.eye_rotate
  389. * @param {Object3D} camobj Camera 3D-object.
  390. * @param {number} phi Azimuth angle in radians.
  391. * @param {number} theta Elevation angle in radians.
  392. * @param {boolean} [is_abs=false] Performs delta rotation if FALSE, sets
  393. * camera's absolute rotation if TRUE.
  394. * @deprecated [17.06] Use {@link module:camera.rotate_camera} instead
  395. */
  396. exports.eye_rotate = function(camobj, phi, theta, is_abs) {
  397. m_print.error_deprecated("eye_rotate", "rotate_camera");
  398. if (!m_cam.is_eye_camera(camobj)) {
  399. m_print.error("eye_rotate(): Wrong camera object or camera move style");
  400. return;
  401. }
  402. is_abs = is_abs || false;
  403. exports.rotate_camera(camobj, phi, theta, is_abs);
  404. }
  405. /**
  406. * Set vertical rotation limits for the EYE camera.
  407. * @see https://www.blend4web.com/doc/en/camera.html#camera-spherical-coordinates
  408. * @method module:camera.eye_set_vertical_limits
  409. * @param {Object3D} camobj Camera 3D-object.
  410. * @param {?VerticalRotationLimits} [limits=null] Vertical rotation limits.
  411. * Pass null to disable the limits.
  412. */
  413. exports.eye_set_vertical_limits = function(camobj, limits) {
  414. if (!m_cam.is_eye_camera(camobj)) {
  415. m_print.error("eye_set_vertical_limits(): Wrong camera object or camera move style");
  416. return;
  417. }
  418. if (limits)
  419. if (typeof limits.down != "number" || typeof limits.up != "number") {
  420. m_print.error("eye_set_vertical_limits(): Incorrect limits object.");
  421. return;
  422. }
  423. m_cam.set_vertical_rot_limits(camobj, limits);
  424. m_trans.update_transform(camobj);
  425. m_phy.sync_transform(camobj);
  426. }
  427. /**
  428. * Get vertical rotation limits of the EYE camera (converted to the [-Pi, Pi] range).
  429. * @see https://www.blend4web.com/doc/en/camera.html#camera-spherical-coordinates
  430. * @method module:camera.eye_get_vertical_limits
  431. * @param {Object3D} camobj Camera 3D-object.
  432. * @param {?VerticalRotationLimits} [dest=new Object();] The receiving object.
  433. * @param {boolean} [local=false] Use camera local space representation.
  434. * @returns {?VerticalRotationLimits} Vertical rotation limits or null if disabled.
  435. */
  436. exports.eye_get_vertical_limits = function(camobj, dest, local) {
  437. if (!m_cam.is_eye_camera(camobj)) {
  438. m_print.error("eye_get_vertical_limits(): Wrong camera object or camera move style");
  439. return null;
  440. }
  441. var render = camobj.render;
  442. if (render.vertical_limits) {
  443. dest = dest || {};
  444. if (local) {
  445. dest.down = render.vertical_limits.down_local;
  446. dest.up = render.vertical_limits.up_local;
  447. } else {
  448. dest.down = render.vertical_limits.down;
  449. dest.up = render.vertical_limits.up;
  450. }
  451. dest.camera_space = local || false;
  452. return dest;
  453. } else
  454. return null;
  455. }
  456. /**
  457. * Set horizontal rotation limits for the EYE camera.
  458. * @see https://www.blend4web.com/doc/en/camera.html#camera-spherical-coordinates
  459. * @method module:camera.eye_set_horizontal_limits
  460. * @param {Object3D} camobj Camera 3D-object.
  461. * @param {?HorizontalRotationLimits} [limits=null] Horizontal rotation limits.
  462. * Pass null to disable the limits.
  463. */
  464. exports.eye_set_horizontal_limits = function(camobj, limits) {
  465. if (!m_cam.is_eye_camera(camobj)) {
  466. m_print.error("eye_set_horizontal_limits(): Wrong camera object or camera move style");
  467. return;
  468. }
  469. if (limits)
  470. if (typeof limits.left != "number" || typeof limits.right != "number") {
  471. m_print.error("eye_set_horizontal_limits(): Incorrect limits object.");
  472. return;
  473. }
  474. m_cam.set_horizontal_rot_limits(camobj, limits);
  475. m_trans.update_transform(camobj);
  476. m_phy.sync_transform(camobj);
  477. }
  478. /**
  479. * Get horizontal angle limits of the EYE camera (converted to the [0, 2Pi] range).
  480. * @see https://www.blend4web.com/doc/en/camera.html#camera-spherical-coordinates
  481. * @method module:camera.eye_get_horizontal_limits
  482. * @param {Object3D} camobj Camera 3D-object.
  483. * @param {?HorizontalRotationLimits} [dest=new Object();] The receiving object.
  484. * @param {boolean} [local=false] Use camera local space representation.
  485. * @returns {?HorizontalRotationLimits} Horizontal rotation limits or null if disabled.
  486. */
  487. exports.eye_get_horizontal_limits = function(camobj, dest, local) {
  488. if (!m_cam.is_eye_camera(camobj)) {
  489. m_print.error("eye_get_horizontal_limits(): Wrong camera object or camera move style");
  490. return null;
  491. }
  492. var render = camobj.render;
  493. if (render.horizontal_limits) {
  494. dest = dest || {};
  495. if (local) {
  496. dest.left = render.horizontal_limits.left_local;
  497. dest.right = render.horizontal_limits.right_local;
  498. } else {
  499. dest.left = render.horizontal_limits.left;
  500. dest.right = render.horizontal_limits.right;
  501. }
  502. dest.camera_space = local || false;
  503. return dest;
  504. } else
  505. return null;
  506. }
  507. /**
  508. * Setup the TARGET camera.
  509. * @method module:camera.target_setup
  510. * @param {Object3D} camobj Camera 3D-object.
  511. * @param {TargetCameraParams} params The parameters of the TARGET camera.
  512. */
  513. exports.target_setup = function(camobj, params) {
  514. if (!m_obj_util.is_camera(camobj)) {
  515. m_print.error("target_setup(): Wrong camera object");
  516. return;
  517. }
  518. if (params.horiz_rot_lim)
  519. if (typeof params.horiz_rot_lim.left != "number"
  520. || typeof params.horiz_rot_lim.right != "number") {
  521. m_print.error("target_setup(): Wrong horizontal limits object");
  522. return;
  523. }
  524. if (params.vert_rot_lim)
  525. if (typeof params.vert_rot_lim.down != "number"
  526. || typeof params.vert_rot_lim.up != "number") {
  527. m_print.error("target_setup(): Wrong vertical limits object");
  528. return;
  529. }
  530. if (params.dist_lim) {
  531. if (typeof params.dist_lim.min != "number"
  532. || typeof params.dist_lim.max != "number"
  533. || params.dist_lim.min > params.dist_lim.max) {
  534. m_print.error("target_setup(): Wrong distance limits object");
  535. return;
  536. }
  537. params.dist_lim.min = Math.max(params.dist_lim.min, 0);
  538. params.dist_lim.max = Math.max(params.dist_lim.max, 0);
  539. }
  540. if (params.pivot_lim) {
  541. if (typeof params.pivot_lim.min_y != "number"
  542. || typeof params.pivot_lim.max_y != "number"
  543. || params.pivot_lim.min_y > params.pivot_lim.max_y) {
  544. m_print.error("target_setup(): Wrong pivot limits object");
  545. return;
  546. }
  547. }
  548. m_cam.wipe_move_style(camobj);
  549. camobj.render.move_style = m_cam.MS_TARGET_CONTROLS;
  550. var pos = params.pos || m_tsr.get_trans_view(camobj.render.world_tsr);
  551. var pivot = params.pivot;
  552. if (!pivot) {
  553. var view_vec = get_view_vector(camobj, _vec3_tmp);
  554. pivot = m_vec3.add(pos, view_vec, view_vec);
  555. }
  556. m_cam.setup_target_model(camobj, pos, pivot, params.horiz_rot_lim,
  557. params.vert_rot_lim, params.dist_lim, params.pivot_lim,
  558. params.use_panning || false);
  559. m_trans.update_transform(camobj);
  560. m_phy.sync_transform(camobj);
  561. // init ortho after the camera was updated
  562. m_cam.init_ortho_props(camobj);
  563. }
  564. /**
  565. * Rotate the TARGET camera counterclockwise (CCW) around its pivot by the given
  566. * angles.
  567. * Performs delta rotation or sets the camera's absolute rotation depending on
  568. * the "is_abs" parameter.
  569. * @see https://www.blend4web.com/doc/en/camera.html#camera-spherical-coordinates
  570. * @method module:camera.target_rotate
  571. * @param {Object3D} camobj Camera 3D-object.
  572. * @param {number} phi Azimuth angle in radians.
  573. * @param {number} theta Elevation angle in radians.
  574. * @param {boolean} [is_abs=false] Performs delta rotation if FALSE, sets
  575. * camera's absolute rotation if TRUE.
  576. * @deprecated [17.06] Use {@link module:camera.rotate_camera} instead
  577. */
  578. exports.target_rotate = function(camobj, phi, theta, is_abs) {
  579. m_print.error_deprecated("target_rotate", "rotate_camera");
  580. if (!m_cam.is_target_camera(camobj)) {
  581. m_print.error("target_rotate(): Wrong camera object or camera move style");
  582. return;
  583. }
  584. is_abs = is_abs || false;
  585. exports.rotate_camera(camobj, phi, theta, is_abs);
  586. }
  587. /**
  588. * Set translation and pivot point for the TARGET camera.
  589. * The result of this operation can be corrected by existing limits.
  590. * @method module:camera.target_set_trans_pivot
  591. * @param {Object3D} camobj Camera 3D-object.
  592. * @param {?Vec3} [trans=null] Translation vector. Pass null to keep the current
  593. * camera position.
  594. * @param {?Vec3} [pivot=null] Pivot vector. Pass null to keep the current pivot
  595. * point.
  596. */
  597. exports.target_set_trans_pivot = function(camobj, trans, pivot) {
  598. if (!m_cam.is_target_camera(camobj)) {
  599. m_print.error("target_set_trans_pivot(): Wrong camera object or camera move style");
  600. return;
  601. }
  602. trans = trans || m_tsr.get_trans_view(camobj.render.world_tsr);
  603. pivot = pivot || camobj.render.pivot;
  604. m_cam.set_trans_pivot(camobj, trans, pivot);
  605. m_trans.update_transform(camobj);
  606. m_phy.sync_transform(camobj);
  607. }
  608. /**
  609. * Performs parallel translation for the TARGET camera. It works as similar as
  610. * the set_translation() method but uses the camera pivot to perform the translation.
  611. * @method module:camera.target_set_pivot_translation
  612. * @param {Object3D} camobj Camera 3D-object.
  613. * @param {Vec3} trans New pivot position.
  614. */
  615. exports.target_set_pivot_translation = function(camobj, trans) {
  616. if (!m_cam.is_target_camera(camobj)) {
  617. m_print.error("target_set_pivot_translation(): Wrong camera object or camera move style");
  618. return;
  619. }
  620. m_cam.set_target_pivot(camobj, trans);
  621. m_trans.update_transform(camobj);
  622. m_phy.sync_transform(camobj);
  623. }
  624. /**
  625. * Set distance to the pivot point for the TARGET camera.
  626. * The result of this operation can be corrected by existing limits.
  627. * @method module:camera.target_set_distance
  628. * @param {Object3D} camobj Camera 3D-object.
  629. * @param {number} distance Distance to the pivot point.
  630. */
  631. exports.target_set_distance = function(camobj, distance) {
  632. if (!m_cam.is_target_camera(camobj)) {
  633. m_print.error("target_set_distance(): Wrong camera object or camera move style");
  634. return;
  635. }
  636. var dist_curr = m_trans.obj_point_distance(camobj, camobj.render.pivot);
  637. var dist_needed = Math.max(0, distance);
  638. // +y move backward
  639. m_trans.move_local(camobj, 0, 0, dist_needed - dist_curr);
  640. m_trans.update_transform(camobj);
  641. m_phy.sync_transform(camobj);
  642. }
  643. /**
  644. * Get distance to the pivot point for the TARGET camera.
  645. * @method module:camera.target_get_distance
  646. * @param {Object3D} camobj Camera 3D-object.
  647. * @returns {number} Distance to the pivot point.
  648. */
  649. exports.target_get_distance = function(camobj) {
  650. if (!m_cam.is_target_camera(camobj)) {
  651. m_print.error("target_get_distance(): Wrong camera object or camera move style");
  652. return 0;
  653. }
  654. return m_trans.obj_point_distance(camobj, camobj.render.pivot);
  655. }
  656. /**
  657. * Zoom the TARGET camera to the object.
  658. * @method module:camera.target_zoom_object
  659. * @param {Object3D} camobj Camera 3D-object.
  660. * @param {Object3D} obj Object 3D.
  661. */
  662. exports.target_zoom_object = function(camobj, obj) {
  663. if (!m_cam.is_target_camera(camobj)) {
  664. m_print.error("target_zoom_object(): Wrong camera object or camera move style");
  665. return;
  666. }
  667. var center = m_trans.get_object_center(obj, false, _vec3_tmp);
  668. var radius = m_trans.get_object_size(obj);
  669. var active_scene = m_scs.get_active();
  670. var cam_scene_data = m_obj_util.get_scene_data(camobj, active_scene);
  671. var main_camera = cam_scene_data.cameras[0];
  672. var ang_radius = m_cam.get_angular_diameter(main_camera) / 2;
  673. var dist_need = radius / Math.sin(ang_radius);
  674. var dist_current = m_trans.obj_point_distance(camobj, center);
  675. m_cam.set_trans_pivot(camobj, m_tsr.get_trans_view(camobj.render.world_tsr), center);
  676. // TODO: excess update
  677. m_trans.update_transform(camobj);
  678. // +y move backward
  679. m_trans.move_local(camobj, 0, 0, dist_need - dist_current);
  680. m_trans.update_transform(camobj);
  681. m_phy.sync_transform(camobj);
  682. }
  683. /**
  684. * Get the pivot point of the TARGET camera.
  685. * @method module:camera.target_get_pivot
  686. * @param {Object3D} camobj Camera 3D-object.
  687. * @param {?Vec3} [dest=new Float32Array(3);] Pivot destination vector.
  688. * @returns {?Vec3} Pivot destination vector.
  689. */
  690. exports.target_get_pivot = function(camobj, dest) {
  691. if (!m_cam.is_target_camera(camobj)) {
  692. m_print.error("target_get_pivot(): Wrong camera object or camera move style");
  693. return null;
  694. }
  695. dest = dest || new Float32Array(3);
  696. m_vec3.copy(camobj.render.pivot, dest);
  697. return dest;
  698. }
  699. /**
  700. * Set distance limits for the TARGET camera.
  701. * @method module:camera.target_set_distance_limits
  702. * @param {Object3D} camobj Camera 3D-object.
  703. * @param {?DistanceLimits} [limits=null] Distance limits. Pass null to disable
  704. * the limits.
  705. */
  706. exports.target_set_distance_limits = function(camobj, limits) {
  707. if (!m_cam.is_target_camera(camobj)) {
  708. m_print.error("target_set_distance_limits(): Wrong camera object or camera move style");
  709. return;
  710. }
  711. if (limits) {
  712. if (typeof limits.min != "number" || typeof limits.max != "number"
  713. || limits.min > limits.max) {
  714. m_print.error("target_set_distance_limits(): Wrong limits object");
  715. return;
  716. }
  717. limits.min = Math.max(limits.min, 0);
  718. limits.max = Math.max(limits.max, 0);
  719. }
  720. m_cam.set_distance_limits(camobj, limits);
  721. m_trans.update_transform(camobj);
  722. m_phy.sync_transform(camobj);
  723. }
  724. /**
  725. * Get distance limits of the TARGET camera.
  726. * @method module:camera.target_get_distance_limits
  727. * @param {Object3D} camobj Camera 3D-object.
  728. * @param {?DistanceLimits} [dest=new Object();] The receiving object.
  729. * @returns {?DistanceLimits} Distance limits or null if disabled.
  730. */
  731. exports.target_get_distance_limits = function(camobj, dest) {
  732. if (!m_cam.is_target_camera(camobj)) {
  733. m_print.error("target_get_distance_limits(): Wrong camera object or camera move style");
  734. return null;
  735. }
  736. var render = camobj.render;
  737. if (render.distance_limits) {
  738. dest = dest || {};
  739. dest.min = render.distance_limits.min;
  740. dest.max = render.distance_limits.max;
  741. return dest;
  742. } else
  743. return null;
  744. }
  745. /**
  746. * Set vertical rotation limits for the TARGET camera.
  747. * @see https://www.blend4web.com/doc/en/camera.html#camera-spherical-coordinates
  748. * @method module:camera.target_set_vertical_limits
  749. * @param {Object3D} camobj Camera 3D-object.
  750. * @param {?VerticalRotationLimits} [limits=null] Vertical rotation limits.
  751. * Pass null to disable the limits.
  752. */
  753. exports.target_set_vertical_limits = function(camobj, limits) {
  754. if (!m_cam.is_target_camera(camobj)) {
  755. m_print.error("target_set_vertical_limits(): Wrong camera object or camera move style");
  756. return;
  757. }
  758. if (limits)
  759. if (typeof limits.down != "number" || typeof limits.up != "number") {
  760. m_print.error("target_set_vertical_limits(): Wrong limits object");
  761. return;
  762. }
  763. m_cam.set_vertical_rot_limits(camobj, limits);
  764. m_trans.update_transform(camobj);
  765. m_phy.sync_transform(camobj);
  766. }
  767. /**
  768. * Get vertical rotation limits of the TARGET camera (converted to the [-Pi, Pi] range).
  769. * @see https://www.blend4web.com/doc/en/camera.html#camera-spherical-coordinates
  770. * @method module:camera.target_get_vertical_limits
  771. * @param {Object3D} camobj Camera 3D-object.
  772. * @param {?VerticalRotationLimits} [dest=new Object();] The receiving object.
  773. * @param {boolean} [local=false] Use camera local space representation.
  774. * @returns {?VerticalRotationLimits} Vertical rotation limits or null if disabled.
  775. */
  776. exports.target_get_vertical_limits = function(camobj, dest, local) {
  777. if (!m_cam.is_target_camera(camobj)) {
  778. m_print.error("target_get_vertical_limits(): Wrong camera object or camera move style");
  779. return null;
  780. }
  781. var render = camobj.render;
  782. if (render.vertical_limits) {
  783. dest = dest || {};
  784. if (local) {
  785. dest.down = render.vertical_limits.down_local;
  786. dest.up = render.vertical_limits.up_local;
  787. } else {
  788. dest.down = render.vertical_limits.down;
  789. dest.up = render.vertical_limits.up;
  790. }
  791. dest.camera_space = local || false;
  792. return dest;
  793. } else
  794. return null;
  795. }
  796. /**
  797. * Set horizontal rotation limits for the TARGET camera.
  798. * @see https://www.blend4web.com/doc/en/camera.html#camera-spherical-coordinates
  799. * @method module:camera.target_set_horizontal_limits
  800. * @param {Object3D} camobj Camera 3D-object.
  801. * @param {?HorizontalRotationLimits} [limits=null] Horizontal rotation limits.
  802. * Pass null to disable the limits.
  803. */
  804. exports.target_set_horizontal_limits = function(camobj, limits) {
  805. if (!m_cam.is_target_camera(camobj)) {
  806. m_print.error("target_set_horizontal_limits(): Wrong camera object or camera move style");
  807. return;
  808. }
  809. if (limits)
  810. if (typeof limits.left != "number" || typeof limits.right != "number") {
  811. m_print.error("target_set_horizontal_limits(): Wrong limits object");
  812. return;
  813. }
  814. m_cam.set_horizontal_rot_limits(camobj, limits);
  815. m_trans.update_transform(camobj);
  816. m_phy.sync_transform(camobj);
  817. }
  818. /**
  819. * Get horizontal rotation limits of the TARGET camera (converted to the [0, 2Pi] range).
  820. * @see https://www.blend4web.com/doc/en/camera.html#camera-spherical-coordinates
  821. * @method module:camera.target_get_horizontal_limits
  822. * @param {Object3D} camobj Camera 3D-object.
  823. * @param {?HorizontalRotationLimits} [dest=new Object();] The receiving object.
  824. * @param {boolean} [local=false] Use camera local space representation.
  825. * @returns {?HorizontalRotationLimits} Horizontal rotation limits or null if disabled.
  826. */
  827. exports.target_get_horizontal_limits = function(camobj, dest, local) {
  828. if (!m_cam.is_target_camera(camobj)) {
  829. m_print.error("target_get_horizontal_limits(): Wrong camera object or camera move style");
  830. return null;
  831. }
  832. var render = camobj.render;
  833. if (render.horizontal_limits) {
  834. dest = dest || {};
  835. if (local) {
  836. dest.left = render.horizontal_limits.left_local;
  837. dest.right = render.horizontal_limits.right_local;
  838. } else {
  839. dest.left = render.horizontal_limits.left;
  840. dest.right = render.horizontal_limits.right;
  841. }
  842. dest.camera_space = local || false;
  843. return dest;
  844. } else
  845. return null;
  846. }
  847. /**
  848. * Set pivot limits for the TARGET camera.
  849. * @method module:camera.target_set_pivot_limits
  850. * @param {Object3D} camobj Camera 3D-object.
  851. * @param {?PivotLimits} [limits=null] Pivot limits. Pass null to
  852. * disable the limits.
  853. */
  854. exports.target_set_pivot_limits = function(camobj, limits) {
  855. if (!m_cam.is_target_camera(camobj)) {
  856. m_print.error("target_set_pivot_limits(): Wrong camera object or camera move style");
  857. return;
  858. }
  859. if (limits) {
  860. if (typeof limits.min_z != "number" || typeof limits.max_z != "number"
  861. || limits.min_z > limits.max_z) {
  862. m_print.error("target_set_pivot_limits(): Wrong limits object");
  863. return;
  864. }
  865. }
  866. m_cam.set_pivot_limits(camobj, limits);
  867. m_trans.update_transform(camobj);
  868. m_phy.sync_transform(camobj);
  869. }
  870. /**
  871. * Get pivot limits of the TARGET camera.
  872. * @method module:camera.target_get_pivot_limits
  873. * @param {Object3D} camobj Camera 3D-object.
  874. * @param {?PivotLimits} [dest=new Object();] The receiving object.
  875. * @returns {?PivotLimits} pivot limits or null if disabled.
  876. */
  877. exports.target_get_pivot_limits = function(camobj, dest) {
  878. if (!m_cam.is_target_camera(camobj)) {
  879. m_print.error("target_get_pivot_limits(): Wrong camera object or camera move style");
  880. return null;
  881. }
  882. var render = camobj.render;
  883. if (render.pivot_limits) {
  884. dest = dest || {};
  885. dest.min_z = render.pivot_limits.min_z;
  886. dest.max_z = render.pivot_limits.max_z;
  887. return dest;
  888. } else
  889. return null;
  890. }
  891. /**
  892. * Translate the pivot point of the TARGET camera in screen space.
  893. * Translation distance is defined with absolute value of parameters.
  894. * +h from left to right
  895. * +v from down to up
  896. * @method module:camera.target_pan_pivot
  897. * @param {Object3D} camobj Camera 3D-object
  898. * @param {number} trans_h_delta Absolute delta of the horizontal translation.
  899. * @param {number} trans_v_delta Absolute delta of the vertical translation.
  900. */
  901. exports.target_pan_pivot = function(camobj, trans_h_delta, trans_v_delta) {
  902. if (!m_cam.is_target_camera(camobj)) {
  903. m_print.error("target_pan_pivot(): wrong object");
  904. return;
  905. }
  906. var render = camobj.render;
  907. if (render.use_panning) {
  908. var trans_vector = _vec3_tmp;
  909. trans_vector[0] = trans_h_delta;
  910. trans_vector[1] = -trans_v_delta;
  911. trans_vector[2] = 0;
  912. m_tsr.transform_dir_vec3(trans_vector, render.world_tsr, trans_vector);
  913. m_vec3.add(render.pivot, trans_vector, render.pivot);
  914. var cam_trans = m_tsr.get_trans_view(render.world_tsr);
  915. m_vec3.add(cam_trans, trans_vector, cam_trans);
  916. m_trans.update_transform(camobj);
  917. m_phy.sync_transform(camobj);
  918. }
  919. }
  920. /**
  921. * Enable/disable panning for the TARGET camera.
  922. * @method module:camera.target_switch_panning
  923. * @param {Object3D} camobj Camera 3D-object.
  924. * @param {boolean} enable Enable or disable panning.
  925. */
  926. exports.target_switch_panning = function(camobj, enable) {
  927. if (!m_cam.is_target_camera(camobj)) {
  928. m_print.error("target_switch_panning(): Wrong camera object or camera move style");
  929. return null;
  930. }
  931. camobj.render.use_panning = enable;
  932. }
  933. /**
  934. * Setup HOVER camera model with the distance and hover angle limits defined as
  935. * variations around the current values which depend on the given camera/pivot
  936. * positions.
  937. * @method module:camera.hover_setup_rel
  938. * @param {Object3D} camobj Camera 3D-object.
  939. * @param {HoverCameraParamsRel} params The parameters of the HOVER camera.
  940. */
  941. exports.hover_setup_rel = function(camobj, params) {
  942. exports.hover_setup(camobj, { pos: params.pos, pivot: params.pivot });
  943. if (typeof params.dist_interval == "undefined")
  944. var dist_interval = 0;
  945. else
  946. var dist_interval = Math.max(params.dist_interval, 0);
  947. if (typeof params.angle_interval == "undefined")
  948. var angle_interval = 0;
  949. else
  950. var angle_interval = Math.max(params.angle_interval, 0);
  951. if (typeof params.t == "undefined")
  952. var t = 0.5;
  953. else
  954. var t = m_util.clamp(params.t, 0, 1);
  955. var dist_lim = exports.hover_get_distance_limits(camobj, _limits_tmp);
  956. dist_lim.min = Math.max(dist_lim.min - t * dist_interval, 0);
  957. dist_lim.max = dist_lim.max + (1 - t) * dist_interval;
  958. var ha_lim = exports.hover_get_vertical_limits(camobj, _limits_tmp2);
  959. ha_lim.down = m_util.clamp(ha_lim.down + t * angle_interval, -Math.PI/2, 0);
  960. ha_lim.up = m_util.clamp(ha_lim.up - (1 - t) * angle_interval, -Math.PI/2, 0);
  961. m_cam.hover_set_distance_limits(camobj, dist_lim);
  962. m_cam.hover_set_vertical_limits(camobj, ha_lim);
  963. m_trans.update_transform(camobj);
  964. m_phy.sync_transform(camobj);
  965. // init ortho after the camera was updated
  966. m_cam.init_ortho_props(camobj);
  967. }
  968. /**
  969. * Setup HOVER camera model.
  970. * @method module:camera.hover_setup
  971. * @param {Object3D} camobj Camera 3D-object.
  972. * @param {HoverCameraParams} params The parameters of the HOVER camera.
  973. */
  974. exports.hover_setup = function(camobj, params) {
  975. if (!m_obj_util.is_camera(camobj)) {
  976. m_print.error("hover_setup(): Wrong camera object");
  977. return;
  978. }
  979. if (params.dist_lim) {
  980. if (typeof params.dist_lim.min != "number"
  981. || typeof params.dist_lim.max != "number"
  982. || params.dist_lim.min > params.dist_lim.max) {
  983. m_print.error("hover_setup(): Wrong distance limits object");
  984. return;
  985. }
  986. params.dist_lim.min = Math.max(params.dist_lim.min, 0);
  987. params.dist_lim.max = Math.max(params.dist_lim.max, 0);
  988. }
  989. if (params.hover_angle_lim)
  990. if (typeof params.hover_angle_lim.down != "number"
  991. || typeof params.hover_angle_lim.up != "number"
  992. || params.hover_angle_lim.down < params.hover_angle_lim.up) {
  993. m_print.error("hover_setup(): Wrong hover angle limits object");
  994. return;
  995. }
  996. if (params.horiz_trans_lim)
  997. if (typeof params.horiz_trans_lim.min != "number"
  998. || typeof params.horiz_trans_lim.max != "number"
  999. || params.horiz_trans_lim.min > params.horiz_trans_lim.max) {
  1000. m_print.error("hover_setup(): Wrong horizontal translation limits object");
  1001. return;
  1002. }
  1003. if (params.vert_trans_lim)
  1004. if (typeof params.vert_trans_lim.min != "number"
  1005. || typeof params.vert_trans_lim.max != "number"
  1006. || params.vert_trans_lim.min > params.vert_trans_lim.max) {
  1007. m_print.error("hover_setup(): Wrong vertical translation limits object");
  1008. return;
  1009. }
  1010. m_cam.wipe_move_style(camobj);
  1011. camobj.render.move_style = m_cam.MS_HOVER_CONTROLS;
  1012. var pos = params.pos || m_tsr.get_trans_view(camobj.render.world_tsr);
  1013. m_cam.setup_hover_model(camobj, pos, params.pivot, params.dist_lim,
  1014. params.hover_angle_lim, params.horiz_trans_lim,
  1015. params.vert_trans_lim, params.enable_horiz_rot || false);
  1016. m_trans.update_transform(camobj);
  1017. m_phy.sync_transform(camobj);
  1018. // init ortho after the camera was updated
  1019. m_cam.init_ortho_props(camobj);
  1020. }
  1021. /**
  1022. * Rotate the HOVER camera around its pivot by the given angles.
  1023. * Performs delta rotation or sets the camera's absolute rotation depending on
  1024. * the "is_abs" parameter.
  1025. * @see https://www.blend4web.com/doc/en/camera.html#camera-spherical-coordinates
  1026. * @method module:camera.hover_rotate
  1027. * @param {Object3D} camobj Camera 3D-object.
  1028. * @param {number} phi Azimuth angle in radians
  1029. * @param {number} theta Elevation angle in radians
  1030. * @param {boolean} [is_abs=false] Performs delta rotation if FALSE, sets
  1031. * camera's absolute rotation if TRUE.
  1032. * @deprecated [17.06] Use {@link module:camera.rotate_camera} instead
  1033. */
  1034. exports.hover_rotate = function(camobj, phi, theta, is_abs) {
  1035. m_print.error_deprecated("hover_rotate", "rotate_camera");
  1036. if (!m_cam.is_hover_camera(camobj)) {
  1037. m_print.error("hover_rotate(): Wrong camera object or camera move style");
  1038. return;
  1039. }
  1040. is_abs = is_abs || false;
  1041. exports.rotate_camera(camobj, phi, theta, is_abs);
  1042. }
  1043. /**
  1044. * Performs parallel translation for the HOVER camera. It works as similar as
  1045. * the set_translation() method but uses the camera pivot to perform the translation.
  1046. * @method module:camera.hover_set_pivot_translation
  1047. * @param {Object3D} camobj Camera 3D-object.
  1048. * @param {Vec3} trans New pivot position.
  1049. */
  1050. exports.hover_set_pivot_translation = function(camobj, trans) {
  1051. if (!m_cam.is_hover_camera(camobj)) {
  1052. m_print.error("hover_set_pivot_translation(): Wrong camera object or camera move style");
  1053. return;
  1054. }
  1055. m_cam.set_hover_pivot(camobj, trans);
  1056. m_trans.update_transform(camobj);
  1057. m_phy.sync_transform(camobj);
  1058. }
  1059. /**
  1060. * Get pivot position of the HOVER camera.
  1061. * @method module:camera.hover_get_pivot
  1062. * @param {Object3D} camobj Camera 3D-object.
  1063. * @param {Vec3} [dest=new Float32Array(3);] Destination vector for the pivot translation.
  1064. * @returns {?Vec3} Destination vector for the pivot translation.
  1065. */
  1066. exports.hover_get_pivot = function(camobj, dest) {
  1067. if (!m_cam.is_hover_camera(camobj)) {
  1068. m_print.error("hover_get_pivot(): Wrong camera object or camera move style");
  1069. return null;
  1070. }
  1071. dest = dest || new Float32Array(3);
  1072. m_vec3.copy(camobj.render.hover_pivot, dest);
  1073. return dest;
  1074. }
  1075. /**
  1076. * Get distance to the pivot point for the HOVER camera.
  1077. * @method module:camera.hover_get_distance
  1078. * @param {Object3D} camobj Camera 3D-object.
  1079. * @returns {number} Distance to the pivot.
  1080. */
  1081. exports.hover_get_distance = function(camobj) {
  1082. if (!m_cam.is_hover_camera(camobj)) {
  1083. m_print.error("hover_get_distance(): Wrong camera object or camera move style");
  1084. return 0;
  1085. }
  1086. return m_trans.obj_point_distance(camobj, camobj.render.hover_pivot);
  1087. }
  1088. /**
  1089. * Set vertical rotation (hover angle) limits for the HOVER camera.
  1090. * @see https://www.blend4web.com/doc/en/camera.html#camera-spherical-coordinates
  1091. * @method module:camera.hover_set_vertical_limits
  1092. * @param {Object3D} camobj Camera 3D-object.
  1093. * @param {HoverAngleLimits} limits Hover angle limits
  1094. */
  1095. exports.hover_set_vertical_limits = function(camobj, limits) {
  1096. if (!m_cam.is_hover_camera(camobj)) {
  1097. m_print.error("hover_set_vertical_limits(): Wrong camera object or camera move style");
  1098. return;
  1099. }
  1100. if (typeof limits.down != "number" || typeof limits.up != "number"
  1101. || limits.down < limits.up) {
  1102. m_print.error("hover_set_vertical_limits(): Wrong limits object");
  1103. return;
  1104. }
  1105. m_cam.hover_set_vertical_limits(camobj, limits);
  1106. m_trans.update_transform(camobj);
  1107. m_phy.sync_transform(camobj);
  1108. }
  1109. /**
  1110. * Get vertical rotation (hover angle) limits for the HOVER camera (converted to the [-Pi, 0] range).
  1111. * @see https://www.blend4web.com/doc/en/camera.html#camera-spherical-coordinates
  1112. * @method module:camera.hover_get_vertical_limits
  1113. * @param {Object3D} camobj Camera 3D-object.
  1114. * @param {?HoverAngleLimits} [dest=new Object();] The receiving object.
  1115. * @returns {?HoverAngleLimits} Hover angle limits.
  1116. */
  1117. exports.hover_get_vertical_limits = function(camobj, dest) {
  1118. if (!m_cam.is_hover_camera(camobj)) {
  1119. m_print.error("hover_get_vertical_limits(): Wrong camera object or camera move style");
  1120. return null;
  1121. }
  1122. var render = camobj.render;
  1123. dest = dest || {};
  1124. dest.down = render.vertical_limits.down;
  1125. dest.up = render.vertical_limits.up;
  1126. return dest;
  1127. }
  1128. /**
  1129. * Set distance limits for the HOVER camera.
  1130. * @method module:camera.hover_set_distance_limits
  1131. * @param {Object3D} camobj Camera 3D-object.
  1132. * @param {DistanceLimits} limits Distance limits.
  1133. */
  1134. exports.hover_set_distance_limits = function(camobj, limits) {
  1135. if (!m_cam.is_hover_camera(camobj)) {
  1136. m_print.error("hover_set_distance_limits(): Wrong camera object or camera move style");
  1137. return;
  1138. }
  1139. if (typeof limits.min != "number" || typeof limits.max != "number"
  1140. || limits.min > limits.max) {
  1141. m_print.error("hover_set_distance_limits(): Wrong limits object");
  1142. return;
  1143. }
  1144. limits.min = Math.max(limits.min, 0);
  1145. limits.max = Math.max(limits.max, 0);
  1146. m_cam.hover_set_distance_limits(camobj, limits);
  1147. m_trans.update_transform(camobj);
  1148. m_phy.sync_transform(camobj);
  1149. }
  1150. /**
  1151. * Get distance limits of the HOVER camera.
  1152. * @method module:camera.hover_get_distance_limits
  1153. * @param {Object3D} camobj Camera 3D-object.
  1154. * @param {?DistanceLimits} [dest=new Object();] The receiving object.
  1155. * @returns {?DistanceLimits} Distance limits.
  1156. */
  1157. exports.hover_get_distance_limits = function(camobj, dest) {
  1158. if (!m_cam.is_hover_camera(camobj)) {
  1159. m_print.error("hover_get_distance_limits(): Wrong camera object or camera move style");
  1160. return null;
  1161. }
  1162. var render = camobj.render;
  1163. dest = dest || {};
  1164. dest.min = render.distance_limits.min;
  1165. dest.max = render.distance_limits.max;
  1166. return dest;
  1167. }
  1168. /**
  1169. * Set vertical (along the Z axis) translation limits for the HOVER camera.
  1170. * @see https://www.blend4web.com/doc/en/camera.html#hover-translation-limits
  1171. * @method module:camera.hover_set_vert_trans_limits
  1172. * @param {Object3D} camobj Camera 3D-object.
  1173. * @param {?VerticalTranslationLimits} limits Vertical translation limits.
  1174. * Pass null to disable the limits.
  1175. */
  1176. exports.hover_set_vert_trans_limits = function(camobj, limits) {
  1177. if (!m_cam.is_hover_camera(camobj)) {
  1178. m_print.error("hover_set_vert_trans_limits(): Wrong camera object or camera move style");
  1179. return;
  1180. }
  1181. if (limits)
  1182. if (typeof limits.min != "number" || typeof limits.max != "number"
  1183. || limits.min > limits.max) {
  1184. m_print.error("hover_set_vert_trans_limits(): Wrong limits object");
  1185. return;
  1186. }
  1187. m_cam.set_vert_trans_limits(camobj, limits);
  1188. m_trans.update_transform(camobj);
  1189. m_phy.sync_transform(camobj);
  1190. }
  1191. /**
  1192. * Get vertical translation limits of the HOVER camera.
  1193. * @see https://www.blend4web.com/doc/en/camera.html#hover-translation-limits
  1194. * @method module:camera.hover_get_vert_trans_limits
  1195. * @param {Object3D} camobj Camera 3D-object.
  1196. * @param {?VerticalTranslationLimits} [dest=new Object();] The receiving object.
  1197. * @returns {?VerticalTranslationLimits} Vertical translation limits or null if disabled.
  1198. */
  1199. exports.hover_get_vert_trans_limits = function(camobj, dest) {
  1200. if (!m_cam.is_hover_camera(camobj)) {
  1201. m_print.error("hover_get_vert_trans_limits(): Wrong camera object or camera move style");
  1202. return null;
  1203. }
  1204. var render = camobj.render;
  1205. if (render.hover_vert_trans_limits) {
  1206. dest = dest || {};
  1207. dest.min = render.hover_vert_trans_limits.min;
  1208. dest.max = render.hover_vert_trans_limits.max;
  1209. return dest;
  1210. } else
  1211. return null;
  1212. }
  1213. /**
  1214. * Set horizontal (along the X axis) translation limits for the HOVER camera.
  1215. * @see https://www.blend4web.com/doc/en/camera.html#hover-translation-limits
  1216. * @method module:camera.hover_set_horiz_trans_limits
  1217. * @param {Object3D} camobj Camera 3D-object.
  1218. * @param {?HorizontalTranslationLimits} limits Horizontal translation limits.
  1219. * Pass null to disable the limits.
  1220. */
  1221. exports.hover_set_horiz_trans_limits = function(camobj, limits) {
  1222. if (!m_cam.is_hover_camera(camobj)) {
  1223. m_print.error("hover_set_horiz_trans_limits(): Wrong camera object or camera move style");
  1224. return;
  1225. }
  1226. if (limits)
  1227. if (typeof limits.min != "number" || typeof limits.max != "number"
  1228. || limits.min > limits.max) {
  1229. m_print.error("hover_set_horiz_trans_limits(): Wrong limits object");
  1230. return;
  1231. }
  1232. m_cam.set_hor_trans_limits(camobj, limits);
  1233. m_trans.update_transform(camobj);
  1234. m_phy.sync_transform(camobj);
  1235. }
  1236. /**
  1237. * Get horizontal translation limits of the HOVER camera.
  1238. * @see https://www.blend4web.com/doc/en/camera.html#hover-translation-limits
  1239. * @method module:camera.hover_get_horiz_trans_limits
  1240. * @param {Object3D} camobj Camera 3D-object.
  1241. * @param {?HorizontalTranslationLimits} [dest=new Object();] The receiving object.
  1242. * @returns {?HorizontalTranslationLimits} Horizontal translation limits or null if disabled.
  1243. */
  1244. exports.hover_get_horiz_trans_limits = function(camobj, dest) {
  1245. if (!m_cam.is_hover_camera(camobj)) {
  1246. m_print.error("hover_get_horiz_trans_limits(): Wrong camera object or camera move style");
  1247. return null;
  1248. }
  1249. var render = camobj.render;
  1250. if (render.hover_horiz_trans_limits) {
  1251. dest = dest || {};
  1252. dest.min = render.hover_horiz_trans_limits.min;
  1253. dest.max = render.hover_horiz_trans_limits.max;
  1254. return dest;
  1255. } else
  1256. return null;
  1257. }
  1258. /**
  1259. * Enable/disable horizontal rotation for the HOVER camera.
  1260. * @method module:camera.hover_switch_horiz_rotation
  1261. * @param {Object3D} camobj Camera 3D-object.
  1262. * @param {boolean} enable Enable or disable the rotation.
  1263. */
  1264. exports.hover_switch_horiz_rotation = function(camobj, enable) {
  1265. if (!m_cam.is_hover_camera(camobj)) {
  1266. m_print.error("hover_switch_horiz_rotation(): Wrong camera object or camera move style");
  1267. return null;
  1268. }
  1269. camobj.render.enable_hover_hor_rotation = enable;
  1270. }
  1271. /**
  1272. * Check if the object is a camera and has the MS_STATIC movement style.
  1273. * @method module:camera.is_static_camera
  1274. * @param {Object3D} obj Object 3D
  1275. * @returns {boolean} The result of the checking.
  1276. */
  1277. exports.is_static_camera = m_cam.is_static_camera;
  1278. /**
  1279. * Check if the object is a camera and has the MS_TARGET_CONTROLS movement style.
  1280. * @method module:camera.is_target_camera
  1281. * @param {Object3D} obj Object 3D
  1282. * @returns {boolean} The result of the checking.
  1283. */
  1284. exports.is_target_camera = m_cam.is_target_camera;
  1285. /**
  1286. * Check if the object is a camera and has the MS_EYE_CONTROLS movement style.
  1287. * @method module:camera.is_eye_camera
  1288. * @param {Object3D} obj Object 3D
  1289. * @returns {boolean} The result of the checking.
  1290. */
  1291. exports.is_eye_camera = m_cam.is_eye_camera;
  1292. /**
  1293. * Check if the object is a camera and has the MS_HOVER_CONTROLS movement style.
  1294. * @method module:camera.is_hover_camera
  1295. * @param {Object3D} obj Object 3D
  1296. * @returns {boolean} The result of the checking.
  1297. */
  1298. exports.is_hover_camera = m_cam.is_hover_camera;
  1299. function is_hmd_camera(camobj) {
  1300. if (!m_obj_util.is_camera(camobj)) {
  1301. m_print.error("is_hmd_camera(): Wrong camera object");
  1302. return false;
  1303. }
  1304. var active_scene = m_scs.get_active();
  1305. var cam_scene_data = m_obj_util.get_scene_data(camobj, active_scene);
  1306. var cameras = cam_scene_data.cameras;
  1307. for (var i = 0; i < cameras.length; i++) {
  1308. var cam = cameras[i];
  1309. if (cam.type == m_cam.TYPE_HMD_LEFT ||
  1310. cam.type == m_cam.TYPE_HMD_RIGHT)
  1311. return true;
  1312. }
  1313. return false;
  1314. }
  1315. /**
  1316. * Get movement style of the camera.
  1317. * @method module:camera.get_move_style
  1318. * @param {Object3D} camobj Camera 3D-object.
  1319. * @returns {?CameraMoveStyle} Camera movement style.
  1320. */
  1321. exports.get_move_style = function(camobj) {
  1322. if (!m_obj_util.is_camera(camobj)) {
  1323. m_print.error("get_move_style(): Wrong camera object");
  1324. return null;
  1325. }
  1326. return m_cam.get_move_style(camobj);
  1327. }
  1328. /**
  1329. * Translates the STATIC/EYE camera. Performs parallel translation for the TARGET/HOVER
  1330. * camera and its pivot.
  1331. * @method module:camera.set_translation
  1332. * @param {Object3D} camobj Camera 3D-object.
  1333. * @param {Vec3} trans New camera position.
  1334. */
  1335. exports.set_translation = function(camobj, trans) {
  1336. if (!m_obj_util.is_camera(camobj)) {
  1337. m_print.error("set_translation(): Wrong camera object");
  1338. return;
  1339. }
  1340. var render = camobj.render;
  1341. if (m_cam.is_target_camera(camobj)) {
  1342. var cam_trans = m_tsr.get_trans_view(render.world_tsr);
  1343. var trans_delta = m_vec3.subtract(trans, cam_trans, _vec3_tmp);
  1344. m_vec3.add(trans_delta, render.pivot, render.pivot);
  1345. } else if (m_cam.is_hover_camera(camobj)) {
  1346. var cam_trans = m_tsr.get_trans_view(render.world_tsr);
  1347. var trans_delta = m_vec3.subtract(trans, cam_trans, _vec3_tmp);
  1348. m_vec3.add(trans_delta, render.hover_pivot, render.hover_pivot);
  1349. }
  1350. m_trans.set_translation(camobj, trans);
  1351. m_trans.update_transform(camobj);
  1352. m_phy.sync_transform(camobj);
  1353. }
  1354. /**
  1355. * Get the camera's translation vector.
  1356. * @method module:camera.get_translation
  1357. * @param {Object3D} camobj Camera 3D-object.
  1358. * @param {Vec3} [dest=new Float32Array(3);] Destination vector.
  1359. * @returns {?Vec3} Destination vector.
  1360. */
  1361. exports.get_translation = function(camobj, dest) {
  1362. if (!m_obj_util.is_camera(camobj)) {
  1363. m_print.error("get_translation(): Wrong camera object");
  1364. return null;
  1365. }
  1366. dest = dest || new Float32Array(3);
  1367. return m_trans.get_translation(camobj, dest);
  1368. }
  1369. /**
  1370. * Rotate the TARGET/EYE/HOVER camera counterclockwise (CCW) by the given
  1371. * angles depending on the camera's movement style.
  1372. * Performs the delta rotation or sets the camera's absolute rotation depending
  1373. * on the "*_is_abs" parameters.
  1374. * @see https://www.blend4web.com/doc/en/camera.html#camera-spherical-coordinates
  1375. * @method module:camera.rotate_camera
  1376. * @param {Object3D} camobj Camera 3D-object.
  1377. * @param {number} phi Azimuth angle in radians
  1378. * @param {number} theta Elevation angle in radians
  1379. * @param {boolean} [is_abs=false] Performs delta rotation if FALSE, sets camera's absolute rotation if TRUE.
  1380. */
  1381. exports.rotate_camera = function(camobj, phi, theta, is_abs) {
  1382. if (!m_obj_util.is_camera(camobj)) {
  1383. m_print.error("rotate_camera(): Wrong camera object");
  1384. return;
  1385. }
  1386. is_abs = is_abs || false;
  1387. if (is_abs)
  1388. m_cam.set_rotation_angles(camobj, phi, theta);
  1389. else
  1390. m_cam.rotate_angles(camobj, phi, theta);
  1391. m_trans.update_transform(camobj);
  1392. m_phy.sync_transform(camobj);
  1393. }
  1394. /**
  1395. * Set velocity parameters for the camera.
  1396. * @method module:camera.set_velocities
  1397. * @param {Object3D} camobj Camera 3D-object.
  1398. * @param {VelocityParams} velocity Velocity parameters.
  1399. */
  1400. exports.set_velocities = function(camobj, velocity) {
  1401. if (!m_obj_util.is_camera(camobj)) {
  1402. m_print.error("set_velocities(): Wrong camera object");
  1403. return;
  1404. }
  1405. var render = camobj.render;
  1406. if (typeof velocity.trans == "number")
  1407. render.velocity_trans = m_util.clamp(velocity.trans, 0, Infinity);
  1408. if (typeof velocity.rot == "number")
  1409. render.velocity_rot = m_util.clamp(velocity.rot, 0, Infinity);
  1410. if (typeof velocity.zoom == "number")
  1411. render.velocity_zoom = m_util.clamp(velocity.zoom, 0, 0.99);
  1412. }
  1413. /**
  1414. * Get velocity parameters of the camera.
  1415. * @method module:camera.get_velocities
  1416. * @param {Object3D} camobj Camera 3D-object.
  1417. * @param {VelocityParams} dest The receiving object.
  1418. * @returns {?VelocityParams} Velocity parameters.
  1419. */
  1420. exports.get_velocities = function(camobj, dest) {
  1421. if (!m_obj_util.is_camera(camobj)) {
  1422. m_print.error("get_velocities(): Wrong camera object");
  1423. return null;
  1424. }
  1425. var render = camobj.render;
  1426. dest = dest || {};
  1427. dest.trans = render.velocity_trans;
  1428. dest.rot = render.velocity_rot;
  1429. dest.zoom = render.velocity_zoom;
  1430. return dest;
  1431. }
  1432. /**
  1433. * Check whether the camera is looking upwards.
  1434. * @method module:camera.is_look_up
  1435. * @param {Object3D} camobj Camera 3D-object.
  1436. * @returns {boolean} The result of the checking.
  1437. */
  1438. exports.is_look_up = function(camobj) {
  1439. if (!m_obj_util.is_camera(camobj)) {
  1440. m_print.error("is_look_up(): Wrong camera object");
  1441. return false;
  1442. }
  1443. var quat = m_tsr.get_quat_view(camobj.render.world_tsr);
  1444. var dir = m_util.quat_to_dir(quat, m_util.AXIS_MZ, _vec3_tmp);
  1445. return dir[1] >= 0;
  1446. }
  1447. /**
  1448. * Get the angles of horizontal (azimuth) and vertical (elevation) rotation
  1449. * (CCW as seen from the rotation axis) of the TARGET/HOVER camera, or the
  1450. * analogous orientation angles of the EYE camera.
  1451. * Intended for the cameras with corrected up vector.
  1452. * @see https://www.blend4web.com/doc/en/camera.html#camera-spherical-coordinates
  1453. * @method module:camera.get_camera_angles
  1454. * @param {Object3D} camobj Camera 3D-object.
  1455. * @param {?Vec2} [dest=new Float32Array(2);] Destination vector for the camera
  1456. * angles (in radians): [phi, theta], phi: [0, 2Pi], theta: [-Pi, Pi].
  1457. * @returns {?Vec2} Destination vector for the camera angles (in radians): [phi, theta].
  1458. */
  1459. exports.get_camera_angles = function(camobj, dest) {
  1460. if (!m_obj_util.is_camera(camobj)) {
  1461. m_print.error("get_camera_angles(): Wrong camera object");
  1462. return null;
  1463. }
  1464. dest = dest || new Float32Array(2);
  1465. m_cam.get_camera_angles(camobj, dest);
  1466. return dest;
  1467. }
  1468. /**
  1469. * Get the angles of horizontal (azimuth) and vertical (elevation) rotation
  1470. * (CCW as seen from the rotation axis) of the TARGET/HOVER camera, or the
  1471. * analogous orientation angles of the EYE camera.
  1472. * The angles are converted for the character object.
  1473. * Intended for the cameras with corrected up vector.
  1474. * @see https://www.blend4web.com/doc/en/camera.html#camera-spherical-coordinates
  1475. * @method module:camera.get_camera_angles_char
  1476. * @param {Object3D} camobj Camera 3D-object.
  1477. * @param {?Vec2} [dest=new Float32Array(2);] Destination vector for the camera
  1478. * angles (in radians): [phi, theta], phi: [0, 2Pi], theta: [-Pi, Pi].
  1479. * @returns {?Vec2} Destination vector for the camera angles (in radians): [phi, theta].
  1480. */
  1481. exports.get_camera_angles_char = function(camobj, dest) {
  1482. if (!m_obj_util.is_camera(camobj)) {
  1483. m_print.error("get_camera_angles_char(): Wrong camera object");
  1484. return null;
  1485. }
  1486. dest = dest || new Float32Array(2);
  1487. m_cam.get_camera_angles_char(camobj, dest);
  1488. return dest;
  1489. }
  1490. /**
  1491. * Get the angles of horizontal (azimuth) and vertical (elevation) rotation
  1492. * (CCW as seen from the rotation axis) of the TARGET/HOVER camera, or the
  1493. * analogous orientation angles of the EYE camera from the given direction
  1494. * representing the view vector of the camera, which up vector is vertically aligned.
  1495. * @see https://www.blend4web.com/doc/en/camera.html#camera-spherical-coordinates
  1496. * @method module:camera.get_camera_angles_dir
  1497. * @param {Vec3} dir Direction representing the view vector of the camera.
  1498. * @param {?Vec2} [dest=new Float32Array(2);] Destination vector for the camera
  1499. * angles (in radians): [phi, theta], phi: [0, 2Pi], theta: [-Pi, Pi].
  1500. * @returns {?Vec2} Destination vector for the camera angles (in radians): [phi, theta].
  1501. * @example
  1502. * var m_cam = require("camera");
  1503. * var m_vec3 = require("vec3");
  1504. *
  1505. * var view_vec = m_vec3.fromValues(10, 5, -3);
  1506. * var angles = new Float32Array(2);
  1507. * m_cam.get_camera_angles_dir(view_vec, angles);
  1508. * var phi = angles[0], theta = angles[1];
  1509. */
  1510. exports.get_camera_angles_dir = function(dir, dest) {
  1511. dest = dest || new Float32Array(2);
  1512. var dir_norm = m_vec3.normalize(dir, _vec3_tmp);
  1513. var quat = m_util.rotation_to_stable(m_util.AXIS_MZ, dir_norm, _quat_tmp);
  1514. m_util.correct_cam_quat_up(quat, true);
  1515. m_cam.get_camera_angles_from_quat(quat, m_util.AXIS_Z, dest);
  1516. return dest;
  1517. }
  1518. /**
  1519. * Set the distance to the convergence plane of the stereoscopic camera.
  1520. * @method module:camera.set_stereo_distance
  1521. * @param {Object3D} camobj Camera 3D-object.
  1522. * @param {number} conv_dist Distance from the convergence plane.
  1523. */
  1524. exports.set_stereo_distance = function(camobj, conv_dist) {
  1525. if (!m_obj_util.is_camera(camobj)) {
  1526. m_print.error("set_stereo_distance(): Wrong camera object");
  1527. return;
  1528. }
  1529. var active_scene = m_scs.get_active();
  1530. var cam_scene_data = m_obj_util.get_scene_data(camobj, active_scene);
  1531. var cameras = cam_scene_data.cameras;
  1532. for (var i = 0; i < cameras.length; i++) {
  1533. var cam = cameras[i];
  1534. if (cam.type == m_cam.TYPE_STEREO_LEFT ||
  1535. cam.type == m_cam.TYPE_STEREO_RIGHT ||
  1536. cam.type == m_cam.TYPE_HMD_LEFT ||
  1537. cam.type == m_cam.TYPE_HMD_RIGHT)
  1538. m_cam.set_stereo_params(cam, conv_dist, cam.stereo_eye_dist);
  1539. }
  1540. }
  1541. /**
  1542. * Get the distance from the convergence plane of the stereoscopic camera.
  1543. * @method module:camera.get_stereo_distance
  1544. * @param {Object3D} camobj Camera 3D-object.
  1545. * @returns {number} Distance from convergence plane.
  1546. */
  1547. exports.get_stereo_distance = function(camobj) {
  1548. if (!m_obj_util.is_camera(camobj)) {
  1549. m_print.error("get_stereo_distance(): Wrong camera object");
  1550. return 0;
  1551. }
  1552. var active_scene = m_scs.get_active();
  1553. var cam_scene_data = m_obj_util.get_scene_data(camobj, active_scene);
  1554. var cameras = cam_scene_data.cameras;
  1555. for (var i = 0; i < cameras.length; i++) {
  1556. var cam = cameras[i];
  1557. if (cam.type == m_cam.TYPE_STEREO_LEFT ||
  1558. cam.type == m_cam.TYPE_STEREO_RIGHT)
  1559. return cam.stereo_conv_dist;
  1560. }
  1561. return 0;
  1562. }
  1563. /**
  1564. * Set the distance between eyes of the stereoscopic camera.
  1565. * @method module:camera.set_eye_distance
  1566. * @param {Object3D} camobj Camera 3D-object.
  1567. * @param {number} eye_dist Distance between eyes.
  1568. */
  1569. exports.set_eye_distance = function(camobj, eye_dist) {
  1570. if (!m_obj_util.is_camera(camobj)) {
  1571. m_print.error("set_eye_distance(): Wrong camera object");
  1572. return;
  1573. }
  1574. var active_scene = m_scs.get_active();
  1575. var cam_scene_data = m_obj_util.get_scene_data(camobj, active_scene);
  1576. var cameras = cam_scene_data.cameras;
  1577. m_cam.set_eye_distance(cameras, eye_dist);
  1578. }
  1579. /**
  1580. * Get the distance between eyes of the stereoscopic camera.
  1581. * @method module:camera.get_eye_distance
  1582. * @param {Object3D} camobj Camera 3D-object.
  1583. * @returns {number} Distance between eyes.
  1584. */
  1585. exports.get_eye_distance = function(camobj) {
  1586. if (!m_obj_util.is_camera(camobj)) {
  1587. m_print.error("get_eye_distance(): Wrong camera object");
  1588. return 0;
  1589. }
  1590. var active_scene = m_scs.get_active();
  1591. var cam_scene_data = m_obj_util.get_scene_data(camobj, active_scene);
  1592. var cameras = cam_scene_data.cameras;
  1593. for (var i = 0; i < cameras.length; i++) {
  1594. var cam = cameras[i];
  1595. if (cam.type == m_cam.TYPE_STEREO_LEFT ||
  1596. cam.type == m_cam.TYPE_STEREO_RIGHT ||
  1597. cam.type == m_cam.TYPE_HMD_LEFT ||
  1598. cam.type == m_cam.TYPE_HMD_RIGHT)
  1599. return cam.stereo_eye_dist;
  1600. }
  1601. return 0;
  1602. }
  1603. /**
  1604. * Set vertical axis of the camera.
  1605. * @method module:camera.set_vertical_axis
  1606. * @param {Object3D} camobj Camera 3D-object.
  1607. * @param {Vec3} axis Vertical axis.
  1608. */
  1609. exports.set_vertical_axis = function(camobj, axis) {
  1610. if (!m_obj_util.is_camera(camobj)) {
  1611. m_print.error("set_vertical_axis(): Wrong camera object");
  1612. return;
  1613. }
  1614. var render = camobj.render;
  1615. m_vec3.copy(axis, render.vertical_axis);
  1616. m_trans.update_transform(camobj);
  1617. m_phy.sync_transform(camobj);
  1618. }
  1619. /**
  1620. * Get vertical axis of the camera.
  1621. * @method module:camera.get_vertical_axis
  1622. * @param {Object3D} camobj Camera 3D-object.
  1623. * @param {?Vec3} [dest=m_vec3.create();] Destination vector.
  1624. * @returns {?Vec3} Destination vector.
  1625. */
  1626. exports.get_vertical_axis = function(camobj, dest) {
  1627. if (!m_obj_util.is_camera(camobj)) {
  1628. m_print.error("get_vertical_axis(): Wrong camera object");
  1629. return;
  1630. }
  1631. if (!dest)
  1632. dest = m_vec3.create();
  1633. var render = camobj.render;
  1634. m_vec3.copy(render.vertical_axis, dest);
  1635. return dest;
  1636. }
  1637. /**
  1638. * Translate the view plane of the camera.
  1639. * Modify the projection matrix of the camera so it appears to be moving in up-down
  1640. * and left-right directions. This method can be used to imitate character
  1641. * walking/running/driving.
  1642. * @method module:camera.translate_view
  1643. * @param {Object3D} camobj Camera 3D-object.
  1644. * @param {number} x X coord (positive - left to right).
  1645. * @param {number} y Y coord (positive - down to up).
  1646. * @param {number} angle Rotation angle in radians (clockwise).
  1647. */
  1648. exports.translate_view = function(camobj, x, y, angle) {
  1649. if (!m_obj_util.is_camera(camobj)) {
  1650. m_print.error("translate_view(): Wrong camera object");
  1651. return;
  1652. }
  1653. var active_scene = m_scs.get_active();
  1654. var cam_scene_data = m_obj_util.get_scene_data(camobj, active_scene);
  1655. var cameras = cam_scene_data.cameras;
  1656. for (var i = 0; i < cameras.length; i++) {
  1657. var cam = cameras[i];
  1658. m_vec3.set(-x, -y, 0, cam.view_transform_params.trans);
  1659. // cam.view_transform_params.angle = angle;
  1660. if (cam.reflection_plane)
  1661. m_cam.set_projection_reflect(cam, false);
  1662. else
  1663. m_cam.set_projection(cam, false);
  1664. }
  1665. }
  1666. /**
  1667. * Get camera view vector in world space.
  1668. * @method module:camera.get_view_vector
  1669. * @param {Object3D} camobj Camera 3D-object.
  1670. * @param {?Vec3} [dest=new Float32Array(3);] Destination vector.
  1671. * @returns {?Vec3} Destination vector.
  1672. */
  1673. exports.get_view_vector = get_view_vector;
  1674. function get_view_vector(camobj, dest) {
  1675. if (!m_obj_util.is_camera(camobj)) {
  1676. m_print.error("get_view_vector(): Wrong camera object");
  1677. return null;
  1678. }
  1679. dest = dest || new Float32Array(3);
  1680. var quat = m_tsr.get_quat_view(camobj.render.world_tsr);
  1681. m_util.quat_to_dir(quat, m_util.AXIS_MZ, dest);
  1682. return dest;
  1683. }
  1684. /**
  1685. * Get the vertical angle of the camera's field of view.
  1686. * @method module:camera.get_fov
  1687. * @param {Object3D} camobj Camera 3D-object.
  1688. * @returns {number} Camera field of view (in radians).
  1689. */
  1690. exports.get_fov = function(camobj) {
  1691. if (!m_obj_util.is_camera(camobj)) {
  1692. m_print.error("get_fov(): Wrong camera object");
  1693. return 0;
  1694. }
  1695. var active_scene = m_scs.get_active();
  1696. var cam_scene_data = m_obj_util.get_scene_data(camobj, active_scene);
  1697. var cameras = cam_scene_data.cameras;
  1698. return m_cam.get_fov(cameras[0]);
  1699. }
  1700. /**
  1701. * Set the vertical angle of the camera's field of view.
  1702. * @method module:camera.set_fov
  1703. * @param {Object3D} camobj Camera 3D-object.
  1704. * @param {number} fov New camera field of view (in radians).
  1705. */
  1706. exports.set_fov = function(camobj, fov) {
  1707. if (!m_obj_util.is_camera(camobj)) {
  1708. m_print.error("set_fov(): Wrong camera object");
  1709. return;
  1710. }
  1711. var active_scene = m_scs.get_active();
  1712. var cam_scene_data = m_obj_util.get_scene_data(camobj, active_scene);
  1713. var cameras = cam_scene_data.cameras;
  1714. for (var i = 0; i < cameras.length; i++) {
  1715. var cam = cameras[i];
  1716. m_cam.set_fov(cam, fov);
  1717. if (cam.reflection_plane)
  1718. m_cam.set_projection_reflect(cam, false);
  1719. else
  1720. m_cam.set_projection(cam, false);
  1721. }
  1722. }
  1723. /**
  1724. * Set the angles of the camera's field of view (for head-mounted display only).
  1725. * @method module:camera.set_hmd_fov
  1726. * @param {Object3D} camobj Camera 3D-object.
  1727. * @param {Vec4} hmd_left_fov New left camera field of view.
  1728. * @param {Vec4} hmd_right_fov New right camera field of view.
  1729. * @deprecated Do not use it anymore
  1730. */
  1731. exports.set_hmd_fov = function(camobj, hmd_left_fov, hmd_right_fov) {
  1732. m_print.error_once("set_hmd_fov() deprecated");
  1733. }
  1734. /**
  1735. * Correct the UP vector of the camera.
  1736. * @method module:camera.correct_up
  1737. * @param {Object3D} camobj Camera 3D-object.
  1738. * @param {Vec3} [z_axis=util.AXIS_Z] Axis vector.
  1739. * @param {boolean} [strict=false] Align camera exactly with the direction of
  1740. * the given axis vector (never with the opposite direction).
  1741. */
  1742. exports.correct_up = function(camobj, z_axis, strict) {
  1743. if (!m_obj_util.is_camera(camobj)) {
  1744. m_print.error("correct_up(): Wrong camera object");
  1745. return;
  1746. }
  1747. z_axis = z_axis || camobj.render.vertical_axis;
  1748. m_cam.correct_up(camobj, z_axis, strict || false);
  1749. m_trans.update_transform(camobj);
  1750. m_phy.sync_transform(camobj);
  1751. }
  1752. /**
  1753. * Set the orthogonal scale of the camera.
  1754. * @method module:camera.set_ortho_scale
  1755. * @param {Object3D} camobj Camera 3D-object.
  1756. * @param {number} ortho_scale Orthogonal scale.
  1757. */
  1758. exports.set_ortho_scale = function(camobj, ortho_scale) {
  1759. if (!m_obj_util.is_camera(camobj) || !m_cam.is_ortho_camera(camobj)) {
  1760. m_print.error("set_ortho_scale(): Wrong camera object");
  1761. return;
  1762. }
  1763. var render = camobj.render;
  1764. if (m_cam.is_target_camera(camobj)) {
  1765. var trans = m_tsr.get_trans_view(render.world_tsr);
  1766. var dir_dist = m_vec3.dist(trans, render.pivot);
  1767. render.init_fov = ortho_scale / 2 * render.init_dist / dir_dist;
  1768. } else if (m_cam.is_hover_camera(camobj)) {
  1769. var trans = m_tsr.get_trans_view(render.world_tsr);
  1770. var dir_dist = m_vec3.distance(trans, render.hover_pivot);
  1771. render.init_fov = ortho_scale / 2 * render.init_dist / dir_dist;
  1772. } else {
  1773. var active_scene = m_scs.get_active();
  1774. var cam_scene_data = m_obj_util.get_scene_data(camobj, active_scene);
  1775. // hover camera without distance limits, EYE or STATIC camera
  1776. cam_scene_data.cameras[0].fov = ortho_scale / 2;
  1777. }
  1778. m_cam.update_ortho_scale(camobj);
  1779. }
  1780. /**
  1781. * Get the orthogonal scale of the camera.
  1782. * @method module:camera.get_ortho_scale
  1783. * @param {Object3D} camobj Camera 3D-object.
  1784. * @returns {number} Orthogonal scale.
  1785. */
  1786. exports.get_ortho_scale = function(camobj) {
  1787. if (!m_obj_util.is_camera(camobj) || !m_cam.is_ortho_camera(camobj)) {
  1788. m_print.error("get_ortho_scale(): Wrong camera object");
  1789. return 0;
  1790. }
  1791. var active_scene = m_scs.get_active();
  1792. var cam_scene_data = m_obj_util.get_scene_data(camobj, active_scene);
  1793. return cam_scene_data.cameras[0].fov * 2;
  1794. }
  1795. /**
  1796. * Check whether the camera is an ORTHO camera.
  1797. * @method module:camera.is_ortho_camera
  1798. * @param {Object3D} camobj Camera 3D-object.
  1799. * @returns {boolean} The result of the checking.
  1800. */
  1801. exports.is_ortho_camera = function(camobj) {
  1802. if (!m_obj_util.is_camera(camobj)) {
  1803. m_print.error("is_ortho_camera(): Wrong camera object");
  1804. return false;
  1805. }
  1806. return m_cam.is_ortho_camera(camobj);
  1807. }
  1808. /**
  1809. * Calculate the direction of the camera ray based on the Canvas coordinates.
  1810. * The origin of the Canvas space is located in the top left corner of the Canvas.
  1811. * @method module:camera.calc_ray
  1812. * @param {Object3D} camobj Camera 3D-object.
  1813. * @param {number} canvas_x X Canvas coordinate.
  1814. * @param {number} canvas_y Y Canvas coordinate.
  1815. * @param {?ParametricLine} [dest=new Float32Array(6);] Destination parametric line.
  1816. * @returns {?ParametricLine} Destination parametric line.
  1817. */
  1818. exports.calc_ray = function(camobj, canvas_x, canvas_y, dest) {
  1819. if (!m_obj_util.is_camera(camobj)) {
  1820. m_print.error("calc_ray(): Wrong camera object");
  1821. return null;
  1822. }
  1823. var active_scene = m_scs.get_active();
  1824. var cam_scene_data = m_obj_util.get_scene_data(camobj, active_scene);
  1825. var cam = cam_scene_data.cameras[0];
  1826. // NOTE: It's for compatibility.
  1827. if (dest && dest.length == 3)
  1828. m_print.error_once("dest parameter in the function \"calc_ray\" " +
  1829. "should be type of parametric line.");
  1830. else
  1831. dest = dest || new Float32Array(6);
  1832. switch (cam.type) {
  1833. case m_cam.TYPE_PERSP:
  1834. case m_cam.TYPE_PERSP_ASPECT:
  1835. case m_cam.TYPE_STEREO_LEFT:
  1836. case m_cam.TYPE_STEREO_RIGHT:
  1837. var top_1m = Math.tan(m_cam.get_vfov(cam) / 2);
  1838. var right_1m = top_1m * cam.aspect;
  1839. var dir = _vec3_tmp;
  1840. var viewport_xy = m_cont.canvas_to_viewport_coords(canvas_x, canvas_y,
  1841. _vec2_tmp, cam);
  1842. // in the camera's local space
  1843. dir[0] = (2.0 * viewport_xy[0] / cam.width - 1.0) * right_1m;
  1844. dir[1] = (1.0 - 2.0 * viewport_xy[1] / cam.height) * top_1m;
  1845. dir[2] = -1;
  1846. m_tsr.transform_dir_vec3(dir, camobj.render.world_tsr, dir);
  1847. m_vec3.normalize(dir, dir);
  1848. if (dest.length == 3)
  1849. m_vec3.copy(dir, dest);
  1850. else {
  1851. var cam_eye = m_trans.get_translation(camobj, _vec3_tmp2);
  1852. m_math.set_pline_initial_point(dest, cam_eye);
  1853. m_math.set_pline_directional_vec(dest, dir);
  1854. }
  1855. return dest;
  1856. case m_cam.TYPE_ORTHO:
  1857. var dir = _vec3_tmp;
  1858. var viewport_xy = m_cont.canvas_to_viewport_coords(canvas_x, canvas_y,
  1859. _vec2_tmp, cam);
  1860. dir[0] = (2.0 * viewport_xy[0] / cam.width - 1.0) * cam.top * cam.aspect;
  1861. dir[1] = (1.0 - 2.0 * viewport_xy[1] / cam.height) * cam.top;
  1862. dir[2] = 0;
  1863. m_tsr.transform_vec3(dir, camobj.render.world_tsr, dir);
  1864. m_vec3.copy(dir, dest);
  1865. var quat = m_tsr.get_quat_view(camobj.render.world_tsr, _vec4_tmp);
  1866. m_vec3.transformQuat(m_util.AXIS_MZ, quat, dir);
  1867. m_math.set_pline_directional_vec(dest, dir);
  1868. return dest;
  1869. default:
  1870. m_print.error("calc_ray(): Non-compatible camera");
  1871. return dest;
  1872. }
  1873. }
  1874. /**
  1875. * Project the 3D point to the Canvas.
  1876. * Returned coordinates are measured in CSS pixels.
  1877. * @method module:camera.project_point
  1878. * @param {Object3D} camobj Camera 3D-object.
  1879. * @param {Vec3} point Point in world space.
  1880. * @param {Vec2|Vec3} [dest=new Float32Array(2);] Destination canvas coordinates
  1881. * (vec2 - X/Y, vec3 - X/Y/DEPTH).
  1882. * @returns {Vec2|Vec3} Destination canvas coordinates.
  1883. */
  1884. exports.project_point = function(camobj, point, dest) {
  1885. if (!m_obj_util.is_camera(camobj)) {
  1886. m_print.error("project_point(): Wrong camera object");
  1887. return null;
  1888. }
  1889. dest = dest || new Float32Array(2);
  1890. return m_cam.project_point(camobj, point, dest);
  1891. }
  1892. /**
  1893. * Check whether the camera has distance limits.
  1894. * @method module:camera.has_distance_limits
  1895. * @param {Object3D} camobj Camera 3D-object.
  1896. * @returns {boolean} The result of the checking.
  1897. */
  1898. exports.has_distance_limits = function(camobj) {
  1899. return (m_cam.is_target_camera(camobj) || m_cam.is_hover_camera(camobj))
  1900. && camobj.render.distance_limits !== null;
  1901. }
  1902. /**
  1903. * Check whether the camera has any vertical rotation limits.
  1904. * @method module:camera.has_vertical_rot_limits
  1905. * @param {Object3D} camobj Camera 3D-object.
  1906. * @returns {boolean} The result of the checking.
  1907. */
  1908. exports.has_vertical_rot_limits = function(camobj) {
  1909. return (m_cam.is_eye_camera(camobj) || m_cam.is_target_camera(camobj)
  1910. || m_cam.is_hover_camera(camobj)) && camobj.render.vertical_limits !== null;
  1911. }
  1912. /**
  1913. * Check whether the camera has any horizontal rotation limits.
  1914. * @method module:camera.has_horizontal_rot_limits
  1915. * @param {Object3D} camobj Camera 3D-object.
  1916. * @returns {boolean} The result of the checking.
  1917. */
  1918. exports.has_horizontal_rot_limits = function(camobj) {
  1919. return (m_cam.is_target_camera(camobj) || m_cam.is_eye_camera(camobj))
  1920. && camobj.render.horizontal_limits !== null;
  1921. }
  1922. /**
  1923. * Check whether the camera has any vertical translation limits.
  1924. * @method module:camera.has_vertical_trans_limits
  1925. * @param {Object3D} camobj Camera 3D-object.
  1926. * @returns {boolean} The result of the checking.
  1927. */
  1928. exports.has_vertical_trans_limits = function(camobj) {
  1929. return m_cam.is_hover_camera(camobj) && camobj.render.hover_vert_trans_limits !== null;
  1930. }
  1931. /**
  1932. * Check whether the camera has any horizontal translation limits.
  1933. * @method module:camera.has_horizontal_trans_limits
  1934. * @param {Object3D} camobj Camera 3D-object.
  1935. * @returns {boolean} The result of the checking.
  1936. */
  1937. exports.has_horizontal_trans_limits = function(camobj) {
  1938. return m_cam.is_hover_camera(camobj) && camobj.render.hover_horiz_trans_limits !== null;
  1939. }
  1940. /**
  1941. * Get camera frustum planes.
  1942. * @method module:camera.get_frustum_planes
  1943. * @param {Object3D} camobj Camera object.
  1944. * @param {FrustumPlanes} planes Frustum planes object.
  1945. * @returns {?FrustumPlanes} Frustum planes object.
  1946. */
  1947. exports.get_frustum_planes = function(camobj, planes) {
  1948. if (!m_obj_util.is_camera(camobj)) {
  1949. m_print.error("get_frustum_planes(): Wrong camera object");
  1950. return null;
  1951. }
  1952. var active_scene = m_scs.get_active();
  1953. var cam_scene_data = m_obj_util.get_scene_data(camobj, active_scene);
  1954. var fr_planes = cam_scene_data.cameras[0].frustum_planes;
  1955. m_vec4.copy(fr_planes.left, planes.left);
  1956. m_vec4.copy(fr_planes.right, planes.right);
  1957. m_vec4.copy(fr_planes.top, planes.top);
  1958. m_vec4.copy(fr_planes.bottom, planes.bottom);
  1959. m_vec4.copy(fr_planes.near, planes.near);
  1960. m_vec4.copy(fr_planes.far, planes.far);
  1961. return planes;
  1962. }
  1963. /**
  1964. * Set camera projection matrix.
  1965. * @method module:camera.set_projection
  1966. * @param {Object3D} camobj Camera object.
  1967. * @param {Float32Array} matrix Projection matrix.
  1968. */
  1969. exports.set_projection = function(camobj, matrix) {
  1970. if (!m_obj_util.is_camera(camobj))
  1971. m_print.error("set_projection(): Wrong camera object");
  1972. else
  1973. m_cam.set_proj_mat(camobj, matrix);
  1974. }
  1975. }
  1976. var camera_factory = register("camera", Camera);
  1977. export default camera_factory;