multimesh_instance_3d.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**************************************************************************/
  2. /* multimesh_instance_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 "multimesh_instance_3d.h"
  31. void MultiMeshInstance3D::_refresh_interpolated() {
  32. if (is_inside_tree() && multimesh.is_valid()) {
  33. bool interpolated = is_physics_interpolated_and_enabled();
  34. multimesh->set_physics_interpolated(interpolated);
  35. }
  36. }
  37. void MultiMeshInstance3D::_physics_interpolated_changed() {
  38. VisualInstance3D::_physics_interpolated_changed();
  39. _refresh_interpolated();
  40. }
  41. void MultiMeshInstance3D::_bind_methods() {
  42. ClassDB::bind_method(D_METHOD("set_multimesh", "multimesh"), &MultiMeshInstance3D::set_multimesh);
  43. ClassDB::bind_method(D_METHOD("get_multimesh"), &MultiMeshInstance3D::get_multimesh);
  44. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "multimesh", PROPERTY_HINT_RESOURCE_TYPE, "MultiMesh"), "set_multimesh", "get_multimesh");
  45. }
  46. void MultiMeshInstance3D::_notification(int p_what) {
  47. if (p_what == NOTIFICATION_ENTER_TREE) {
  48. _refresh_interpolated();
  49. }
  50. }
  51. void MultiMeshInstance3D::set_multimesh(const Ref<MultiMesh> &p_multimesh) {
  52. multimesh = p_multimesh;
  53. if (multimesh.is_valid()) {
  54. set_base(multimesh->get_rid());
  55. _refresh_interpolated();
  56. } else {
  57. set_base(RID());
  58. }
  59. }
  60. Ref<MultiMesh> MultiMeshInstance3D::get_multimesh() const {
  61. return multimesh;
  62. }
  63. Array MultiMeshInstance3D::get_meshes() const {
  64. if (multimesh.is_null() || multimesh->get_mesh().is_null() || multimesh->get_transform_format() != MultiMesh::TransformFormat::TRANSFORM_3D) {
  65. return Array();
  66. }
  67. int count = multimesh->get_visible_instance_count();
  68. if (count == -1) {
  69. count = multimesh->get_instance_count();
  70. }
  71. Ref<Mesh> mesh = multimesh->get_mesh();
  72. Array results;
  73. for (int i = 0; i < count; i++) {
  74. results.push_back(multimesh->get_instance_transform(i));
  75. results.push_back(mesh);
  76. }
  77. return results;
  78. }
  79. AABB MultiMeshInstance3D::get_aabb() const {
  80. if (multimesh.is_null()) {
  81. return AABB();
  82. } else {
  83. return multimesh->get_aabb();
  84. }
  85. }
  86. MultiMeshInstance3D::MultiMeshInstance3D() {
  87. }
  88. MultiMeshInstance3D::~MultiMeshInstance3D() {
  89. }