renderer_canvas_render.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**************************************************************************/
  2. /* renderer_canvas_render.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 "renderer_canvas_render.h"
  31. #include "servers/rendering/rendering_server_globals.h"
  32. RendererCanvasRender *RendererCanvasRender::singleton = nullptr;
  33. const Rect2 &RendererCanvasRender::Item::get_rect() const {
  34. if (custom_rect || (!rect_dirty && !update_when_visible && skeleton == RID())) {
  35. return rect;
  36. }
  37. //must update rect
  38. if (commands == nullptr) {
  39. rect = Rect2();
  40. rect_dirty = false;
  41. return rect;
  42. }
  43. Transform2D xf;
  44. bool found_xform = false;
  45. bool first = true;
  46. const Item::Command *c = commands;
  47. while (c) {
  48. Rect2 r;
  49. switch (c->type) {
  50. case Item::Command::TYPE_RECT: {
  51. const Item::CommandRect *crect = static_cast<const Item::CommandRect *>(c);
  52. r = crect->rect;
  53. } break;
  54. case Item::Command::TYPE_NINEPATCH: {
  55. const Item::CommandNinePatch *style = static_cast<const Item::CommandNinePatch *>(c);
  56. r = style->rect;
  57. } break;
  58. case Item::Command::TYPE_POLYGON: {
  59. const Item::CommandPolygon *polygon = static_cast<const Item::CommandPolygon *>(c);
  60. r = polygon->polygon.rect_cache;
  61. } break;
  62. case Item::Command::TYPE_PRIMITIVE: {
  63. const Item::CommandPrimitive *primitive = static_cast<const Item::CommandPrimitive *>(c);
  64. for (uint32_t j = 0; j < primitive->point_count; j++) {
  65. if (j == 0) {
  66. r.position = primitive->points[0];
  67. } else {
  68. r.expand_to(primitive->points[j]);
  69. }
  70. }
  71. } break;
  72. case Item::Command::TYPE_MESH: {
  73. const Item::CommandMesh *mesh = static_cast<const Item::CommandMesh *>(c);
  74. AABB aabb = RSG::mesh_storage->mesh_get_aabb(mesh->mesh, skeleton);
  75. r = Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);
  76. } break;
  77. case Item::Command::TYPE_MULTIMESH: {
  78. const Item::CommandMultiMesh *multimesh = static_cast<const Item::CommandMultiMesh *>(c);
  79. AABB aabb = RSG::mesh_storage->multimesh_get_aabb(multimesh->multimesh);
  80. r = Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);
  81. } break;
  82. case Item::Command::TYPE_PARTICLES: {
  83. const Item::CommandParticles *particles_cmd = static_cast<const Item::CommandParticles *>(c);
  84. if (particles_cmd->particles.is_valid()) {
  85. AABB aabb = RSG::particles_storage->particles_get_aabb(particles_cmd->particles);
  86. r = Rect2(aabb.position.x, aabb.position.y, aabb.size.x, aabb.size.y);
  87. }
  88. } break;
  89. case Item::Command::TYPE_TRANSFORM: {
  90. const Item::CommandTransform *transform = static_cast<const Item::CommandTransform *>(c);
  91. xf = transform->xform;
  92. found_xform = true;
  93. [[fallthrough]];
  94. }
  95. default: {
  96. c = c->next;
  97. continue;
  98. }
  99. }
  100. if (found_xform) {
  101. r = xf.xform(r);
  102. }
  103. if (first) {
  104. rect = r;
  105. first = false;
  106. } else {
  107. rect = rect.merge(r);
  108. }
  109. c = c->next;
  110. }
  111. rect_dirty = false;
  112. return rect;
  113. }
  114. RendererCanvasRender::Item::CommandMesh::~CommandMesh() {
  115. if (mesh_instance.is_valid()) {
  116. RSG::mesh_storage->mesh_instance_free(mesh_instance);
  117. }
  118. }