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