sprite_3d.h 6.2 KB

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