camera_3d.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /*************************************************************************/
  2. /* camera_3d.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_3d.h"
  31. #include "collision_object_3d.h"
  32. #include "core/core_string_names.h"
  33. #include "core/math/projection.h"
  34. #include "scene/main/viewport.h"
  35. void Camera3D::_update_audio_listener_state() {
  36. }
  37. void Camera3D::_request_camera_update() {
  38. _update_camera();
  39. }
  40. void Camera3D::_update_camera_mode() {
  41. force_change = true;
  42. switch (mode) {
  43. case PROJECTION_PERSPECTIVE: {
  44. set_perspective(fov, near, far);
  45. } break;
  46. case PROJECTION_ORTHOGONAL: {
  47. set_orthogonal(size, near, far);
  48. } break;
  49. case PROJECTION_FRUSTUM: {
  50. set_frustum(size, frustum_offset, near, far);
  51. } break;
  52. }
  53. }
  54. void Camera3D::_validate_property(PropertyInfo &p_property) const {
  55. if (p_property.name == "fov") {
  56. if (mode != PROJECTION_PERSPECTIVE) {
  57. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  58. }
  59. } else if (p_property.name == "size") {
  60. if (mode != PROJECTION_ORTHOGONAL && mode != PROJECTION_FRUSTUM) {
  61. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  62. }
  63. } else if (p_property.name == "frustum_offset") {
  64. if (mode != PROJECTION_FRUSTUM) {
  65. p_property.usage = PROPERTY_USAGE_NO_EDITOR;
  66. }
  67. }
  68. if (attributes.is_valid()) {
  69. const CameraAttributesPhysical *physical_attributes = Object::cast_to<CameraAttributesPhysical>(attributes.ptr());
  70. if (physical_attributes) {
  71. if (p_property.name == "near" || p_property.name == "far" || p_property.name == "fov" || p_property.name == "keep_aspect") {
  72. p_property.usage = PROPERTY_USAGE_READ_ONLY | PROPERTY_USAGE_INTERNAL | PROPERTY_USAGE_EDITOR;
  73. }
  74. }
  75. }
  76. Node3D::_validate_property(p_property);
  77. }
  78. void Camera3D::_update_camera() {
  79. if (!is_inside_tree()) {
  80. return;
  81. }
  82. RenderingServer::get_singleton()->camera_set_transform(camera, get_camera_transform());
  83. if (get_tree()->is_node_being_edited(this) || !is_current()) {
  84. return;
  85. }
  86. get_viewport()->_camera_3d_transform_changed_notify();
  87. }
  88. void Camera3D::_notification(int p_what) {
  89. switch (p_what) {
  90. case NOTIFICATION_ENTER_WORLD: {
  91. // Needs to track the Viewport because it's needed on NOTIFICATION_EXIT_WORLD
  92. // and Spatial will handle it first, including clearing its reference to the Viewport,
  93. // therefore making it impossible to subclasses to access it
  94. viewport = get_viewport();
  95. ERR_FAIL_COND(!viewport);
  96. bool first_camera = viewport->_camera_3d_add(this);
  97. if (current || first_camera) {
  98. viewport->_camera_3d_set(this);
  99. }
  100. } break;
  101. case NOTIFICATION_TRANSFORM_CHANGED: {
  102. _request_camera_update();
  103. if (doppler_tracking != DOPPLER_TRACKING_DISABLED) {
  104. velocity_tracker->update_position(get_global_transform().origin);
  105. }
  106. } break;
  107. case NOTIFICATION_EXIT_WORLD: {
  108. if (!get_tree()->is_node_being_edited(this)) {
  109. if (is_current()) {
  110. clear_current();
  111. current = true; //keep it true
  112. } else {
  113. current = false;
  114. }
  115. }
  116. if (viewport) {
  117. viewport->_camera_3d_remove(this);
  118. viewport = nullptr;
  119. }
  120. } break;
  121. case NOTIFICATION_BECAME_CURRENT: {
  122. if (viewport) {
  123. viewport->find_world_3d()->_register_camera(this);
  124. }
  125. } break;
  126. case NOTIFICATION_LOST_CURRENT: {
  127. if (viewport) {
  128. viewport->find_world_3d()->_remove_camera(this);
  129. }
  130. } break;
  131. }
  132. }
  133. Transform3D Camera3D::get_camera_transform() const {
  134. Transform3D tr = get_global_transform().orthonormalized();
  135. tr.origin += tr.basis.get_column(1) * v_offset;
  136. tr.origin += tr.basis.get_column(0) * h_offset;
  137. return tr;
  138. }
  139. void Camera3D::set_perspective(real_t p_fovy_degrees, real_t p_z_near, real_t p_z_far) {
  140. if (!force_change && fov == p_fovy_degrees && p_z_near == near && p_z_far == far && mode == PROJECTION_PERSPECTIVE) {
  141. return;
  142. }
  143. fov = p_fovy_degrees;
  144. near = p_z_near;
  145. far = p_z_far;
  146. mode = PROJECTION_PERSPECTIVE;
  147. RenderingServer::get_singleton()->camera_set_perspective(camera, fov, near, far);
  148. update_gizmos();
  149. force_change = false;
  150. }
  151. void Camera3D::set_orthogonal(real_t p_size, real_t p_z_near, real_t p_z_far) {
  152. if (!force_change && size == p_size && p_z_near == near && p_z_far == far && mode == PROJECTION_ORTHOGONAL) {
  153. return;
  154. }
  155. size = p_size;
  156. near = p_z_near;
  157. far = p_z_far;
  158. mode = PROJECTION_ORTHOGONAL;
  159. force_change = false;
  160. RenderingServer::get_singleton()->camera_set_orthogonal(camera, size, near, far);
  161. update_gizmos();
  162. }
  163. void Camera3D::set_frustum(real_t p_size, Vector2 p_offset, real_t p_z_near, real_t p_z_far) {
  164. if (!force_change && size == p_size && frustum_offset == p_offset && p_z_near == near && p_z_far == far && mode == PROJECTION_FRUSTUM) {
  165. return;
  166. }
  167. size = p_size;
  168. frustum_offset = p_offset;
  169. near = p_z_near;
  170. far = p_z_far;
  171. mode = PROJECTION_FRUSTUM;
  172. force_change = false;
  173. RenderingServer::get_singleton()->camera_set_frustum(camera, size, frustum_offset, near, far);
  174. update_gizmos();
  175. }
  176. void Camera3D::set_projection(ProjectionType p_mode) {
  177. if (p_mode == PROJECTION_PERSPECTIVE || p_mode == PROJECTION_ORTHOGONAL || p_mode == PROJECTION_FRUSTUM) {
  178. mode = p_mode;
  179. _update_camera_mode();
  180. notify_property_list_changed();
  181. }
  182. }
  183. RID Camera3D::get_camera() const {
  184. return camera;
  185. };
  186. void Camera3D::make_current() {
  187. current = true;
  188. if (!is_inside_tree()) {
  189. return;
  190. }
  191. get_viewport()->_camera_3d_set(this);
  192. }
  193. void Camera3D::clear_current(bool p_enable_next) {
  194. current = false;
  195. if (!is_inside_tree()) {
  196. return;
  197. }
  198. if (get_viewport()->get_camera_3d() == this) {
  199. get_viewport()->_camera_3d_set(nullptr);
  200. if (p_enable_next) {
  201. get_viewport()->_camera_3d_make_next_current(this);
  202. }
  203. }
  204. }
  205. void Camera3D::set_current(bool p_enabled) {
  206. if (p_enabled) {
  207. make_current();
  208. } else {
  209. clear_current();
  210. }
  211. }
  212. bool Camera3D::is_current() const {
  213. if (is_inside_tree() && !get_tree()->is_node_being_edited(this)) {
  214. return get_viewport()->get_camera_3d() == this;
  215. } else {
  216. return current;
  217. }
  218. }
  219. Vector3 Camera3D::project_ray_normal(const Point2 &p_pos) const {
  220. Vector3 ray = project_local_ray_normal(p_pos);
  221. return get_camera_transform().basis.xform(ray).normalized();
  222. };
  223. Vector3 Camera3D::project_local_ray_normal(const Point2 &p_pos) const {
  224. ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector3(), "Camera is not inside scene.");
  225. Size2 viewport_size = get_viewport()->get_camera_rect_size();
  226. Vector2 cpos = get_viewport()->get_camera_coords(p_pos);
  227. Vector3 ray;
  228. if (mode == PROJECTION_ORTHOGONAL) {
  229. ray = Vector3(0, 0, -1);
  230. } else {
  231. Projection cm;
  232. cm.set_perspective(fov, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
  233. Vector2 screen_he = cm.get_viewport_half_extents();
  234. 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();
  235. }
  236. return ray;
  237. };
  238. Vector3 Camera3D::project_ray_origin(const Point2 &p_pos) const {
  239. ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector3(), "Camera is not inside scene.");
  240. Size2 viewport_size = get_viewport()->get_camera_rect_size();
  241. Vector2 cpos = get_viewport()->get_camera_coords(p_pos);
  242. ERR_FAIL_COND_V(viewport_size.y == 0, Vector3());
  243. if (mode == PROJECTION_PERSPECTIVE) {
  244. return get_camera_transform().origin;
  245. } else {
  246. Vector2 pos = cpos / viewport_size;
  247. real_t vsize, hsize;
  248. if (keep_aspect == KEEP_WIDTH) {
  249. vsize = size / viewport_size.aspect();
  250. hsize = size;
  251. } else {
  252. hsize = size * viewport_size.aspect();
  253. vsize = size;
  254. }
  255. Vector3 ray;
  256. ray.x = pos.x * (hsize)-hsize / 2;
  257. ray.y = (1.0 - pos.y) * (vsize)-vsize / 2;
  258. ray.z = -near;
  259. ray = get_camera_transform().xform(ray);
  260. return ray;
  261. };
  262. };
  263. bool Camera3D::is_position_behind(const Vector3 &p_pos) const {
  264. Transform3D t = get_global_transform();
  265. Vector3 eyedir = -t.basis.get_column(2).normalized();
  266. return eyedir.dot(p_pos - t.origin) < near;
  267. }
  268. Vector<Vector3> Camera3D::get_near_plane_points() const {
  269. ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector<Vector3>(), "Camera is not inside scene.");
  270. Size2 viewport_size = get_viewport()->get_visible_rect().size;
  271. Projection cm;
  272. if (mode == PROJECTION_ORTHOGONAL) {
  273. cm.set_orthogonal(size, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
  274. } else {
  275. cm.set_perspective(fov, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
  276. }
  277. Vector3 endpoints[8];
  278. cm.get_endpoints(Transform3D(), endpoints);
  279. Vector<Vector3> points = {
  280. Vector3(),
  281. endpoints[4],
  282. endpoints[5],
  283. endpoints[6],
  284. endpoints[7]
  285. };
  286. return points;
  287. }
  288. Point2 Camera3D::unproject_position(const Vector3 &p_pos) const {
  289. ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector2(), "Camera is not inside scene.");
  290. Size2 viewport_size = get_viewport()->get_visible_rect().size;
  291. Projection cm;
  292. if (mode == PROJECTION_ORTHOGONAL) {
  293. cm.set_orthogonal(size, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
  294. } else {
  295. cm.set_perspective(fov, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
  296. }
  297. Plane p(get_camera_transform().xform_inv(p_pos), 1.0);
  298. p = cm.xform4(p);
  299. p.normal /= p.d;
  300. Point2 res;
  301. res.x = (p.normal.x * 0.5 + 0.5) * viewport_size.x;
  302. res.y = (-p.normal.y * 0.5 + 0.5) * viewport_size.y;
  303. return res;
  304. }
  305. Vector3 Camera3D::project_position(const Point2 &p_point, real_t p_z_depth) const {
  306. ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector3(), "Camera is not inside scene.");
  307. if (p_z_depth == 0 && mode != PROJECTION_ORTHOGONAL) {
  308. return get_global_transform().origin;
  309. }
  310. Size2 viewport_size = get_viewport()->get_visible_rect().size;
  311. Projection cm;
  312. if (mode == PROJECTION_ORTHOGONAL) {
  313. cm.set_orthogonal(size, viewport_size.aspect(), p_z_depth, far, keep_aspect == KEEP_WIDTH);
  314. } else {
  315. cm.set_perspective(fov, viewport_size.aspect(), p_z_depth, far, keep_aspect == KEEP_WIDTH);
  316. }
  317. Vector2 vp_he = cm.get_viewport_half_extents();
  318. Vector2 point;
  319. point.x = (p_point.x / viewport_size.x) * 2.0 - 1.0;
  320. point.y = (1.0 - (p_point.y / viewport_size.y)) * 2.0 - 1.0;
  321. point *= vp_he;
  322. Vector3 p(point.x, point.y, -p_z_depth);
  323. return get_camera_transform().xform(p);
  324. }
  325. void Camera3D::set_environment(const Ref<Environment> &p_environment) {
  326. environment = p_environment;
  327. if (environment.is_valid()) {
  328. RS::get_singleton()->camera_set_environment(camera, environment->get_rid());
  329. } else {
  330. RS::get_singleton()->camera_set_environment(camera, RID());
  331. }
  332. _update_camera_mode();
  333. }
  334. Ref<Environment> Camera3D::get_environment() const {
  335. return environment;
  336. }
  337. void Camera3D::set_attributes(const Ref<CameraAttributes> &p_attributes) {
  338. if (attributes.is_valid()) {
  339. CameraAttributesPhysical *physical_attributes = Object::cast_to<CameraAttributesPhysical>(attributes.ptr());
  340. if (physical_attributes) {
  341. attributes->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Camera3D::_attributes_changed));
  342. }
  343. }
  344. attributes = p_attributes;
  345. if (attributes.is_valid()) {
  346. CameraAttributesPhysical *physical_attributes = Object::cast_to<CameraAttributesPhysical>(attributes.ptr());
  347. if (physical_attributes) {
  348. attributes->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &Camera3D::_attributes_changed));
  349. _attributes_changed();
  350. }
  351. RS::get_singleton()->camera_set_camera_attributes(camera, attributes->get_rid());
  352. } else {
  353. RS::get_singleton()->camera_set_camera_attributes(camera, RID());
  354. }
  355. notify_property_list_changed();
  356. }
  357. Ref<CameraAttributes> Camera3D::get_attributes() const {
  358. return attributes;
  359. }
  360. void Camera3D::_attributes_changed() {
  361. CameraAttributesPhysical *physical_attributes = Object::cast_to<CameraAttributesPhysical>(attributes.ptr());
  362. ERR_FAIL_COND(!physical_attributes);
  363. fov = physical_attributes->get_fov();
  364. near = physical_attributes->get_near();
  365. far = physical_attributes->get_far();
  366. keep_aspect = KEEP_HEIGHT;
  367. _update_camera_mode();
  368. }
  369. void Camera3D::set_keep_aspect_mode(KeepAspect p_aspect) {
  370. keep_aspect = p_aspect;
  371. RenderingServer::get_singleton()->camera_set_use_vertical_aspect(camera, p_aspect == KEEP_WIDTH);
  372. _update_camera_mode();
  373. notify_property_list_changed();
  374. }
  375. Camera3D::KeepAspect Camera3D::get_keep_aspect_mode() const {
  376. return keep_aspect;
  377. }
  378. void Camera3D::set_doppler_tracking(DopplerTracking p_tracking) {
  379. if (doppler_tracking == p_tracking) {
  380. return;
  381. }
  382. doppler_tracking = p_tracking;
  383. if (p_tracking != DOPPLER_TRACKING_DISABLED) {
  384. velocity_tracker->set_track_physics_step(doppler_tracking == DOPPLER_TRACKING_PHYSICS_STEP);
  385. if (is_inside_tree()) {
  386. velocity_tracker->reset(get_global_transform().origin);
  387. }
  388. }
  389. _update_camera_mode();
  390. }
  391. Camera3D::DopplerTracking Camera3D::get_doppler_tracking() const {
  392. return doppler_tracking;
  393. }
  394. void Camera3D::_bind_methods() {
  395. ClassDB::bind_method(D_METHOD("project_ray_normal", "screen_point"), &Camera3D::project_ray_normal);
  396. ClassDB::bind_method(D_METHOD("project_local_ray_normal", "screen_point"), &Camera3D::project_local_ray_normal);
  397. ClassDB::bind_method(D_METHOD("project_ray_origin", "screen_point"), &Camera3D::project_ray_origin);
  398. ClassDB::bind_method(D_METHOD("unproject_position", "world_point"), &Camera3D::unproject_position);
  399. ClassDB::bind_method(D_METHOD("is_position_behind", "world_point"), &Camera3D::is_position_behind);
  400. ClassDB::bind_method(D_METHOD("project_position", "screen_point", "z_depth"), &Camera3D::project_position);
  401. ClassDB::bind_method(D_METHOD("set_perspective", "fov", "z_near", "z_far"), &Camera3D::set_perspective);
  402. ClassDB::bind_method(D_METHOD("set_orthogonal", "size", "z_near", "z_far"), &Camera3D::set_orthogonal);
  403. ClassDB::bind_method(D_METHOD("set_frustum", "size", "offset", "z_near", "z_far"), &Camera3D::set_frustum);
  404. ClassDB::bind_method(D_METHOD("make_current"), &Camera3D::make_current);
  405. ClassDB::bind_method(D_METHOD("clear_current", "enable_next"), &Camera3D::clear_current, DEFVAL(true));
  406. ClassDB::bind_method(D_METHOD("set_current", "enabled"), &Camera3D::set_current);
  407. ClassDB::bind_method(D_METHOD("is_current"), &Camera3D::is_current);
  408. ClassDB::bind_method(D_METHOD("get_camera_transform"), &Camera3D::get_camera_transform);
  409. ClassDB::bind_method(D_METHOD("get_fov"), &Camera3D::get_fov);
  410. ClassDB::bind_method(D_METHOD("get_frustum_offset"), &Camera3D::get_frustum_offset);
  411. ClassDB::bind_method(D_METHOD("get_size"), &Camera3D::get_size);
  412. ClassDB::bind_method(D_METHOD("get_far"), &Camera3D::get_far);
  413. ClassDB::bind_method(D_METHOD("get_near"), &Camera3D::get_near);
  414. ClassDB::bind_method(D_METHOD("set_fov", "fov"), &Camera3D::set_fov);
  415. ClassDB::bind_method(D_METHOD("set_frustum_offset", "offset"), &Camera3D::set_frustum_offset);
  416. ClassDB::bind_method(D_METHOD("set_size", "size"), &Camera3D::set_size);
  417. ClassDB::bind_method(D_METHOD("set_far", "far"), &Camera3D::set_far);
  418. ClassDB::bind_method(D_METHOD("set_near", "near"), &Camera3D::set_near);
  419. ClassDB::bind_method(D_METHOD("get_projection"), &Camera3D::get_projection);
  420. ClassDB::bind_method(D_METHOD("set_projection", "mode"), &Camera3D::set_projection);
  421. ClassDB::bind_method(D_METHOD("set_h_offset", "offset"), &Camera3D::set_h_offset);
  422. ClassDB::bind_method(D_METHOD("get_h_offset"), &Camera3D::get_h_offset);
  423. ClassDB::bind_method(D_METHOD("set_v_offset", "offset"), &Camera3D::set_v_offset);
  424. ClassDB::bind_method(D_METHOD("get_v_offset"), &Camera3D::get_v_offset);
  425. ClassDB::bind_method(D_METHOD("set_cull_mask", "mask"), &Camera3D::set_cull_mask);
  426. ClassDB::bind_method(D_METHOD("get_cull_mask"), &Camera3D::get_cull_mask);
  427. ClassDB::bind_method(D_METHOD("set_environment", "env"), &Camera3D::set_environment);
  428. ClassDB::bind_method(D_METHOD("get_environment"), &Camera3D::get_environment);
  429. ClassDB::bind_method(D_METHOD("set_attributes", "env"), &Camera3D::set_attributes);
  430. ClassDB::bind_method(D_METHOD("get_attributes"), &Camera3D::get_attributes);
  431. ClassDB::bind_method(D_METHOD("set_keep_aspect_mode", "mode"), &Camera3D::set_keep_aspect_mode);
  432. ClassDB::bind_method(D_METHOD("get_keep_aspect_mode"), &Camera3D::get_keep_aspect_mode);
  433. ClassDB::bind_method(D_METHOD("set_doppler_tracking", "mode"), &Camera3D::set_doppler_tracking);
  434. ClassDB::bind_method(D_METHOD("get_doppler_tracking"), &Camera3D::get_doppler_tracking);
  435. ClassDB::bind_method(D_METHOD("get_frustum"), &Camera3D::_get_frustum);
  436. ClassDB::bind_method(D_METHOD("is_position_in_frustum", "world_point"), &Camera3D::is_position_in_frustum);
  437. ClassDB::bind_method(D_METHOD("get_camera_rid"), &Camera3D::get_camera);
  438. ClassDB::bind_method(D_METHOD("get_pyramid_shape_rid"), &Camera3D::get_pyramid_shape_rid);
  439. ClassDB::bind_method(D_METHOD("set_cull_mask_value", "layer_number", "value"), &Camera3D::set_cull_mask_value);
  440. ClassDB::bind_method(D_METHOD("get_cull_mask_value", "layer_number"), &Camera3D::get_cull_mask_value);
  441. //ClassDB::bind_method(D_METHOD("_camera_make_current"),&Camera::_camera_make_current );
  442. ADD_PROPERTY(PropertyInfo(Variant::INT, "keep_aspect", PROPERTY_HINT_ENUM, "Keep Width,Keep Height"), "set_keep_aspect_mode", "get_keep_aspect_mode");
  443. ADD_PROPERTY(PropertyInfo(Variant::INT, "cull_mask", PROPERTY_HINT_LAYERS_3D_RENDER), "set_cull_mask", "get_cull_mask");
  444. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "environment", PROPERTY_HINT_RESOURCE_TYPE, "Environment"), "set_environment", "get_environment");
  445. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "attributes", PROPERTY_HINT_RESOURCE_TYPE, "CameraAttributesPractical,CameraAttributesPhysical"), "set_attributes", "get_attributes");
  446. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "h_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_h_offset", "get_h_offset");
  447. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "v_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_v_offset", "get_v_offset");
  448. ADD_PROPERTY(PropertyInfo(Variant::INT, "doppler_tracking", PROPERTY_HINT_ENUM, "Disabled,Idle,Physics"), "set_doppler_tracking", "get_doppler_tracking");
  449. ADD_PROPERTY(PropertyInfo(Variant::INT, "projection", PROPERTY_HINT_ENUM, "Perspective,Orthogonal,Frustum"), "set_projection", "get_projection");
  450. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "current"), "set_current", "is_current");
  451. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fov", PROPERTY_HINT_RANGE, "1,179,0.1,degrees"), "set_fov", "get_fov");
  452. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "size", PROPERTY_HINT_RANGE, "0.001,16384,0.001,suffix:m"), "set_size", "get_size");
  453. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "frustum_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_frustum_offset", "get_frustum_offset");
  454. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "near", PROPERTY_HINT_RANGE, "0.001,10,0.001,or_greater,exp,suffix:m"), "set_near", "get_near");
  455. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "far", PROPERTY_HINT_RANGE, "0.01,4000,0.01,or_greater,exp,suffix:m"), "set_far", "get_far");
  456. BIND_ENUM_CONSTANT(PROJECTION_PERSPECTIVE);
  457. BIND_ENUM_CONSTANT(PROJECTION_ORTHOGONAL);
  458. BIND_ENUM_CONSTANT(PROJECTION_FRUSTUM);
  459. BIND_ENUM_CONSTANT(KEEP_WIDTH);
  460. BIND_ENUM_CONSTANT(KEEP_HEIGHT);
  461. BIND_ENUM_CONSTANT(DOPPLER_TRACKING_DISABLED);
  462. BIND_ENUM_CONSTANT(DOPPLER_TRACKING_IDLE_STEP);
  463. BIND_ENUM_CONSTANT(DOPPLER_TRACKING_PHYSICS_STEP);
  464. }
  465. real_t Camera3D::get_fov() const {
  466. return fov;
  467. }
  468. real_t Camera3D::get_size() const {
  469. return size;
  470. }
  471. real_t Camera3D::get_near() const {
  472. return near;
  473. }
  474. Vector2 Camera3D::get_frustum_offset() const {
  475. return frustum_offset;
  476. }
  477. real_t Camera3D::get_far() const {
  478. return far;
  479. }
  480. Camera3D::ProjectionType Camera3D::get_projection() const {
  481. return mode;
  482. }
  483. void Camera3D::set_fov(real_t p_fov) {
  484. ERR_FAIL_COND(p_fov < 1 || p_fov > 179);
  485. fov = p_fov;
  486. _update_camera_mode();
  487. }
  488. void Camera3D::set_size(real_t p_size) {
  489. ERR_FAIL_COND(p_size < 0.001 || p_size > 16384);
  490. size = p_size;
  491. _update_camera_mode();
  492. }
  493. void Camera3D::set_near(real_t p_near) {
  494. near = p_near;
  495. _update_camera_mode();
  496. }
  497. void Camera3D::set_frustum_offset(Vector2 p_offset) {
  498. frustum_offset = p_offset;
  499. _update_camera_mode();
  500. }
  501. void Camera3D::set_far(real_t p_far) {
  502. far = p_far;
  503. _update_camera_mode();
  504. }
  505. void Camera3D::set_cull_mask(uint32_t p_layers) {
  506. layers = p_layers;
  507. RenderingServer::get_singleton()->camera_set_cull_mask(camera, layers);
  508. _update_camera_mode();
  509. }
  510. uint32_t Camera3D::get_cull_mask() const {
  511. return layers;
  512. }
  513. void Camera3D::set_cull_mask_value(int p_layer_number, bool p_value) {
  514. ERR_FAIL_COND_MSG(p_layer_number < 1, "Render layer number must be between 1 and 20 inclusive.");
  515. ERR_FAIL_COND_MSG(p_layer_number > 20, "Render layer number must be between 1 and 20 inclusive.");
  516. uint32_t mask = get_cull_mask();
  517. if (p_value) {
  518. mask |= 1 << (p_layer_number - 1);
  519. } else {
  520. mask &= ~(1 << (p_layer_number - 1));
  521. }
  522. set_cull_mask(mask);
  523. }
  524. bool Camera3D::get_cull_mask_value(int p_layer_number) const {
  525. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Render layer number must be between 1 and 20 inclusive.");
  526. ERR_FAIL_COND_V_MSG(p_layer_number > 20, false, "Render layer number must be between 1 and 20 inclusive.");
  527. return layers & (1 << (p_layer_number - 1));
  528. }
  529. Vector<Plane> Camera3D::get_frustum() const {
  530. ERR_FAIL_COND_V(!is_inside_world(), Vector<Plane>());
  531. Size2 viewport_size = get_viewport()->get_visible_rect().size;
  532. Projection cm;
  533. if (mode == PROJECTION_PERSPECTIVE) {
  534. cm.set_perspective(fov, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
  535. } else {
  536. cm.set_orthogonal(size, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
  537. }
  538. return cm.get_projection_planes(get_camera_transform());
  539. }
  540. TypedArray<Plane> Camera3D::_get_frustum() const {
  541. Variant ret = get_frustum();
  542. return ret;
  543. }
  544. bool Camera3D::is_position_in_frustum(const Vector3 &p_position) const {
  545. Vector<Plane> frustum = get_frustum();
  546. for (int i = 0; i < frustum.size(); i++) {
  547. if (frustum[i].is_point_over(p_position)) {
  548. return false;
  549. }
  550. }
  551. return true;
  552. }
  553. void Camera3D::set_v_offset(real_t p_offset) {
  554. v_offset = p_offset;
  555. _update_camera();
  556. }
  557. real_t Camera3D::get_v_offset() const {
  558. return v_offset;
  559. }
  560. void Camera3D::set_h_offset(real_t p_offset) {
  561. h_offset = p_offset;
  562. _update_camera();
  563. }
  564. real_t Camera3D::get_h_offset() const {
  565. return h_offset;
  566. }
  567. Vector3 Camera3D::get_doppler_tracked_velocity() const {
  568. if (doppler_tracking != DOPPLER_TRACKING_DISABLED) {
  569. return velocity_tracker->get_tracked_linear_velocity();
  570. } else {
  571. return Vector3();
  572. }
  573. }
  574. RID Camera3D::get_pyramid_shape_rid() {
  575. ERR_FAIL_COND_V_MSG(!is_inside_tree(), RID(), "Camera is not inside scene.");
  576. if (pyramid_shape == RID()) {
  577. pyramid_shape_points = get_near_plane_points();
  578. pyramid_shape = PhysicsServer3D::get_singleton()->convex_polygon_shape_create();
  579. PhysicsServer3D::get_singleton()->shape_set_data(pyramid_shape, pyramid_shape_points);
  580. } else { //check if points changed
  581. Vector<Vector3> local_points = get_near_plane_points();
  582. bool all_equal = true;
  583. for (int i = 0; i < 5; i++) {
  584. if (local_points[i] != pyramid_shape_points[i]) {
  585. all_equal = false;
  586. break;
  587. }
  588. }
  589. if (!all_equal) {
  590. PhysicsServer3D::get_singleton()->shape_set_data(pyramid_shape, local_points);
  591. pyramid_shape_points = local_points;
  592. }
  593. }
  594. return pyramid_shape;
  595. }
  596. Camera3D::Camera3D() {
  597. camera = RenderingServer::get_singleton()->camera_create();
  598. set_perspective(75.0, 0.05, 4000.0);
  599. RenderingServer::get_singleton()->camera_set_cull_mask(camera, layers);
  600. //active=false;
  601. velocity_tracker.instantiate();
  602. set_notify_transform(true);
  603. set_disable_scale(true);
  604. }
  605. Camera3D::~Camera3D() {
  606. RenderingServer::get_singleton()->free(camera);
  607. if (pyramid_shape.is_valid()) {
  608. PhysicsServer3D::get_singleton()->free(pyramid_shape);
  609. }
  610. }