camera.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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. velocity_tracker->reset(get_global_transform().origin);
  344. }
  345. _update_camera_mode();
  346. }
  347. Camera::DopplerTracking Camera::get_doppler_tracking() const {
  348. return doppler_tracking;
  349. }
  350. void Camera::_bind_methods() {
  351. ClassDB::bind_method(D_METHOD("project_ray_normal", "screen_point"), &Camera::project_ray_normal);
  352. ClassDB::bind_method(D_METHOD("project_local_ray_normal", "screen_point"), &Camera::project_local_ray_normal);
  353. ClassDB::bind_method(D_METHOD("project_ray_origin", "screen_point"), &Camera::project_ray_origin);
  354. ClassDB::bind_method(D_METHOD("unproject_position", "world_point"), &Camera::unproject_position);
  355. ClassDB::bind_method(D_METHOD("is_position_behind", "world_point"), &Camera::is_position_behind);
  356. ClassDB::bind_method(D_METHOD("project_position", "screen_point"), &Camera::project_position);
  357. ClassDB::bind_method(D_METHOD("set_perspective", "fov", "z_near", "z_far"), &Camera::set_perspective);
  358. ClassDB::bind_method(D_METHOD("set_orthogonal", "size", "z_near", "z_far"), &Camera::set_orthogonal);
  359. ClassDB::bind_method(D_METHOD("make_current"), &Camera::make_current);
  360. ClassDB::bind_method(D_METHOD("clear_current", "enable_next"), &Camera::clear_current, DEFVAL(true));
  361. ClassDB::bind_method(D_METHOD("set_current"), &Camera::set_current);
  362. ClassDB::bind_method(D_METHOD("is_current"), &Camera::is_current);
  363. ClassDB::bind_method(D_METHOD("get_camera_transform"), &Camera::get_camera_transform);
  364. ClassDB::bind_method(D_METHOD("get_fov"), &Camera::get_fov);
  365. ClassDB::bind_method(D_METHOD("get_size"), &Camera::get_size);
  366. ClassDB::bind_method(D_METHOD("get_zfar"), &Camera::get_zfar);
  367. ClassDB::bind_method(D_METHOD("get_znear"), &Camera::get_znear);
  368. ClassDB::bind_method(D_METHOD("set_fov"), &Camera::set_fov);
  369. ClassDB::bind_method(D_METHOD("set_size"), &Camera::set_size);
  370. ClassDB::bind_method(D_METHOD("set_zfar"), &Camera::set_zfar);
  371. ClassDB::bind_method(D_METHOD("set_znear"), &Camera::set_znear);
  372. ClassDB::bind_method(D_METHOD("get_projection"), &Camera::get_projection);
  373. ClassDB::bind_method(D_METHOD("set_projection"), &Camera::set_projection);
  374. ClassDB::bind_method(D_METHOD("set_h_offset", "ofs"), &Camera::set_h_offset);
  375. ClassDB::bind_method(D_METHOD("get_h_offset"), &Camera::get_h_offset);
  376. ClassDB::bind_method(D_METHOD("set_v_offset", "ofs"), &Camera::set_v_offset);
  377. ClassDB::bind_method(D_METHOD("get_v_offset"), &Camera::get_v_offset);
  378. ClassDB::bind_method(D_METHOD("set_cull_mask", "mask"), &Camera::set_cull_mask);
  379. ClassDB::bind_method(D_METHOD("get_cull_mask"), &Camera::get_cull_mask);
  380. ClassDB::bind_method(D_METHOD("set_environment", "env"), &Camera::set_environment);
  381. ClassDB::bind_method(D_METHOD("get_environment"), &Camera::get_environment);
  382. ClassDB::bind_method(D_METHOD("set_keep_aspect_mode", "mode"), &Camera::set_keep_aspect_mode);
  383. ClassDB::bind_method(D_METHOD("get_keep_aspect_mode"), &Camera::get_keep_aspect_mode);
  384. ClassDB::bind_method(D_METHOD("set_doppler_tracking", "mode"), &Camera::set_doppler_tracking);
  385. ClassDB::bind_method(D_METHOD("get_doppler_tracking"), &Camera::get_doppler_tracking);
  386. ClassDB::bind_method(D_METHOD("set_cull_mask_bit", "layer", "enable"), &Camera::set_cull_mask_bit);
  387. ClassDB::bind_method(D_METHOD("get_cull_mask_bit", "layer"), &Camera::get_cull_mask_bit);
  388. //ClassDB::bind_method(D_METHOD("_camera_make_current"),&Camera::_camera_make_current );
  389. ADD_PROPERTY(PropertyInfo(Variant::INT, "keep_aspect", PROPERTY_HINT_ENUM, "Keep Width,Keep Height"), "set_keep_aspect_mode", "get_keep_aspect_mode");
  390. ADD_PROPERTY(PropertyInfo(Variant::INT, "cull_mask", PROPERTY_HINT_LAYERS_3D_RENDER), "set_cull_mask", "get_cull_mask");
  391. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "environment", PROPERTY_HINT_RESOURCE_TYPE, "Environment"), "set_environment", "get_environment");
  392. ADD_PROPERTY(PropertyInfo(Variant::REAL, "h_offset"), "set_h_offset", "get_h_offset");
  393. ADD_PROPERTY(PropertyInfo(Variant::REAL, "v_offset"), "set_v_offset", "get_v_offset");
  394. ADD_PROPERTY(PropertyInfo(Variant::INT, "doppler_tracking", PROPERTY_HINT_ENUM, "Disabled,Idle,Physics"), "set_doppler_tracking", "get_doppler_tracking");
  395. ADD_PROPERTY(PropertyInfo(Variant::INT, "projection", PROPERTY_HINT_ENUM, "Perspective,Orthogonal"), "set_projection", "get_projection");
  396. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "current"), "set_current", "is_current");
  397. ADD_PROPERTY(PropertyInfo(Variant::REAL, "fov", PROPERTY_HINT_RANGE, "1,179,0.1"), "set_fov", "get_fov");
  398. ADD_PROPERTY(PropertyInfo(Variant::REAL, "size", PROPERTY_HINT_RANGE, "0.1,16384,0.01"), "set_size", "get_size");
  399. ADD_PROPERTY(PropertyInfo(Variant::REAL, "near", PROPERTY_HINT_EXP_RANGE, "0.01,8192,0.01,or_greater"), "set_znear", "get_znear");
  400. ADD_PROPERTY(PropertyInfo(Variant::REAL, "far", PROPERTY_HINT_EXP_RANGE, "0.1,8192,0.1,or_greater"), "set_zfar", "get_zfar");
  401. BIND_ENUM_CONSTANT(PROJECTION_PERSPECTIVE);
  402. BIND_ENUM_CONSTANT(PROJECTION_ORTHOGONAL);
  403. BIND_ENUM_CONSTANT(KEEP_WIDTH);
  404. BIND_ENUM_CONSTANT(KEEP_HEIGHT);
  405. BIND_ENUM_CONSTANT(DOPPLER_TRACKING_DISABLED)
  406. BIND_ENUM_CONSTANT(DOPPLER_TRACKING_IDLE_STEP)
  407. BIND_ENUM_CONSTANT(DOPPLER_TRACKING_PHYSICS_STEP)
  408. }
  409. float Camera::get_fov() const {
  410. return fov;
  411. }
  412. float Camera::get_size() const {
  413. return size;
  414. }
  415. float Camera::get_znear() const {
  416. return near;
  417. }
  418. float Camera::get_zfar() const {
  419. return far;
  420. }
  421. Camera::Projection Camera::get_projection() const {
  422. return mode;
  423. }
  424. void Camera::set_fov(float p_fov) {
  425. fov = p_fov;
  426. _update_camera_mode();
  427. _change_notify("fov");
  428. }
  429. void Camera::set_size(float p_size) {
  430. size = p_size;
  431. _update_camera_mode();
  432. _change_notify("size");
  433. }
  434. void Camera::set_znear(float p_znear) {
  435. near = p_znear;
  436. _update_camera_mode();
  437. }
  438. void Camera::set_zfar(float p_zfar) {
  439. far = p_zfar;
  440. _update_camera_mode();
  441. }
  442. void Camera::set_cull_mask(uint32_t p_layers) {
  443. layers = p_layers;
  444. VisualServer::get_singleton()->camera_set_cull_mask(camera, layers);
  445. _update_camera_mode();
  446. }
  447. uint32_t Camera::get_cull_mask() const {
  448. return layers;
  449. }
  450. void Camera::set_cull_mask_bit(int p_layer, bool p_enable) {
  451. ERR_FAIL_INDEX(p_layer, 32);
  452. if (p_enable) {
  453. set_cull_mask(layers | (1 << p_layer));
  454. } else {
  455. set_cull_mask(layers & (~(1 << p_layer)));
  456. }
  457. }
  458. bool Camera::get_cull_mask_bit(int p_layer) const {
  459. ERR_FAIL_INDEX_V(p_layer, 32, false);
  460. return (layers & (1 << p_layer));
  461. }
  462. Vector<Plane> Camera::get_frustum() const {
  463. ERR_FAIL_COND_V(!is_inside_world(), Vector<Plane>());
  464. Size2 viewport_size = get_viewport()->get_visible_rect().size;
  465. CameraMatrix cm;
  466. if (mode == PROJECTION_PERSPECTIVE)
  467. cm.set_perspective(fov, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
  468. else
  469. cm.set_orthogonal(size, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
  470. return cm.get_projection_planes(get_camera_transform());
  471. }
  472. void Camera::set_v_offset(float p_offset) {
  473. v_offset = p_offset;
  474. _update_camera();
  475. }
  476. float Camera::get_v_offset() const {
  477. return v_offset;
  478. }
  479. void Camera::set_h_offset(float p_offset) {
  480. h_offset = p_offset;
  481. _update_camera();
  482. }
  483. float Camera::get_h_offset() const {
  484. return h_offset;
  485. }
  486. Vector3 Camera::get_doppler_tracked_velocity() const {
  487. if (doppler_tracking != DOPPLER_TRACKING_DISABLED) {
  488. return velocity_tracker->get_tracked_linear_velocity();
  489. } else {
  490. return Vector3();
  491. }
  492. }
  493. Camera::Camera() {
  494. camera = VisualServer::get_singleton()->camera_create();
  495. size = 1;
  496. fov = 0;
  497. near = 0;
  498. far = 0;
  499. current = false;
  500. force_change = false;
  501. mode = PROJECTION_PERSPECTIVE;
  502. set_perspective(70.0, 0.05, 100.0);
  503. keep_aspect = KEEP_HEIGHT;
  504. layers = 0xfffff;
  505. v_offset = 0;
  506. h_offset = 0;
  507. VisualServer::get_singleton()->camera_set_cull_mask(camera, layers);
  508. //active=false;
  509. velocity_tracker.instance();
  510. doppler_tracking = DOPPLER_TRACKING_DISABLED;
  511. set_notify_transform(true);
  512. set_disable_scale(true);
  513. }
  514. Camera::~Camera() {
  515. VisualServer::get_singleton()->free(camera);
  516. }
  517. ////////////////////////////////////////
  518. void ClippedCamera::set_margin(float p_margin) {
  519. margin = p_margin;
  520. }
  521. float ClippedCamera::get_margin() const {
  522. return margin;
  523. }
  524. void ClippedCamera::set_process_mode(ProcessMode p_mode) {
  525. if (process_mode == p_mode) {
  526. return;
  527. }
  528. set_process_internal(p_mode == CLIP_PROCESS_IDLE);
  529. set_physics_process_internal(p_mode == CLIP_PROCESS_PHYSICS);
  530. }
  531. ClippedCamera::ProcessMode ClippedCamera::get_process_mode() const {
  532. return process_mode;
  533. }
  534. Transform ClippedCamera::get_camera_transform() const {
  535. Transform t = Camera::get_camera_transform();
  536. t.origin += -t.basis.get_axis(Vector3::AXIS_Z).normalized() * clip_offset;
  537. return t;
  538. }
  539. void ClippedCamera::_notification(int p_what) {
  540. if (p_what == NOTIFICATION_INTERNAL_PROCESS || p_what == NOTIFICATION_INTERNAL_PHYSICS_PROCESS) {
  541. Spatial *parent = Object::cast_to<Spatial>(get_parent());
  542. if (!parent) {
  543. return;
  544. }
  545. PhysicsDirectSpaceState *dspace = get_world()->get_direct_space_state();
  546. ERR_FAIL_COND(!dspace); // most likely physics set to threads
  547. Vector3 cam_fw = -get_global_transform().basis.get_axis(Vector3::AXIS_Z).normalized();
  548. Vector3 cam_pos = get_global_transform().origin;
  549. Vector3 parent_pos = parent->get_global_transform().origin;
  550. Plane parent_plane(parent_pos, cam_fw);
  551. if (parent_plane.is_point_over(cam_pos)) {
  552. //cam is beyond parent plane
  553. return;
  554. }
  555. Vector3 ray_from = parent_plane.project(cam_pos);
  556. clip_offset = 0; //reset by defau;t
  557. { //check if points changed
  558. Vector<Vector3> local_points = get_near_plane_points();
  559. bool all_equal = true;
  560. for (int i = 0; i < 5; i++) {
  561. if (points[i] != local_points[i]) {
  562. all_equal = false;
  563. break;
  564. }
  565. }
  566. if (!all_equal) {
  567. PhysicsServer::get_singleton()->shape_set_data(pyramid_shape, local_points);
  568. points = local_points;
  569. }
  570. }
  571. Transform xf = get_global_transform();
  572. xf.origin = ray_from;
  573. xf.orthonormalize();
  574. float csafe, cunsafe;
  575. if (dspace->cast_motion(pyramid_shape, xf, cam_pos - ray_from, margin, csafe, cunsafe, exclude, collision_mask, clip_to_bodies, clip_to_areas)) {
  576. clip_offset = cam_pos.distance_to(ray_from + (cam_pos - ray_from).normalized() * csafe);
  577. }
  578. _update_camera();
  579. }
  580. if (p_what == NOTIFICATION_LOCAL_TRANSFORM_CHANGED) {
  581. update_gizmo();
  582. }
  583. }
  584. void ClippedCamera::set_collision_mask(uint32_t p_mask) {
  585. collision_mask = p_mask;
  586. }
  587. uint32_t ClippedCamera::get_collision_mask() const {
  588. return collision_mask;
  589. }
  590. void ClippedCamera::set_collision_mask_bit(int p_bit, bool p_value) {
  591. uint32_t mask = get_collision_mask();
  592. if (p_value)
  593. mask |= 1 << p_bit;
  594. else
  595. mask &= ~(1 << p_bit);
  596. set_collision_mask(mask);
  597. }
  598. bool ClippedCamera::get_collision_mask_bit(int p_bit) const {
  599. return get_collision_mask() & (1 << p_bit);
  600. }
  601. void ClippedCamera::add_exception_rid(const RID &p_rid) {
  602. exclude.insert(p_rid);
  603. }
  604. void ClippedCamera::add_exception(const Object *p_object) {
  605. ERR_FAIL_NULL(p_object);
  606. const CollisionObject *co = Object::cast_to<CollisionObject>(p_object);
  607. if (!co)
  608. return;
  609. add_exception_rid(co->get_rid());
  610. }
  611. void ClippedCamera::remove_exception_rid(const RID &p_rid) {
  612. exclude.erase(p_rid);
  613. }
  614. void ClippedCamera::remove_exception(const Object *p_object) {
  615. ERR_FAIL_NULL(p_object);
  616. const CollisionObject *co = Object::cast_to<CollisionObject>(p_object);
  617. if (!co)
  618. return;
  619. remove_exception_rid(co->get_rid());
  620. }
  621. void ClippedCamera::clear_exceptions() {
  622. exclude.clear();
  623. }
  624. void ClippedCamera::set_clip_to_areas(bool p_clip) {
  625. clip_to_areas = p_clip;
  626. }
  627. bool ClippedCamera::is_clip_to_areas_enabled() const {
  628. return clip_to_areas;
  629. }
  630. void ClippedCamera::set_clip_to_bodies(bool p_clip) {
  631. clip_to_bodies = p_clip;
  632. }
  633. bool ClippedCamera::is_clip_to_bodies_enabled() const {
  634. return clip_to_bodies;
  635. }
  636. void ClippedCamera::_bind_methods() {
  637. ClassDB::bind_method(D_METHOD("set_margin", "margin"), &ClippedCamera::set_margin);
  638. ClassDB::bind_method(D_METHOD("get_margin"), &ClippedCamera::get_margin);
  639. ClassDB::bind_method(D_METHOD("set_process_mode", "process_mode"), &ClippedCamera::set_process_mode);
  640. ClassDB::bind_method(D_METHOD("get_process_mode"), &ClippedCamera::get_process_mode);
  641. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &ClippedCamera::set_collision_mask);
  642. ClassDB::bind_method(D_METHOD("get_collision_mask"), &ClippedCamera::get_collision_mask);
  643. ClassDB::bind_method(D_METHOD("set_collision_mask_bit", "bit", "value"), &ClippedCamera::set_collision_mask_bit);
  644. ClassDB::bind_method(D_METHOD("get_collision_mask_bit", "bit"), &ClippedCamera::get_collision_mask_bit);
  645. ClassDB::bind_method(D_METHOD("add_exception_rid", "rid"), &ClippedCamera::add_exception_rid);
  646. ClassDB::bind_method(D_METHOD("add_exception", "node"), &ClippedCamera::add_exception);
  647. ClassDB::bind_method(D_METHOD("remove_exception_rid", "rid"), &ClippedCamera::remove_exception_rid);
  648. ClassDB::bind_method(D_METHOD("remove_exception", "node"), &ClippedCamera::remove_exception);
  649. ClassDB::bind_method(D_METHOD("set_clip_to_areas", "enable"), &ClippedCamera::set_clip_to_areas);
  650. ClassDB::bind_method(D_METHOD("is_clip_to_areas_enabled"), &ClippedCamera::is_clip_to_areas_enabled);
  651. ClassDB::bind_method(D_METHOD("set_clip_to_bodies", "enable"), &ClippedCamera::set_clip_to_bodies);
  652. ClassDB::bind_method(D_METHOD("is_clip_to_bodies_enabled"), &ClippedCamera::is_clip_to_bodies_enabled);
  653. ClassDB::bind_method(D_METHOD("clear_exceptions"), &ClippedCamera::clear_exceptions);
  654. ADD_PROPERTY(PropertyInfo(Variant::REAL, "margin", PROPERTY_HINT_RANGE, "0,32,0.01"), "set_margin", "get_margin");
  655. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_mode", PROPERTY_HINT_ENUM, "Physics,Idle"), "set_process_mode", "get_process_mode");
  656. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  657. ADD_GROUP("Clip To", "clip_to");
  658. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_to_areas", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_clip_to_areas", "is_clip_to_areas_enabled");
  659. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_to_bodies", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_clip_to_bodies", "is_clip_to_bodies_enabled");
  660. BIND_ENUM_CONSTANT(CLIP_PROCESS_PHYSICS);
  661. BIND_ENUM_CONSTANT(CLIP_PROCESS_IDLE);
  662. }
  663. ClippedCamera::ClippedCamera() {
  664. margin = 0;
  665. clip_offset = 0;
  666. process_mode = CLIP_PROCESS_PHYSICS;
  667. set_physics_process_internal(true);
  668. collision_mask = 1;
  669. set_notify_local_transform(Engine::get_singleton()->is_editor_hint());
  670. points.resize(5);
  671. pyramid_shape = PhysicsServer::get_singleton()->shape_create(PhysicsServer::SHAPE_CONVEX_POLYGON);
  672. clip_to_areas = false;
  673. clip_to_bodies = true;
  674. }
  675. ClippedCamera::~ClippedCamera() {
  676. PhysicsServer::get_singleton()->free(pyramid_shape);
  677. }