sprite_3d.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*************************************************************************/
  2. /* sprite_3d.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  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/2d/animated_sprite.h"
  33. #include "scene/3d/visual_instance.h"
  34. class SpriteBase3D : public GeometryInstance {
  35. GDCLASS(SpriteBase3D, GeometryInstance);
  36. public:
  37. enum DrawFlags {
  38. FLAG_TRANSPARENT,
  39. FLAG_SHADED,
  40. FLAG_DOUBLE_SIDED,
  41. FLAG_MAX
  42. };
  43. enum AlphaCutMode {
  44. ALPHA_CUT_DISABLED,
  45. ALPHA_CUT_DISCARD,
  46. ALPHA_CUT_OPAQUE_PREPASS
  47. };
  48. private:
  49. bool color_dirty;
  50. Color color_accum;
  51. SpriteBase3D *parent_sprite;
  52. List<SpriteBase3D *> children;
  53. List<SpriteBase3D *>::Element *pI;
  54. bool centered;
  55. Point2 offset;
  56. bool hflip;
  57. bool vflip;
  58. Color modulate;
  59. float opacity;
  60. Vector3::Axis axis;
  61. float pixel_size;
  62. Rect3 aabb;
  63. RID immediate;
  64. bool flags[FLAG_MAX];
  65. AlphaCutMode alpha_cut;
  66. bool pending_update;
  67. void _im_update();
  68. void _propagate_color_changed();
  69. protected:
  70. Color _get_color_accum();
  71. void _notification(int p_what);
  72. static void _bind_methods();
  73. virtual void _draw() = 0;
  74. _FORCE_INLINE_ void set_aabb(const Rect3 &p_aabb) { aabb = p_aabb; }
  75. _FORCE_INLINE_ RID &get_immediate() { return immediate; }
  76. void _queue_update();
  77. public:
  78. void set_centered(bool p_center);
  79. bool is_centered() const;
  80. void set_offset(const Point2 &p_offset);
  81. Point2 get_offset() const;
  82. void set_flip_h(bool p_flip);
  83. bool is_flipped_h() const;
  84. void set_flip_v(bool p_flip);
  85. bool is_flipped_v() const;
  86. void set_region(bool p_region);
  87. bool is_region() const;
  88. void set_region_rect(const Rect2 &p_region_rect);
  89. Rect2 get_region_rect() const;
  90. void set_modulate(const Color &p_color);
  91. Color get_modulate() const;
  92. void set_opacity(float p_amount);
  93. float get_opacity() const;
  94. void set_pixel_size(float p_amount);
  95. float get_pixel_size() const;
  96. void set_axis(Vector3::Axis p_axis);
  97. Vector3::Axis get_axis() const;
  98. void set_draw_flag(DrawFlags p_flag, bool p_enable);
  99. bool get_draw_flag(DrawFlags p_flag) const;
  100. void set_alpha_cut_mode(AlphaCutMode p_mode);
  101. AlphaCutMode get_alpha_cut_mode() const;
  102. virtual Rect2 get_item_rect() const = 0;
  103. virtual Rect3 get_aabb() const;
  104. virtual PoolVector<Face3> get_faces(uint32_t p_usage_flags) const;
  105. SpriteBase3D();
  106. ~SpriteBase3D();
  107. };
  108. class Sprite3D : public SpriteBase3D {
  109. GDCLASS(Sprite3D, SpriteBase3D);
  110. Ref<Texture> texture;
  111. bool region;
  112. Rect2 region_rect;
  113. int frame;
  114. int vframes;
  115. int hframes;
  116. protected:
  117. virtual void _draw();
  118. static void _bind_methods();
  119. virtual void _validate_property(PropertyInfo &property) const;
  120. public:
  121. void set_texture(const Ref<Texture> &p_texture);
  122. Ref<Texture> get_texture() const;
  123. void set_region(bool p_region);
  124. bool is_region() const;
  125. void set_region_rect(const Rect2 &p_region_rect);
  126. Rect2 get_region_rect() const;
  127. void set_frame(int p_frame);
  128. int get_frame() const;
  129. void set_vframes(int p_amount);
  130. int get_vframes() const;
  131. void set_hframes(int p_amount);
  132. int get_hframes() const;
  133. virtual Rect2 get_item_rect() const;
  134. Sprite3D();
  135. //~Sprite3D();
  136. };
  137. class AnimatedSprite3D : public SpriteBase3D {
  138. GDCLASS(AnimatedSprite3D, SpriteBase3D);
  139. Ref<SpriteFrames> frames;
  140. bool playing;
  141. StringName animation;
  142. int frame;
  143. bool centered;
  144. Point2 offset;
  145. float timeout;
  146. bool hflip;
  147. bool vflip;
  148. Color modulate;
  149. void _res_changed();
  150. void _reset_timeout();
  151. void _set_playing(bool p_playing);
  152. bool _is_playing() const;
  153. protected:
  154. virtual void _draw();
  155. static void _bind_methods();
  156. void _notification(int p_what);
  157. virtual void _validate_property(PropertyInfo &property) const;
  158. public:
  159. void set_sprite_frames(const Ref<SpriteFrames> &p_frames);
  160. Ref<SpriteFrames> get_sprite_frames() const;
  161. void play(const StringName &p_animation = StringName());
  162. void stop();
  163. bool is_playing() const;
  164. void set_animation(const StringName &p_animation);
  165. StringName get_animation() const;
  166. void set_frame(int p_frame);
  167. int get_frame() const;
  168. virtual Rect2 get_item_rect() const;
  169. virtual String get_configuration_warning() const;
  170. AnimatedSprite3D();
  171. };
  172. VARIANT_ENUM_CAST(SpriteBase3D::DrawFlags);
  173. VARIANT_ENUM_CAST(SpriteBase3D::AlphaCutMode);
  174. #endif // SPRITE_3D_H