sprite_3d.h 8.8 KB

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