texture.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**************************************************************************/
  2. /* 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 "texture.h"
  31. #include "scene/resources/placeholder_textures.h"
  32. #include "servers/rendering/rendering_server.h"
  33. int Texture2D::get_width() const {
  34. int ret = 0;
  35. GDVIRTUAL_CALL(_get_width, ret);
  36. return ret;
  37. }
  38. int Texture2D::get_height() const {
  39. int ret = 0;
  40. GDVIRTUAL_CALL(_get_height, ret);
  41. return ret;
  42. }
  43. Size2 Texture2D::get_size() const {
  44. return Size2(get_width(), get_height());
  45. }
  46. bool Texture2D::is_pixel_opaque(int p_x, int p_y) const {
  47. bool ret = true;
  48. GDVIRTUAL_CALL(_is_pixel_opaque, p_x, p_y, ret);
  49. return ret;
  50. }
  51. bool Texture2D::has_alpha() const {
  52. bool ret = true;
  53. GDVIRTUAL_CALL(_has_alpha, ret);
  54. return ret;
  55. }
  56. void Texture2D::draw(RID p_canvas_item, const Point2 &p_pos, const Color &p_modulate, bool p_transpose) const {
  57. if (GDVIRTUAL_CALL(_draw, p_canvas_item, p_pos, p_modulate, p_transpose)) {
  58. return;
  59. }
  60. RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, Rect2(p_pos, get_size()), get_rid(), false, p_modulate, p_transpose);
  61. }
  62. void Texture2D::draw_rect(RID p_canvas_item, const Rect2 &p_rect, bool p_tile, const Color &p_modulate, bool p_transpose) const {
  63. if (GDVIRTUAL_CALL(_draw_rect, p_canvas_item, p_rect, p_tile, p_modulate, p_transpose)) {
  64. return;
  65. }
  66. RenderingServer::get_singleton()->canvas_item_add_texture_rect(p_canvas_item, p_rect, get_rid(), p_tile, p_modulate, p_transpose);
  67. }
  68. void Texture2D::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 {
  69. if (GDVIRTUAL_CALL(_draw_rect_region, p_canvas_item, p_rect, p_src_rect, p_modulate, p_transpose, p_clip_uv)) {
  70. return;
  71. }
  72. RenderingServer::get_singleton()->canvas_item_add_texture_rect_region(p_canvas_item, p_rect, get_rid(), p_src_rect, p_modulate, p_transpose, p_clip_uv);
  73. }
  74. bool Texture2D::get_rect_region(const Rect2 &p_rect, const Rect2 &p_src_rect, Rect2 &r_rect, Rect2 &r_src_rect) const {
  75. r_rect = p_rect;
  76. r_src_rect = p_src_rect;
  77. return true;
  78. }
  79. Ref<Resource> Texture2D::create_placeholder() const {
  80. Ref<PlaceholderTexture2D> placeholder;
  81. placeholder.instantiate();
  82. placeholder->set_size(get_size());
  83. return placeholder;
  84. }
  85. void Texture2D::_bind_methods() {
  86. ClassDB::bind_method(D_METHOD("get_width"), &Texture2D::get_width);
  87. ClassDB::bind_method(D_METHOD("get_height"), &Texture2D::get_height);
  88. ClassDB::bind_method(D_METHOD("get_size"), &Texture2D::get_size);
  89. ClassDB::bind_method(D_METHOD("has_alpha"), &Texture2D::has_alpha);
  90. ClassDB::bind_method(D_METHOD("draw", "canvas_item", "position", "modulate", "transpose"), &Texture2D::draw, DEFVAL(Color(1, 1, 1)), DEFVAL(false));
  91. ClassDB::bind_method(D_METHOD("draw_rect", "canvas_item", "rect", "tile", "modulate", "transpose"), &Texture2D::draw_rect, DEFVAL(Color(1, 1, 1)), DEFVAL(false));
  92. ClassDB::bind_method(D_METHOD("draw_rect_region", "canvas_item", "rect", "src_rect", "modulate", "transpose", "clip_uv"), &Texture2D::draw_rect_region, DEFVAL(Color(1, 1, 1)), DEFVAL(false), DEFVAL(true));
  93. ClassDB::bind_method(D_METHOD("get_image"), &Texture2D::get_image);
  94. ClassDB::bind_method(D_METHOD("create_placeholder"), &Texture2D::create_placeholder);
  95. ADD_GROUP("", "");
  96. GDVIRTUAL_BIND(_get_width);
  97. GDVIRTUAL_BIND(_get_height);
  98. GDVIRTUAL_BIND(_is_pixel_opaque, "x", "y");
  99. GDVIRTUAL_BIND(_has_alpha);
  100. GDVIRTUAL_BIND(_draw, "to_canvas_item", "pos", "modulate", "transpose")
  101. GDVIRTUAL_BIND(_draw_rect, "to_canvas_item", "rect", "tile", "modulate", "transpose")
  102. GDVIRTUAL_BIND(_draw_rect_region, "to_canvas_item", "rect", "src_rect", "modulate", "transpose", "clip_uv");
  103. }
  104. Texture2D::Texture2D() {
  105. }
  106. TypedArray<Image> Texture3D::_get_datai() const {
  107. Vector<Ref<Image>> data = get_data();
  108. TypedArray<Image> ret;
  109. ret.resize(data.size());
  110. for (int i = 0; i < data.size(); i++) {
  111. ret[i] = data[i];
  112. }
  113. return ret;
  114. }
  115. Image::Format Texture3D::get_format() const {
  116. Image::Format ret = Image::FORMAT_MAX;
  117. GDVIRTUAL_CALL(_get_format, ret);
  118. return ret;
  119. }
  120. int Texture3D::get_width() const {
  121. int ret = 0;
  122. GDVIRTUAL_CALL(_get_width, ret);
  123. return ret;
  124. }
  125. int Texture3D::get_height() const {
  126. int ret = 0;
  127. GDVIRTUAL_CALL(_get_height, ret);
  128. return ret;
  129. }
  130. int Texture3D::get_depth() const {
  131. int ret = 0;
  132. GDVIRTUAL_CALL(_get_depth, ret);
  133. return ret;
  134. }
  135. bool Texture3D::has_mipmaps() const {
  136. bool ret = false;
  137. GDVIRTUAL_CALL(_has_mipmaps, ret);
  138. return ret;
  139. }
  140. Vector<Ref<Image>> Texture3D::get_data() const {
  141. TypedArray<Image> ret;
  142. GDVIRTUAL_CALL(_get_data, ret);
  143. Vector<Ref<Image>> data;
  144. data.resize(ret.size());
  145. for (int i = 0; i < data.size(); i++) {
  146. data.write[i] = ret[i];
  147. }
  148. return data;
  149. }
  150. void Texture3D::_bind_methods() {
  151. ClassDB::bind_method(D_METHOD("get_format"), &Texture3D::get_format);
  152. ClassDB::bind_method(D_METHOD("get_width"), &Texture3D::get_width);
  153. ClassDB::bind_method(D_METHOD("get_height"), &Texture3D::get_height);
  154. ClassDB::bind_method(D_METHOD("get_depth"), &Texture3D::get_depth);
  155. ClassDB::bind_method(D_METHOD("has_mipmaps"), &Texture3D::has_mipmaps);
  156. ClassDB::bind_method(D_METHOD("get_data"), &Texture3D::_get_datai);
  157. ClassDB::bind_method(D_METHOD("create_placeholder"), &Texture3D::create_placeholder);
  158. GDVIRTUAL_BIND(_get_format);
  159. GDVIRTUAL_BIND(_get_width);
  160. GDVIRTUAL_BIND(_get_height);
  161. GDVIRTUAL_BIND(_get_depth);
  162. GDVIRTUAL_BIND(_has_mipmaps);
  163. GDVIRTUAL_BIND(_get_data);
  164. }
  165. Ref<Resource> Texture3D::create_placeholder() const {
  166. Ref<PlaceholderTexture3D> placeholder;
  167. placeholder.instantiate();
  168. placeholder->set_size(Vector3i(get_width(), get_height(), get_depth()));
  169. return placeholder;
  170. }
  171. Image::Format TextureLayered::get_format() const {
  172. Image::Format ret = Image::FORMAT_MAX;
  173. GDVIRTUAL_CALL(_get_format, ret);
  174. return ret;
  175. }
  176. TextureLayered::LayeredType TextureLayered::get_layered_type() const {
  177. uint32_t ret = LAYERED_TYPE_2D_ARRAY;
  178. GDVIRTUAL_CALL(_get_layered_type, ret);
  179. return (LayeredType)ret;
  180. }
  181. int TextureLayered::get_width() const {
  182. int ret = 0;
  183. GDVIRTUAL_CALL(_get_width, ret);
  184. return ret;
  185. }
  186. int TextureLayered::get_height() const {
  187. int ret = 0;
  188. GDVIRTUAL_CALL(_get_height, ret);
  189. return ret;
  190. }
  191. int TextureLayered::get_layers() const {
  192. int ret = 0;
  193. GDVIRTUAL_CALL(_get_layers, ret);
  194. return ret;
  195. }
  196. bool TextureLayered::has_mipmaps() const {
  197. bool ret = false;
  198. GDVIRTUAL_CALL(_has_mipmaps, ret);
  199. return ret;
  200. }
  201. Ref<Image> TextureLayered::get_layer_data(int p_layer) const {
  202. Ref<Image> ret;
  203. GDVIRTUAL_CALL(_get_layer_data, p_layer, ret);
  204. return ret;
  205. }
  206. void TextureLayered::_bind_methods() {
  207. ClassDB::bind_method(D_METHOD("get_format"), &TextureLayered::get_format);
  208. ClassDB::bind_method(D_METHOD("get_layered_type"), &TextureLayered::get_layered_type);
  209. ClassDB::bind_method(D_METHOD("get_width"), &TextureLayered::get_width);
  210. ClassDB::bind_method(D_METHOD("get_height"), &TextureLayered::get_height);
  211. ClassDB::bind_method(D_METHOD("get_layers"), &TextureLayered::get_layers);
  212. ClassDB::bind_method(D_METHOD("has_mipmaps"), &TextureLayered::has_mipmaps);
  213. ClassDB::bind_method(D_METHOD("get_layer_data", "layer"), &TextureLayered::get_layer_data);
  214. BIND_ENUM_CONSTANT(LAYERED_TYPE_2D_ARRAY);
  215. BIND_ENUM_CONSTANT(LAYERED_TYPE_CUBEMAP);
  216. BIND_ENUM_CONSTANT(LAYERED_TYPE_CUBEMAP_ARRAY);
  217. GDVIRTUAL_BIND(_get_format);
  218. GDVIRTUAL_BIND(_get_layered_type);
  219. GDVIRTUAL_BIND(_get_width);
  220. GDVIRTUAL_BIND(_get_height);
  221. GDVIRTUAL_BIND(_get_layers);
  222. GDVIRTUAL_BIND(_has_mipmaps);
  223. GDVIRTUAL_BIND(_get_layer_data, "layer_index");
  224. }