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