gltf_camera.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**************************************************************************/
  2. /* gltf_camera.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 "gltf_camera.h"
  31. #include "scene/3d/camera_3d.h"
  32. void GLTFCamera::_bind_methods() {
  33. ClassDB::bind_static_method("GLTFCamera", D_METHOD("from_node", "camera_node"), &GLTFCamera::from_node);
  34. ClassDB::bind_method(D_METHOD("to_node"), &GLTFCamera::to_node);
  35. ClassDB::bind_static_method("GLTFCamera", D_METHOD("from_dictionary", "dictionary"), &GLTFCamera::from_dictionary);
  36. ClassDB::bind_method(D_METHOD("to_dictionary"), &GLTFCamera::to_dictionary);
  37. ClassDB::bind_method(D_METHOD("get_perspective"), &GLTFCamera::get_perspective);
  38. ClassDB::bind_method(D_METHOD("set_perspective", "perspective"), &GLTFCamera::set_perspective);
  39. ClassDB::bind_method(D_METHOD("get_fov"), &GLTFCamera::get_fov);
  40. ClassDB::bind_method(D_METHOD("set_fov", "fov"), &GLTFCamera::set_fov);
  41. ClassDB::bind_method(D_METHOD("get_size_mag"), &GLTFCamera::get_size_mag);
  42. ClassDB::bind_method(D_METHOD("set_size_mag", "size_mag"), &GLTFCamera::set_size_mag);
  43. ClassDB::bind_method(D_METHOD("get_depth_far"), &GLTFCamera::get_depth_far);
  44. ClassDB::bind_method(D_METHOD("set_depth_far", "zdepth_far"), &GLTFCamera::set_depth_far);
  45. ClassDB::bind_method(D_METHOD("get_depth_near"), &GLTFCamera::get_depth_near);
  46. ClassDB::bind_method(D_METHOD("set_depth_near", "zdepth_near"), &GLTFCamera::set_depth_near);
  47. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "perspective"), "set_perspective", "get_perspective");
  48. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fov"), "set_fov", "get_fov");
  49. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "size_mag"), "set_size_mag", "get_size_mag");
  50. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth_far"), "set_depth_far", "get_depth_far");
  51. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth_near"), "set_depth_near", "get_depth_near");
  52. }
  53. Ref<GLTFCamera> GLTFCamera::from_node(const Camera3D *p_camera) {
  54. Ref<GLTFCamera> c;
  55. c.instantiate();
  56. ERR_FAIL_COND_V_MSG(!p_camera, c, "Tried to create a GLTFCamera from a Camera3D node, but the given node was null.");
  57. c->set_perspective(p_camera->get_projection() == Camera3D::ProjectionType::PROJECTION_PERSPECTIVE);
  58. // GLTF spec (yfov) is in radians, Godot's camera (fov) is in degrees.
  59. c->set_fov(Math::deg_to_rad(p_camera->get_fov()));
  60. // GLTF spec (xmag and ymag) is a radius in meters, Godot's camera (size) is a diameter in meters.
  61. c->set_size_mag(p_camera->get_size() * 0.5f);
  62. c->set_depth_far(p_camera->get_far());
  63. c->set_depth_near(p_camera->get_near());
  64. return c;
  65. }
  66. Camera3D *GLTFCamera::to_node() const {
  67. Camera3D *camera = memnew(Camera3D);
  68. camera->set_projection(perspective ? Camera3D::PROJECTION_PERSPECTIVE : Camera3D::PROJECTION_ORTHOGONAL);
  69. // GLTF spec (yfov) is in radians, Godot's camera (fov) is in degrees.
  70. camera->set_fov(Math::rad_to_deg(fov));
  71. // GLTF spec (xmag and ymag) is a radius in meters, Godot's camera (size) is a diameter in meters.
  72. camera->set_size(size_mag * 2.0f);
  73. camera->set_near(depth_near);
  74. camera->set_far(depth_far);
  75. return camera;
  76. }
  77. Ref<GLTFCamera> GLTFCamera::from_dictionary(const Dictionary p_dictionary) {
  78. ERR_FAIL_COND_V_MSG(!p_dictionary.has("type"), Ref<GLTFCamera>(), "Failed to parse GLTF camera, missing required field 'type'.");
  79. Ref<GLTFCamera> camera;
  80. camera.instantiate();
  81. const String &type = p_dictionary["type"];
  82. if (type == "perspective") {
  83. camera->set_perspective(true);
  84. if (p_dictionary.has("perspective")) {
  85. const Dictionary &persp = p_dictionary["perspective"];
  86. camera->set_fov(persp["yfov"]);
  87. if (persp.has("zfar")) {
  88. camera->set_depth_far(persp["zfar"]);
  89. }
  90. camera->set_depth_near(persp["znear"]);
  91. }
  92. } else if (type == "orthographic") {
  93. camera->set_perspective(false);
  94. if (p_dictionary.has("orthographic")) {
  95. const Dictionary &ortho = p_dictionary["orthographic"];
  96. camera->set_size_mag(ortho["ymag"]);
  97. camera->set_depth_far(ortho["zfar"]);
  98. camera->set_depth_near(ortho["znear"]);
  99. }
  100. } else {
  101. ERR_PRINT("Error parsing GLTF camera: Camera type '" + type + "' is unknown, should be perspective or orthographic.");
  102. }
  103. return camera;
  104. }
  105. Dictionary GLTFCamera::to_dictionary() const {
  106. Dictionary d;
  107. if (perspective) {
  108. Dictionary persp;
  109. persp["yfov"] = fov;
  110. persp["zfar"] = depth_far;
  111. persp["znear"] = depth_near;
  112. d["perspective"] = persp;
  113. d["type"] = "perspective";
  114. } else {
  115. Dictionary ortho;
  116. ortho["ymag"] = size_mag;
  117. ortho["xmag"] = size_mag;
  118. ortho["zfar"] = depth_far;
  119. ortho["znear"] = depth_near;
  120. d["orthographic"] = ortho;
  121. d["type"] = "orthographic";
  122. }
  123. return d;
  124. }