camera_3d.cpp 25 KB

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