camera_3d.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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(_near);
  440. Plane z_slice(Vector3(0, 0, 1), -p_z_depth);
  441. Vector3 res;
  442. z_slice.intersect_3(cm.get_projection_plane(Projection::Planes::PLANE_RIGHT), cm.get_projection_plane(Projection::Planes::PLANE_TOP), &res);
  443. Vector2 vp_he(res.x, res.y);
  444. Vector2 point;
  445. point.x = (p_point.x / viewport_size.x) * 2.0 - 1.0;
  446. point.y = (1.0 - (p_point.y / viewport_size.y)) * 2.0 - 1.0;
  447. point *= vp_he;
  448. Vector3 p(point.x, point.y, -p_z_depth);
  449. return get_camera_transform().xform(p);
  450. }
  451. void Camera3D::set_environment(const Ref<Environment> &p_environment) {
  452. environment = p_environment;
  453. if (environment.is_valid()) {
  454. RS::get_singleton()->camera_set_environment(camera, environment->get_rid());
  455. } else {
  456. RS::get_singleton()->camera_set_environment(camera, RID());
  457. }
  458. _update_camera_mode();
  459. }
  460. Ref<Environment> Camera3D::get_environment() const {
  461. return environment;
  462. }
  463. void Camera3D::set_attributes(const Ref<CameraAttributes> &p_attributes) {
  464. if (attributes.is_valid()) {
  465. CameraAttributesPhysical *physical_attributes = Object::cast_to<CameraAttributesPhysical>(attributes.ptr());
  466. if (physical_attributes) {
  467. attributes->disconnect_changed(callable_mp(this, &Camera3D::_attributes_changed));
  468. }
  469. }
  470. attributes = p_attributes;
  471. if (attributes.is_valid()) {
  472. CameraAttributesPhysical *physical_attributes = Object::cast_to<CameraAttributesPhysical>(attributes.ptr());
  473. if (physical_attributes) {
  474. attributes->connect_changed(callable_mp(this, &Camera3D::_attributes_changed));
  475. _attributes_changed();
  476. }
  477. RS::get_singleton()->camera_set_camera_attributes(camera, attributes->get_rid());
  478. } else {
  479. RS::get_singleton()->camera_set_camera_attributes(camera, RID());
  480. }
  481. notify_property_list_changed();
  482. }
  483. Ref<CameraAttributes> Camera3D::get_attributes() const {
  484. return attributes;
  485. }
  486. void Camera3D::_attributes_changed() {
  487. CameraAttributesPhysical *physical_attributes = Object::cast_to<CameraAttributesPhysical>(attributes.ptr());
  488. ERR_FAIL_NULL(physical_attributes);
  489. fov = physical_attributes->get_fov();
  490. _near = physical_attributes->get_near();
  491. _far = physical_attributes->get_far();
  492. keep_aspect = KEEP_HEIGHT;
  493. _update_camera_mode();
  494. }
  495. void Camera3D::set_compositor(const Ref<Compositor> &p_compositor) {
  496. compositor = p_compositor;
  497. if (compositor.is_valid()) {
  498. RS::get_singleton()->camera_set_compositor(camera, compositor->get_rid());
  499. } else {
  500. RS::get_singleton()->camera_set_compositor(camera, RID());
  501. }
  502. _update_camera_mode();
  503. }
  504. Ref<Compositor> Camera3D::get_compositor() const {
  505. return compositor;
  506. }
  507. void Camera3D::set_keep_aspect_mode(KeepAspect p_aspect) {
  508. keep_aspect = p_aspect;
  509. RenderingServer::get_singleton()->camera_set_use_vertical_aspect(camera, p_aspect == KEEP_WIDTH);
  510. _update_camera_mode();
  511. notify_property_list_changed();
  512. }
  513. Camera3D::KeepAspect Camera3D::get_keep_aspect_mode() const {
  514. return keep_aspect;
  515. }
  516. void Camera3D::set_doppler_tracking(DopplerTracking p_tracking) {
  517. if (doppler_tracking == p_tracking) {
  518. return;
  519. }
  520. doppler_tracking = p_tracking;
  521. if (p_tracking != DOPPLER_TRACKING_DISABLED) {
  522. velocity_tracker->set_track_physics_step(doppler_tracking == DOPPLER_TRACKING_PHYSICS_STEP);
  523. if (is_inside_tree()) {
  524. velocity_tracker->reset(get_global_transform().origin);
  525. }
  526. }
  527. _update_camera_mode();
  528. }
  529. Camera3D::DopplerTracking Camera3D::get_doppler_tracking() const {
  530. return doppler_tracking;
  531. }
  532. void Camera3D::_bind_methods() {
  533. ClassDB::bind_method(D_METHOD("project_ray_normal", "screen_point"), &Camera3D::project_ray_normal);
  534. ClassDB::bind_method(D_METHOD("project_local_ray_normal", "screen_point"), &Camera3D::project_local_ray_normal);
  535. ClassDB::bind_method(D_METHOD("project_ray_origin", "screen_point"), &Camera3D::project_ray_origin);
  536. ClassDB::bind_method(D_METHOD("unproject_position", "world_point"), &Camera3D::unproject_position);
  537. ClassDB::bind_method(D_METHOD("is_position_behind", "world_point"), &Camera3D::is_position_behind);
  538. ClassDB::bind_method(D_METHOD("project_position", "screen_point", "z_depth"), &Camera3D::project_position);
  539. ClassDB::bind_method(D_METHOD("set_perspective", "fov", "z_near", "z_far"), &Camera3D::set_perspective);
  540. ClassDB::bind_method(D_METHOD("set_orthogonal", "size", "z_near", "z_far"), &Camera3D::set_orthogonal);
  541. ClassDB::bind_method(D_METHOD("set_frustum", "size", "offset", "z_near", "z_far"), &Camera3D::set_frustum);
  542. ClassDB::bind_method(D_METHOD("make_current"), &Camera3D::make_current);
  543. ClassDB::bind_method(D_METHOD("clear_current", "enable_next"), &Camera3D::clear_current, DEFVAL(true));
  544. ClassDB::bind_method(D_METHOD("set_current", "enabled"), &Camera3D::set_current);
  545. ClassDB::bind_method(D_METHOD("is_current"), &Camera3D::is_current);
  546. ClassDB::bind_method(D_METHOD("get_camera_transform"), &Camera3D::get_camera_transform);
  547. ClassDB::bind_method(D_METHOD("get_camera_projection"), &Camera3D::get_camera_projection);
  548. ClassDB::bind_method(D_METHOD("get_fov"), &Camera3D::get_fov);
  549. ClassDB::bind_method(D_METHOD("get_frustum_offset"), &Camera3D::get_frustum_offset);
  550. ClassDB::bind_method(D_METHOD("get_size"), &Camera3D::get_size);
  551. ClassDB::bind_method(D_METHOD("get_far"), &Camera3D::get_far);
  552. ClassDB::bind_method(D_METHOD("get_near"), &Camera3D::get_near);
  553. ClassDB::bind_method(D_METHOD("set_fov", "fov"), &Camera3D::set_fov);
  554. ClassDB::bind_method(D_METHOD("set_frustum_offset", "offset"), &Camera3D::set_frustum_offset);
  555. ClassDB::bind_method(D_METHOD("set_size", "size"), &Camera3D::set_size);
  556. ClassDB::bind_method(D_METHOD("set_far", "far"), &Camera3D::set_far);
  557. ClassDB::bind_method(D_METHOD("set_near", "near"), &Camera3D::set_near);
  558. ClassDB::bind_method(D_METHOD("get_projection"), &Camera3D::get_projection);
  559. ClassDB::bind_method(D_METHOD("set_projection", "mode"), &Camera3D::set_projection);
  560. ClassDB::bind_method(D_METHOD("set_h_offset", "offset"), &Camera3D::set_h_offset);
  561. ClassDB::bind_method(D_METHOD("get_h_offset"), &Camera3D::get_h_offset);
  562. ClassDB::bind_method(D_METHOD("set_v_offset", "offset"), &Camera3D::set_v_offset);
  563. ClassDB::bind_method(D_METHOD("get_v_offset"), &Camera3D::get_v_offset);
  564. ClassDB::bind_method(D_METHOD("set_cull_mask", "mask"), &Camera3D::set_cull_mask);
  565. ClassDB::bind_method(D_METHOD("get_cull_mask"), &Camera3D::get_cull_mask);
  566. ClassDB::bind_method(D_METHOD("set_environment", "env"), &Camera3D::set_environment);
  567. ClassDB::bind_method(D_METHOD("get_environment"), &Camera3D::get_environment);
  568. ClassDB::bind_method(D_METHOD("set_attributes", "env"), &Camera3D::set_attributes);
  569. ClassDB::bind_method(D_METHOD("get_attributes"), &Camera3D::get_attributes);
  570. ClassDB::bind_method(D_METHOD("set_compositor", "compositor"), &Camera3D::set_compositor);
  571. ClassDB::bind_method(D_METHOD("get_compositor"), &Camera3D::get_compositor);
  572. ClassDB::bind_method(D_METHOD("set_keep_aspect_mode", "mode"), &Camera3D::set_keep_aspect_mode);
  573. ClassDB::bind_method(D_METHOD("get_keep_aspect_mode"), &Camera3D::get_keep_aspect_mode);
  574. ClassDB::bind_method(D_METHOD("set_doppler_tracking", "mode"), &Camera3D::set_doppler_tracking);
  575. ClassDB::bind_method(D_METHOD("get_doppler_tracking"), &Camera3D::get_doppler_tracking);
  576. ClassDB::bind_method(D_METHOD("get_frustum"), &Camera3D::_get_frustum);
  577. ClassDB::bind_method(D_METHOD("is_position_in_frustum", "world_point"), &Camera3D::is_position_in_frustum);
  578. ClassDB::bind_method(D_METHOD("get_camera_rid"), &Camera3D::get_camera);
  579. ClassDB::bind_method(D_METHOD("get_pyramid_shape_rid"), &Camera3D::get_pyramid_shape_rid);
  580. ClassDB::bind_method(D_METHOD("set_cull_mask_value", "layer_number", "value"), &Camera3D::set_cull_mask_value);
  581. ClassDB::bind_method(D_METHOD("get_cull_mask_value", "layer_number"), &Camera3D::get_cull_mask_value);
  582. //ClassDB::bind_method(D_METHOD("_camera_make_current"),&Camera::_camera_make_current );
  583. ADD_PROPERTY(PropertyInfo(Variant::INT, "keep_aspect", PROPERTY_HINT_ENUM, "Keep Width,Keep Height"), "set_keep_aspect_mode", "get_keep_aspect_mode");
  584. ADD_PROPERTY(PropertyInfo(Variant::INT, "cull_mask", PROPERTY_HINT_LAYERS_3D_RENDER), "set_cull_mask", "get_cull_mask");
  585. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "environment", PROPERTY_HINT_RESOURCE_TYPE, "Environment"), "set_environment", "get_environment");
  586. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "attributes", PROPERTY_HINT_RESOURCE_TYPE, "CameraAttributesPractical,CameraAttributesPhysical"), "set_attributes", "get_attributes");
  587. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "compositor", PROPERTY_HINT_RESOURCE_TYPE, "Compositor"), "set_compositor", "get_compositor");
  588. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "h_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_h_offset", "get_h_offset");
  589. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "v_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_v_offset", "get_v_offset");
  590. ADD_PROPERTY(PropertyInfo(Variant::INT, "doppler_tracking", PROPERTY_HINT_ENUM, "Disabled,Idle,Physics"), "set_doppler_tracking", "get_doppler_tracking");
  591. ADD_PROPERTY(PropertyInfo(Variant::INT, "projection", PROPERTY_HINT_ENUM, "Perspective,Orthogonal,Frustum"), "set_projection", "get_projection");
  592. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "current"), "set_current", "is_current");
  593. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fov", PROPERTY_HINT_RANGE, "1,179,0.1,degrees"), "set_fov", "get_fov");
  594. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "size", PROPERTY_HINT_RANGE, "0.001,100,0.001,or_greater,suffix:m"), "set_size", "get_size");
  595. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "frustum_offset", PROPERTY_HINT_NONE, "suffix:m"), "set_frustum_offset", "get_frustum_offset");
  596. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "near", PROPERTY_HINT_RANGE, "0.001,10,0.001,or_greater,exp,suffix:m"), "set_near", "get_near");
  597. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "far", PROPERTY_HINT_RANGE, "0.01,4000,0.01,or_greater,exp,suffix:m"), "set_far", "get_far");
  598. BIND_ENUM_CONSTANT(PROJECTION_PERSPECTIVE);
  599. BIND_ENUM_CONSTANT(PROJECTION_ORTHOGONAL);
  600. BIND_ENUM_CONSTANT(PROJECTION_FRUSTUM);
  601. BIND_ENUM_CONSTANT(KEEP_WIDTH);
  602. BIND_ENUM_CONSTANT(KEEP_HEIGHT);
  603. BIND_ENUM_CONSTANT(DOPPLER_TRACKING_DISABLED);
  604. BIND_ENUM_CONSTANT(DOPPLER_TRACKING_IDLE_STEP);
  605. BIND_ENUM_CONSTANT(DOPPLER_TRACKING_PHYSICS_STEP);
  606. }
  607. real_t Camera3D::get_fov() const {
  608. return fov;
  609. }
  610. real_t Camera3D::get_size() const {
  611. return size;
  612. }
  613. real_t Camera3D::get_near() const {
  614. return _near;
  615. }
  616. Vector2 Camera3D::get_frustum_offset() const {
  617. return frustum_offset;
  618. }
  619. real_t Camera3D::get_far() const {
  620. return _far;
  621. }
  622. Camera3D::ProjectionType Camera3D::get_projection() const {
  623. return mode;
  624. }
  625. void Camera3D::set_fov(real_t p_fov) {
  626. ERR_FAIL_COND(p_fov < 1 || p_fov > 179);
  627. fov = p_fov;
  628. _update_camera_mode();
  629. }
  630. void Camera3D::set_size(real_t p_size) {
  631. ERR_FAIL_COND(p_size <= CMP_EPSILON);
  632. size = p_size;
  633. _update_camera_mode();
  634. }
  635. void Camera3D::set_near(real_t p_near) {
  636. _near = p_near;
  637. _update_camera_mode();
  638. }
  639. void Camera3D::set_frustum_offset(Vector2 p_offset) {
  640. frustum_offset = p_offset;
  641. _update_camera_mode();
  642. }
  643. void Camera3D::set_far(real_t p_far) {
  644. _far = p_far;
  645. _update_camera_mode();
  646. }
  647. void Camera3D::set_cull_mask(uint32_t p_layers) {
  648. layers = p_layers;
  649. RenderingServer::get_singleton()->camera_set_cull_mask(camera, layers);
  650. _update_camera_mode();
  651. }
  652. uint32_t Camera3D::get_cull_mask() const {
  653. return layers;
  654. }
  655. void Camera3D::set_cull_mask_value(int p_layer_number, bool p_value) {
  656. ERR_FAIL_COND_MSG(p_layer_number < 1, "Render layer number must be between 1 and 20 inclusive.");
  657. ERR_FAIL_COND_MSG(p_layer_number > 20, "Render layer number must be between 1 and 20 inclusive.");
  658. uint32_t mask = get_cull_mask();
  659. if (p_value) {
  660. mask |= 1 << (p_layer_number - 1);
  661. } else {
  662. mask &= ~(1 << (p_layer_number - 1));
  663. }
  664. set_cull_mask(mask);
  665. }
  666. bool Camera3D::get_cull_mask_value(int p_layer_number) const {
  667. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Render layer number must be between 1 and 20 inclusive.");
  668. ERR_FAIL_COND_V_MSG(p_layer_number > 20, false, "Render layer number must be between 1 and 20 inclusive.");
  669. return layers & (1 << (p_layer_number - 1));
  670. }
  671. Vector<Plane> Camera3D::get_frustum() const {
  672. ERR_FAIL_COND_V(!is_inside_world(), Vector<Plane>());
  673. Projection cm = _get_camera_projection(_near);
  674. return cm.get_projection_planes(get_camera_transform());
  675. }
  676. TypedArray<Plane> Camera3D::_get_frustum() const {
  677. Variant ret = get_frustum();
  678. return ret;
  679. }
  680. bool Camera3D::is_position_in_frustum(const Vector3 &p_position) const {
  681. Vector<Plane> frustum = get_frustum();
  682. for (int i = 0; i < frustum.size(); i++) {
  683. if (frustum[i].is_point_over(p_position)) {
  684. return false;
  685. }
  686. }
  687. return true;
  688. }
  689. void Camera3D::set_v_offset(real_t p_offset) {
  690. v_offset = p_offset;
  691. _update_camera();
  692. }
  693. real_t Camera3D::get_v_offset() const {
  694. return v_offset;
  695. }
  696. void Camera3D::set_h_offset(real_t p_offset) {
  697. h_offset = p_offset;
  698. _update_camera();
  699. }
  700. real_t Camera3D::get_h_offset() const {
  701. return h_offset;
  702. }
  703. Vector3 Camera3D::get_doppler_tracked_velocity() const {
  704. if (doppler_tracking != DOPPLER_TRACKING_DISABLED) {
  705. return velocity_tracker->get_tracked_linear_velocity();
  706. } else {
  707. return Vector3();
  708. }
  709. }
  710. RID Camera3D::get_pyramid_shape_rid() {
  711. ERR_FAIL_COND_V_MSG(!is_inside_tree(), RID(), "Camera is not inside scene.");
  712. if (pyramid_shape == RID()) {
  713. pyramid_shape_points = get_near_plane_points();
  714. pyramid_shape = PhysicsServer3D::get_singleton()->convex_polygon_shape_create();
  715. PhysicsServer3D::get_singleton()->shape_set_data(pyramid_shape, pyramid_shape_points);
  716. } else { //check if points changed
  717. Vector<Vector3> local_points = get_near_plane_points();
  718. bool all_equal = true;
  719. for (int i = 0; i < 5; i++) {
  720. if (local_points[i] != pyramid_shape_points[i]) {
  721. all_equal = false;
  722. break;
  723. }
  724. }
  725. if (!all_equal) {
  726. PhysicsServer3D::get_singleton()->shape_set_data(pyramid_shape, local_points);
  727. pyramid_shape_points = local_points;
  728. }
  729. }
  730. return pyramid_shape;
  731. }
  732. Camera3D::Camera3D() {
  733. camera = RenderingServer::get_singleton()->camera_create();
  734. set_perspective(75.0, 0.05, 4000.0);
  735. RenderingServer::get_singleton()->camera_set_cull_mask(camera, layers);
  736. //active=false;
  737. velocity_tracker.instantiate();
  738. set_notify_transform(true);
  739. set_disable_scale(true);
  740. }
  741. Camera3D::~Camera3D() {
  742. ERR_FAIL_NULL(RenderingServer::get_singleton());
  743. RenderingServer::get_singleton()->free(camera);
  744. if (pyramid_shape.is_valid()) {
  745. ERR_FAIL_NULL(PhysicsServer3D::get_singleton());
  746. PhysicsServer3D::get_singleton()->free(pyramid_shape);
  747. }
  748. }