camera.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. /**************************************************************************/
  2. /* camera.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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. case PROJECTION_FRUSTUM: {
  51. set_frustum(size, frustum_offset, near, far);
  52. } break;
  53. }
  54. }
  55. void Camera::_validate_property(PropertyInfo &p_property) const {
  56. if (p_property.name == "fov") {
  57. if (mode != PROJECTION_PERSPECTIVE) {
  58. p_property.usage = PROPERTY_USAGE_NOEDITOR;
  59. }
  60. } else if (p_property.name == "size") {
  61. if (mode != PROJECTION_ORTHOGONAL && mode != PROJECTION_FRUSTUM) {
  62. p_property.usage = PROPERTY_USAGE_NOEDITOR;
  63. }
  64. } else if (p_property.name == "frustum_offset") {
  65. if (mode != PROJECTION_FRUSTUM) {
  66. p_property.usage = PROPERTY_USAGE_NOEDITOR;
  67. }
  68. }
  69. }
  70. void Camera::_update_camera() {
  71. if (!is_inside_tree()) {
  72. return;
  73. }
  74. VisualServer::get_singleton()->camera_set_transform(camera, get_camera_transform());
  75. // here goes listener stuff
  76. /*
  77. if (viewport_ptr && is_inside_scene() && is_current())
  78. get_viewport()->_camera_transform_changed_notify();
  79. */
  80. if (get_tree()->is_node_being_edited(this) || !is_current()) {
  81. return;
  82. }
  83. get_viewport()->_camera_transform_changed_notify();
  84. if (get_world().is_valid()) {
  85. get_world()->_update_camera(this);
  86. }
  87. }
  88. void Camera::_physics_interpolated_changed() {
  89. VisualServer::get_singleton()->camera_set_interpolated(camera, is_physics_interpolated());
  90. }
  91. void Camera::_notification(int p_what) {
  92. switch (p_what) {
  93. case NOTIFICATION_ENTER_WORLD: {
  94. // Needs to track the Viewport because it's needed on NOTIFICATION_EXIT_WORLD
  95. // and Spatial will handle it first, including clearing its reference to the Viewport,
  96. // therefore making it impossible to subclasses to access it
  97. viewport = get_viewport();
  98. ERR_FAIL_COND(!viewport);
  99. bool first_camera = viewport->_camera_add(this);
  100. if (current || first_camera) {
  101. viewport->_camera_set(this);
  102. }
  103. } break;
  104. case NOTIFICATION_TRANSFORM_CHANGED: {
  105. _request_camera_update();
  106. if (doppler_tracking != DOPPLER_TRACKING_DISABLED) {
  107. velocity_tracker->update_position(get_global_transform().origin);
  108. }
  109. } break;
  110. case NOTIFICATION_RESET_PHYSICS_INTERPOLATION: {
  111. if (is_physics_interpolated()) {
  112. VisualServer::get_singleton()->camera_reset_physics_interpolation(camera);
  113. }
  114. } break;
  115. case NOTIFICATION_EXIT_WORLD: {
  116. if (!get_tree()->is_node_being_edited(this)) {
  117. if (is_current()) {
  118. clear_current();
  119. current = true; //keep it true
  120. } else {
  121. current = false;
  122. }
  123. }
  124. if (viewport) {
  125. viewport->_camera_remove(this);
  126. viewport = nullptr;
  127. }
  128. } break;
  129. case NOTIFICATION_BECAME_CURRENT: {
  130. if (viewport) {
  131. viewport->find_world()->_register_camera(this);
  132. }
  133. } break;
  134. case NOTIFICATION_LOST_CURRENT: {
  135. if (viewport) {
  136. viewport->find_world()->_remove_camera(this);
  137. }
  138. } break;
  139. }
  140. }
  141. Transform Camera::get_camera_transform() const {
  142. Transform tr = get_global_transform().orthonormalized();
  143. tr.origin += tr.basis.get_axis(1) * v_offset;
  144. tr.origin += tr.basis.get_axis(0) * h_offset;
  145. return tr;
  146. }
  147. void Camera::set_perspective(float p_fovy_degrees, float p_z_near, float p_z_far) {
  148. if (!force_change && fov == p_fovy_degrees && p_z_near == near && p_z_far == far && mode == PROJECTION_PERSPECTIVE) {
  149. return;
  150. }
  151. fov = p_fovy_degrees;
  152. near = p_z_near;
  153. far = p_z_far;
  154. mode = PROJECTION_PERSPECTIVE;
  155. VisualServer::get_singleton()->camera_set_perspective(camera, fov, near, far);
  156. update_gizmo();
  157. force_change = false;
  158. }
  159. void Camera::set_orthogonal(float p_size, float p_z_near, float p_z_far) {
  160. if (!force_change && size == p_size && p_z_near == near && p_z_far == far && mode == PROJECTION_ORTHOGONAL) {
  161. return;
  162. }
  163. size = p_size;
  164. near = p_z_near;
  165. far = p_z_far;
  166. mode = PROJECTION_ORTHOGONAL;
  167. force_change = false;
  168. VisualServer::get_singleton()->camera_set_orthogonal(camera, size, near, far);
  169. update_gizmo();
  170. }
  171. void Camera::set_frustum(float p_size, Vector2 p_offset, float p_z_near, float p_z_far) {
  172. if (!force_change && size == p_size && frustum_offset == p_offset && p_z_near == near && p_z_far == far && mode == PROJECTION_FRUSTUM) {
  173. return;
  174. }
  175. size = p_size;
  176. frustum_offset = p_offset;
  177. near = p_z_near;
  178. far = p_z_far;
  179. mode = PROJECTION_FRUSTUM;
  180. force_change = false;
  181. VisualServer::get_singleton()->camera_set_frustum(camera, size, frustum_offset, near, far);
  182. update_gizmo();
  183. }
  184. void Camera::set_projection(Camera::Projection p_mode) {
  185. if (p_mode == PROJECTION_PERSPECTIVE || p_mode == PROJECTION_ORTHOGONAL || p_mode == PROJECTION_FRUSTUM) {
  186. mode = p_mode;
  187. _update_camera_mode();
  188. _change_notify();
  189. }
  190. }
  191. RID Camera::get_camera() const {
  192. return camera;
  193. };
  194. void Camera::make_current() {
  195. current = true;
  196. if (!is_inside_tree()) {
  197. return;
  198. }
  199. get_viewport()->_camera_set(this);
  200. //get_scene()->call_group(SceneMainLoop::GROUP_CALL_REALTIME,camera_group,"_camera_make_current",this);
  201. }
  202. void Camera::clear_current(bool p_enable_next) {
  203. current = false;
  204. if (!is_inside_tree()) {
  205. return;
  206. }
  207. if (get_viewport()->get_camera() == this) {
  208. get_viewport()->_camera_set(nullptr);
  209. if (p_enable_next) {
  210. get_viewport()->_camera_make_next_current(this);
  211. }
  212. }
  213. }
  214. void Camera::set_current(bool p_current) {
  215. if (p_current) {
  216. make_current();
  217. } else {
  218. clear_current();
  219. }
  220. }
  221. bool Camera::is_current() const {
  222. if (is_inside_tree() && !get_tree()->is_node_being_edited(this)) {
  223. return get_viewport()->get_camera() == this;
  224. } else {
  225. return current;
  226. }
  227. }
  228. Vector3 Camera::project_ray_normal(const Point2 &p_pos) const {
  229. Vector3 ray = project_local_ray_normal(p_pos);
  230. return get_camera_transform().basis.xform(ray).normalized();
  231. };
  232. Vector3 Camera::project_local_ray_normal(const Point2 &p_pos) const {
  233. ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector3(), "Camera is not inside scene.");
  234. Size2 viewport_size = get_viewport()->get_camera_rect_size();
  235. Vector2 cpos = get_viewport()->get_camera_coords(p_pos);
  236. Vector3 ray;
  237. if (mode == PROJECTION_ORTHOGONAL) {
  238. ray = Vector3(0, 0, -1);
  239. } else {
  240. CameraMatrix cm;
  241. cm.set_perspective(fov, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
  242. Vector2 screen_he = cm.get_viewport_half_extents();
  243. ray = Vector3(((cpos.x / viewport_size.width) * 2.0 - 1.0) * screen_he.x, ((1.0 - (cpos.y / viewport_size.height)) * 2.0 - 1.0) * screen_he.y, -near).normalized();
  244. }
  245. return ray;
  246. };
  247. Vector3 Camera::project_ray_origin(const Point2 &p_pos) const {
  248. ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector3(), "Camera is not inside scene.");
  249. Size2 viewport_size = get_viewport()->get_camera_rect_size();
  250. Vector2 cpos = get_viewport()->get_camera_coords(p_pos);
  251. ERR_FAIL_COND_V(viewport_size.y == 0, Vector3());
  252. if (mode == PROJECTION_PERSPECTIVE) {
  253. return get_camera_transform().origin;
  254. } else {
  255. Vector2 pos = cpos / viewport_size;
  256. float vsize, hsize;
  257. if (keep_aspect == KEEP_WIDTH) {
  258. vsize = size / viewport_size.aspect();
  259. hsize = size;
  260. } else {
  261. hsize = size * viewport_size.aspect();
  262. vsize = size;
  263. }
  264. Vector3 ray;
  265. ray.x = pos.x * (hsize)-hsize / 2;
  266. ray.y = (1.0 - pos.y) * (vsize)-vsize / 2;
  267. ray.z = -near;
  268. ray = get_camera_transform().xform(ray);
  269. return ray;
  270. };
  271. };
  272. bool Camera::is_position_behind(const Vector3 &p_pos) const {
  273. Transform t = get_global_transform();
  274. Vector3 eyedir = -t.basis.get_axis(2).normalized();
  275. return eyedir.dot(p_pos - t.origin) < near;
  276. }
  277. Vector<Vector3> Camera::get_near_plane_points() const {
  278. ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector<Vector3>(), "Camera is not inside scene.");
  279. Size2 viewport_size = get_viewport()->get_visible_rect().size;
  280. CameraMatrix cm;
  281. if (mode == PROJECTION_ORTHOGONAL) {
  282. cm.set_orthogonal(size, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
  283. } else {
  284. cm.set_perspective(fov, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
  285. }
  286. Vector3 endpoints[8];
  287. cm.get_endpoints(Transform(), endpoints);
  288. Vector<Vector3> points;
  289. points.push_back(Vector3());
  290. for (int i = 0; i < 4; i++) {
  291. points.push_back(endpoints[i + 4]);
  292. }
  293. return points;
  294. }
  295. Point2 Camera::unproject_position(const Vector3 &p_pos) const {
  296. ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector2(), "Camera is not inside scene.");
  297. Size2 viewport_size = get_viewport()->get_visible_rect().size;
  298. CameraMatrix cm;
  299. if (mode == PROJECTION_ORTHOGONAL) {
  300. cm.set_orthogonal(size, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
  301. } else {
  302. cm.set_perspective(fov, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
  303. }
  304. Plane p(get_camera_transform().xform_inv(p_pos), 1.0);
  305. p = cm.xform4(p);
  306. p.normal /= p.d;
  307. Point2 res;
  308. res.x = (p.normal.x * 0.5 + 0.5) * viewport_size.x;
  309. res.y = (-p.normal.y * 0.5 + 0.5) * viewport_size.y;
  310. return res;
  311. }
  312. Vector3 Camera::project_position(const Point2 &p_point, float p_z_depth) const {
  313. ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector3(), "Camera is not inside scene.");
  314. if (p_z_depth == 0 && mode != PROJECTION_ORTHOGONAL) {
  315. return get_global_transform().origin;
  316. }
  317. Size2 viewport_size = get_viewport()->get_visible_rect().size;
  318. CameraMatrix cm;
  319. if (mode == PROJECTION_ORTHOGONAL) {
  320. cm.set_orthogonal(size, viewport_size.aspect(), p_z_depth, far, keep_aspect == KEEP_WIDTH);
  321. } else {
  322. cm.set_perspective(fov, viewport_size.aspect(), p_z_depth, far, keep_aspect == KEEP_WIDTH);
  323. }
  324. Vector2 vp_he = cm.get_viewport_half_extents();
  325. Vector2 point;
  326. point.x = (p_point.x / viewport_size.x) * 2.0 - 1.0;
  327. point.y = (1.0 - (p_point.y / viewport_size.y)) * 2.0 - 1.0;
  328. point *= vp_he;
  329. Vector3 p(point.x, point.y, -p_z_depth);
  330. return get_camera_transform().xform(p);
  331. }
  332. /*
  333. void Camera::_camera_make_current(Node *p_camera) {
  334. if (p_camera==this) {
  335. VisualServer::get_singleton()->viewport_attach_camera(viewport_id,camera);
  336. active=true;
  337. } else {
  338. if (active && p_camera==NULL) {
  339. //detech camera because no one else will claim it
  340. VisualServer::get_singleton()->viewport_attach_camera(viewport_id,RID());
  341. }
  342. active=false;
  343. }
  344. }
  345. */
  346. void Camera::set_environment(const Ref<Environment> &p_environment) {
  347. environment = p_environment;
  348. if (environment.is_valid()) {
  349. VS::get_singleton()->camera_set_environment(camera, environment->get_rid());
  350. } else {
  351. VS::get_singleton()->camera_set_environment(camera, RID());
  352. }
  353. _update_camera_mode();
  354. }
  355. Ref<Environment> Camera::get_environment() const {
  356. return environment;
  357. }
  358. void Camera::set_keep_aspect_mode(KeepAspect p_aspect) {
  359. keep_aspect = p_aspect;
  360. VisualServer::get_singleton()->camera_set_use_vertical_aspect(camera, p_aspect == KEEP_WIDTH);
  361. _update_camera_mode();
  362. _change_notify();
  363. }
  364. Camera::KeepAspect Camera::get_keep_aspect_mode() const {
  365. return keep_aspect;
  366. }
  367. void Camera::set_doppler_tracking(DopplerTracking p_tracking) {
  368. if (doppler_tracking == p_tracking) {
  369. return;
  370. }
  371. doppler_tracking = p_tracking;
  372. if (p_tracking != DOPPLER_TRACKING_DISABLED) {
  373. velocity_tracker->set_track_physics_step(doppler_tracking == DOPPLER_TRACKING_PHYSICS_STEP);
  374. if (is_inside_tree()) {
  375. velocity_tracker->reset(get_global_transform().origin);
  376. }
  377. }
  378. _update_camera_mode();
  379. }
  380. Camera::DopplerTracking Camera::get_doppler_tracking() const {
  381. return doppler_tracking;
  382. }
  383. void Camera::_bind_methods() {
  384. ClassDB::bind_method(D_METHOD("project_ray_normal", "screen_point"), &Camera::project_ray_normal);
  385. ClassDB::bind_method(D_METHOD("project_local_ray_normal", "screen_point"), &Camera::project_local_ray_normal);
  386. ClassDB::bind_method(D_METHOD("project_ray_origin", "screen_point"), &Camera::project_ray_origin);
  387. ClassDB::bind_method(D_METHOD("unproject_position", "world_point"), &Camera::unproject_position);
  388. ClassDB::bind_method(D_METHOD("is_position_behind", "world_point"), &Camera::is_position_behind);
  389. ClassDB::bind_method(D_METHOD("project_position", "screen_point", "z_depth"), &Camera::project_position);
  390. ClassDB::bind_method(D_METHOD("set_perspective", "fov", "z_near", "z_far"), &Camera::set_perspective);
  391. ClassDB::bind_method(D_METHOD("set_orthogonal", "size", "z_near", "z_far"), &Camera::set_orthogonal);
  392. ClassDB::bind_method(D_METHOD("set_frustum", "size", "offset", "z_near", "z_far"), &Camera::set_frustum);
  393. ClassDB::bind_method(D_METHOD("make_current"), &Camera::make_current);
  394. ClassDB::bind_method(D_METHOD("clear_current", "enable_next"), &Camera::clear_current, DEFVAL(true));
  395. ClassDB::bind_method(D_METHOD("set_current", "enable"), &Camera::set_current);
  396. ClassDB::bind_method(D_METHOD("is_current"), &Camera::is_current);
  397. ClassDB::bind_method(D_METHOD("get_camera_transform"), &Camera::get_camera_transform);
  398. ClassDB::bind_method(D_METHOD("get_fov"), &Camera::get_fov);
  399. ClassDB::bind_method(D_METHOD("get_frustum_offset"), &Camera::get_frustum_offset);
  400. ClassDB::bind_method(D_METHOD("get_size"), &Camera::get_size);
  401. ClassDB::bind_method(D_METHOD("get_zfar"), &Camera::get_zfar);
  402. ClassDB::bind_method(D_METHOD("get_znear"), &Camera::get_znear);
  403. ClassDB::bind_method(D_METHOD("set_fov", "fov"), &Camera::set_fov);
  404. ClassDB::bind_method(D_METHOD("set_frustum_offset", "frustum_offset"), &Camera::set_frustum_offset);
  405. ClassDB::bind_method(D_METHOD("set_size", "size"), &Camera::set_size);
  406. ClassDB::bind_method(D_METHOD("set_zfar", "zfar"), &Camera::set_zfar);
  407. ClassDB::bind_method(D_METHOD("set_znear", "znear"), &Camera::set_znear);
  408. ClassDB::bind_method(D_METHOD("get_projection"), &Camera::get_projection);
  409. ClassDB::bind_method(D_METHOD("set_projection", "projection"), &Camera::set_projection);
  410. ClassDB::bind_method(D_METHOD("set_h_offset", "ofs"), &Camera::set_h_offset);
  411. ClassDB::bind_method(D_METHOD("get_h_offset"), &Camera::get_h_offset);
  412. ClassDB::bind_method(D_METHOD("set_v_offset", "ofs"), &Camera::set_v_offset);
  413. ClassDB::bind_method(D_METHOD("get_v_offset"), &Camera::get_v_offset);
  414. ClassDB::bind_method(D_METHOD("set_cull_mask", "mask"), &Camera::set_cull_mask);
  415. ClassDB::bind_method(D_METHOD("get_cull_mask"), &Camera::get_cull_mask);
  416. ClassDB::bind_method(D_METHOD("set_environment", "env"), &Camera::set_environment);
  417. ClassDB::bind_method(D_METHOD("get_environment"), &Camera::get_environment);
  418. ClassDB::bind_method(D_METHOD("set_keep_aspect_mode", "mode"), &Camera::set_keep_aspect_mode);
  419. ClassDB::bind_method(D_METHOD("get_keep_aspect_mode"), &Camera::get_keep_aspect_mode);
  420. ClassDB::bind_method(D_METHOD("set_doppler_tracking", "mode"), &Camera::set_doppler_tracking);
  421. ClassDB::bind_method(D_METHOD("get_doppler_tracking"), &Camera::get_doppler_tracking);
  422. ClassDB::bind_method(D_METHOD("get_frustum"), &Camera::get_frustum);
  423. ClassDB::bind_method(D_METHOD("get_camera_rid"), &Camera::get_camera);
  424. ClassDB::bind_method(D_METHOD("set_cull_mask_bit", "layer", "enable"), &Camera::set_cull_mask_bit);
  425. ClassDB::bind_method(D_METHOD("get_cull_mask_bit", "layer"), &Camera::get_cull_mask_bit);
  426. //ClassDB::bind_method(D_METHOD("_camera_make_current"),&Camera::_camera_make_current );
  427. ADD_PROPERTY(PropertyInfo(Variant::INT, "keep_aspect", PROPERTY_HINT_ENUM, "Keep Width,Keep Height"), "set_keep_aspect_mode", "get_keep_aspect_mode");
  428. ADD_PROPERTY(PropertyInfo(Variant::INT, "cull_mask", PROPERTY_HINT_LAYERS_3D_RENDER), "set_cull_mask", "get_cull_mask");
  429. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "environment", PROPERTY_HINT_RESOURCE_TYPE, "Environment"), "set_environment", "get_environment");
  430. ADD_PROPERTY(PropertyInfo(Variant::REAL, "h_offset"), "set_h_offset", "get_h_offset");
  431. ADD_PROPERTY(PropertyInfo(Variant::REAL, "v_offset"), "set_v_offset", "get_v_offset");
  432. ADD_PROPERTY(PropertyInfo(Variant::INT, "doppler_tracking", PROPERTY_HINT_ENUM, "Disabled,Idle,Physics"), "set_doppler_tracking", "get_doppler_tracking");
  433. ADD_PROPERTY(PropertyInfo(Variant::INT, "projection", PROPERTY_HINT_ENUM, "Perspective,Orthogonal,Frustum"), "set_projection", "get_projection");
  434. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "current"), "set_current", "is_current");
  435. ADD_PROPERTY(PropertyInfo(Variant::REAL, "fov", PROPERTY_HINT_RANGE, "1,179,0.1"), "set_fov", "get_fov");
  436. ADD_PROPERTY(PropertyInfo(Variant::REAL, "size", PROPERTY_HINT_RANGE, "0.001,16384,0.001"), "set_size", "get_size");
  437. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "frustum_offset"), "set_frustum_offset", "get_frustum_offset");
  438. ADD_PROPERTY(PropertyInfo(Variant::REAL, "near", PROPERTY_HINT_EXP_RANGE, "0.01,8192,0.01,or_greater"), "set_znear", "get_znear");
  439. ADD_PROPERTY(PropertyInfo(Variant::REAL, "far", PROPERTY_HINT_EXP_RANGE, "0.1,8192,0.1,or_greater"), "set_zfar", "get_zfar");
  440. BIND_ENUM_CONSTANT(PROJECTION_PERSPECTIVE);
  441. BIND_ENUM_CONSTANT(PROJECTION_ORTHOGONAL);
  442. BIND_ENUM_CONSTANT(PROJECTION_FRUSTUM);
  443. BIND_ENUM_CONSTANT(KEEP_WIDTH);
  444. BIND_ENUM_CONSTANT(KEEP_HEIGHT);
  445. BIND_ENUM_CONSTANT(DOPPLER_TRACKING_DISABLED);
  446. BIND_ENUM_CONSTANT(DOPPLER_TRACKING_IDLE_STEP);
  447. BIND_ENUM_CONSTANT(DOPPLER_TRACKING_PHYSICS_STEP);
  448. }
  449. float Camera::get_fov() const {
  450. return fov;
  451. }
  452. float Camera::get_size() const {
  453. return size;
  454. }
  455. float Camera::get_znear() const {
  456. return near;
  457. }
  458. Vector2 Camera::get_frustum_offset() const {
  459. return frustum_offset;
  460. }
  461. float Camera::get_zfar() const {
  462. return far;
  463. }
  464. Camera::Projection Camera::get_projection() const {
  465. return mode;
  466. }
  467. void Camera::set_fov(float p_fov) {
  468. ERR_FAIL_COND(p_fov < 1 || p_fov > 179);
  469. fov = p_fov;
  470. _update_camera_mode();
  471. _change_notify("fov");
  472. }
  473. void Camera::set_size(float p_size) {
  474. ERR_FAIL_COND(p_size < 0.001 || p_size > 16384);
  475. size = p_size;
  476. _update_camera_mode();
  477. _change_notify("size");
  478. }
  479. void Camera::set_znear(float p_znear) {
  480. near = p_znear;
  481. _update_camera_mode();
  482. }
  483. void Camera::set_frustum_offset(Vector2 p_offset) {
  484. frustum_offset = p_offset;
  485. _update_camera_mode();
  486. }
  487. void Camera::set_zfar(float p_zfar) {
  488. far = p_zfar;
  489. _update_camera_mode();
  490. }
  491. void Camera::set_cull_mask(uint32_t p_layers) {
  492. layers = p_layers;
  493. VisualServer::get_singleton()->camera_set_cull_mask(camera, layers);
  494. _update_camera_mode();
  495. }
  496. uint32_t Camera::get_cull_mask() const {
  497. return layers;
  498. }
  499. void Camera::set_cull_mask_bit(int p_layer, bool p_enable) {
  500. ERR_FAIL_INDEX(p_layer, 32);
  501. if (p_enable) {
  502. set_cull_mask(layers | (1 << p_layer));
  503. } else {
  504. set_cull_mask(layers & (~(1 << p_layer)));
  505. }
  506. }
  507. bool Camera::get_cull_mask_bit(int p_layer) const {
  508. ERR_FAIL_INDEX_V(p_layer, 32, false);
  509. return (layers & (1 << p_layer));
  510. }
  511. Vector<Plane> Camera::get_frustum() const {
  512. ERR_FAIL_COND_V(!is_inside_world(), Vector<Plane>());
  513. Size2 viewport_size = get_viewport()->get_visible_rect().size;
  514. CameraMatrix cm;
  515. if (mode == PROJECTION_PERSPECTIVE) {
  516. cm.set_perspective(fov, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
  517. } else {
  518. cm.set_orthogonal(size, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
  519. }
  520. return cm.get_projection_planes(get_camera_transform());
  521. }
  522. void Camera::set_v_offset(float p_offset) {
  523. v_offset = p_offset;
  524. _update_camera();
  525. }
  526. float Camera::get_v_offset() const {
  527. return v_offset;
  528. }
  529. void Camera::set_h_offset(float p_offset) {
  530. h_offset = p_offset;
  531. _update_camera();
  532. }
  533. float Camera::get_h_offset() const {
  534. return h_offset;
  535. }
  536. Vector3 Camera::get_doppler_tracked_velocity() const {
  537. if (doppler_tracking != DOPPLER_TRACKING_DISABLED) {
  538. return velocity_tracker->get_tracked_linear_velocity();
  539. } else {
  540. return Vector3();
  541. }
  542. }
  543. Camera::Camera() {
  544. camera = RID_PRIME(VisualServer::get_singleton()->camera_create());
  545. size = 1;
  546. fov = 0;
  547. frustum_offset = Vector2();
  548. near = 0;
  549. far = 0;
  550. current = false;
  551. viewport = nullptr;
  552. force_change = false;
  553. mode = PROJECTION_PERSPECTIVE;
  554. set_perspective(70.0, 0.05, 100.0);
  555. keep_aspect = KEEP_HEIGHT;
  556. layers = 0xfffff;
  557. v_offset = 0;
  558. h_offset = 0;
  559. VisualServer::get_singleton()->camera_set_cull_mask(camera, layers);
  560. //active=false;
  561. velocity_tracker.instance();
  562. doppler_tracking = DOPPLER_TRACKING_DISABLED;
  563. set_notify_transform(true);
  564. set_disable_scale(true);
  565. }
  566. Camera::~Camera() {
  567. VisualServer::get_singleton()->free(camera);
  568. }
  569. ////////////////////////////////////////
  570. void ClippedCamera::set_margin(float p_margin) {
  571. margin = p_margin;
  572. }
  573. float ClippedCamera::get_margin() const {
  574. return margin;
  575. }
  576. void ClippedCamera::set_process_mode(ProcessMode p_mode) {
  577. if (process_mode == p_mode) {
  578. return;
  579. }
  580. process_mode = p_mode;
  581. set_process_internal(process_mode == CLIP_PROCESS_IDLE);
  582. set_physics_process_internal(process_mode == CLIP_PROCESS_PHYSICS);
  583. }
  584. ClippedCamera::ProcessMode ClippedCamera::get_process_mode() const {
  585. return process_mode;
  586. }
  587. Transform ClippedCamera::get_camera_transform() const {
  588. Transform t = Camera::get_camera_transform();
  589. t.origin += -t.basis.get_axis(Vector3::AXIS_Z).normalized() * clip_offset;
  590. return t;
  591. }
  592. void ClippedCamera::_notification(int p_what) {
  593. if (p_what == NOTIFICATION_INTERNAL_PROCESS || p_what == NOTIFICATION_INTERNAL_PHYSICS_PROCESS) {
  594. Spatial *parent = Object::cast_to<Spatial>(get_parent());
  595. if (!parent) {
  596. return;
  597. }
  598. PhysicsDirectSpaceState *dspace = get_world()->get_direct_space_state();
  599. ERR_FAIL_COND(!dspace); // most likely physics set to threads
  600. Vector3 cam_fw = -get_global_transform().basis.get_axis(Vector3::AXIS_Z).normalized();
  601. Vector3 cam_pos = get_global_transform().origin;
  602. Vector3 parent_pos = parent->get_global_transform().origin;
  603. Plane parent_plane(parent_pos, cam_fw);
  604. if (parent_plane.is_point_over(cam_pos)) {
  605. //cam is beyond parent plane
  606. return;
  607. }
  608. Vector3 ray_from = parent_plane.project(cam_pos);
  609. clip_offset = 0; //reset by defau;t
  610. { //check if points changed
  611. Vector<Vector3> local_points = get_near_plane_points();
  612. bool all_equal = true;
  613. for (int i = 0; i < 5; i++) {
  614. if (points[i] != local_points[i]) {
  615. all_equal = false;
  616. break;
  617. }
  618. }
  619. if (!all_equal) {
  620. PhysicsServer::get_singleton()->shape_set_data(pyramid_shape, local_points);
  621. points = local_points;
  622. }
  623. }
  624. Transform xf = get_global_transform();
  625. xf.origin = ray_from;
  626. xf.orthonormalize();
  627. float closest_safe = 1.0f, closest_unsafe = 1.0f;
  628. if (dspace->cast_motion(pyramid_shape, xf, cam_pos - ray_from, margin, closest_safe, closest_unsafe, exclude, collision_mask, clip_to_bodies, clip_to_areas)) {
  629. clip_offset = cam_pos.distance_to(ray_from + (cam_pos - ray_from) * closest_safe);
  630. }
  631. _update_camera();
  632. }
  633. if (p_what == NOTIFICATION_LOCAL_TRANSFORM_CHANGED) {
  634. update_gizmo();
  635. }
  636. }
  637. void ClippedCamera::set_collision_mask(uint32_t p_mask) {
  638. collision_mask = p_mask;
  639. }
  640. uint32_t ClippedCamera::get_collision_mask() const {
  641. return collision_mask;
  642. }
  643. void ClippedCamera::set_collision_mask_bit(int p_bit, bool p_value) {
  644. ERR_FAIL_INDEX_MSG(p_bit, 32, "Collision layer bit must be between 0 and 31 inclusive.");
  645. uint32_t mask = get_collision_mask();
  646. if (p_value) {
  647. mask |= 1 << p_bit;
  648. } else {
  649. mask &= ~(1 << p_bit);
  650. }
  651. set_collision_mask(mask);
  652. }
  653. bool ClippedCamera::get_collision_mask_bit(int p_bit) const {
  654. ERR_FAIL_INDEX_V_MSG(p_bit, 32, false, "Collision mask bit must be between 0 and 31 inclusive.");
  655. return get_collision_mask() & (1 << p_bit);
  656. }
  657. void ClippedCamera::add_exception_rid(const RID &p_rid) {
  658. exclude.insert(p_rid);
  659. }
  660. void ClippedCamera::add_exception(const Object *p_object) {
  661. ERR_FAIL_NULL(p_object);
  662. const CollisionObject *co = Object::cast_to<CollisionObject>(p_object);
  663. if (!co) {
  664. return;
  665. }
  666. add_exception_rid(co->get_rid());
  667. }
  668. void ClippedCamera::remove_exception_rid(const RID &p_rid) {
  669. exclude.erase(p_rid);
  670. }
  671. void ClippedCamera::remove_exception(const Object *p_object) {
  672. ERR_FAIL_NULL(p_object);
  673. const CollisionObject *co = Object::cast_to<CollisionObject>(p_object);
  674. if (!co) {
  675. return;
  676. }
  677. remove_exception_rid(co->get_rid());
  678. }
  679. void ClippedCamera::clear_exceptions() {
  680. exclude.clear();
  681. }
  682. float ClippedCamera::get_clip_offset() const {
  683. return clip_offset;
  684. }
  685. void ClippedCamera::set_clip_to_areas(bool p_clip) {
  686. clip_to_areas = p_clip;
  687. }
  688. bool ClippedCamera::is_clip_to_areas_enabled() const {
  689. return clip_to_areas;
  690. }
  691. void ClippedCamera::set_clip_to_bodies(bool p_clip) {
  692. clip_to_bodies = p_clip;
  693. }
  694. bool ClippedCamera::is_clip_to_bodies_enabled() const {
  695. return clip_to_bodies;
  696. }
  697. void ClippedCamera::_bind_methods() {
  698. ClassDB::bind_method(D_METHOD("set_margin", "margin"), &ClippedCamera::set_margin);
  699. ClassDB::bind_method(D_METHOD("get_margin"), &ClippedCamera::get_margin);
  700. ClassDB::bind_method(D_METHOD("set_process_mode", "process_mode"), &ClippedCamera::set_process_mode);
  701. ClassDB::bind_method(D_METHOD("get_process_mode"), &ClippedCamera::get_process_mode);
  702. ClassDB::bind_method(D_METHOD("set_collision_mask", "mask"), &ClippedCamera::set_collision_mask);
  703. ClassDB::bind_method(D_METHOD("get_collision_mask"), &ClippedCamera::get_collision_mask);
  704. ClassDB::bind_method(D_METHOD("set_collision_mask_bit", "bit", "value"), &ClippedCamera::set_collision_mask_bit);
  705. ClassDB::bind_method(D_METHOD("get_collision_mask_bit", "bit"), &ClippedCamera::get_collision_mask_bit);
  706. ClassDB::bind_method(D_METHOD("add_exception_rid", "rid"), &ClippedCamera::add_exception_rid);
  707. ClassDB::bind_method(D_METHOD("add_exception", "node"), &ClippedCamera::add_exception);
  708. ClassDB::bind_method(D_METHOD("remove_exception_rid", "rid"), &ClippedCamera::remove_exception_rid);
  709. ClassDB::bind_method(D_METHOD("remove_exception", "node"), &ClippedCamera::remove_exception);
  710. ClassDB::bind_method(D_METHOD("set_clip_to_areas", "enable"), &ClippedCamera::set_clip_to_areas);
  711. ClassDB::bind_method(D_METHOD("is_clip_to_areas_enabled"), &ClippedCamera::is_clip_to_areas_enabled);
  712. ClassDB::bind_method(D_METHOD("get_clip_offset"), &ClippedCamera::get_clip_offset);
  713. ClassDB::bind_method(D_METHOD("set_clip_to_bodies", "enable"), &ClippedCamera::set_clip_to_bodies);
  714. ClassDB::bind_method(D_METHOD("is_clip_to_bodies_enabled"), &ClippedCamera::is_clip_to_bodies_enabled);
  715. ClassDB::bind_method(D_METHOD("clear_exceptions"), &ClippedCamera::clear_exceptions);
  716. ADD_PROPERTY(PropertyInfo(Variant::REAL, "margin", PROPERTY_HINT_RANGE, "0,32,0.01"), "set_margin", "get_margin");
  717. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_mode", PROPERTY_HINT_ENUM, "Physics,Idle"), "set_process_mode", "get_process_mode");
  718. ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_collision_mask", "get_collision_mask");
  719. ADD_GROUP("Clip To", "clip_to");
  720. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_to_areas", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_clip_to_areas", "is_clip_to_areas_enabled");
  721. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "clip_to_bodies", PROPERTY_HINT_LAYERS_3D_PHYSICS), "set_clip_to_bodies", "is_clip_to_bodies_enabled");
  722. BIND_ENUM_CONSTANT(CLIP_PROCESS_PHYSICS);
  723. BIND_ENUM_CONSTANT(CLIP_PROCESS_IDLE);
  724. }
  725. ClippedCamera::ClippedCamera() {
  726. margin = 0;
  727. clip_offset = 0;
  728. process_mode = CLIP_PROCESS_PHYSICS;
  729. set_physics_process_internal(true);
  730. collision_mask = 1;
  731. set_notify_local_transform(Engine::get_singleton()->is_editor_hint());
  732. points.resize(5);
  733. pyramid_shape = RID_PRIME(PhysicsServer::get_singleton()->shape_create(PhysicsServer::SHAPE_CONVEX_POLYGON));
  734. clip_to_areas = false;
  735. clip_to_bodies = true;
  736. }
  737. ClippedCamera::~ClippedCamera() {
  738. PhysicsServer::get_singleton()->free(pyramid_shape);
  739. }