mesh_texture.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**************************************************************************/
  2. /* mesh_texture.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 "mesh_texture.h"
  31. #include "scene/resources/mesh.h"
  32. int MeshTexture::get_width() const {
  33. return size.width;
  34. }
  35. int MeshTexture::get_height() const {
  36. return size.height;
  37. }
  38. RID MeshTexture::get_rid() const {
  39. return RID();
  40. }
  41. bool MeshTexture::has_alpha() const {
  42. return false;
  43. }
  44. void MeshTexture::set_mesh(const Ref<Mesh> &p_mesh) {
  45. mesh = p_mesh;
  46. }
  47. Ref<Mesh> MeshTexture::get_mesh() const {
  48. return mesh;
  49. }
  50. void MeshTexture::set_image_size(const Size2 &p_size) {
  51. size = p_size;
  52. }
  53. Size2 MeshTexture::get_image_size() const {
  54. return size;
  55. }
  56. void MeshTexture::set_base_texture(const Ref<Texture2D> &p_texture) {
  57. base_texture = p_texture;
  58. }
  59. Ref<Texture2D> MeshTexture::get_base_texture() const {
  60. return base_texture;
  61. }
  62. void MeshTexture::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
  63. if (mesh.is_null() || base_texture.is_null()) {
  64. return;
  65. }
  66. Transform2D xform;
  67. xform.set_origin(p_pos);
  68. if (p_transpose) {
  69. SWAP(xform.columns[0][1], xform.columns[1][0]);
  70. SWAP(xform.columns[0][0], xform.columns[1][1]);
  71. }
  72. RenderingServer::get_singleton()->canvas_item_add_mesh(p_canvas_item, mesh->get_rid(), xform, p_modulate, base_texture->get_rid());
  73. }
  74. void MeshTexture::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const {
  75. if (mesh.is_null() || base_texture.is_null()) {
  76. return;
  77. }
  78. Transform2D xform;
  79. Vector2 origin = p_rect.position;
  80. if (p_rect.size.x < 0) {
  81. origin.x += size.x;
  82. }
  83. if (p_rect.size.y < 0) {
  84. origin.y += size.y;
  85. }
  86. xform.set_origin(origin);
  87. xform.set_scale(p_rect.size / size);
  88. if (p_transpose) {
  89. SWAP(xform.columns[0][1], xform.columns[1][0]);
  90. SWAP(xform.columns[0][0], xform.columns[1][1]);
  91. }
  92. RenderingServer::get_singleton()->canvas_item_add_mesh(p_canvas_item, mesh->get_rid(), xform, p_modulate, base_texture->get_rid());
  93. }
  94. void MeshTexture::draw_rect_region(RID p_canvas_item, const Rect2 &p_rect, const Rect2 &p_src_rect, const Color &p_modulate, bool p_transpose, bool p_clip_uv) const {
  95. if (mesh.is_null() || base_texture.is_null()) {
  96. return;
  97. }
  98. Transform2D xform;
  99. Vector2 origin = p_rect.position;
  100. if (p_rect.size.x < 0) {
  101. origin.x += size.x;
  102. }
  103. if (p_rect.size.y < 0) {
  104. origin.y += size.y;
  105. }
  106. xform.set_origin(origin);
  107. xform.set_scale(p_rect.size / size);
  108. if (p_transpose) {
  109. SWAP(xform.columns[0][1], xform.columns[1][0]);
  110. SWAP(xform.columns[0][0], xform.columns[1][1]);
  111. }
  112. RenderingServer::get_singleton()->canvas_item_add_mesh(p_canvas_item, mesh->get_rid(), xform, p_modulate, base_texture->get_rid());
  113. }
  114. bool MeshTexture::get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const {
  115. r_rect = p_rect;
  116. r_src_rect = p_src_rect;
  117. return true;
  118. }
  119. bool MeshTexture::is_pixel_opaque(int p_x, int p_y) const {
  120. return true;
  121. }
  122. void MeshTexture::_bind_methods() {
  123. ClassDB::bind_method(D_METHOD("set_mesh", "mesh"), &MeshTexture::set_mesh);
  124. ClassDB::bind_method(D_METHOD("get_mesh"), &MeshTexture::get_mesh);
  125. ClassDB::bind_method(D_METHOD("set_image_size", "size"), &MeshTexture::set_image_size);
  126. ClassDB::bind_method(D_METHOD("get_image_size"), &MeshTexture::get_image_size);
  127. ClassDB::bind_method(D_METHOD("set_base_texture", "texture"), &MeshTexture::set_base_texture);
  128. ClassDB::bind_method(D_METHOD("get_base_texture"), &MeshTexture::get_base_texture);
  129. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "mesh", PROPERTY_HINT_RESOURCE_TYPE, "Mesh"), "set_mesh", "get_mesh");
  130. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "base_texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_base_texture", "get_base_texture");
  131. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "image_size", PROPERTY_HINT_RANGE, "0,16384,1,suffix:px"), "set_image_size", "get_image_size");
  132. }
  133. MeshTexture::MeshTexture() {
  134. }