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