camera_3d.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900
  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 "core/math/transform_interpolator.h"
  33. #include "scene/main/viewport.h"
  34. #include "servers/rendering/rendering_server_constants.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. if (!is_physics_interpolated_and_enabled()) {
  83. RenderingServer::get_singleton()->camera_set_transform(camera, get_camera_transform());
  84. } else {
  85. // Ideally we shouldn't be moving a physics interpolated camera within a frame,
  86. // because it will break smooth interpolation, but it may occur on e.g. level load.
  87. if (!Engine::get_singleton()->is_in_physics_frame() && camera.is_valid()) {
  88. _physics_interpolation_ensure_transform_calculated(true);
  89. RenderingServer::get_singleton()->camera_set_transform(camera, _interpolation_data.camera_xform_interpolated);
  90. }
  91. }
  92. if (is_part_of_edited_scene() || !is_current()) {
  93. return;
  94. }
  95. get_viewport()->_camera_3d_transform_changed_notify();
  96. }
  97. void Camera3D::_physics_interpolated_changed() {
  98. _update_process_mode();
  99. }
  100. void Camera3D::_physics_interpolation_ensure_data_flipped() {
  101. // The curr -> previous update can either occur
  102. // on the INTERNAL_PHYSICS_PROCESS OR
  103. // on NOTIFICATION_TRANSFORM_CHANGED,
  104. // if NOTIFICATION_TRANSFORM_CHANGED takes place
  105. // earlier than INTERNAL_PHYSICS_PROCESS on a tick.
  106. // This is to ensure that the data keeps flowing, but the new data
  107. // doesn't overwrite before prev has been set.
  108. // Keep the data flowing.
  109. uint64_t tick = Engine::get_singleton()->get_physics_frames();
  110. if (_interpolation_data.last_update_physics_tick != tick) {
  111. _interpolation_data.xform_prev = _interpolation_data.xform_curr;
  112. _interpolation_data.last_update_physics_tick = tick;
  113. physics_interpolation_flip_data();
  114. }
  115. }
  116. void Camera3D::_physics_interpolation_ensure_transform_calculated(bool p_force) const {
  117. DEV_CHECK_ONCE(!Engine::get_singleton()->is_in_physics_frame());
  118. InterpolationData &id = _interpolation_data;
  119. uint64_t frame = Engine::get_singleton()->get_frames_drawn();
  120. if (id.last_update_frame != frame || p_force) {
  121. id.last_update_frame = frame;
  122. TransformInterpolator::interpolate_transform_3d(id.xform_prev, id.xform_curr, id.xform_interpolated, Engine::get_singleton()->get_physics_interpolation_fraction());
  123. Transform3D &tr = id.camera_xform_interpolated;
  124. tr = _get_adjusted_camera_transform(id.xform_interpolated);
  125. }
  126. }
  127. void Camera3D::set_desired_process_modes(bool p_process_internal, bool p_physics_process_internal) {
  128. _desired_process_internal = p_process_internal;
  129. _desired_physics_process_internal = p_physics_process_internal;
  130. _update_process_mode();
  131. }
  132. void Camera3D::_update_process_mode() {
  133. bool process = _desired_process_internal;
  134. bool physics_process = _desired_physics_process_internal;
  135. if (is_physics_interpolated_and_enabled()) {
  136. if (is_current()) {
  137. process = true;
  138. physics_process = true;
  139. }
  140. }
  141. set_process_internal(process);
  142. set_physics_process_internal(physics_process);
  143. }
  144. void Camera3D::_notification(int p_what) {
  145. switch (p_what) {
  146. case NOTIFICATION_ENTER_WORLD: {
  147. // Needs to track the Viewport because it's needed on NOTIFICATION_EXIT_WORLD
  148. // and Spatial will handle it first, including clearing its reference to the Viewport,
  149. // therefore making it impossible to subclasses to access it
  150. viewport = get_viewport();
  151. ERR_FAIL_NULL(viewport);
  152. bool first_camera = viewport->_camera_3d_add(this);
  153. if (current || first_camera) {
  154. viewport->_camera_3d_set(this);
  155. }
  156. #ifdef TOOLS_ENABLED
  157. if (Engine::get_singleton()->is_editor_hint()) {
  158. viewport->connect(SNAME("size_changed"), callable_mp((Node3D *)this, &Camera3D::update_gizmos));
  159. }
  160. #endif
  161. } break;
  162. case NOTIFICATION_INTERNAL_PROCESS: {
  163. if (is_physics_interpolated_and_enabled() && camera.is_valid()) {
  164. _physics_interpolation_ensure_transform_calculated();
  165. #ifdef RENDERING_SERVER_DEBUG_PHYSICS_INTERPOLATION
  166. print_line("\t\tinterpolated Camera3D: " + rtos(_interpolation_data.xform_interpolated.origin.x) + "\t( prev " + rtos(_interpolation_data.xform_prev.origin.x) + ", curr " + rtos(_interpolation_data.xform_curr.origin.x) + " ) on tick " + itos(Engine::get_singleton()->get_physics_frames()));
  167. #endif
  168. RenderingServer::get_singleton()->camera_set_transform(camera, _interpolation_data.camera_xform_interpolated);
  169. }
  170. } break;
  171. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  172. if (is_physics_interpolated_and_enabled()) {
  173. _physics_interpolation_ensure_data_flipped();
  174. _interpolation_data.xform_curr = get_global_transform();
  175. }
  176. } break;
  177. case NOTIFICATION_TRANSFORM_CHANGED: {
  178. if (is_physics_interpolated_and_enabled()) {
  179. _physics_interpolation_ensure_data_flipped();
  180. _interpolation_data.xform_curr = get_global_transform();
  181. #if defined(DEBUG_ENABLED) && defined(TOOLS_ENABLED)
  182. if (!Engine::get_singleton()->is_in_physics_frame()) {
  183. PHYSICS_INTERPOLATION_NODE_WARNING(get_instance_id(), "Interpolated Camera3D triggered from outside physics process");
  184. }
  185. #endif
  186. }
  187. _request_camera_update();
  188. if (doppler_tracking != DOPPLER_TRACKING_DISABLED) {
  189. velocity_tracker->update_position(get_global_transform().origin);
  190. }
  191. // Allow auto-reset when first adding to the tree, as a convenience.
  192. if (_is_physics_interpolation_reset_requested() && is_inside_tree()) {
  193. _notification(NOTIFICATION_RESET_PHYSICS_INTERPOLATION);
  194. _set_physics_interpolation_reset_requested(false);
  195. }
  196. } break;
  197. case NOTIFICATION_RESET_PHYSICS_INTERPOLATION: {
  198. if (is_inside_tree()) {
  199. _interpolation_data.xform_curr = get_global_transform();
  200. _interpolation_data.xform_prev = _interpolation_data.xform_curr;
  201. }
  202. } break;
  203. case NOTIFICATION_SUSPENDED:
  204. case NOTIFICATION_PAUSED: {
  205. if (is_physics_interpolated_and_enabled() && is_inside_tree() && is_visible_in_tree()) {
  206. _physics_interpolation_ensure_transform_calculated(true);
  207. RenderingServer::get_singleton()->camera_set_transform(camera, _interpolation_data.camera_xform_interpolated);
  208. }
  209. } break;
  210. case NOTIFICATION_EXIT_WORLD: {
  211. if (!is_part_of_edited_scene()) {
  212. if (is_current()) {
  213. clear_current();
  214. current = true; //keep it true
  215. } else {
  216. current = false;
  217. }
  218. }
  219. if (viewport) {
  220. #ifdef TOOLS_ENABLED
  221. if (Engine::get_singleton()->is_editor_hint()) {
  222. viewport->disconnect(SNAME("size_changed"), callable_mp((Node3D *)this, &Camera3D::update_gizmos));
  223. }
  224. #endif
  225. viewport->_camera_3d_remove(this);
  226. viewport = nullptr;
  227. }
  228. } break;
  229. case NOTIFICATION_BECAME_CURRENT: {
  230. if (viewport) {
  231. viewport->find_world_3d()->_register_camera(this);
  232. }
  233. _update_process_mode();
  234. } break;
  235. case NOTIFICATION_LOST_CURRENT: {
  236. if (viewport) {
  237. viewport->find_world_3d()->_remove_camera(this);
  238. }
  239. _update_process_mode();
  240. } break;
  241. }
  242. }
  243. Transform3D Camera3D::_get_adjusted_camera_transform(const Transform3D &p_xform) const {
  244. Transform3D tr = p_xform.orthonormalized();
  245. tr.origin += tr.basis.get_column(1) * v_offset;
  246. tr.origin += tr.basis.get_column(0) * h_offset;
  247. return tr;
  248. }
  249. Transform3D Camera3D::get_camera_transform() const {
  250. if (is_physics_interpolated_and_enabled() && !Engine::get_singleton()->is_in_physics_frame()) {
  251. _physics_interpolation_ensure_transform_calculated();
  252. return _interpolation_data.camera_xform_interpolated;
  253. }
  254. return _get_adjusted_camera_transform(get_global_transform());
  255. }
  256. Projection Camera3D::_get_camera_projection(real_t p_near) const {
  257. Size2 viewport_size = get_viewport()->get_visible_rect().size;
  258. Projection cm;
  259. switch (mode) {
  260. case PROJECTION_PERSPECTIVE: {
  261. cm.set_perspective(fov, viewport_size.aspect(), p_near, _far, keep_aspect == KEEP_WIDTH);
  262. } break;
  263. case PROJECTION_ORTHOGONAL: {
  264. cm.set_orthogonal(size, viewport_size.aspect(), p_near, _far, keep_aspect == KEEP_WIDTH);
  265. } break;
  266. case PROJECTION_FRUSTUM: {
  267. cm.set_frustum(size, viewport_size.aspect(), frustum_offset, p_near, _far);
  268. } break;
  269. }
  270. return cm;
  271. }
  272. Projection Camera3D::get_camera_projection() const {
  273. ERR_FAIL_COND_V_MSG(!is_inside_tree(), Projection(), "Camera is not inside the scene tree.");
  274. return _get_camera_projection(_near);
  275. }
  276. void Camera3D::set_perspective(real_t p_fovy_degrees, real_t p_z_near, real_t p_z_far) {
  277. if (!force_change && fov == p_fovy_degrees && p_z_near == _near && p_z_far == _far && mode == PROJECTION_PERSPECTIVE) {
  278. return;
  279. }
  280. fov = p_fovy_degrees;
  281. _near = p_z_near;
  282. _far = p_z_far;
  283. mode = PROJECTION_PERSPECTIVE;
  284. RenderingServer::get_singleton()->camera_set_perspective(camera, fov, _near, _far);
  285. update_gizmos();
  286. force_change = false;
  287. }
  288. void Camera3D::set_orthogonal(real_t p_size, real_t p_z_near, real_t p_z_far) {
  289. if (!force_change && size == p_size && p_z_near == _near && p_z_far == _far && mode == PROJECTION_ORTHOGONAL) {
  290. return;
  291. }
  292. size = p_size;
  293. _near = p_z_near;
  294. _far = p_z_far;
  295. mode = PROJECTION_ORTHOGONAL;
  296. force_change = false;
  297. RenderingServer::get_singleton()->camera_set_orthogonal(camera, size, _near, _far);
  298. update_gizmos();
  299. }
  300. void Camera3D::set_frustum(real_t p_size, Vector2 p_offset, real_t p_z_near, real_t p_z_far) {
  301. if (!force_change && size == p_size && frustum_offset == p_offset && p_z_near == _near && p_z_far == _far && mode == PROJECTION_FRUSTUM) {
  302. return;
  303. }
  304. size = p_size;
  305. frustum_offset = p_offset;
  306. _near = p_z_near;
  307. _far = p_z_far;
  308. mode = PROJECTION_FRUSTUM;
  309. force_change = false;
  310. RenderingServer::get_singleton()->camera_set_frustum(camera, size, frustum_offset, _near, _far);
  311. update_gizmos();
  312. }
  313. void Camera3D::set_projection(ProjectionType p_mode) {
  314. if (p_mode == PROJECTION_PERSPECTIVE || p_mode == PROJECTION_ORTHOGONAL || p_mode == PROJECTION_FRUSTUM) {
  315. mode = p_mode;
  316. _update_camera_mode();
  317. notify_property_list_changed();
  318. }
  319. }
  320. RID Camera3D::get_camera() const {
  321. return camera;
  322. }
  323. void Camera3D::make_current() {
  324. current = true;
  325. if (!is_inside_tree()) {
  326. return;
  327. }
  328. get_viewport()->_camera_3d_set(this);
  329. }
  330. void Camera3D::clear_current(bool p_enable_next) {
  331. current = false;
  332. if (!is_inside_tree()) {
  333. return;
  334. }
  335. if (get_viewport()->get_camera_3d() == this) {
  336. get_viewport()->_camera_3d_set(nullptr);
  337. if (p_enable_next && !Engine::get_singleton()->is_editor_hint()) {
  338. get_viewport()->_camera_3d_make_next_current(this);
  339. }
  340. }
  341. }
  342. void Camera3D::set_current(bool p_enabled) {
  343. if (p_enabled) {
  344. make_current();
  345. } else {
  346. clear_current();
  347. }
  348. }
  349. bool Camera3D::is_current() const {
  350. if (is_inside_tree() && !is_part_of_edited_scene()) {
  351. return get_viewport()->get_camera_3d() == this;
  352. } else {
  353. return current;
  354. }
  355. }
  356. Vector3 Camera3D::project_ray_normal(const Point2 &p_pos) const {
  357. Vector3 ray = project_local_ray_normal(p_pos);
  358. return get_camera_transform().basis.xform(ray).normalized();
  359. }
  360. Vector3 Camera3D::project_local_ray_normal(const Point2 &p_pos) const {
  361. ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector3(), "Camera is not inside scene.");
  362. Size2 viewport_size = get_viewport()->get_camera_rect_size();
  363. Vector2 cpos = get_viewport()->get_camera_coords(p_pos);
  364. Vector3 ray;
  365. if (mode == PROJECTION_ORTHOGONAL) {
  366. ray = Vector3(0, 0, -1);
  367. } else {
  368. Projection cm = _get_camera_projection(_near);
  369. Vector2 screen_he = cm.get_viewport_half_extents();
  370. 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();
  371. }
  372. return ray;
  373. }
  374. Vector3 Camera3D::project_ray_origin(const Point2 &p_pos) const {
  375. ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector3(), "Camera is not inside scene.");
  376. Size2 viewport_size = get_viewport()->get_camera_rect_size();
  377. Vector2 cpos = get_viewport()->get_camera_coords(p_pos);
  378. ERR_FAIL_COND_V(viewport_size.y == 0, Vector3());
  379. if (mode == PROJECTION_ORTHOGONAL) {
  380. Vector2 pos = cpos / viewport_size;
  381. real_t vsize, hsize;
  382. if (keep_aspect == KEEP_WIDTH) {
  383. vsize = size / viewport_size.aspect();
  384. hsize = size;
  385. } else {
  386. hsize = size * viewport_size.aspect();
  387. vsize = size;
  388. }
  389. Vector3 ray;
  390. ray.x = pos.x * (hsize)-hsize / 2;
  391. ray.y = (1.0 - pos.y) * (vsize)-vsize / 2;
  392. ray.z = -_near;
  393. ray = get_camera_transform().xform(ray);
  394. return ray;
  395. } else {
  396. return get_camera_transform().origin;
  397. };
  398. }
  399. bool Camera3D::is_position_behind(const Vector3 &p_pos) const {
  400. Transform3D t = get_global_transform();
  401. Vector3 eyedir = -t.basis.get_column(2).normalized();
  402. return eyedir.dot(p_pos - t.origin) < _near;
  403. }
  404. Vector<Vector3> Camera3D::get_near_plane_points() const {
  405. ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector<Vector3>(), "Camera is not inside scene.");
  406. Projection cm = _get_camera_projection(_near);
  407. Vector3 endpoints[8];
  408. cm.get_endpoints(Transform3D(), endpoints);
  409. Vector<Vector3> points = {
  410. Vector3(),
  411. endpoints[4],
  412. endpoints[5],
  413. endpoints[6],
  414. endpoints[7]
  415. };
  416. return points;
  417. }
  418. Point2 Camera3D::unproject_position(const Vector3 &p_pos) const {
  419. ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector2(), "Camera is not inside scene.");
  420. Size2 viewport_size = get_viewport()->get_visible_rect().size;
  421. Projection cm = _get_camera_projection(_near);
  422. Plane p(get_camera_transform().xform_inv(p_pos), 1.0);
  423. p = cm.xform4(p);
  424. // Prevent divide by zero.
  425. // TODO: Investigate, this was causing NaNs.
  426. ERR_FAIL_COND_V(p.d == 0, Point2());
  427. p.normal /= p.d;
  428. Point2 res;
  429. res.x = (p.normal.x * 0.5 + 0.5) * viewport_size.x;
  430. res.y = (-p.normal.y * 0.5 + 0.5) * viewport_size.y;
  431. return res;
  432. }
  433. Vector3 Camera3D::project_position(const Point2 &p_point, real_t p_z_depth) const {
  434. ERR_FAIL_COND_V_MSG(!is_inside_tree(), Vector3(), "Camera is not inside scene.");
  435. if (p_z_depth == 0 && mode != PROJECTION_ORTHOGONAL) {
  436. return get_global_transform().origin;
  437. }
  438. Size2 viewport_size = get_viewport()->get_visible_rect().size;
  439. Projection cm = _get_camera_projection(p_z_depth);
  440. Vector2 vp_he = cm.get_viewport_half_extents();
  441. Vector2 point;
  442. point.x = (p_point.x / viewport_size.x) * 2.0 - 1.0;
  443. point.y = (1.0 - (p_point.y / viewport_size.y)) * 2.0 - 1.0;
  444. point *= vp_he;
  445. Vector3 p(point.x, point.y, -p_z_depth);
  446. return get_camera_transform().xform(p);
  447. }
  448. void Camera3D::set_environment(const Ref<Environment> &p_environment) {
  449. environment = p_environment;
  450. if (environment.is_valid()) {
  451. RS::get_singleton()->camera_set_environment(camera, environment->get_rid());
  452. } else {
  453. RS::get_singleton()->camera_set_environment(camera, RID());
  454. }
  455. _update_camera_mode();
  456. }
  457. Ref<Environment> Camera3D::get_environment() const {
  458. return environment;
  459. }
  460. void Camera3D::set_attributes(const Ref<CameraAttributes> &p_attributes) {
  461. if (attributes.is_valid()) {
  462. CameraAttributesPhysical *physical_attributes = Object::cast_to<CameraAttributesPhysical>(attributes.ptr());
  463. if (physical_attributes) {
  464. attributes->disconnect_changed(callable_mp(this, &Camera3D::_attributes_changed));
  465. }
  466. }
  467. attributes = p_attributes;
  468. if (attributes.is_valid()) {
  469. CameraAttributesPhysical *physical_attributes = Object::cast_to<CameraAttributesPhysical>(attributes.ptr());
  470. if (physical_attributes) {
  471. attributes->connect_changed(callable_mp(this, &Camera3D::_attributes_changed));
  472. _attributes_changed();
  473. }
  474. RS::get_singleton()->camera_set_camera_attributes(camera, attributes->get_rid());
  475. } else {
  476. RS::get_singleton()->camera_set_camera_attributes(camera, RID());
  477. }
  478. notify_property_list_changed();
  479. }
  480. Ref<CameraAttributes> Camera3D::get_attributes() const {
  481. return attributes;
  482. }
  483. void Camera3D::_attributes_changed() {
  484. CameraAttributesPhysical *physical_attributes = Object::cast_to<CameraAttributesPhysical>(attributes.ptr());
  485. ERR_FAIL_NULL(physical_attributes);
  486. fov = physical_attributes->get_fov();
  487. _near = physical_attributes->get_near();
  488. _far = physical_attributes->get_far();
  489. keep_aspect = KEEP_HEIGHT;
  490. _update_camera_mode();
  491. }
  492. void Camera3D::set_compositor(const Ref<Compositor> &p_compositor) {
  493. compositor = p_compositor;
  494. if (compositor.is_valid()) {
  495. RS::get_singleton()->camera_set_compositor(camera, compositor->get_rid());
  496. } else {
  497. RS::get_singleton()->camera_set_compositor(camera, RID());
  498. }
  499. _update_camera_mode();
  500. }
  501. Ref<Compositor> Camera3D::get_compositor() const {
  502. return compositor;
  503. }
  504. void Camera3D::set_keep_aspect_mode(KeepAspect p_aspect) {
  505. keep_aspect = p_aspect;
  506. RenderingServer::get_singleton()->camera_set_use_vertical_aspect(camera, p_aspect == KEEP_WIDTH);
  507. _update_camera_mode();
  508. notify_property_list_changed();
  509. }
  510. Camera3D::KeepAspect Camera3D::get_keep_aspect_mode() const {
  511. return keep_aspect;
  512. }
  513. void Camera3D::set_doppler_tracking(DopplerTracking p_tracking) {
  514. if (doppler_tracking == p_tracking) {
  515. return;
  516. }
  517. doppler_tracking = p_tracking;
  518. if (p_tracking != DOPPLER_TRACKING_DISABLED) {
  519. velocity_tracker->set_track_physics_step(doppler_tracking == DOPPLER_TRACKING_PHYSICS_STEP);
  520. if (is_inside_tree()) {
  521. velocity_tracker->reset(get_global_transform().origin);
  522. }
  523. }
  524. _update_camera_mode();
  525. }
  526. Camera3D::DopplerTracking Camera3D::get_doppler_tracking() const {
  527. return doppler_tracking;
  528. }
  529. void Camera3D::_bind_methods() {
  530. ClassDB::bind_method(D_METHOD("project_ray_normal", "screen_point"), &Camera3D::project_ray_normal);
  531. ClassDB::bind_method(D_METHOD("project_local_ray_normal", "screen_point"), &Camera3D::project_local_ray_normal);
  532. ClassDB::bind_method(D_METHOD("project_ray_origin", "screen_point"), &Camera3D::project_ray_origin);
  533. ClassDB::bind_method(D_METHOD("unproject_position", "world_point"), &Camera3D::unproject_position);
  534. ClassDB::bind_method(D_METHOD("is_position_behind", "world_point"), &Camera3D::is_position_behind);
  535. ClassDB::bind_method(D_METHOD("project_position", "screen_point", "z_depth"), &Camera3D::project_position);
  536. ClassDB::bind_method(D_METHOD("set_perspective", "fov", "z_near", "z_far"), &Camera3D::set_perspective);
  537. ClassDB::bind_method(D_METHOD("set_orthogonal", "size", "z_near", "z_far"), &Camera3D::set_orthogonal);
  538. ClassDB::bind_method(D_METHOD("set_frustum", "size", "offset", "z_near", "z_far"), &Camera3D::set_frustum);
  539. ClassDB::bind_method(D_METHOD("make_current"), &Camera3D::make_current);
  540. ClassDB::bind_method(D_METHOD("clear_current", "enable_next"), &Camera3D::clear_current, DEFVAL(true));
  541. ClassDB::bind_method(D_METHOD("set_current", "enabled"), &Camera3D::set_current);
  542. ClassDB::bind_method(D_METHOD("is_current"), &Camera3D::is_current);
  543. ClassDB::bind_method(D_METHOD("get_camera_transform"), &Camera3D::get_camera_transform);
  544. ClassDB::bind_method(D_METHOD("get_camera_projection"), &Camera3D::get_camera_projection);
  545. ClassDB::bind_method(D_METHOD("get_fov"), &Camera3D::get_fov);
  546. ClassDB::bind_method(D_METHOD("get_frustum_offset"), &Camera3D::get_frustum_offset);
  547. ClassDB::bind_method(D_METHOD("get_size"), &Camera3D::get_size);
  548. ClassDB::bind_method(D_METHOD("get_far"), &Camera3D::get_far);
  549. ClassDB::bind_method(D_METHOD("get_near"), &Camera3D::get_near);
  550. ClassDB::bind_method(D_METHOD("set_fov", "fov"), &Camera3D::set_fov);
  551. ClassDB::bind_method(D_METHOD("set_frustum_offset", "offset"), &Camera3D::set_frustum_offset);
  552. ClassDB::bind_method(D_METHOD("set_size", "size"), &Camera3D::set_size);
  553. ClassDB::bind_method(D_METHOD("set_far", "far"), &Camera3D::set_far);
  554. ClassDB::bind_method(D_METHOD("set_near", "near"), &Camera3D::set_near);
  555. ClassDB::bind_method(D_METHOD("get_projection"), &Camera3D::get_projection);
  556. ClassDB::bind_method(D_METHOD("set_projection", "mode"), &Camera3D::set_projection);
  557. ClassDB::bind_method(D_METHOD("set_h_offset", "offset"), &Camera3D::set_h_offset);
  558. ClassDB::bind_method(D_METHOD("get_h_offset"), &Camera3D::get_h_offset);
  559. ClassDB::bind_method(D_METHOD("set_v_offset", "offset"), &Camera3D::set_v_offset);
  560. ClassDB::bind_method(D_METHOD("get_v_offset"), &Camera3D::get_v_offset);
  561. ClassDB::bind_method(D_METHOD("set_cull_mask", "mask"), &Camera3D::set_cull_mask);
  562. ClassDB::bind_method(D_METHOD("get_cull_mask"), &Camera3D::get_cull_mask);
  563. ClassDB::bind_method(D_METHOD("set_environment", "env"), &Camera3D::set_environment);
  564. ClassDB::bind_method(D_METHOD("get_environment"), &Camera3D::get_environment);
  565. ClassDB::bind_method(D_METHOD("set_attributes", "env"), &Camera3D::set_attributes);
  566. ClassDB::bind_method(D_METHOD("get_attributes"), &Camera3D::get_attributes);
  567. ClassDB::bind_method(D_METHOD("set_compositor", "compositor"), &Camera3D::set_compositor);
  568. ClassDB::bind_method(D_METHOD("get_compositor"), &Camera3D::get_compositor);
  569. ClassDB::bind_method(D_METHOD("set_keep_aspect_mode", "mode"), &Camera3D::set_keep_aspect_mode);
  570. ClassDB::bind_method(D_METHOD("get_keep_aspect_mode"), &Camera3D::get_keep_aspect_mode);
  571. ClassDB::bind_method(D_METHOD("set_doppler_tracking", "mode"), &Camera3D::set_doppler_tracking);
  572. ClassDB::bind_method(D_METHOD("get_doppler_tracking"), &Camera3D::get_doppler_tracking);
  573. ClassDB::bind_method(D_METHOD("get_frustum"), &Camera3D::_get_frustum);
  574. ClassDB::bind_method(D_METHOD("is_position_in_frustum", "world_point"), &Camera3D::is_position_in_frustum);
  575. ClassDB::bind_method(D_METHOD("get_camera_rid"), &Camera3D::get_camera);
  576. ClassDB::bind_method(D_METHOD("get_pyramid_shape_rid"), &Camera3D::get_pyramid_shape_rid);
  577. ClassDB::bind_method(D_METHOD("set_cull_mask_value", "layer_number", "value"), &Camera3D::set_cull_mask_value);
  578. ClassDB::bind_method(D_METHOD("get_cull_mask_value", "layer_number"), &Camera3D::get_cull_mask_value);
  579. //ClassDB::bind_method(D_METHOD("_camera_make_current"),&Camera::_camera_make_current );
  580. ADD_PROPERTY(PropertyInfo(Variant::INT, "keep_aspect", PROPERTY_HINT_ENUM, "Keep Width,Keep Height"), "set_keep_aspect_mode", "get_keep_aspect_mode");
  581. ADD_PROPERTY(PropertyInfo(Variant::INT, "cull_mask", PROPERTY_HINT_LAYERS_3D_RENDER), "set_cull_mask", "get_cull_mask");
  582. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "environment", PROPERTY_HINT_RESOURCE_TYPE, "Environment"), "set_environment", "get_environment");
  583. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "attributes", PROPERTY_HINT_RESOURCE_TYPE, "CameraAttributesPractical,CameraAttributesPhysical"), "set_attributes", "get_attributes");
  584. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "compositor", PROPERTY_HINT_RESOURCE_TYPE, "Compositor"), "set_compositor", "get_compositor");
  585. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "h_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_h_offset", "get_h_offset");
  586. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "v_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_v_offset", "get_v_offset");
  587. ADD_PROPERTY(PropertyInfo(Variant::INT, "doppler_tracking", PROPERTY_HINT_ENUM, "Disabled,Idle,Physics"), "set_doppler_tracking", "get_doppler_tracking");
  588. ADD_PROPERTY(PropertyInfo(Variant::INT, "projection", PROPERTY_HINT_ENUM, "Perspective,Orthogonal,Frustum"), "set_projection", "get_projection");
  589. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "current"), "set_current", "is_current");
  590. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fov", PROPERTY_HINT_RANGE, "1,179,0.1,degrees"), "set_fov", "get_fov");
  591. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "size", PROPERTY_HINT_RANGE, "0.001,100,0.001,or_greater,suffix:m"), "set_size", "get_size");
  592. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "frustum_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_frustum_offset", "get_frustum_offset");
  593. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "near", PROPERTY_HINT_RANGE, "0.001,10,0.001,or_greater,exp,suffix:m"), "set_near", "get_near");
  594. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "far", PROPERTY_HINT_RANGE, "0.01,4000,0.01,or_greater,exp,suffix:m"), "set_far", "get_far");
  595. BIND_ENUM_CONSTANT(PROJECTION_PERSPECTIVE);
  596. BIND_ENUM_CONSTANT(PROJECTION_ORTHOGONAL);
  597. BIND_ENUM_CONSTANT(PROJECTION_FRUSTUM);
  598. BIND_ENUM_CONSTANT(KEEP_WIDTH);
  599. BIND_ENUM_CONSTANT(KEEP_HEIGHT);
  600. BIND_ENUM_CONSTANT(DOPPLER_TRACKING_DISABLED);
  601. BIND_ENUM_CONSTANT(DOPPLER_TRACKING_IDLE_STEP);
  602. BIND_ENUM_CONSTANT(DOPPLER_TRACKING_PHYSICS_STEP);
  603. }
  604. real_t Camera3D::get_fov() const {
  605. return fov;
  606. }
  607. real_t Camera3D::get_size() const {
  608. return size;
  609. }
  610. real_t Camera3D::get_near() const {
  611. return _near;
  612. }
  613. Vector2 Camera3D::get_frustum_offset() const {
  614. return frustum_offset;
  615. }
  616. real_t Camera3D::get_far() const {
  617. return _far;
  618. }
  619. Camera3D::ProjectionType Camera3D::get_projection() const {
  620. return mode;
  621. }
  622. void Camera3D::set_fov(real_t p_fov) {
  623. ERR_FAIL_COND(p_fov < 1 || p_fov > 179);
  624. fov = p_fov;
  625. _update_camera_mode();
  626. }
  627. void Camera3D::set_size(real_t p_size) {
  628. ERR_FAIL_COND(p_size <= CMP_EPSILON);
  629. size = p_size;
  630. _update_camera_mode();
  631. }
  632. void Camera3D::set_near(real_t p_near) {
  633. _near = p_near;
  634. _update_camera_mode();
  635. }
  636. void Camera3D::set_frustum_offset(Vector2 p_offset) {
  637. frustum_offset = p_offset;
  638. _update_camera_mode();
  639. }
  640. void Camera3D::set_far(real_t p_far) {
  641. _far = p_far;
  642. _update_camera_mode();
  643. }
  644. void Camera3D::set_cull_mask(uint32_t p_layers) {
  645. layers = p_layers;
  646. RenderingServer::get_singleton()->camera_set_cull_mask(camera, layers);
  647. _update_camera_mode();
  648. }
  649. uint32_t Camera3D::get_cull_mask() const {
  650. return layers;
  651. }
  652. void Camera3D::set_cull_mask_value(int p_layer_number, bool p_value) {
  653. ERR_FAIL_COND_MSG(p_layer_number < 1, "Render layer number must be between 1 and 20 inclusive.");
  654. ERR_FAIL_COND_MSG(p_layer_number > 20, "Render layer number must be between 1 and 20 inclusive.");
  655. uint32_t mask = get_cull_mask();
  656. if (p_value) {
  657. mask |= 1 << (p_layer_number - 1);
  658. } else {
  659. mask &= ~(1 << (p_layer_number - 1));
  660. }
  661. set_cull_mask(mask);
  662. }
  663. bool Camera3D::get_cull_mask_value(int p_layer_number) const {
  664. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Render layer number must be between 1 and 20 inclusive.");
  665. ERR_FAIL_COND_V_MSG(p_layer_number > 20, false, "Render layer number must be between 1 and 20 inclusive.");
  666. return layers & (1 << (p_layer_number - 1));
  667. }
  668. Vector<Plane> Camera3D::get_frustum() const {
  669. ERR_FAIL_COND_V(!is_inside_world(), Vector<Plane>());
  670. Projection cm = _get_camera_projection(_near);
  671. return cm.get_projection_planes(get_camera_transform());
  672. }
  673. TypedArray<Plane> Camera3D::_get_frustum() const {
  674. Variant ret = get_frustum();
  675. return ret;
  676. }
  677. bool Camera3D::is_position_in_frustum(const Vector3 &p_position) const {
  678. Vector<Plane> frustum = get_frustum();
  679. for (int i = 0; i < frustum.size(); i++) {
  680. if (frustum[i].is_point_over(p_position)) {
  681. return false;
  682. }
  683. }
  684. return true;
  685. }
  686. void Camera3D::set_v_offset(real_t p_offset) {
  687. v_offset = p_offset;
  688. _update_camera();
  689. }
  690. real_t Camera3D::get_v_offset() const {
  691. return v_offset;
  692. }
  693. void Camera3D::set_h_offset(real_t p_offset) {
  694. h_offset = p_offset;
  695. _update_camera();
  696. }
  697. real_t Camera3D::get_h_offset() const {
  698. return h_offset;
  699. }
  700. Vector3 Camera3D::get_doppler_tracked_velocity() const {
  701. if (doppler_tracking != DOPPLER_TRACKING_DISABLED) {
  702. return velocity_tracker->get_tracked_linear_velocity();
  703. } else {
  704. return Vector3();
  705. }
  706. }
  707. RID Camera3D::get_pyramid_shape_rid() {
  708. ERR_FAIL_COND_V_MSG(!is_inside_tree(), RID(), "Camera is not inside scene.");
  709. if (pyramid_shape == RID()) {
  710. pyramid_shape_points = get_near_plane_points();
  711. pyramid_shape = PhysicsServer3D::get_singleton()->convex_polygon_shape_create();
  712. PhysicsServer3D::get_singleton()->shape_set_data(pyramid_shape, pyramid_shape_points);
  713. } else { //check if points changed
  714. Vector<Vector3> local_points = get_near_plane_points();
  715. bool all_equal = true;
  716. for (int i = 0; i < 5; i++) {
  717. if (local_points[i] != pyramid_shape_points[i]) {
  718. all_equal = false;
  719. break;
  720. }
  721. }
  722. if (!all_equal) {
  723. PhysicsServer3D::get_singleton()->shape_set_data(pyramid_shape, local_points);
  724. pyramid_shape_points = local_points;
  725. }
  726. }
  727. return pyramid_shape;
  728. }
  729. Camera3D::Camera3D() {
  730. camera = RenderingServer::get_singleton()->camera_create();
  731. set_perspective(75.0, 0.05, 4000.0);
  732. RenderingServer::get_singleton()->camera_set_cull_mask(camera, layers);
  733. //active=false;
  734. velocity_tracker.instantiate();
  735. set_notify_transform(true);
  736. set_disable_scale(true);
  737. }
  738. Camera3D::~Camera3D() {
  739. ERR_FAIL_NULL(RenderingServer::get_singleton());
  740. RenderingServer::get_singleton()->free(camera);
  741. if (pyramid_shape.is_valid()) {
  742. ERR_FAIL_NULL(PhysicsServer3D::get_singleton());
  743. PhysicsServer3D::get_singleton()->free(pyramid_shape);
  744. }
  745. }