camera.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. /*************************************************************************/
  2. /* camera.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "camera.h"
  31. #include "collision_object.h"
  32. #include "core/engine.h"
  33. #include "core/math/camera_matrix.h"
  34. #include "scene/resources/material.h"
  35. #include "scene/resources/surface_tool.h"
  36. void Camera::_update_audio_listener_state() {
  37. }
  38. void Camera::_request_camera_update() {
  39. _update_camera();
  40. }
  41. void Camera::_update_camera_mode() {
  42. force_change = true;
  43. switch (mode) {
  44. case PROJECTION_PERSPECTIVE: {
  45. set_perspective(fov, near, far);
  46. } break;
  47. case PROJECTION_ORTHOGONAL: {
  48. set_orthogonal(size, near, far);
  49. } break;
  50. }
  51. }
  52. void Camera::_validate_property(PropertyInfo &p_property) const {
  53. if (p_property.name == "fov") {
  54. if (mode == PROJECTION_ORTHOGONAL) {
  55. p_property.usage = PROPERTY_USAGE_NOEDITOR;
  56. }
  57. } else if (p_property.name == "size") {
  58. if (mode == PROJECTION_PERSPECTIVE) {
  59. p_property.usage = PROPERTY_USAGE_NOEDITOR;
  60. }
  61. }
  62. }
  63. void Camera::_update_camera() {
  64. if (!is_inside_tree())
  65. return;
  66. VisualServer::get_singleton()->camera_set_transform(camera, get_camera_transform());
  67. // here goes listener stuff
  68. /*
  69. if (viewport_ptr && is_inside_scene() && is_current())
  70. get_viewport()->_camera_transform_changed_notify();
  71. */
  72. if (get_tree()->is_node_being_edited(this) || !is_current())
  73. return;
  74. get_viewport()->_camera_transform_changed_notify();
  75. if (get_world().is_valid()) {
  76. get_world()->_update_camera(this);
  77. }
  78. }
  79. void Camera::_notification(int p_what) {
  80. switch (p_what) {
  81. case NOTIFICATION_ENTER_WORLD: {
  82. bool first_camera = get_viewport()->_camera_add(this);
  83. if (!get_tree()->is_node_being_edited(this) && (current || first_camera))
  84. make_current();
  85. } break;
  86. case NOTIFICATION_TRANSFORM_CHANGED: {
  87. _request_camera_update();
  88. if (doppler_tracking != DOPPLER_TRACKING_DISABLED) {
  89. velocity_tracker->update_position(get_global_transform().origin);
  90. }
  91. } break;
  92. case NOTIFICATION_EXIT_WORLD: {
  93. if (!get_tree()->is_node_being_edited(this)) {
  94. if (is_current()) {
  95. clear_current();
  96. current = true; //keep it true
  97. } else {
  98. current = false;
  99. }
  100. }
  101. get_viewport()->_camera_remove(this);
  102. } break;
  103. case NOTIFICATION_BECAME_CURRENT: {
  104. if (get_world().is_valid()) {
  105. get_world()->_register_camera(this);
  106. }
  107. } break;
  108. case NOTIFICATION_LOST_CURRENT: {
  109. if (get_world().is_valid()) {
  110. get_world()->_remove_camera(this);
  111. }
  112. } break;
  113. }
  114. }
  115. Transform Camera::get_camera_transform() const {
  116. Transform tr = get_global_transform().orthonormalized();
  117. tr.origin += tr.basis.get_axis(1) * v_offset;
  118. tr.origin += tr.basis.get_axis(0) * h_offset;
  119. return tr;
  120. }
  121. void Camera::set_perspective(float p_fovy_degrees, float p_z_near, float p_z_far) {
  122. if (!force_change && fov == p_fovy_degrees && p_z_near == near && p_z_far == far && mode == PROJECTION_PERSPECTIVE)
  123. return;
  124. fov = p_fovy_degrees;
  125. near = p_z_near;
  126. far = p_z_far;
  127. mode = PROJECTION_PERSPECTIVE;
  128. VisualServer::get_singleton()->camera_set_perspective(camera, fov, near, far);
  129. update_gizmo();
  130. force_change = false;
  131. }
  132. void Camera::set_orthogonal(float p_size, float p_z_near, float p_z_far) {
  133. if (!force_change && size == p_size && p_z_near == near && p_z_far == far && mode == PROJECTION_ORTHOGONAL)
  134. return;
  135. size = p_size;
  136. near = p_z_near;
  137. far = p_z_far;
  138. mode = PROJECTION_ORTHOGONAL;
  139. force_change = false;
  140. VisualServer::get_singleton()->camera_set_orthogonal(camera, size, near, far);
  141. update_gizmo();
  142. }
  143. void Camera::set_projection(Camera::Projection p_mode) {
  144. if (p_mode == PROJECTION_PERSPECTIVE || p_mode == PROJECTION_ORTHOGONAL) {
  145. mode = p_mode;
  146. _update_camera_mode();
  147. _change_notify();
  148. }
  149. }
  150. RID Camera::get_camera() const {
  151. return camera;
  152. };
  153. void Camera::make_current() {
  154. current = true;
  155. if (!is_inside_tree())
  156. return;
  157. get_viewport()->_camera_set(this);
  158. //get_scene()->call_group(SceneMainLoop::GROUP_CALL_REALTIME,camera_group,"_camera_make_current",this);
  159. }
  160. void Camera::clear_current(bool p_enable_next) {
  161. current = false;
  162. if (!is_inside_tree())
  163. return;
  164. if (get_viewport()->get_camera() == this) {
  165. get_viewport()->_camera_set(NULL);
  166. if (p_enable_next) {
  167. get_viewport()->_camera_make_next_current(this);
  168. }
  169. }
  170. }
  171. void Camera::set_current(bool p_current) {
  172. if (p_current) {
  173. make_current();
  174. } else {
  175. clear_current();
  176. }
  177. }
  178. bool Camera::is_current() const {
  179. if (is_inside_tree() && !get_tree()->is_node_being_edited(this)) {
  180. return get_viewport()->get_camera() == this;
  181. } else
  182. return current;
  183. return false;
  184. }
  185. bool Camera::_can_gizmo_scale() const {
  186. return false;
  187. }
  188. Vector3 Camera::project_ray_normal(const Point2 &p_pos) const {
  189. Vector3 ray = project_local_ray_normal(p_pos);
  190. return get_camera_transform().basis.xform(ray).normalized();
  191. };
  192. Vector3 Camera::project_local_ray_normal(const Point2 &p_pos) const {
  193. if (!is_inside_tree()) {
  194. ERR_EXPLAIN("Camera is not inside scene.");
  195. ERR_FAIL_COND_V(!is_inside_tree(), Vector3());
  196. }
  197. Size2 viewport_size = get_viewport()->get_camera_rect_size();
  198. Vector2 cpos = get_viewport()->get_camera_coords(p_pos);
  199. Vector3 ray;
  200. if (mode == PROJECTION_ORTHOGONAL) {
  201. ray = Vector3(0, 0, -1);
  202. } else {
  203. CameraMatrix cm;
  204. cm.set_perspective(fov, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
  205. float screen_w, screen_h;
  206. cm.get_viewport_size(screen_w, screen_h);
  207. ray = Vector3(((cpos.x / viewport_size.width) * 2.0 - 1.0) * screen_w, ((1.0 - (cpos.y / viewport_size.height)) * 2.0 - 1.0) * screen_h, -near).normalized();
  208. }
  209. return ray;
  210. };
  211. Vector3 Camera::project_ray_origin(const Point2 &p_pos) const {
  212. if (!is_inside_tree()) {
  213. ERR_EXPLAIN("Camera is not inside scene.");
  214. ERR_FAIL_COND_V(!is_inside_tree(), Vector3());
  215. }
  216. Size2 viewport_size = get_viewport()->get_camera_rect_size();
  217. Vector2 cpos = get_viewport()->get_camera_coords(p_pos);
  218. ERR_FAIL_COND_V(viewport_size.y == 0, Vector3());
  219. if (mode == PROJECTION_PERSPECTIVE) {
  220. return get_camera_transform().origin;
  221. } else {
  222. Vector2 pos = cpos / viewport_size;
  223. float vsize, hsize;
  224. if (keep_aspect == KEEP_WIDTH) {
  225. vsize = size / viewport_size.aspect();
  226. hsize = size;
  227. } else {
  228. hsize = size * viewport_size.aspect();
  229. vsize = size;
  230. }
  231. Vector3 ray;
  232. ray.x = pos.x * (hsize)-hsize / 2;
  233. ray.y = (1.0 - pos.y) * (vsize)-vsize / 2;
  234. ray.z = -near;
  235. ray = get_camera_transform().xform(ray);
  236. return ray;
  237. };
  238. };
  239. bool Camera::is_position_behind(const Vector3 &p_pos) const {
  240. Transform t = get_global_transform();
  241. Vector3 eyedir = -get_global_transform().basis.get_axis(2).normalized();
  242. return eyedir.dot(p_pos) < (eyedir.dot(t.origin) + near);
  243. }
  244. Vector<Vector3> Camera::get_near_plane_points() const {
  245. if (!is_inside_tree()) {
  246. ERR_EXPLAIN("Camera is not inside scene.");
  247. ERR_FAIL_COND_V(!is_inside_tree(), Vector<Vector3>());
  248. }
  249. Size2 viewport_size = get_viewport()->get_visible_rect().size;
  250. CameraMatrix cm;
  251. if (mode == PROJECTION_ORTHOGONAL)
  252. cm.set_orthogonal(size, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
  253. else
  254. cm.set_perspective(fov, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
  255. Vector3 endpoints[8];
  256. cm.get_endpoints(Transform(), endpoints);
  257. Vector<Vector3> points;
  258. points.push_back(Vector3());
  259. for (int i = 0; i < 4; i++) {
  260. points.push_back(endpoints[i + 4]);
  261. }
  262. return points;
  263. }
  264. Point2 Camera::unproject_position(const Vector3 &p_pos) const {
  265. if (!is_inside_tree()) {
  266. ERR_EXPLAIN("Camera is not inside scene.");
  267. ERR_FAIL_COND_V(!is_inside_tree(), Vector2());
  268. }
  269. Size2 viewport_size = get_viewport()->get_visible_rect().size;
  270. CameraMatrix cm;
  271. if (mode == PROJECTION_ORTHOGONAL)
  272. cm.set_orthogonal(size, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
  273. else
  274. cm.set_perspective(fov, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
  275. Plane p(get_camera_transform().xform_inv(p_pos), 1.0);
  276. p = cm.xform4(p);
  277. p.normal /= p.d;
  278. Point2 res;
  279. res.x = (p.normal.x * 0.5 + 0.5) * viewport_size.x;
  280. res.y = (-p.normal.y * 0.5 + 0.5) * viewport_size.y;
  281. return res;
  282. }
  283. Vector3 Camera::project_position(const Point2 &p_point) const {
  284. if (!is_inside_tree()) {
  285. ERR_EXPLAIN("Camera is not inside scene.");
  286. ERR_FAIL_COND_V(!is_inside_tree(), Vector3());
  287. }
  288. Size2 viewport_size = get_viewport()->get_visible_rect().size;
  289. CameraMatrix cm;
  290. if (mode == PROJECTION_ORTHOGONAL)
  291. cm.set_orthogonal(size, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
  292. else
  293. cm.set_perspective(fov, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
  294. Size2 vp_size;
  295. cm.get_viewport_size(vp_size.x, vp_size.y);
  296. Vector2 point;
  297. point.x = (p_point.x / viewport_size.x) * 2.0 - 1.0;
  298. point.y = (1.0 - (p_point.y / viewport_size.y)) * 2.0 - 1.0;
  299. point *= vp_size;
  300. Vector3 p(point.x, point.y, -near);
  301. return get_camera_transform().xform(p);
  302. }
  303. /*
  304. void Camera::_camera_make_current(Node *p_camera) {
  305. if (p_camera==this) {
  306. VisualServer::get_singleton()->viewport_attach_camera(viewport_id,camera);
  307. active=true;
  308. } else {
  309. if (active && p_camera==NULL) {
  310. //detech camera because no one else will claim it
  311. VisualServer::get_singleton()->viewport_attach_camera(viewport_id,RID());
  312. }
  313. active=false;
  314. }
  315. }
  316. */
  317. void Camera::set_environment(const Ref<Environment> &p_environment) {
  318. environment = p_environment;
  319. if (environment.is_valid())
  320. VS::get_singleton()->camera_set_environment(camera, environment->get_rid());
  321. else
  322. VS::get_singleton()->camera_set_environment(camera, RID());
  323. _update_camera_mode();
  324. }
  325. Ref<Environment> Camera::get_environment() const {
  326. return environment;
  327. }
  328. void Camera::set_keep_aspect_mode(KeepAspect p_aspect) {
  329. keep_aspect = p_aspect;
  330. VisualServer::get_singleton()->camera_set_use_vertical_aspect(camera, p_aspect == KEEP_WIDTH);
  331. _update_camera_mode();
  332. _change_notify();
  333. }
  334. Camera::KeepAspect Camera::get_keep_aspect_mode() const {
  335. return keep_aspect;
  336. }
  337. void Camera::set_doppler_tracking(DopplerTracking p_tracking) {
  338. if (doppler_tracking == p_tracking)
  339. return;
  340. doppler_tracking = p_tracking;
  341. if (p_tracking != DOPPLER_TRACKING_DISABLED) {
  342. velocity_tracker->set_track_physics_step(doppler_tracking == DOPPLER_TRACKING_PHYSICS_STEP);
  343. if (is_inside_tree()) {
  344. velocity_tracker->reset(get_global_transform().origin);
  345. }
  346. }
  347. _update_camera_mode();
  348. }
  349. Camera::DopplerTracking Camera::get_doppler_tracking() const {
  350. return doppler_tracking;
  351. }
  352. void Camera::_bind_methods() {
  353. ClassDB::bind_method(D_METHOD("project_ray_normal", "screen_point"), &Camera::project_ray_normal);
  354. ClassDB::bind_method(D_METHOD("project_local_ray_normal", "screen_point"), &Camera::project_local_ray_normal);
  355. ClassDB::bind_method(D_METHOD("project_ray_origin", "screen_point"), &Camera::project_ray_origin);
  356. ClassDB::bind_method(D_METHOD("unproject_position", "world_point"), &Camera::unproject_position);
  357. ClassDB::bind_method(D_METHOD("is_position_behind", "world_point"), &Camera::is_position_behind);
  358. ClassDB::bind_method(D_METHOD("project_position", "screen_point"), &Camera::project_position);
  359. ClassDB::bind_method(D_METHOD("set_perspective", "fov", "z_near", "z_far"), &Camera::set_perspective);
  360. ClassDB::bind_method(D_METHOD("set_orthogonal", "size", "z_near", "z_far"), &Camera::set_orthogonal);
  361. ClassDB::bind_method(D_METHOD("make_current"), &Camera::make_current);
  362. ClassDB::bind_method(D_METHOD("clear_current", "enable_next"), &Camera::clear_current, DEFVAL(true));
  363. ClassDB::bind_method(D_METHOD("set_current"), &Camera::set_current);
  364. ClassDB::bind_method(D_METHOD("is_current"), &Camera::is_current);
  365. ClassDB::bind_method(D_METHOD("get_camera_transform"), &Camera::get_camera_transform);
  366. ClassDB::bind_method(D_METHOD("get_fov"), &Camera::get_fov);
  367. ClassDB::bind_method(D_METHOD("get_size"), &Camera::get_size);
  368. ClassDB::bind_method(D_METHOD("get_zfar"), &Camera::get_zfar);
  369. ClassDB::bind_method(D_METHOD("get_znear"), &Camera::get_znear);
  370. ClassDB::bind_method(D_METHOD("set_fov"), &Camera::set_fov);
  371. ClassDB::bind_method(D_METHOD("set_size"), &Camera::set_size);
  372. ClassDB::bind_method(D_METHOD("set_zfar"), &Camera::set_zfar);
  373. ClassDB::bind_method(D_METHOD("set_znear"), &Camera::set_znear);
  374. ClassDB::bind_method(D_METHOD("get_projection"), &Camera::get_projection);
  375. ClassDB::bind_method(D_METHOD("set_projection"), &Camera::set_projection);
  376. ClassDB::bind_method(D_METHOD("set_h_offset", "ofs"), &Camera::set_h_offset);
  377. ClassDB::bind_method(D_METHOD("get_h_offset"), &Camera::get_h_offset);
  378. ClassDB::bind_method(D_METHOD("set_v_offset", "ofs"), &Camera::set_v_offset);
  379. ClassDB::bind_method(D_METHOD("get_v_offset"), &Camera::get_v_offset);
  380. ClassDB::bind_method(D_METHOD("set_cull_mask", "mask"), &Camera::set_cull_mask);
  381. ClassDB::bind_method(D_METHOD("get_cull_mask"), &Camera::get_cull_mask);
  382. ClassDB::bind_method(D_METHOD("set_environment", "env"), &Camera::set_environment);
  383. ClassDB::bind_method(D_METHOD("get_environment"), &Camera::get_environment);
  384. ClassDB::bind_method(D_METHOD("set_keep_aspect_mode", "mode"), &Camera::set_keep_aspect_mode);
  385. ClassDB::bind_method(D_METHOD("get_keep_aspect_mode"), &Camera::get_keep_aspect_mode);
  386. ClassDB::bind_method(D_METHOD("set_doppler_tracking", "mode"), &Camera::set_doppler_tracking);
  387. ClassDB::bind_method(D_METHOD("get_doppler_tracking"), &Camera::get_doppler_tracking);
  388. ClassDB::bind_method(D_METHOD("get_frustum"), &Camera::get_frustum);
  389. ClassDB::bind_method(D_METHOD("set_cull_mask_bit", "layer", "enable"), &Camera::set_cull_mask_bit);
  390. ClassDB::bind_method(D_METHOD("get_cull_mask_bit", "layer"), &Camera::get_cull_mask_bit);
  391. //ClassDB::bind_method(D_METHOD("_camera_make_current"),&Camera::_camera_make_current );
  392. ADD_PROPERTY(PropertyInfo(Variant::INT, "keep_aspect", PROPERTY_HINT_ENUM, "Keep Width,Keep Height"), "set_keep_aspect_mode", "get_keep_aspect_mode");
  393. ADD_PROPERTY(PropertyInfo(Variant::INT, "cull_mask", PROPERTY_HINT_LAYERS_3D_RENDER), "set_cull_mask", "get_cull_mask");
  394. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "environment", PROPERTY_HINT_RESOURCE_TYPE, "Environment"), "set_environment", "get_environment");
  395. ADD_PROPERTY(PropertyInfo(Variant::REAL, "h_offset"), "set_h_offset", "get_h_offset");
  396. ADD_PROPERTY(PropertyInfo(Variant::REAL, "v_offset"), "set_v_offset", "get_v_offset");
  397. ADD_PROPERTY(PropertyInfo(Variant::INT, "doppler_tracking", PROPERTY_HINT_ENUM, "Disabled,Idle,Physics"), "set_doppler_tracking", "get_doppler_tracking");
  398. ADD_PROPERTY(PropertyInfo(Variant::INT, "projection", PROPERTY_HINT_ENUM, "Perspective,Orthogonal"), "set_projection", "get_projection");
  399. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "current"), "set_current", "is_current");
  400. ADD_PROPERTY(PropertyInfo(Variant::REAL, "fov", PROPERTY_HINT_RANGE, "1,179,0.1"), "set_fov", "get_fov");
  401. ADD_PROPERTY(PropertyInfo(Variant::REAL, "size", PROPERTY_HINT_RANGE, "0.1,16384,0.01"), "set_size", "get_size");
  402. ADD_PROPERTY(PropertyInfo(Variant::REAL, "near", PROPERTY_HINT_EXP_RANGE, "0.01,8192,0.01,or_greater"), "set_znear", "get_znear");
  403. ADD_PROPERTY(PropertyInfo(Variant::REAL, "far", PROPERTY_HINT_EXP_RANGE, "0.1,8192,0.1,or_greater"), "set_zfar", "get_zfar");
  404. BIND_ENUM_CONSTANT(PROJECTION_PERSPECTIVE);
  405. BIND_ENUM_CONSTANT(PROJECTION_ORTHOGONAL);
  406. BIND_ENUM_CONSTANT(KEEP_WIDTH);
  407. BIND_ENUM_CONSTANT(KEEP_HEIGHT);
  408. BIND_ENUM_CONSTANT(DOPPLER_TRACKING_DISABLED)
  409. BIND_ENUM_CONSTANT(DOPPLER_TRACKING_IDLE_STEP)
  410. BIND_ENUM_CONSTANT(DOPPLER_TRACKING_PHYSICS_STEP)
  411. }
  412. float Camera::get_fov() const {
  413. return fov;
  414. }
  415. float Camera::get_size() const {
  416. return size;
  417. }
  418. float Camera::get_znear() const {
  419. return near;
  420. }
  421. float Camera::get_zfar() const {
  422. return far;
  423. }
  424. Camera::Projection Camera::get_projection() const {
  425. return mode;
  426. }
  427. void Camera::set_fov(float p_fov) {
  428. fov = p_fov;
  429. _update_camera_mode();
  430. _change_notify("fov");
  431. }
  432. void Camera::set_size(float p_size) {
  433. size = p_size;
  434. _update_camera_mode();
  435. _change_notify("size");
  436. }
  437. void Camera::set_znear(float p_znear) {
  438. near = p_znear;
  439. _update_camera_mode();
  440. }
  441. void Camera::set_zfar(float p_zfar) {
  442. far = p_zfar;
  443. _update_camera_mode();
  444. }
  445. void Camera::set_cull_mask(uint32_t p_layers) {
  446. layers = p_layers;
  447. VisualServer::get_singleton()->camera_set_cull_mask(camera, layers);
  448. _update_camera_mode();
  449. }
  450. uint32_t Camera::get_cull_mask() const {
  451. return layers;
  452. }
  453. void Camera::set_cull_mask_bit(int p_layer, bool p_enable) {
  454. ERR_FAIL_INDEX(p_layer, 32);
  455. if (p_enable) {
  456. set_cull_mask(layers | (1 << p_layer));
  457. } else {
  458. set_cull_mask(layers & (~(1 << p_layer)));
  459. }
  460. }
  461. bool Camera::get_cull_mask_bit(int p_layer) const {
  462. ERR_FAIL_INDEX_V(p_layer, 32, false);
  463. return (layers & (1 << p_layer));
  464. }
  465. Vector<Plane> Camera::get_frustum() const {
  466. ERR_FAIL_COND_V(!is_inside_world(), Vector<Plane>());
  467. Size2 viewport_size = get_viewport()->get_visible_rect().size;
  468. CameraMatrix cm;
  469. if (mode == PROJECTION_PERSPECTIVE)
  470. cm.set_perspective(fov, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
  471. else
  472. cm.set_orthogonal(size, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
  473. return cm.get_projection_planes(get_camera_transform());
  474. }
  475. void Camera::set_v_offset(float p_offset) {
  476. v_offset = p_offset;
  477. _update_camera();
  478. }
  479. float Camera::get_v_offset() const {
  480. return v_offset;
  481. }
  482. void Camera::set_h_offset(float p_offset) {
  483. h_offset = p_offset;
  484. _update_camera();
  485. }
  486. float Camera::get_h_offset() const {
  487. return h_offset;
  488. }
  489. Vector3 Camera::get_doppler_tracked_velocity() const {
  490. if (doppler_tracking != DOPPLER_TRACKING_DISABLED) {
  491. return velocity_tracker->get_tracked_linear_velocity();
  492. } else {
  493. return Vector3();
  494. }
  495. }
  496. Camera::Camera() {
  497. camera = VisualServer::get_singleton()->camera_create();
  498. size = 1;
  499. fov = 0;
  500. near = 0;
  501. far = 0;
  502. current = false;
  503. force_change = false;
  504. mode = PROJECTION_PERSPECTIVE;
  505. set_perspective(70.0, 0.05, 100.0);
  506. keep_aspect = KEEP_HEIGHT;
  507. layers = 0xfffff;
  508. v_offset = 0;
  509. h_offset = 0;
  510. VisualServer::get_singleton()->camera_set_cull_mask(camera, layers);
  511. //active=false;
  512. velocity_tracker.instance();
  513. doppler_tracking = DOPPLER_TRACKING_DISABLED;
  514. set_notify_transform(true);
  515. set_disable_scale(true);
  516. }
  517. Camera::~Camera() {
  518. VisualServer::get_singleton()->free(camera);
  519. }
  520. ////////////////////////////////////////
  521. void ClippedCamera::set_margin(float p_margin) {
  522. margin = p_margin;
  523. }
  524. float ClippedCamera::get_margin() const {
  525. return margin;
  526. }
  527. void ClippedCamera::set_process_mode(ProcessMode p_mode) {
  528. if (process_mode == p_mode) {
  529. return;
  530. }
  531. set_process_internal(p_mode == CLIP_PROCESS_IDLE);
  532. set_physics_process_internal(p_mode == CLIP_PROCESS_PHYSICS);
  533. }
  534. ClippedCamera::ProcessMode ClippedCamera::get_process_mode() const {
  535. return process_mode;
  536. }
  537. Transform ClippedCamera::get_camera_transform() const {
  538. Transform t = Camera::get_camera_transform();
  539. t.origin += -t.basis.get_axis(Vector3::AXIS_Z).normalized() * clip_offset;
  540. return t;
  541. }
  542. void ClippedCamera::_notification(int p_what) {
  543. if (p_what == NOTIFICATION_INTERNAL_PROCESS || p_what == NOTIFICATION_INTERNAL_PHYSICS_PROCESS) {
  544. Spatial *parent = Object::cast_to<Spatial>(get_parent());
  545. if (!parent) {
  546. return;
  547. }
  548. PhysicsDirectSpaceState *dspace = get_world()->get_direct_space_state();
  549. ERR_FAIL_COND(!dspace); // most likely physics set to threads
  550. Vector3 cam_fw = -get_global_transform().basis.get_axis(Vector3::AXIS_Z).normalized();
  551. Vector3 cam_pos = get_global_transform().origin;
  552. Vector3 parent_pos = parent->get_global_transform().origin;
  553. Plane parent_plane(parent_pos, cam_fw);
  554. if (parent_plane.is_point_over(cam_pos)) {
  555. //cam is beyond parent plane
  556. return;
  557. }
  558. Vector3 ray_from = parent_plane.project(cam_pos);
  559. clip_offset = 0; //reset by defau;t
  560. { //check if points changed
  561. Vector<Vector3> local_points = get_near_plane_points();
  562. bool all_equal = true;
  563. for (int i = 0; i < 5; i++) {
  564. if (points[i] != local_points[i]) {
  565. all_equal = false;
  566. break;
  567. }
  568. }
  569. if (!all_equal) {
  570. PhysicsServer::get_singleton()->shape_set_data(pyramid_shape, local_points);
  571. points = local_points;
  572. }
  573. }
  574. Transform xf = get_global_transform();
  575. xf.origin = ray_from;
  576. xf.orthonormalize();
  577. float csafe, cunsafe;
  578. if (dspace->cast_motion(pyramid_shape, xf, cam_pos - ray_from, margin, csafe, cunsafe, exclude, collision_mask, clip_to_bodies, clip_to_areas)) {
  579. clip_offset = cam_pos.distance_to(ray_from + (cam_pos - ray_from).normalized() * csafe);
  580. }
  581. _update_camera();
  582. }
  583. if (p_what == NOTIFICATION_LOCAL_TRANSFORM_CHANGED) {
  584. update_gizmo();
  585. }
  586. }
  587. void ClippedCamera::set_collision_mask(uint32_t p_mask) {
  588. collision_mask = p_mask;
  589. }
  590. uint32_t ClippedCamera::get_collision_mask() const {
  591. return collision_mask;
  592. }
  593. void ClippedCamera::set_collision_mask_bit(int p_bit, bool p_value) {
  594. uint32_t mask = get_collision_mask();
  595. if (p_value)
  596. mask |= 1 << p_bit;
  597. else
  598. mask &= ~(1 << p_bit);
  599. set_collision_mask(mask);
  600. }
  601. bool ClippedCamera::get_collision_mask_bit(int p_bit) const {
  602. return get_collision_mask() & (1 << p_bit);
  603. }
  604. void ClippedCamera::add_exception_rid(const RID &p_rid) {
  605. exclude.insert(p_rid);
  606. }
  607. void ClippedCamera::add_exception(const Object *p_object) {
  608. ERR_FAIL_NULL(p_object);
  609. const CollisionObject *co = Object::cast_to<CollisionObject>(p_object);
  610. if (!co)
  611. return;
  612. add_exception_rid(co->get_rid());
  613. }
  614. void ClippedCamera::remove_exception_rid(const RID &p_rid) {
  615. exclude.erase(p_rid);
  616. }
  617. void ClippedCamera::remove_exception(const Object *p_object) {
  618. ERR_FAIL_NULL(p_object);
  619. const CollisionObject *co = Object::cast_to<CollisionObject>(p_object);
  620. if (!co)
  621. return;
  622. remove_exception_rid(co->get_rid());
  623. }
  624. void ClippedCamera::clear_exceptions() {
  625. exclude.clear();
  626. }
  627. void ClippedCamera::set_clip_to_areas(bool p_clip) {
  628. clip_to_areas = p_clip;
  629. }
  630. bool ClippedCamera::is_clip_to_areas_enabled() const {
  631. return clip_to_areas;
  632. }
  633. void ClippedCamera::set_clip_to_bodies(bool p_clip) {
  634. clip_to_bodies = p_clip;
  635. }
  636. bool ClippedCamera::is_clip_to_bodies_enabled() const {
  637. return clip_to_bodies;
  638. }
  639. void ClippedCamera::_bind_methods() {
  640. ClassDB::bind_method(D_METHOD("set_margin", "margin"), &ClippedCamera::set_margin);
  641. ClassDB::bind_method(D_METHOD("get_margin"), &ClippedCamera::get_margin);
  642. ClassDB::bind_method(D_METHOD("set_process_mode", "process_mode"), &ClippedCamera::set_process_mode);
  643. ClassDB::bind_method(D_METHOD("get_process_mode"), &ClippedCamera::get_process_mode);
  644. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &ClippedCamera::set_collision_mask);
  645. ClassDB::bind_method(D_METHOD("get_collision_mask"), &ClippedCamera::get_collision_mask);
  646. ClassDB::bind_method(D_METHOD("set_collision_mask_bit", "bit", "value"), &ClippedCamera::set_collision_mask_bit);
  647. ClassDB::bind_method(D_METHOD("get_collision_mask_bit", "bit"), &ClippedCamera::get_collision_mask_bit);
  648. ClassDB::bind_method(D_METHOD("add_exception_rid", "rid"), &ClippedCamera::add_exception_rid);
  649. ClassDB::bind_method(D_METHOD("add_exception", "node"), &ClippedCamera::add_exception);
  650. ClassDB::bind_method(D_METHOD("remove_exception_rid", "rid"), &ClippedCamera::remove_exception_rid);
  651. ClassDB::bind_method(D_METHOD("remove_exception", "node"), &ClippedCamera::remove_exception);
  652. ClassDB::bind_method(D_METHOD("set_clip_to_areas", "enable"), &ClippedCamera::set_clip_to_areas);
  653. ClassDB::bind_method(D_METHOD("is_clip_to_areas_enabled"), &ClippedCamera::is_clip_to_areas_enabled);
  654. ClassDB::bind_method(D_METHOD("set_clip_to_bodies", "enable"), &ClippedCamera::set_clip_to_bodies);
  655. ClassDB::bind_method(D_METHOD("is_clip_to_bodies_enabled"), &ClippedCamera::is_clip_to_bodies_enabled);
  656. ClassDB::bind_method(D_METHOD("clear_exceptions"), &ClippedCamera::clear_exceptions);
  657. ADD_PROPERTY(PropertyInfo(Variant::REAL, "margin", PROPERTY_HINT_RANGE, "0,32,0.01"), "set_margin", "get_margin");
  658. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_mode", PROPERTY_HINT_ENUM, "Physics,Idle"), "set_process_mode", "get_process_mode");
  659. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  660. ADD_GROUP("Clip To", "clip_to");
  661. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_to_areas", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_clip_to_areas", "is_clip_to_areas_enabled");
  662. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_to_bodies", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_clip_to_bodies", "is_clip_to_bodies_enabled");
  663. BIND_ENUM_CONSTANT(CLIP_PROCESS_PHYSICS);
  664. BIND_ENUM_CONSTANT(CLIP_PROCESS_IDLE);
  665. }
  666. ClippedCamera::ClippedCamera() {
  667. margin = 0;
  668. clip_offset = 0;
  669. process_mode = CLIP_PROCESS_PHYSICS;
  670. set_physics_process_internal(true);
  671. collision_mask = 1;
  672. set_notify_local_transform(Engine::get_singleton()->is_editor_hint());
  673. points.resize(5);
  674. pyramid_shape = PhysicsServer::get_singleton()->shape_create(PhysicsServer::SHAPE_CONVEX_POLYGON);
  675. clip_to_areas = false;
  676. clip_to_bodies = true;
  677. }
  678. ClippedCamera::~ClippedCamera() {
  679. PhysicsServer::get_singleton()->free(pyramid_shape);
  680. }