sprite_3d.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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/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_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. };
  51. private:
  52. bool color_dirty;
  53. Color color_accum;
  54. SpriteBase3D *parent_sprite;
  55. List<SpriteBase3D *> children;
  56. List<SpriteBase3D *>::Element *pI;
  57. bool centered;
  58. Point2 offset;
  59. bool hflip;
  60. bool vflip;
  61. Color modulate;
  62. int render_priority = 0;
  63. float opacity;
  64. Vector3::Axis axis;
  65. float pixel_size;
  66. AABB aabb;
  67. RID mesh;
  68. RID material;
  69. bool flags[FLAG_MAX];
  70. AlphaCutMode alpha_cut;
  71. SpatialMaterial::BillboardMode billboard_mode;
  72. bool pending_update;
  73. void _im_update();
  74. void _propagate_color_changed();
  75. protected:
  76. Color _get_color_accum();
  77. void _notification(int p_what);
  78. static void _bind_methods();
  79. virtual void _draw() = 0;
  80. void draw_texture_rect(Ref<Texture> p_texture, Rect2 p_dst_rect, Rect2 p_src_rect);
  81. _FORCE_INLINE_ void set_aabb(const AABB &p_aabb) { aabb = p_aabb; }
  82. _FORCE_INLINE_ RID &get_mesh() { return mesh; }
  83. _FORCE_INLINE_ RID &get_material() { return material; }
  84. uint32_t mesh_surface_offsets[VS::ARRAY_MAX];
  85. PoolByteArray mesh_buffer;
  86. uint32_t mesh_stride[VS::ARRAY_MAX];
  87. uint32_t mesh_surface_format;
  88. void _queue_update();
  89. public:
  90. void set_centered(bool p_center);
  91. bool is_centered() const;
  92. void set_offset(const Point2 &p_offset);
  93. Point2 get_offset() const;
  94. void set_flip_h(bool p_flip);
  95. bool is_flipped_h() const;
  96. void set_flip_v(bool p_flip);
  97. bool is_flipped_v() const;
  98. void set_modulate(const Color &p_color);
  99. Color get_modulate() const;
  100. void set_opacity(float p_amount);
  101. float get_opacity() const;
  102. void set_render_priority(int p_priority);
  103. int get_render_priority() const;
  104. void set_pixel_size(float p_amount);
  105. float get_pixel_size() const;
  106. void set_axis(Vector3::Axis p_axis);
  107. Vector3::Axis get_axis() const;
  108. void set_draw_flag(DrawFlags p_flag, bool p_enable);
  109. bool get_draw_flag(DrawFlags p_flag) const;
  110. void set_alpha_cut_mode(AlphaCutMode p_mode);
  111. AlphaCutMode get_alpha_cut_mode() const;
  112. void set_billboard_mode(SpatialMaterial::BillboardMode p_mode);
  113. SpatialMaterial::BillboardMode get_billboard_mode() const;
  114. virtual Rect2 get_item_rect() const = 0;
  115. virtual AABB get_aabb() const;
  116. virtual PoolVector<Face3> get_faces(uint32_t p_usage_flags) const;
  117. Ref<TriangleMesh> generate_triangle_mesh() const;
  118. SpriteBase3D();
  119. ~SpriteBase3D();
  120. };
  121. class Sprite3D : public SpriteBase3D {
  122. GDCLASS(Sprite3D, SpriteBase3D);
  123. Ref<Texture> texture;
  124. bool region;
  125. Rect2 region_rect;
  126. int frame;
  127. int vframes;
  128. int hframes;
  129. protected:
  130. virtual void _draw();
  131. static void _bind_methods();
  132. virtual void _validate_property(PropertyInfo &property) const;
  133. public:
  134. void set_texture(const Ref<Texture> &p_texture);
  135. Ref<Texture> get_texture() const;
  136. void set_region(bool p_region);
  137. bool is_region() const;
  138. void set_region_rect(const Rect2 &p_region_rect);
  139. Rect2 get_region_rect() const;
  140. void set_frame(int p_frame);
  141. int get_frame() const;
  142. void set_frame_coords(const Vector2 &p_coord);
  143. Vector2 get_frame_coords() const;
  144. void set_vframes(int p_amount);
  145. int get_vframes() const;
  146. void set_hframes(int p_amount);
  147. int get_hframes() const;
  148. virtual Rect2 get_item_rect() const;
  149. Sprite3D();
  150. //~Sprite3D();
  151. };
  152. class AnimatedSprite3D : public SpriteBase3D {
  153. GDCLASS(AnimatedSprite3D, SpriteBase3D);
  154. Ref<SpriteFrames> frames;
  155. bool playing;
  156. StringName animation;
  157. int frame;
  158. bool centered;
  159. float timeout;
  160. bool hflip;
  161. bool vflip;
  162. Color modulate;
  163. void _res_changed();
  164. void _reset_timeout();
  165. void _set_playing(bool p_playing);
  166. bool _is_playing() const;
  167. protected:
  168. virtual void _draw();
  169. static void _bind_methods();
  170. void _notification(int p_what);
  171. virtual void _validate_property(PropertyInfo &property) const;
  172. public:
  173. void set_sprite_frames(const Ref<SpriteFrames> &p_frames);
  174. Ref<SpriteFrames> get_sprite_frames() const;
  175. void play(const StringName &p_animation = StringName());
  176. void stop();
  177. bool is_playing() const;
  178. void set_animation(const StringName &p_animation);
  179. StringName get_animation() const;
  180. void set_frame(int p_frame);
  181. int get_frame() const;
  182. virtual Rect2 get_item_rect() const;
  183. virtual String get_configuration_warning() const;
  184. virtual void get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const;
  185. AnimatedSprite3D();
  186. };
  187. VARIANT_ENUM_CAST(SpriteBase3D::DrawFlags);
  188. VARIANT_ENUM_CAST(SpriteBase3D::AlphaCutMode);
  189. #endif // SPRITE_3D_H