1 /*************************************************************************/
2 /*  sprite_3d.h                                                          */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 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 
31 #ifndef SPRITE_3D_H
32 #define SPRITE_3D_H
33 
34 #include "scene/2d/animated_sprite.h"
35 #include "scene/3d/visual_instance.h"
36 
37 class SpriteBase3D : public GeometryInstance {
38 
39 	GDCLASS(SpriteBase3D, GeometryInstance);
40 
41 	mutable Ref<TriangleMesh> triangle_mesh; //cached
42 
43 public:
44 	enum DrawFlags {
45 		FLAG_TRANSPARENT,
46 		FLAG_SHADED,
47 		FLAG_DOUBLE_SIDED,
48 		FLAG_MAX
49 
50 	};
51 
52 	enum AlphaCutMode {
53 		ALPHA_CUT_DISABLED,
54 		ALPHA_CUT_DISCARD,
55 		ALPHA_CUT_OPAQUE_PREPASS
56 	};
57 
58 private:
59 	bool color_dirty;
60 	Color color_accum;
61 
62 	SpriteBase3D *parent_sprite;
63 	List<SpriteBase3D *> children;
64 	List<SpriteBase3D *>::Element *pI;
65 
66 	bool centered;
67 	Point2 offset;
68 
69 	bool hflip;
70 	bool vflip;
71 
72 	Color modulate;
73 	float opacity;
74 
75 	Vector3::Axis axis;
76 	float pixel_size;
77 	AABB aabb;
78 
79 	RID mesh;
80 	RID material;
81 
82 	bool flags[FLAG_MAX];
83 	AlphaCutMode alpha_cut;
84 	SpatialMaterial::BillboardMode billboard_mode;
85 	bool pending_update;
86 	void _im_update();
87 
88 	void _propagate_color_changed();
89 
90 protected:
91 	Color _get_color_accum();
92 	void _notification(int p_what);
93 	static void _bind_methods();
94 	virtual void _draw() = 0;
set_aabb(const AABB & p_aabb)95 	_FORCE_INLINE_ void set_aabb(const AABB &p_aabb) { aabb = p_aabb; }
get_mesh()96 	_FORCE_INLINE_ RID &get_mesh() { return mesh; }
get_material()97 	_FORCE_INLINE_ RID &get_material() { return material; }
98 
99 	uint32_t mesh_surface_offsets[VS::ARRAY_MAX];
100 	PoolByteArray mesh_buffer;
101 	uint32_t mesh_stride;
102 	uint32_t mesh_surface_format;
103 
104 	void _queue_update();
105 
106 public:
107 	void set_centered(bool p_center);
108 	bool is_centered() const;
109 
110 	void set_offset(const Point2 &p_offset);
111 	Point2 get_offset() const;
112 
113 	void set_flip_h(bool p_flip);
114 	bool is_flipped_h() const;
115 
116 	void set_flip_v(bool p_flip);
117 	bool is_flipped_v() const;
118 
119 	void set_region(bool p_region);
120 	bool is_region() const;
121 
122 	void set_region_rect(const Rect2 &p_region_rect);
123 	Rect2 get_region_rect() const;
124 
125 	void set_modulate(const Color &p_color);
126 	Color get_modulate() const;
127 
128 	void set_opacity(float p_amount);
129 	float get_opacity() const;
130 
131 	void set_pixel_size(float p_amount);
132 	float get_pixel_size() const;
133 
134 	void set_axis(Vector3::Axis p_axis);
135 	Vector3::Axis get_axis() const;
136 
137 	void set_draw_flag(DrawFlags p_flag, bool p_enable);
138 	bool get_draw_flag(DrawFlags p_flag) const;
139 
140 	void set_alpha_cut_mode(AlphaCutMode p_mode);
141 	AlphaCutMode get_alpha_cut_mode() const;
142 	void set_billboard_mode(SpatialMaterial::BillboardMode p_mode);
143 	SpatialMaterial::BillboardMode get_billboard_mode() const;
144 
145 	virtual Rect2 get_item_rect() const = 0;
146 
147 	virtual AABB get_aabb() const;
148 	virtual PoolVector<Face3> get_faces(uint32_t p_usage_flags) const;
149 	Ref<TriangleMesh> generate_triangle_mesh() const;
150 
151 	SpriteBase3D();
152 	~SpriteBase3D();
153 };
154 
155 class Sprite3D : public SpriteBase3D {
156 
157 	GDCLASS(Sprite3D, SpriteBase3D);
158 	Ref<Texture> texture;
159 
160 	bool region;
161 	Rect2 region_rect;
162 
163 	int frame;
164 
165 	int vframes;
166 	int hframes;
167 
168 protected:
169 	virtual void _draw();
170 	static void _bind_methods();
171 
172 	virtual void _validate_property(PropertyInfo &property) const;
173 
174 public:
175 	void set_texture(const Ref<Texture> &p_texture);
176 	Ref<Texture> get_texture() const;
177 
178 	void set_region(bool p_region);
179 	bool is_region() const;
180 
181 	void set_region_rect(const Rect2 &p_region_rect);
182 	Rect2 get_region_rect() const;
183 
184 	void set_frame(int p_frame);
185 	int get_frame() const;
186 
187 	void set_frame_coords(const Vector2 &p_coord);
188 	Vector2 get_frame_coords() const;
189 
190 	void set_vframes(int p_amount);
191 	int get_vframes() const;
192 
193 	void set_hframes(int p_amount);
194 	int get_hframes() const;
195 
196 	virtual Rect2 get_item_rect() const;
197 
198 	Sprite3D();
199 	//~Sprite3D();
200 };
201 
202 class AnimatedSprite3D : public SpriteBase3D {
203 
204 	GDCLASS(AnimatedSprite3D, SpriteBase3D);
205 
206 	Ref<SpriteFrames> frames;
207 	bool playing;
208 	StringName animation;
209 	int frame;
210 
211 	bool centered;
212 
213 	float timeout;
214 
215 	bool hflip;
216 	bool vflip;
217 
218 	Color modulate;
219 
220 	void _res_changed();
221 
222 	void _reset_timeout();
223 	void _set_playing(bool p_playing);
224 	bool _is_playing() const;
225 
226 protected:
227 	virtual void _draw();
228 	static void _bind_methods();
229 	void _notification(int p_what);
230 	virtual void _validate_property(PropertyInfo &property) const;
231 
232 public:
233 	void set_sprite_frames(const Ref<SpriteFrames> &p_frames);
234 	Ref<SpriteFrames> get_sprite_frames() const;
235 
236 	void play(const StringName &p_animation = StringName());
237 	void stop();
238 	bool is_playing() const;
239 
240 	void set_animation(const StringName &p_animation);
241 	StringName get_animation() const;
242 
243 	void set_frame(int p_frame);
244 	int get_frame() const;
245 
246 	virtual Rect2 get_item_rect() const;
247 
248 	virtual String get_configuration_warning() const;
249 	AnimatedSprite3D();
250 };
251 
252 VARIANT_ENUM_CAST(SpriteBase3D::DrawFlags);
253 VARIANT_ENUM_CAST(SpriteBase3D::AlphaCutMode);
254 #endif // SPRITE_3D_H
255