camera_3d.cpp 26 KB

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