sprite_3d.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /**************************************************************************/
  2. /* sprite_3d.h */
  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. #ifndef SPRITE_3D_H
  31. #define SPRITE_3D_H
  32. #include "scene/3d/visual_instance_3d.h"
  33. #include "scene/resources/sprite_frames.h"
  34. class SpriteBase3D : public GeometryInstance3D {
  35. GDCLASS(SpriteBase3D, GeometryInstance3D);
  36. mutable Ref<TriangleMesh> triangle_mesh; //cached
  37. public:
  38. enum DrawFlags {
  39. FLAG_TRANSPARENT,
  40. FLAG_SHADED,
  41. FLAG_DOUBLE_SIDED,
  42. FLAG_DISABLE_DEPTH_TEST,
  43. FLAG_FIXED_SIZE,
  44. FLAG_MAX
  45. };
  46. enum AlphaCutMode {
  47. ALPHA_CUT_DISABLED,
  48. ALPHA_CUT_DISCARD,
  49. ALPHA_CUT_OPAQUE_PREPASS,
  50. ALPHA_CUT_HASH,
  51. ALPHA_CUT_MAX
  52. };
  53. private:
  54. bool color_dirty = true;
  55. Color color_accum;
  56. SpriteBase3D *parent_sprite = nullptr;
  57. List<SpriteBase3D *> children;
  58. List<SpriteBase3D *>::Element *pI = nullptr;
  59. bool centered = true;
  60. Point2 offset;
  61. bool hflip = false;
  62. bool vflip = false;
  63. Color modulate = Color(1, 1, 1, 1);
  64. int render_priority = 0;
  65. Vector3::Axis axis = Vector3::AXIS_Z;
  66. real_t pixel_size = 0.01;
  67. AABB aabb;
  68. RID mesh;
  69. RID material;
  70. RID last_shader;
  71. RID last_texture;
  72. bool flags[FLAG_MAX] = {};
  73. AlphaCutMode alpha_cut = ALPHA_CUT_DISABLED;
  74. float alpha_scissor_threshold = 0.5;
  75. float alpha_hash_scale = 1.0;
  76. StandardMaterial3D::AlphaAntiAliasing alpha_antialiasing_mode = StandardMaterial3D::ALPHA_ANTIALIASING_OFF;
  77. float alpha_antialiasing_edge = 0.0f;
  78. StandardMaterial3D::BillboardMode billboard_mode = StandardMaterial3D::BILLBOARD_DISABLED;
  79. StandardMaterial3D::TextureFilter texture_filter = StandardMaterial3D::TEXTURE_FILTER_LINEAR_WITH_MIPMAPS;
  80. bool pending_update = false;
  81. void _im_update();
  82. void _propagate_color_changed();
  83. protected:
  84. Color _get_color_accum();
  85. void _notification(int p_what);
  86. static void _bind_methods();
  87. virtual void _draw() = 0;
  88. void draw_texture_rect(Ref<Texture2D> p_texture, Rect2 p_dst_rect, Rect2 p_src_rect);
  89. _FORCE_INLINE_ void set_aabb(const AABB &p_aabb) { aabb = p_aabb; }
  90. _FORCE_INLINE_ RID &get_mesh() { return mesh; }
  91. _FORCE_INLINE_ RID &get_material() { return material; }
  92. uint32_t mesh_surface_offsets[RS::ARRAY_MAX];
  93. PackedByteArray vertex_buffer;
  94. PackedByteArray attribute_buffer;
  95. uint32_t vertex_stride = 0;
  96. uint32_t normal_tangent_stride = 0;
  97. uint32_t attrib_stride = 0;
  98. uint32_t skin_stride = 0;
  99. uint32_t mesh_surface_format = 0;
  100. void _queue_redraw();
  101. public:
  102. void set_centered(bool p_center);
  103. bool is_centered() const;
  104. void set_offset(const Point2 &p_offset);
  105. Point2 get_offset() const;
  106. void set_flip_h(bool p_flip);
  107. bool is_flipped_h() const;
  108. void set_flip_v(bool p_flip);
  109. bool is_flipped_v() const;
  110. void set_render_priority(int p_priority);
  111. int get_render_priority() const;
  112. void set_modulate(const Color &p_color);
  113. Color get_modulate() const;
  114. void set_pixel_size(real_t p_amount);
  115. real_t get_pixel_size() const;
  116. void set_axis(Vector3::Axis p_axis);
  117. Vector3::Axis get_axis() const;
  118. void set_draw_flag(DrawFlags p_flag, bool p_enable);
  119. bool get_draw_flag(DrawFlags p_flag) const;
  120. void set_alpha_cut_mode(AlphaCutMode p_mode);
  121. AlphaCutMode get_alpha_cut_mode() const;
  122. void set_alpha_scissor_threshold(float p_threshold);
  123. float get_alpha_scissor_threshold() const;
  124. void set_alpha_hash_scale(float p_hash_scale);
  125. float get_alpha_hash_scale() const;
  126. void set_alpha_antialiasing(BaseMaterial3D::AlphaAntiAliasing p_alpha_aa);
  127. BaseMaterial3D::AlphaAntiAliasing get_alpha_antialiasing() const;
  128. void set_alpha_antialiasing_edge(float p_edge);
  129. float get_alpha_antialiasing_edge() const;
  130. void set_billboard_mode(StandardMaterial3D::BillboardMode p_mode);
  131. StandardMaterial3D::BillboardMode get_billboard_mode() const;
  132. void set_texture_filter(StandardMaterial3D::TextureFilter p_filter);
  133. StandardMaterial3D::TextureFilter get_texture_filter() const;
  134. virtual Rect2 get_item_rect() const = 0;
  135. virtual AABB get_aabb() const override;
  136. Ref<TriangleMesh> generate_triangle_mesh() const;
  137. SpriteBase3D();
  138. ~SpriteBase3D();
  139. };
  140. class Sprite3D : public SpriteBase3D {
  141. GDCLASS(Sprite3D, SpriteBase3D);
  142. Ref<Texture2D> texture;
  143. bool region = false;
  144. Rect2 region_rect;
  145. int frame = 0;
  146. int vframes = 1;
  147. int hframes = 1;
  148. protected:
  149. virtual void _draw() override;
  150. static void _bind_methods();
  151. void _validate_property(PropertyInfo &p_property) const;
  152. public:
  153. void set_texture(const Ref<Texture2D> &p_texture);
  154. Ref<Texture2D> get_texture() const;
  155. void set_region_enabled(bool p_region);
  156. bool is_region_enabled() const;
  157. void set_region_rect(const Rect2 &p_region_rect);
  158. Rect2 get_region_rect() const;
  159. void set_frame(int p_frame);
  160. int get_frame() const;
  161. void set_frame_coords(const Vector2i &p_coord);
  162. Vector2i get_frame_coords() const;
  163. void set_vframes(int p_amount);
  164. int get_vframes() const;
  165. void set_hframes(int p_amount);
  166. int get_hframes() const;
  167. virtual Rect2 get_item_rect() const override;
  168. Sprite3D();
  169. //~Sprite3D();
  170. };
  171. class AnimatedSprite3D : public SpriteBase3D {
  172. GDCLASS(AnimatedSprite3D, SpriteBase3D);
  173. Ref<SpriteFrames> frames;
  174. String autoplay;
  175. bool playing = false;
  176. StringName animation = "default";
  177. int frame = 0;
  178. float speed_scale = 1.0;
  179. float custom_speed_scale = 1.0;
  180. real_t frame_speed_scale = 1.0;
  181. real_t frame_progress = 0.0;
  182. void _res_changed();
  183. double _get_frame_duration();
  184. void _calc_frame_speed_scale();
  185. void _stop_internal(bool p_reset);
  186. protected:
  187. #ifndef DISABLE_DEPRECATED
  188. bool _set(const StringName &p_name, const Variant &p_value);
  189. #endif
  190. virtual void _draw() override;
  191. static void _bind_methods();
  192. void _notification(int p_what);
  193. void _validate_property(PropertyInfo &p_property) const;
  194. public:
  195. void set_sprite_frames(const Ref<SpriteFrames> &p_frames);
  196. Ref<SpriteFrames> get_sprite_frames() const;
  197. void play(const StringName &p_name = StringName(), float p_custom_scale = 1.0, bool p_from_end = false);
  198. void play_backwards(const StringName &p_name = StringName());
  199. void pause();
  200. void stop();
  201. bool is_playing() const;
  202. void set_animation(const StringName &p_name);
  203. StringName get_animation() const;
  204. void set_autoplay(const String &p_name);
  205. String get_autoplay() const;
  206. void set_frame(int p_frame);
  207. int get_frame() const;
  208. void set_frame_progress(real_t p_progress);
  209. real_t get_frame_progress() const;
  210. void set_frame_and_progress(int p_frame, real_t p_progress);
  211. void set_speed_scale(float p_speed_scale);
  212. float get_speed_scale() const;
  213. float get_playing_speed() const;
  214. virtual Rect2 get_item_rect() const override;
  215. virtual PackedStringArray get_configuration_warnings() const override;
  216. #ifdef TOOLS_ENABLED
  217. virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const override;
  218. #endif
  219. AnimatedSprite3D();
  220. };
  221. VARIANT_ENUM_CAST(SpriteBase3D::DrawFlags);
  222. VARIANT_ENUM_CAST(SpriteBase3D::AlphaCutMode);
  223. #endif // SPRITE_3D_H