| 1 | /**************************************************************************/ |
| 2 | /* sprite_3d.cpp */ |
| 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 | #include "sprite_3d.h" |
| 32 | |
| 33 | #include "scene/resources/atlas_texture.h" |
| 34 | #include "scene/resources/mesh.h" |
| 35 | |
| 36 | Color SpriteBase3D::_get_color_accum() { |
| 37 | if (!color_dirty) { |
| 38 | return color_accum; |
| 39 | } |
| 40 | |
| 41 | if (parent_sprite) { |
| 42 | color_accum = parent_sprite->_get_color_accum(); |
| 43 | } else { |
| 44 | color_accum = Color(1, 1, 1, 1); |
| 45 | } |
| 46 | |
| 47 | color_accum.r *= modulate.r; |
| 48 | color_accum.g *= modulate.g; |
| 49 | color_accum.b *= modulate.b; |
| 50 | color_accum.a *= modulate.a; |
| 51 | color_dirty = false; |
| 52 | return color_accum; |
| 53 | } |
| 54 | |
| 55 | void SpriteBase3D::_propagate_color_changed() { |
| 56 | if (color_dirty) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | color_dirty = true; |
| 61 | _queue_redraw(); |
| 62 | |
| 63 | for (SpriteBase3D *&E : children) { |
| 64 | E->_propagate_color_changed(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void SpriteBase3D::_notification(int p_what) { |
| 69 | switch (p_what) { |
| 70 | case NOTIFICATION_ENTER_TREE: { |
| 71 | _im_update(); |
| 72 | } break; |
| 73 | |
| 74 | case NOTIFICATION_PARENTED: { |
| 75 | parent_sprite = Object::cast_to<SpriteBase3D>(get_parent()); |
| 76 | if (parent_sprite) { |
| 77 | pI = parent_sprite->children.push_back(this); |
| 78 | |
| 79 | _propagate_color_changed(); |
| 80 | } |
| 81 | } break; |
| 82 | |
| 83 | case NOTIFICATION_UNPARENTED: { |
| 84 | if (parent_sprite) { |
| 85 | parent_sprite->children.erase(pI); |
| 86 | pI = nullptr; |
| 87 | parent_sprite = nullptr; |
| 88 | |
| 89 | _propagate_color_changed(); |
| 90 | } |
| 91 | } break; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | void SpriteBase3D::draw_texture_rect(Ref<Texture2D> p_texture, Rect2 p_dst_rect, Rect2 p_src_rect) { |
| 96 | ERR_FAIL_COND(p_texture.is_null()); |
| 97 | |
| 98 | Rect2 final_rect; |
| 99 | Rect2 final_src_rect; |
| 100 | if (!p_texture->get_rect_region(p_dst_rect, p_src_rect, final_rect, final_src_rect)) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | if (final_rect.size.x == 0 || final_rect.size.y == 0) { |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | // 2D: 3D plane (axes match exactly when `axis == Vector3::AXIS_Z`): |
| 109 | // -X+ -X+ |
| 110 | // - + |
| 111 | // Y +--------+ +--------+ +--------+ Y +--------+ |
| 112 | // + | +--+ | | | (2) | | - | 0--1 | |
| 113 | // | |ab| | (1) | +--+ | (3) | 3--2 | | |ab| | |
| 114 | // | |cd| | --> | |ab| | --> | |cd| | <==> | |cd| | |
| 115 | // | +--+ | | |cd| | | |ab| | | 3--2 | |
| 116 | // | | | +--+ | | 0--1 | | | |
| 117 | // +--------+ +--------+ +--------+ +--------+ |
| 118 | |
| 119 | // (1) Y-wise shift `final_rect` within `p_dst_rect` so after inverting Y |
| 120 | // axis distances between top/bottom borders will be preserved (so for |
| 121 | // example AtlasTextures with vertical margins will look the same in 2D/3D). |
| 122 | final_rect.position.y = (p_dst_rect.position.y + p_dst_rect.size.y) - ((final_rect.position.y + final_rect.size.y) - p_dst_rect.position.y); |
| 123 | |
| 124 | Color color = _get_color_accum(); |
| 125 | |
| 126 | real_t px_size = get_pixel_size(); |
| 127 | |
| 128 | // (2) Order vertices (0123) bottom-top in 2D / top-bottom in 3D. |
| 129 | Vector2 vertices[4] = { |
| 130 | (final_rect.position + Vector2(0, final_rect.size.y)) * px_size, |
| 131 | (final_rect.position + final_rect.size) * px_size, |
| 132 | (final_rect.position + Vector2(final_rect.size.x, 0)) * px_size, |
| 133 | final_rect.position * px_size, |
| 134 | }; |
| 135 | |
| 136 | Vector2 src_tsize = p_texture->get_size(); |
| 137 | |
| 138 | // Properly setup UVs for impostor textures (AtlasTexture). |
| 139 | Ref<AtlasTexture> atlas_tex = p_texture; |
| 140 | if (atlas_tex.is_valid()) { |
| 141 | src_tsize[0] = atlas_tex->get_atlas()->get_width(); |
| 142 | src_tsize[1] = atlas_tex->get_atlas()->get_height(); |
| 143 | } |
| 144 | |
| 145 | // (3) Assign UVs (abcd) according to the vertices order (bottom-top in 2D / top-bottom in 3D). |
| 146 | Vector2 uvs[4] = { |
| 147 | final_src_rect.position / src_tsize, |
| 148 | (final_src_rect.position + Vector2(final_src_rect.size.x, 0)) / src_tsize, |
| 149 | (final_src_rect.position + final_src_rect.size) / src_tsize, |
| 150 | (final_src_rect.position + Vector2(0, final_src_rect.size.y)) / src_tsize, |
| 151 | }; |
| 152 | |
| 153 | if (is_flipped_h()) { |
| 154 | SWAP(uvs[0], uvs[1]); |
| 155 | SWAP(uvs[2], uvs[3]); |
| 156 | } |
| 157 | |
| 158 | if (is_flipped_v()) { |
| 159 | SWAP(uvs[0], uvs[3]); |
| 160 | SWAP(uvs[1], uvs[2]); |
| 161 | } |
| 162 | |
| 163 | bool texture_repeat = (MIN(uvs[0].x, uvs[2].x) < 0.0) || (MIN(uvs[0].y, uvs[2].y) < 0.0) || (MAX(uvs[0].x, uvs[2].x) > 1.0) || (MAX(uvs[0].y, uvs[2].y) > 1.0); |
| 164 | |
| 165 | Vector3 normal; |
| 166 | int ax = get_axis(); |
| 167 | normal[ax] = 1.0; |
| 168 | |
| 169 | Plane tangent; |
| 170 | if (ax == Vector3::AXIS_X) { |
| 171 | tangent = Plane(0, 0, -1, 1); |
| 172 | } else { |
| 173 | tangent = Plane(1, 0, 0, 1); |
| 174 | } |
| 175 | |
| 176 | int x_axis = ((ax + 1) % 3); |
| 177 | int y_axis = ((ax + 2) % 3); |
| 178 | |
| 179 | if (ax != Vector3::AXIS_Z) { |
| 180 | SWAP(x_axis, y_axis); |
| 181 | |
| 182 | for (int i = 0; i < 4; i++) { |
| 183 | //uvs[i] = Vector2(1.0,1.0)-uvs[i]; |
| 184 | //SWAP(vertices[i].x,vertices[i].y); |
| 185 | if (ax == Vector3::AXIS_Y) { |
| 186 | vertices[i].y = -vertices[i].y; |
| 187 | } else if (ax == Vector3::AXIS_X) { |
| 188 | vertices[i].x = -vertices[i].x; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | AABB aabb_new; |
| 194 | |
| 195 | // Everything except position and UV is compressed. |
| 196 | uint8_t *vertex_write_buffer = vertex_buffer.ptrw(); |
| 197 | uint8_t *attribute_write_buffer = attribute_buffer.ptrw(); |
| 198 | |
| 199 | uint32_t v_normal; |
| 200 | { |
| 201 | Vector2 res = normal.octahedron_encode(); |
| 202 | uint32_t value = 0; |
| 203 | value |= (uint16_t)CLAMP(res.x * 65535, 0, 65535); |
| 204 | value |= (uint16_t)CLAMP(res.y * 65535, 0, 65535) << 16; |
| 205 | |
| 206 | v_normal = value; |
| 207 | } |
| 208 | uint32_t v_tangent; |
| 209 | { |
| 210 | Plane t = tangent; |
| 211 | Vector2 res = t.normal.octahedron_tangent_encode(t.d); |
| 212 | uint32_t value = 0; |
| 213 | value |= (uint16_t)CLAMP(res.x * 65535, 0, 65535); |
| 214 | value |= (uint16_t)CLAMP(res.y * 65535, 0, 65535) << 16; |
| 215 | if (value == 4294901760) { |
| 216 | // (1, 1) and (0, 1) decode to the same value, but (0, 1) messes with our compression detection. |
| 217 | // So we sanitize here. |
| 218 | value = 4294967295; |
| 219 | } |
| 220 | |
| 221 | v_tangent = value; |
| 222 | } |
| 223 | |
| 224 | uint8_t v_color[4] = { |
| 225 | uint8_t(CLAMP(color.r * 255.0, 0.0, 255.0)), |
| 226 | uint8_t(CLAMP(color.g * 255.0, 0.0, 255.0)), |
| 227 | uint8_t(CLAMP(color.b * 255.0, 0.0, 255.0)), |
| 228 | uint8_t(CLAMP(color.a * 255.0, 0.0, 255.0)) |
| 229 | }; |
| 230 | |
| 231 | for (int i = 0; i < 4; i++) { |
| 232 | Vector3 vtx; |
| 233 | vtx[x_axis] = vertices[i][0]; |
| 234 | vtx[y_axis] = vertices[i][1]; |
| 235 | if (i == 0) { |
| 236 | aabb_new.position = vtx; |
| 237 | aabb_new.size = Vector3(); |
| 238 | } else { |
| 239 | aabb_new.expand_to(vtx); |
| 240 | } |
| 241 | |
| 242 | float v_uv[2] = { (float)uvs[i].x, (float)uvs[i].y }; |
| 243 | memcpy(&attribute_write_buffer[i * attrib_stride + mesh_surface_offsets[RS::ARRAY_TEX_UV]], v_uv, 8); |
| 244 | |
| 245 | float v_vertex[3] = { (float)vtx.x, (float)vtx.y, (float)vtx.z }; |
| 246 | |
| 247 | memcpy(&vertex_write_buffer[i * vertex_stride + mesh_surface_offsets[RS::ARRAY_VERTEX]], &v_vertex, sizeof(float) * 3); |
| 248 | memcpy(&vertex_write_buffer[i * normal_tangent_stride + mesh_surface_offsets[RS::ARRAY_NORMAL]], &v_normal, 4); |
| 249 | memcpy(&vertex_write_buffer[i * normal_tangent_stride + mesh_surface_offsets[RS::ARRAY_TANGENT]], &v_tangent, 4); |
| 250 | memcpy(&attribute_write_buffer[i * attrib_stride + mesh_surface_offsets[RS::ARRAY_COLOR]], v_color, 4); |
| 251 | } |
| 252 | |
| 253 | switch (get_billboard_mode()) { |
| 254 | case StandardMaterial3D::BILLBOARD_ENABLED: { |
| 255 | real_t size_new = MAX(Math::abs(final_rect.position.x) * px_size, (final_rect.position.x + final_rect.size.x) * px_size); |
| 256 | size_new = MAX(size_new, MAX(Math::abs(final_rect.position.y) * px_size, (final_rect.position.y + final_rect.size.y) * px_size)); |
| 257 | aabb_new.position = Vector3(-size_new, -size_new, -size_new); |
| 258 | aabb_new.size = Vector3(size_new * 2.0, size_new * 2.0, size_new * 2.0); |
| 259 | } break; |
| 260 | case StandardMaterial3D::BILLBOARD_FIXED_Y: { |
| 261 | real_t size_new = MAX(Math::abs(final_rect.position.x) * px_size, (final_rect.position.x + final_rect.size.x) * px_size); |
| 262 | if (ax == Vector3::AXIS_Y) { |
| 263 | size_new = MAX(size_new, MAX(Math::abs(final_rect.position.y) * px_size, (final_rect.position.y + final_rect.size.y) * px_size)); |
| 264 | } |
| 265 | aabb_new.position.x = -size_new; |
| 266 | aabb_new.position.z = -size_new; |
| 267 | aabb_new.size.x = size_new * 2.0; |
| 268 | aabb_new.size.z = size_new * 2.0; |
| 269 | } break; |
| 270 | default: |
| 271 | break; |
| 272 | } |
| 273 | |
| 274 | RID mesh_new = get_mesh(); |
| 275 | RS::get_singleton()->mesh_surface_update_vertex_region(mesh_new, 0, 0, vertex_buffer); |
| 276 | RS::get_singleton()->mesh_surface_update_attribute_region(mesh_new, 0, 0, attribute_buffer); |
| 277 | |
| 278 | RS::get_singleton()->mesh_set_custom_aabb(mesh_new, aabb_new); |
| 279 | set_aabb(aabb_new); |
| 280 | |
| 281 | RS::get_singleton()->material_set_param(get_material(), "alpha_scissor_threshold", alpha_scissor_threshold); |
| 282 | RS::get_singleton()->material_set_param(get_material(), "alpha_hash_scale", alpha_hash_scale); |
| 283 | RS::get_singleton()->material_set_param(get_material(), "alpha_antialiasing_edge", alpha_antialiasing_edge); |
| 284 | |
| 285 | BaseMaterial3D::Transparency mat_transparency = BaseMaterial3D::Transparency::TRANSPARENCY_DISABLED; |
| 286 | if (get_draw_flag(FLAG_TRANSPARENT)) { |
| 287 | if (get_alpha_cut_mode() == ALPHA_CUT_DISCARD) { |
| 288 | mat_transparency = BaseMaterial3D::Transparency::TRANSPARENCY_ALPHA_SCISSOR; |
| 289 | } else if (get_alpha_cut_mode() == ALPHA_CUT_OPAQUE_PREPASS) { |
| 290 | mat_transparency = BaseMaterial3D::Transparency::TRANSPARENCY_ALPHA_DEPTH_PRE_PASS; |
| 291 | } else if (get_alpha_cut_mode() == ALPHA_CUT_HASH) { |
| 292 | mat_transparency = BaseMaterial3D::Transparency::TRANSPARENCY_ALPHA_HASH; |
| 293 | } else { |
| 294 | mat_transparency = BaseMaterial3D::Transparency::TRANSPARENCY_ALPHA; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | RID shader_rid; |
| 299 | StandardMaterial3D::get_material_for_2d(get_draw_flag(FLAG_SHADED), mat_transparency, get_draw_flag(FLAG_DOUBLE_SIDED), get_billboard_mode() == StandardMaterial3D::BILLBOARD_ENABLED, get_billboard_mode() == StandardMaterial3D::BILLBOARD_FIXED_Y, false, get_draw_flag(FLAG_DISABLE_DEPTH_TEST), get_draw_flag(FLAG_FIXED_SIZE), get_texture_filter(), alpha_antialiasing_mode, texture_repeat, &shader_rid); |
| 300 | |
| 301 | if (last_shader != shader_rid) { |
| 302 | RS::get_singleton()->material_set_shader(get_material(), shader_rid); |
| 303 | last_shader = shader_rid; |
| 304 | } |
| 305 | if (last_texture != p_texture->get_rid()) { |
| 306 | RS::get_singleton()->material_set_param(get_material(), "texture_albedo", p_texture->get_rid()); |
| 307 | RS::get_singleton()->material_set_param(get_material(), "albedo_texture_size", Vector2i(p_texture->get_width(), p_texture->get_height())); |
| 308 | last_texture = p_texture->get_rid(); |
| 309 | } |
| 310 | if (get_alpha_cut_mode() == ALPHA_CUT_DISABLED) { |
| 311 | RS::get_singleton()->material_set_render_priority(get_material(), get_render_priority()); |
| 312 | RS::get_singleton()->mesh_surface_set_material(mesh, 0, get_material()); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | void SpriteBase3D::set_centered(bool p_center) { |
| 317 | if (centered == p_center) { |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | centered = p_center; |
| 322 | _queue_redraw(); |
| 323 | } |
| 324 | |
| 325 | bool SpriteBase3D::is_centered() const { |
| 326 | return centered; |
| 327 | } |
| 328 | |
| 329 | void SpriteBase3D::set_offset(const Point2 &p_offset) { |
| 330 | if (offset == p_offset) { |
| 331 | return; |
| 332 | } |
| 333 | |
| 334 | offset = p_offset; |
| 335 | _queue_redraw(); |
| 336 | } |
| 337 | |
| 338 | Point2 SpriteBase3D::get_offset() const { |
| 339 | return offset; |
| 340 | } |
| 341 | |
| 342 | void SpriteBase3D::set_flip_h(bool p_flip) { |
| 343 | if (hflip == p_flip) { |
| 344 | return; |
| 345 | } |
| 346 | |
| 347 | hflip = p_flip; |
| 348 | _queue_redraw(); |
| 349 | } |
| 350 | |
| 351 | bool SpriteBase3D::is_flipped_h() const { |
| 352 | return hflip; |
| 353 | } |
| 354 | |
| 355 | void SpriteBase3D::set_flip_v(bool p_flip) { |
| 356 | if (vflip == p_flip) { |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | vflip = p_flip; |
| 361 | _queue_redraw(); |
| 362 | } |
| 363 | |
| 364 | bool SpriteBase3D::is_flipped_v() const { |
| 365 | return vflip; |
| 366 | } |
| 367 | |
| 368 | void SpriteBase3D::set_modulate(const Color &p_color) { |
| 369 | if (modulate == p_color) { |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | modulate = p_color; |
| 374 | _propagate_color_changed(); |
| 375 | _queue_redraw(); |
| 376 | } |
| 377 | |
| 378 | Color SpriteBase3D::get_modulate() const { |
| 379 | return modulate; |
| 380 | } |
| 381 | |
| 382 | void SpriteBase3D::set_render_priority(int p_priority) { |
| 383 | ERR_FAIL_COND(p_priority < RS::MATERIAL_RENDER_PRIORITY_MIN || p_priority > RS::MATERIAL_RENDER_PRIORITY_MAX); |
| 384 | |
| 385 | if (render_priority == p_priority) { |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | render_priority = p_priority; |
| 390 | _queue_redraw(); |
| 391 | } |
| 392 | |
| 393 | int SpriteBase3D::get_render_priority() const { |
| 394 | return render_priority; |
| 395 | } |
| 396 | |
| 397 | void SpriteBase3D::set_pixel_size(real_t p_amount) { |
| 398 | if (pixel_size == p_amount) { |
| 399 | return; |
| 400 | } |
| 401 | |
| 402 | pixel_size = p_amount; |
| 403 | _queue_redraw(); |
| 404 | } |
| 405 | |
| 406 | real_t SpriteBase3D::get_pixel_size() const { |
| 407 | return pixel_size; |
| 408 | } |
| 409 | |
| 410 | void SpriteBase3D::set_axis(Vector3::Axis p_axis) { |
| 411 | ERR_FAIL_INDEX(p_axis, 3); |
| 412 | |
| 413 | if (axis == p_axis) { |
| 414 | return; |
| 415 | } |
| 416 | |
| 417 | axis = p_axis; |
| 418 | _queue_redraw(); |
| 419 | } |
| 420 | |
| 421 | Vector3::Axis SpriteBase3D::get_axis() const { |
| 422 | return axis; |
| 423 | } |
| 424 | |
| 425 | void SpriteBase3D::_im_update() { |
| 426 | if (!redraw_needed) { |
| 427 | return; |
| 428 | } |
| 429 | _draw(); |
| 430 | redraw_needed = false; |
| 431 | |
| 432 | pending_update = false; |
| 433 | } |
| 434 | |
| 435 | void SpriteBase3D::_queue_redraw() { |
| 436 | // The 3D equivalent of CanvasItem.queue_redraw(). |
| 437 | redraw_needed = true; |
| 438 | if (!is_inside_tree()) { |
| 439 | return; |
| 440 | } |
| 441 | if (pending_update) { |
| 442 | return; |
| 443 | } |
| 444 | |
| 445 | triangle_mesh.unref(); |
| 446 | update_gizmos(); |
| 447 | |
| 448 | pending_update = true; |
| 449 | callable_mp(this, &SpriteBase3D::_im_update).call_deferred(); |
| 450 | } |
| 451 | |
| 452 | AABB SpriteBase3D::get_aabb() const { |
| 453 | return aabb; |
| 454 | } |
| 455 | |
| 456 | Ref<TriangleMesh> SpriteBase3D::generate_triangle_mesh() const { |
| 457 | if (triangle_mesh.is_valid()) { |
| 458 | return triangle_mesh; |
| 459 | } |
| 460 | |
| 461 | Vector<Vector3> faces; |
| 462 | faces.resize(6); |
| 463 | Vector3 *facesw = faces.ptrw(); |
| 464 | |
| 465 | Rect2 final_rect = get_item_rect(); |
| 466 | |
| 467 | if (final_rect.size.x == 0 || final_rect.size.y == 0) { |
| 468 | return Ref<TriangleMesh>(); |
| 469 | } |
| 470 | |
| 471 | real_t px_size = get_pixel_size(); |
| 472 | |
| 473 | Vector2 vertices[4] = { |
| 474 | (final_rect.position + Vector2(0, final_rect.size.y)) * px_size, |
| 475 | (final_rect.position + final_rect.size) * px_size, |
| 476 | (final_rect.position + Vector2(final_rect.size.x, 0)) * px_size, |
| 477 | final_rect.position * px_size, |
| 478 | }; |
| 479 | |
| 480 | int x_axis = ((axis + 1) % 3); |
| 481 | int y_axis = ((axis + 2) % 3); |
| 482 | |
| 483 | if (axis != Vector3::AXIS_Z) { |
| 484 | SWAP(x_axis, y_axis); |
| 485 | |
| 486 | for (int i = 0; i < 4; i++) { |
| 487 | if (axis == Vector3::AXIS_Y) { |
| 488 | vertices[i].y = -vertices[i].y; |
| 489 | } else if (axis == Vector3::AXIS_X) { |
| 490 | vertices[i].x = -vertices[i].x; |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | static const int indices[6] = { |
| 496 | 0, 1, 2, |
| 497 | 0, 2, 3 |
| 498 | }; |
| 499 | |
| 500 | for (int j = 0; j < 6; j++) { |
| 501 | int i = indices[j]; |
| 502 | Vector3 vtx; |
| 503 | vtx[x_axis] = vertices[i][0]; |
| 504 | vtx[y_axis] = vertices[i][1]; |
| 505 | facesw[j] = vtx; |
| 506 | } |
| 507 | |
| 508 | triangle_mesh.instantiate(); |
| 509 | triangle_mesh->create(faces); |
| 510 | |
| 511 | return triangle_mesh; |
| 512 | } |
| 513 | |
| 514 | void SpriteBase3D::set_draw_flag(DrawFlags p_flag, bool p_enable) { |
| 515 | ERR_FAIL_INDEX(p_flag, FLAG_MAX); |
| 516 | |
| 517 | if (flags[p_flag] == p_enable) { |
| 518 | return; |
| 519 | } |
| 520 | |
| 521 | flags[p_flag] = p_enable; |
| 522 | _queue_redraw(); |
| 523 | } |
| 524 | |
| 525 | bool SpriteBase3D::get_draw_flag(DrawFlags p_flag) const { |
| 526 | ERR_FAIL_INDEX_V(p_flag, FLAG_MAX, false); |
| 527 | return flags[p_flag]; |
| 528 | } |
| 529 | |
| 530 | void SpriteBase3D::set_alpha_cut_mode(AlphaCutMode p_mode) { |
| 531 | ERR_FAIL_INDEX(p_mode, ALPHA_CUT_MAX); |
| 532 | |
| 533 | if (alpha_cut == p_mode) { |
| 534 | return; |
| 535 | } |
| 536 | |
| 537 | alpha_cut = p_mode; |
| 538 | _queue_redraw(); |
| 539 | } |
| 540 | |
| 541 | SpriteBase3D::AlphaCutMode SpriteBase3D::get_alpha_cut_mode() const { |
| 542 | return alpha_cut; |
| 543 | } |
| 544 | |
| 545 | void SpriteBase3D::set_alpha_hash_scale(float p_hash_scale) { |
| 546 | if (alpha_hash_scale == p_hash_scale) { |
| 547 | return; |
| 548 | } |
| 549 | |
| 550 | alpha_hash_scale = p_hash_scale; |
| 551 | _queue_redraw(); |
| 552 | } |
| 553 | |
| 554 | float SpriteBase3D::get_alpha_hash_scale() const { |
| 555 | return alpha_hash_scale; |
| 556 | } |
| 557 | |
| 558 | void SpriteBase3D::set_alpha_scissor_threshold(float p_threshold) { |
| 559 | if (alpha_scissor_threshold == p_threshold) { |
| 560 | return; |
| 561 | } |
| 562 | |
| 563 | alpha_scissor_threshold = p_threshold; |
| 564 | _queue_redraw(); |
| 565 | } |
| 566 | |
| 567 | float SpriteBase3D::get_alpha_scissor_threshold() const { |
| 568 | return alpha_scissor_threshold; |
| 569 | } |
| 570 | |
| 571 | void SpriteBase3D::set_alpha_antialiasing(BaseMaterial3D::AlphaAntiAliasing p_alpha_aa) { |
| 572 | if (alpha_antialiasing_mode == p_alpha_aa) { |
| 573 | return; |
| 574 | } |
| 575 | |
| 576 | alpha_antialiasing_mode = p_alpha_aa; |
| 577 | _queue_redraw(); |
| 578 | } |
| 579 | |
| 580 | BaseMaterial3D::AlphaAntiAliasing SpriteBase3D::get_alpha_antialiasing() const { |
| 581 | return alpha_antialiasing_mode; |
| 582 | } |
| 583 | |
| 584 | void SpriteBase3D::set_alpha_antialiasing_edge(float p_edge) { |
| 585 | if (alpha_antialiasing_edge == p_edge) { |
| 586 | return; |
| 587 | } |
| 588 | |
| 589 | alpha_antialiasing_edge = p_edge; |
| 590 | _queue_redraw(); |
| 591 | } |
| 592 | |
| 593 | float SpriteBase3D::get_alpha_antialiasing_edge() const { |
| 594 | return alpha_antialiasing_edge; |
| 595 | } |
| 596 | |
| 597 | void SpriteBase3D::set_billboard_mode(StandardMaterial3D::BillboardMode p_mode) { |
| 598 | ERR_FAIL_INDEX(p_mode, 3); // Cannot use BILLBOARD_PARTICLES. |
| 599 | |
| 600 | if (billboard_mode == p_mode) { |
| 601 | return; |
| 602 | } |
| 603 | |
| 604 | billboard_mode = p_mode; |
| 605 | _queue_redraw(); |
| 606 | } |
| 607 | |
| 608 | StandardMaterial3D::BillboardMode SpriteBase3D::get_billboard_mode() const { |
| 609 | return billboard_mode; |
| 610 | } |
| 611 | |
| 612 | void SpriteBase3D::set_texture_filter(StandardMaterial3D::TextureFilter p_filter) { |
| 613 | if (texture_filter == p_filter) { |
| 614 | return; |
| 615 | } |
| 616 | |
| 617 | texture_filter = p_filter; |
| 618 | _queue_redraw(); |
| 619 | } |
| 620 | |
| 621 | StandardMaterial3D::TextureFilter SpriteBase3D::get_texture_filter() const { |
| 622 | return texture_filter; |
| 623 | } |
| 624 | |
| 625 | void SpriteBase3D::_bind_methods() { |
| 626 | ClassDB::bind_method(D_METHOD("set_centered", "centered"), &SpriteBase3D::set_centered); |
| 627 | ClassDB::bind_method(D_METHOD("is_centered"), &SpriteBase3D::is_centered); |
| 628 | |
| 629 | ClassDB::bind_method(D_METHOD("set_offset", "offset"), &SpriteBase3D::set_offset); |
| 630 | ClassDB::bind_method(D_METHOD("get_offset"), &SpriteBase3D::get_offset); |
| 631 | |
| 632 | ClassDB::bind_method(D_METHOD("set_flip_h", "flip_h"), &SpriteBase3D::set_flip_h); |
| 633 | ClassDB::bind_method(D_METHOD("is_flipped_h"), &SpriteBase3D::is_flipped_h); |
| 634 | |
| 635 | ClassDB::bind_method(D_METHOD("set_flip_v", "flip_v"), &SpriteBase3D::set_flip_v); |
| 636 | ClassDB::bind_method(D_METHOD("is_flipped_v"), &SpriteBase3D::is_flipped_v); |
| 637 | |
| 638 | ClassDB::bind_method(D_METHOD("set_modulate", "modulate"), &SpriteBase3D::set_modulate); |
| 639 | ClassDB::bind_method(D_METHOD("get_modulate"), &SpriteBase3D::get_modulate); |
| 640 | |
| 641 | ClassDB::bind_method(D_METHOD("set_render_priority", "priority"), &SpriteBase3D::set_render_priority); |
| 642 | ClassDB::bind_method(D_METHOD("get_render_priority"), &SpriteBase3D::get_render_priority); |
| 643 | |
| 644 | ClassDB::bind_method(D_METHOD("set_pixel_size", "pixel_size"), &SpriteBase3D::set_pixel_size); |
| 645 | ClassDB::bind_method(D_METHOD("get_pixel_size"), &SpriteBase3D::get_pixel_size); |
| 646 | |
| 647 | ClassDB::bind_method(D_METHOD("set_axis", "axis"), &SpriteBase3D::set_axis); |
| 648 | ClassDB::bind_method(D_METHOD("get_axis"), &SpriteBase3D::get_axis); |
| 649 | |
| 650 | ClassDB::bind_method(D_METHOD("set_draw_flag", "flag", "enabled"), &SpriteBase3D::set_draw_flag); |
| 651 | ClassDB::bind_method(D_METHOD("get_draw_flag", "flag"), &SpriteBase3D::get_draw_flag); |
| 652 | |
| 653 | ClassDB::bind_method(D_METHOD("set_alpha_cut_mode", "mode"), &SpriteBase3D::set_alpha_cut_mode); |
| 654 | ClassDB::bind_method(D_METHOD("get_alpha_cut_mode"), &SpriteBase3D::get_alpha_cut_mode); |
| 655 | |
| 656 | ClassDB::bind_method(D_METHOD("set_alpha_scissor_threshold", "threshold"), &SpriteBase3D::set_alpha_scissor_threshold); |
| 657 | ClassDB::bind_method(D_METHOD("get_alpha_scissor_threshold"), &SpriteBase3D::get_alpha_scissor_threshold); |
| 658 | |
| 659 | ClassDB::bind_method(D_METHOD("set_alpha_hash_scale", "threshold"), &SpriteBase3D::set_alpha_hash_scale); |
| 660 | ClassDB::bind_method(D_METHOD("get_alpha_hash_scale"), &SpriteBase3D::get_alpha_hash_scale); |
| 661 | |
| 662 | ClassDB::bind_method(D_METHOD("set_alpha_antialiasing", "alpha_aa"), &SpriteBase3D::set_alpha_antialiasing); |
| 663 | ClassDB::bind_method(D_METHOD("get_alpha_antialiasing"), &SpriteBase3D::get_alpha_antialiasing); |
| 664 | |
| 665 | ClassDB::bind_method(D_METHOD("set_alpha_antialiasing_edge", "edge"), &SpriteBase3D::set_alpha_antialiasing_edge); |
| 666 | ClassDB::bind_method(D_METHOD("get_alpha_antialiasing_edge"), &SpriteBase3D::get_alpha_antialiasing_edge); |
| 667 | |
| 668 | ClassDB::bind_method(D_METHOD("set_billboard_mode", "mode"), &SpriteBase3D::set_billboard_mode); |
| 669 | ClassDB::bind_method(D_METHOD("get_billboard_mode"), &SpriteBase3D::get_billboard_mode); |
| 670 | |
| 671 | ClassDB::bind_method(D_METHOD("set_texture_filter", "mode"), &SpriteBase3D::set_texture_filter); |
| 672 | ClassDB::bind_method(D_METHOD("get_texture_filter"), &SpriteBase3D::get_texture_filter); |
| 673 | |
| 674 | ClassDB::bind_method(D_METHOD("get_item_rect"), &SpriteBase3D::get_item_rect); |
| 675 | ClassDB::bind_method(D_METHOD("generate_triangle_mesh"), &SpriteBase3D::generate_triangle_mesh); |
| 676 | |
| 677 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "centered"), "set_centered", "is_centered"); |
| 678 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset", PROPERTY_HINT_NONE, "suffix:px"), "set_offset", "get_offset"); |
| 679 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_h"), "set_flip_h", "is_flipped_h"); |
| 680 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_v"), "set_flip_v", "is_flipped_v"); |
| 681 | ADD_PROPERTY(PropertyInfo(Variant::COLOR, "modulate"), "set_modulate", "get_modulate"); |
| 682 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "pixel_size", PROPERTY_HINT_RANGE, "0.0001,128,0.0000001,suffix:m"), "set_pixel_size", "get_pixel_size"); |
| 683 | ADD_PROPERTY(PropertyInfo(Variant::INT, "axis", PROPERTY_HINT_ENUM, "X-Axis,Y-Axis,Z-Axis"), "set_axis", "get_axis"); |
| 684 | ADD_GROUP("Flags", ""); |
| 685 | ADD_PROPERTY(PropertyInfo(Variant::INT, "billboard", PROPERTY_HINT_ENUM, "Disabled,Enabled,Y-Billboard"), "set_billboard_mode", "get_billboard_mode"); |
| 686 | ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "transparent"), "set_draw_flag", "get_draw_flag", FLAG_TRANSPARENT); |
| 687 | ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "shaded"), "set_draw_flag", "get_draw_flag", FLAG_SHADED); |
| 688 | ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "double_sided"), "set_draw_flag", "get_draw_flag", FLAG_DOUBLE_SIDED); |
| 689 | ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "no_depth_test"), "set_draw_flag", "get_draw_flag", FLAG_DISABLE_DEPTH_TEST); |
| 690 | ADD_PROPERTYI(PropertyInfo(Variant::BOOL, "fixed_size"), "set_draw_flag", "get_draw_flag", FLAG_FIXED_SIZE); |
| 691 | ADD_PROPERTY(PropertyInfo(Variant::INT, "alpha_cut", PROPERTY_HINT_ENUM, "Disabled,Discard,Opaque Pre-Pass,Alpha Hash"), "set_alpha_cut_mode", "get_alpha_cut_mode"); |
| 692 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "alpha_scissor_threshold", PROPERTY_HINT_RANGE, "0,1,0.001"), "set_alpha_scissor_threshold", "get_alpha_scissor_threshold"); |
| 693 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "alpha_hash_scale", PROPERTY_HINT_RANGE, "0,2,0.01"), "set_alpha_hash_scale", "get_alpha_hash_scale"); |
| 694 | ADD_PROPERTY(PropertyInfo(Variant::INT, "alpha_antialiasing_mode", PROPERTY_HINT_ENUM, "Disabled,Alpha Edge Blend,Alpha Edge Clip"), "set_alpha_antialiasing", "get_alpha_antialiasing"); |
| 695 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "alpha_antialiasing_edge", PROPERTY_HINT_RANGE, "0,1,0.01"), "set_alpha_antialiasing_edge", "get_alpha_antialiasing_edge"); |
| 696 | ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_filter", PROPERTY_HINT_ENUM, "Nearest,Linear,Nearest Mipmap,Linear Mipmap,Nearest Mipmap Anisotropic,Linear Mipmap Anisotropic"), "set_texture_filter", "get_texture_filter"); |
| 697 | ADD_PROPERTY(PropertyInfo(Variant::INT, "render_priority", PROPERTY_HINT_RANGE, itos(RS::MATERIAL_RENDER_PRIORITY_MIN) + "," + itos(RS::MATERIAL_RENDER_PRIORITY_MAX) + ",1"), "set_render_priority", "get_render_priority"); |
| 698 | |
| 699 | BIND_ENUM_CONSTANT(FLAG_TRANSPARENT); |
| 700 | BIND_ENUM_CONSTANT(FLAG_SHADED); |
| 701 | BIND_ENUM_CONSTANT(FLAG_DOUBLE_SIDED); |
| 702 | BIND_ENUM_CONSTANT(FLAG_DISABLE_DEPTH_TEST); |
| 703 | BIND_ENUM_CONSTANT(FLAG_FIXED_SIZE); |
| 704 | BIND_ENUM_CONSTANT(FLAG_MAX); |
| 705 | |
| 706 | BIND_ENUM_CONSTANT(ALPHA_CUT_DISABLED); |
| 707 | BIND_ENUM_CONSTANT(ALPHA_CUT_DISCARD); |
| 708 | BIND_ENUM_CONSTANT(ALPHA_CUT_OPAQUE_PREPASS); |
| 709 | BIND_ENUM_CONSTANT(ALPHA_CUT_HASH); |
| 710 | } |
| 711 | |
| 712 | SpriteBase3D::SpriteBase3D() { |
| 713 | for (int i = 0; i < FLAG_MAX; i++) { |
| 714 | flags[i] = i == FLAG_TRANSPARENT || i == FLAG_DOUBLE_SIDED; |
| 715 | } |
| 716 | |
| 717 | material = RenderingServer::get_singleton()->material_create(); |
| 718 | // Set defaults for material, names need to match up those in StandardMaterial3D. |
| 719 | RS::get_singleton()->material_set_param(material, "albedo", Color(1, 1, 1, 1)); |
| 720 | RS::get_singleton()->material_set_param(material, "specular", 0.5); |
| 721 | RS::get_singleton()->material_set_param(material, "metallic", 0.0); |
| 722 | RS::get_singleton()->material_set_param(material, "roughness", 1.0); |
| 723 | RS::get_singleton()->material_set_param(material, "uv1_offset", Vector3(0, 0, 0)); |
| 724 | RS::get_singleton()->material_set_param(material, "uv1_scale", Vector3(1, 1, 1)); |
| 725 | RS::get_singleton()->material_set_param(material, "uv2_offset", Vector3(0, 0, 0)); |
| 726 | RS::get_singleton()->material_set_param(material, "uv2_scale", Vector3(1, 1, 1)); |
| 727 | |
| 728 | mesh = RenderingServer::get_singleton()->mesh_create(); |
| 729 | |
| 730 | PackedVector3Array mesh_vertices; |
| 731 | PackedVector3Array mesh_normals; |
| 732 | PackedFloat32Array mesh_tangents; |
| 733 | PackedColorArray mesh_colors; |
| 734 | PackedVector2Array mesh_uvs; |
| 735 | PackedInt32Array indices; |
| 736 | |
| 737 | mesh_vertices.resize(4); |
| 738 | mesh_normals.resize(4); |
| 739 | mesh_tangents.resize(16); |
| 740 | mesh_colors.resize(4); |
| 741 | mesh_uvs.resize(4); |
| 742 | |
| 743 | // Create basic mesh and store format information. |
| 744 | for (int i = 0; i < 4; i++) { |
| 745 | mesh_normals.write[i] = Vector3(0.0, 0.0, 1.0); |
| 746 | mesh_tangents.write[i * 4 + 0] = 1.0; |
| 747 | mesh_tangents.write[i * 4 + 1] = 0.0; |
| 748 | mesh_tangents.write[i * 4 + 2] = 0.0; |
| 749 | mesh_tangents.write[i * 4 + 3] = 1.0; |
| 750 | mesh_colors.write[i] = Color(1.0, 1.0, 1.0, 1.0); |
| 751 | mesh_uvs.write[i] = Vector2(0.0, 0.0); |
| 752 | mesh_vertices.write[i] = Vector3(0.0, 0.0, 0.0); |
| 753 | } |
| 754 | |
| 755 | indices.resize(6); |
| 756 | indices.write[0] = 0; |
| 757 | indices.write[1] = 1; |
| 758 | indices.write[2] = 2; |
| 759 | indices.write[3] = 0; |
| 760 | indices.write[4] = 2; |
| 761 | indices.write[5] = 3; |
| 762 | |
| 763 | Array mesh_array; |
| 764 | mesh_array.resize(RS::ARRAY_MAX); |
| 765 | mesh_array[RS::ARRAY_VERTEX] = mesh_vertices; |
| 766 | mesh_array[RS::ARRAY_NORMAL] = mesh_normals; |
| 767 | mesh_array[RS::ARRAY_TANGENT] = mesh_tangents; |
| 768 | mesh_array[RS::ARRAY_COLOR] = mesh_colors; |
| 769 | mesh_array[RS::ARRAY_TEX_UV] = mesh_uvs; |
| 770 | mesh_array[RS::ARRAY_INDEX] = indices; |
| 771 | |
| 772 | RS::SurfaceData sd; |
| 773 | RS::get_singleton()->mesh_create_surface_data_from_arrays(&sd, RS::PRIMITIVE_TRIANGLES, mesh_array); |
| 774 | |
| 775 | mesh_surface_format = sd.format; |
| 776 | vertex_buffer = sd.vertex_data; |
| 777 | attribute_buffer = sd.attribute_data; |
| 778 | |
| 779 | sd.material = material; |
| 780 | |
| 781 | RS::get_singleton()->mesh_surface_make_offsets_from_format(sd.format, sd.vertex_count, sd.index_count, mesh_surface_offsets, vertex_stride, normal_tangent_stride, attrib_stride, skin_stride); |
| 782 | RS::get_singleton()->mesh_add_surface(mesh, sd); |
| 783 | set_base(mesh); |
| 784 | } |
| 785 | |
| 786 | SpriteBase3D::~SpriteBase3D() { |
| 787 | ERR_FAIL_NULL(RenderingServer::get_singleton()); |
| 788 | RenderingServer::get_singleton()->free_rid(mesh); |
| 789 | RenderingServer::get_singleton()->free_rid(material); |
| 790 | } |
| 791 | |
| 792 | /////////////////////////////////////////// |
| 793 | |
| 794 | void Sprite3D::_draw() { |
| 795 | if (get_base() != get_mesh()) { |
| 796 | set_base(get_mesh()); |
| 797 | } |
| 798 | if (texture.is_null()) { |
| 799 | set_base(RID()); |
| 800 | return; |
| 801 | } |
| 802 | Vector2 tsize = texture->get_size(); |
| 803 | if (tsize.x == 0 || tsize.y == 0) { |
| 804 | return; |
| 805 | } |
| 806 | |
| 807 | Rect2 base_rect; |
| 808 | if (region) { |
| 809 | base_rect = region_rect; |
| 810 | } else { |
| 811 | base_rect = Rect2(0, 0, texture->get_width(), texture->get_height()); |
| 812 | } |
| 813 | |
| 814 | Size2 frame_size = base_rect.size / Size2(hframes, vframes); |
| 815 | Point2 frame_offset = Point2(frame % hframes, frame / hframes) * frame_size; |
| 816 | |
| 817 | Point2 dst_offset = get_offset(); |
| 818 | if (is_centered()) { |
| 819 | dst_offset -= frame_size / 2.0f; |
| 820 | } |
| 821 | |
| 822 | Rect2 src_rect(base_rect.position + frame_offset, frame_size); |
| 823 | Rect2 dst_rect(dst_offset, frame_size); |
| 824 | |
| 825 | draw_texture_rect(texture, dst_rect, src_rect); |
| 826 | } |
| 827 | |
| 828 | void Sprite3D::set_texture(const Ref<Texture2D> &p_texture) { |
| 829 | if (p_texture == texture) { |
| 830 | return; |
| 831 | } |
| 832 | if (texture.is_valid()) { |
| 833 | texture->disconnect(CoreStringName(changed), callable_mp((SpriteBase3D *)this, &Sprite3D::_queue_redraw)); |
| 834 | } |
| 835 | texture = p_texture; |
| 836 | if (texture.is_valid()) { |
| 837 | texture->connect(CoreStringName(changed), callable_mp((SpriteBase3D *)this, &Sprite3D::_queue_redraw)); |
| 838 | } |
| 839 | |
| 840 | _queue_redraw(); |
| 841 | emit_signal(SceneStringName(texture_changed)); |
| 842 | } |
| 843 | |
| 844 | Ref<Texture2D> Sprite3D::get_texture() const { |
| 845 | return texture; |
| 846 | } |
| 847 | |
| 848 | void Sprite3D::set_region_enabled(bool p_region) { |
| 849 | if (p_region == region) { |
| 850 | return; |
| 851 | } |
| 852 | |
| 853 | region = p_region; |
| 854 | _queue_redraw(); |
| 855 | notify_property_list_changed(); |
| 856 | } |
| 857 | |
| 858 | bool Sprite3D::is_region_enabled() const { |
| 859 | return region; |
| 860 | } |
| 861 | |
| 862 | void Sprite3D::set_region_rect(const Rect2 &p_region_rect) { |
| 863 | if (region_rect == p_region_rect) { |
| 864 | return; |
| 865 | } |
| 866 | |
| 867 | region_rect = p_region_rect; |
| 868 | if (region) { |
| 869 | _queue_redraw(); |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | Rect2 Sprite3D::get_region_rect() const { |
| 874 | return region_rect; |
| 875 | } |
| 876 | |
| 877 | void Sprite3D::set_frame(int p_frame) { |
| 878 | ERR_FAIL_INDEX(p_frame, int64_t(vframes) * hframes); |
| 879 | |
| 880 | if (frame == p_frame) { |
| 881 | return; |
| 882 | } |
| 883 | |
| 884 | frame = p_frame; |
| 885 | _queue_redraw(); |
| 886 | emit_signal(SceneStringName(frame_changed)); |
| 887 | } |
| 888 | |
| 889 | int Sprite3D::get_frame() const { |
| 890 | return frame; |
| 891 | } |
| 892 | |
| 893 | void Sprite3D::set_frame_coords(const Vector2i &p_coord) { |
| 894 | ERR_FAIL_INDEX(p_coord.x, hframes); |
| 895 | ERR_FAIL_INDEX(p_coord.y, vframes); |
| 896 | |
| 897 | set_frame(p_coord.y * hframes + p_coord.x); |
| 898 | } |
| 899 | |
| 900 | Vector2i Sprite3D::get_frame_coords() const { |
| 901 | return Vector2i(frame % hframes, frame / hframes); |
| 902 | } |
| 903 | |
| 904 | void Sprite3D::set_vframes(int p_amount) { |
| 905 | ERR_FAIL_COND_MSG(p_amount < 1, "Amount of vframes cannot be smaller than 1."); |
| 906 | |
| 907 | if (vframes == p_amount) { |
| 908 | return; |
| 909 | } |
| 910 | |
| 911 | vframes = p_amount; |
| 912 | if (frame >= vframes * hframes) { |
| 913 | frame = 0; |
| 914 | } |
| 915 | _queue_redraw(); |
| 916 | notify_property_list_changed(); |
| 917 | } |
| 918 | |
| 919 | int Sprite3D::get_vframes() const { |
| 920 | return vframes; |
| 921 | } |
| 922 | |
| 923 | void Sprite3D::set_hframes(int p_amount) { |
| 924 | ERR_FAIL_COND_MSG(p_amount < 1, "Amount of hframes cannot be smaller than 1."); |
| 925 | |
| 926 | if (hframes == p_amount) { |
| 927 | return; |
| 928 | } |
| 929 | |
| 930 | if (vframes > 1) { |
| 931 | // Adjust the frame to fit new sheet dimensions. |
| 932 | int original_column = frame % hframes; |
| 933 | if (original_column >= p_amount) { |
| 934 | // Frame's column was dropped, reset. |
| 935 | frame = 0; |
| 936 | } else { |
| 937 | int original_row = frame / hframes; |
| 938 | frame = original_row * p_amount + original_column; |
| 939 | } |
| 940 | } |
| 941 | hframes = p_amount; |
| 942 | if (frame >= vframes * hframes) { |
| 943 | frame = 0; |
| 944 | } |
| 945 | _queue_redraw(); |
| 946 | notify_property_list_changed(); |
| 947 | } |
| 948 | |
| 949 | int Sprite3D::get_hframes() const { |
| 950 | return hframes; |
| 951 | } |
| 952 | |
| 953 | Rect2 Sprite3D::get_item_rect() const { |
| 954 | if (texture.is_null()) { |
| 955 | return Rect2(0, 0, 1, 1); |
| 956 | } |
| 957 | |
| 958 | Size2 s; |
| 959 | |
| 960 | if (region) { |
| 961 | s = region_rect.size; |
| 962 | } else { |
| 963 | s = texture->get_size(); |
| 964 | s = s / Point2(hframes, vframes); |
| 965 | } |
| 966 | |
| 967 | Point2 ofs = get_offset(); |
| 968 | if (is_centered()) { |
| 969 | ofs -= s / 2; |
| 970 | } |
| 971 | |
| 972 | if (s == Size2(0, 0)) { |
| 973 | s = Size2(1, 1); |
| 974 | } |
| 975 | |
| 976 | return Rect2(ofs, s); |
| 977 | } |
| 978 | |
| 979 | void Sprite3D::_validate_property(PropertyInfo &p_property) const { |
| 980 | if (!Engine::get_singleton()->is_editor_hint()) { |
| 981 | return; |
| 982 | } |
| 983 | if (p_property.name == "frame") { |
| 984 | p_property.hint = PROPERTY_HINT_RANGE; |
| 985 | p_property.hint_string = "0," + itos(vframes * hframes - 1) + ",1"; |
| 986 | p_property.usage |= PROPERTY_USAGE_KEYING_INCREMENTS; |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | void Sprite3D::_bind_methods() { |
| 991 | ClassDB::bind_method(D_METHOD("set_texture", "texture"), &Sprite3D::set_texture); |
| 992 | ClassDB::bind_method(D_METHOD("get_texture"), &Sprite3D::get_texture); |
| 993 | |
| 994 | ClassDB::bind_method(D_METHOD("set_region_enabled", "enabled"), &Sprite3D::set_region_enabled); |
| 995 | ClassDB::bind_method(D_METHOD("is_region_enabled"), &Sprite3D::is_region_enabled); |
| 996 | |
| 997 | ClassDB::bind_method(D_METHOD("set_region_rect", "rect"), &Sprite3D::set_region_rect); |
| 998 | ClassDB::bind_method(D_METHOD("get_region_rect"), &Sprite3D::get_region_rect); |
| 999 | |
| 1000 | ClassDB::bind_method(D_METHOD("set_frame", "frame"), &Sprite3D::set_frame); |
| 1001 | ClassDB::bind_method(D_METHOD("get_frame"), &Sprite3D::get_frame); |
| 1002 | |
| 1003 | ClassDB::bind_method(D_METHOD("set_frame_coords", "coords"), &Sprite3D::set_frame_coords); |
| 1004 | ClassDB::bind_method(D_METHOD("get_frame_coords"), &Sprite3D::get_frame_coords); |
| 1005 | |
| 1006 | ClassDB::bind_method(D_METHOD("set_vframes", "vframes"), &Sprite3D::set_vframes); |
| 1007 | ClassDB::bind_method(D_METHOD("get_vframes"), &Sprite3D::get_vframes); |
| 1008 | |
| 1009 | ClassDB::bind_method(D_METHOD("set_hframes", "hframes"), &Sprite3D::set_hframes); |
| 1010 | ClassDB::bind_method(D_METHOD("get_hframes"), &Sprite3D::get_hframes); |
| 1011 | |
| 1012 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture2D"), "set_texture", "get_texture"); |
| 1013 | ADD_GROUP("Animation", ""); |
| 1014 | ADD_PROPERTY(PropertyInfo(Variant::INT, "hframes", PROPERTY_HINT_RANGE, "1,16384,1"), "set_hframes", "get_hframes"); |
| 1015 | ADD_PROPERTY(PropertyInfo(Variant::INT, "vframes", PROPERTY_HINT_RANGE, "1,16384,1"), "set_vframes", "get_vframes"); |
| 1016 | ADD_PROPERTY(PropertyInfo(Variant::INT, "frame"), "set_frame", "get_frame"); |
| 1017 | ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "frame_coords", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_KEYING_INCREMENTS), "set_frame_coords", "get_frame_coords"); |
| 1018 | ADD_GROUP("Region", "region_"); |
| 1019 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "region_enabled", PROPERTY_HINT_GROUP_ENABLE), "set_region_enabled", "is_region_enabled"); |
| 1020 | ADD_PROPERTY(PropertyInfo(Variant::RECT2, "region_rect", PROPERTY_HINT_NONE, "suffix:px"), "set_region_rect", "get_region_rect"); |
| 1021 | |
| 1022 | ADD_SIGNAL(MethodInfo("frame_changed")); |
| 1023 | ADD_SIGNAL(MethodInfo("texture_changed")); |
| 1024 | } |
| 1025 | |
| 1026 | Sprite3D::Sprite3D() { |
| 1027 | } |
| 1028 | |
| 1029 | //////////////////////////////////////// |
| 1030 | |
| 1031 | void AnimatedSprite3D::_draw() { |
| 1032 | if (get_base() != get_mesh()) { |
| 1033 | set_base(get_mesh()); |
| 1034 | } |
| 1035 | |
| 1036 | if (frames.is_null() || !frames->has_animation(animation)) { |
| 1037 | return; |
| 1038 | } |
| 1039 | |
| 1040 | Ref<Texture2D> texture = frames->get_frame_texture(animation, frame); |
| 1041 | if (texture.is_null()) { |
| 1042 | set_base(RID()); |
| 1043 | return; |
| 1044 | } |
| 1045 | Size2 tsize = texture->get_size(); |
| 1046 | if (tsize.x == 0 || tsize.y == 0) { |
| 1047 | return; |
| 1048 | } |
| 1049 | |
| 1050 | Rect2 src_rect; |
| 1051 | src_rect.size = tsize; |
| 1052 | |
| 1053 | Point2 ofs = get_offset(); |
| 1054 | if (is_centered()) { |
| 1055 | ofs -= tsize / 2; |
| 1056 | } |
| 1057 | |
| 1058 | Rect2 dst_rect(ofs, tsize); |
| 1059 | |
| 1060 | draw_texture_rect(texture, dst_rect, src_rect); |
| 1061 | } |
| 1062 | |
| 1063 | void AnimatedSprite3D::_validate_property(PropertyInfo &p_property) const { |
| 1064 | if (frames.is_null()) { |
| 1065 | return; |
| 1066 | } |
| 1067 | if (!Engine::get_singleton()->is_editor_hint()) { |
| 1068 | if (p_property.name == "frame" && playing) { |
| 1069 | p_property.usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_READ_ONLY; |
| 1070 | } |
| 1071 | return; |
| 1072 | } |
| 1073 | if (p_property.name == "animation") { |
| 1074 | List<StringName> names; |
| 1075 | frames->get_animation_list(&names); |
| 1076 | names.sort_custom<StringName::AlphCompare>(); |
| 1077 | |
| 1078 | bool current_found = false; |
| 1079 | bool is_first_element = true; |
| 1080 | |
| 1081 | for (const StringName &E : names) { |
| 1082 | if (!is_first_element) { |
| 1083 | p_property.hint_string += ","; |
| 1084 | } else { |
| 1085 | is_first_element = false; |
| 1086 | } |
| 1087 | |
| 1088 | p_property.hint_string += String(E); |
| 1089 | if (animation == E) { |
| 1090 | current_found = true; |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | if (!current_found) { |
| 1095 | if (p_property.hint_string.is_empty()) { |
| 1096 | p_property.hint_string = String(animation); |
| 1097 | } else { |
| 1098 | p_property.hint_string = String(animation) + "," + p_property.hint_string; |
| 1099 | } |
| 1100 | } |
| 1101 | return; |
| 1102 | } |
| 1103 | |
| 1104 | if (p_property.name == "frame") { |
| 1105 | if (playing) { |
| 1106 | p_property.usage = PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_READ_ONLY; |
| 1107 | return; |
| 1108 | } |
| 1109 | |
| 1110 | p_property.hint = PROPERTY_HINT_RANGE; |
| 1111 | if (frames->has_animation(animation) && frames->get_frame_count(animation) > 0) { |
| 1112 | p_property.hint_string = "0," + itos(frames->get_frame_count(animation) - 1) + ",1"; |
| 1113 | } else { |
| 1114 | // Avoid an error, `hint_string` is required for `PROPERTY_HINT_RANGE`. |
| 1115 | p_property.hint_string = "0,0,1"; |
| 1116 | } |
| 1117 | p_property.usage |= PROPERTY_USAGE_KEYING_INCREMENTS; |
| 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | void AnimatedSprite3D::_notification(int p_what) { |
| 1122 | switch (p_what) { |
| 1123 | case NOTIFICATION_READY: { |
| 1124 | if (!Engine::get_singleton()->is_editor_hint() && frames.is_valid() && frames->has_animation(autoplay)) { |
| 1125 | play(autoplay); |
| 1126 | } |
| 1127 | } break; |
| 1128 | |
| 1129 | case NOTIFICATION_INTERNAL_PROCESS: { |
| 1130 | if (frames.is_null() || !frames->has_animation(animation)) { |
| 1131 | return; |
| 1132 | } |
| 1133 | |
| 1134 | double remaining = get_process_delta_time(); |
| 1135 | int i = 0; |
| 1136 | while (remaining) { |
| 1137 | // Animation speed may be changed by animation_finished or frame_changed signals. |
| 1138 | double speed = frames->get_animation_speed(animation) * speed_scale * custom_speed_scale * frame_speed_scale; |
| 1139 | double abs_speed = Math::abs(speed); |
| 1140 | |
| 1141 | if (speed == 0) { |
| 1142 | return; // Do nothing. |
| 1143 | } |
| 1144 | |
| 1145 | // Frame count may be changed by animation_finished or frame_changed signals. |
| 1146 | int fc = frames->get_frame_count(animation); |
| 1147 | |
| 1148 | int last_frame = fc - 1; |
| 1149 | if (!std::signbit(speed)) { |
| 1150 | // Forwards. |
| 1151 | if (frame_progress >= 1.0) { |
| 1152 | if (frame >= last_frame) { |
| 1153 | if (frames->get_animation_loop(animation)) { |
| 1154 | frame = 0; |
| 1155 | emit_signal("animation_looped"); |
| 1156 | } else { |
| 1157 | frame = last_frame; |
| 1158 | pause(); |
| 1159 | emit_signal(SceneStringName(animation_finished)); |
| 1160 | return; |
| 1161 | } |
| 1162 | } else { |
| 1163 | frame++; |
| 1164 | } |
| 1165 | _calc_frame_speed_scale(); |
| 1166 | frame_progress = 0.0; |
| 1167 | _queue_redraw(); |
| 1168 | emit_signal(SceneStringName(frame_changed)); |
| 1169 | } |
| 1170 | double to_process = MIN((1.0 - frame_progress) / abs_speed, remaining); |
| 1171 | frame_progress += to_process * abs_speed; |
| 1172 | remaining -= to_process; |
| 1173 | } else { |
| 1174 | // Backwards. |
| 1175 | if (frame_progress <= 0) { |
| 1176 | if (frame <= 0) { |
| 1177 | if (frames->get_animation_loop(animation)) { |
| 1178 | frame = last_frame; |
| 1179 | emit_signal("animation_looped"); |
| 1180 | } else { |
| 1181 | frame = 0; |
| 1182 | pause(); |
| 1183 | emit_signal(SceneStringName(animation_finished)); |
| 1184 | return; |
| 1185 | } |
| 1186 | } else { |
| 1187 | frame--; |
| 1188 | } |
| 1189 | _calc_frame_speed_scale(); |
| 1190 | frame_progress = 1.0; |
| 1191 | _queue_redraw(); |
| 1192 | emit_signal(SceneStringName(frame_changed)); |
| 1193 | } |
| 1194 | double to_process = MIN(frame_progress / abs_speed, remaining); |
| 1195 | frame_progress -= to_process * abs_speed; |
| 1196 | remaining -= to_process; |
| 1197 | } |
| 1198 | |
| 1199 | i++; |
| 1200 | if (i > fc) { |
| 1201 | return; // Prevents freezing if to_process is each time much less than remaining. |
| 1202 | } |
| 1203 | } |
| 1204 | } break; |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | void AnimatedSprite3D::set_sprite_frames(const Ref<SpriteFrames> &p_frames) { |
| 1209 | if (frames == p_frames) { |
| 1210 | return; |
| 1211 | } |
| 1212 | |
| 1213 | if (frames.is_valid()) { |
| 1214 | frames->disconnect(CoreStringName(changed), callable_mp(this, &AnimatedSprite3D::_res_changed)); |
| 1215 | } |
| 1216 | stop(); |
| 1217 | frames = p_frames; |
| 1218 | if (frames.is_valid()) { |
| 1219 | frames->connect(CoreStringName(changed), callable_mp(this, &AnimatedSprite3D::_res_changed)); |
| 1220 | |
| 1221 | List<StringName> al; |
| 1222 | frames->get_animation_list(&al); |
| 1223 | if (al.is_empty()) { |
| 1224 | set_animation(StringName()); |
| 1225 | autoplay = String(); |
| 1226 | } else { |
| 1227 | if (!frames->has_animation(animation)) { |
| 1228 | set_animation(al.front()->get()); |
| 1229 | } |
| 1230 | if (!frames->has_animation(autoplay)) { |
| 1231 | autoplay = String(); |
| 1232 | } |
| 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | notify_property_list_changed(); |
| 1237 | _queue_redraw(); |
| 1238 | update_configuration_warnings(); |
| 1239 | emit_signal("sprite_frames_changed"); |
| 1240 | } |
| 1241 | |
| 1242 | Ref<SpriteFrames> AnimatedSprite3D::get_sprite_frames() const { |
| 1243 | return frames; |
| 1244 | } |
| 1245 | |
| 1246 | void AnimatedSprite3D::set_frame(int p_frame) { |
| 1247 | set_frame_and_progress(p_frame, std::signbit(get_playing_speed()) ? 1.0 : 0.0); |
| 1248 | } |
| 1249 | |
| 1250 | int AnimatedSprite3D::get_frame() const { |
| 1251 | return frame; |
| 1252 | } |
| 1253 | |
| 1254 | void AnimatedSprite3D::set_frame_progress(real_t p_progress) { |
| 1255 | frame_progress = p_progress; |
| 1256 | } |
| 1257 | |
| 1258 | real_t AnimatedSprite3D::get_frame_progress() const { |
| 1259 | return frame_progress; |
| 1260 | } |
| 1261 | |
| 1262 | void AnimatedSprite3D::set_frame_and_progress(int p_frame, real_t p_progress) { |
| 1263 | if (frames.is_null()) { |
| 1264 | return; |
| 1265 | } |
| 1266 | |
| 1267 | bool has_animation = frames->has_animation(animation); |
| 1268 | int end_frame = has_animation ? MAX(0, frames->get_frame_count(animation) - 1) : 0; |
| 1269 | bool is_changed = frame != p_frame; |
| 1270 | |
| 1271 | if (p_frame < 0) { |
| 1272 | frame = 0; |
| 1273 | } else if (has_animation && p_frame > end_frame) { |
| 1274 | frame = end_frame; |
| 1275 | } else { |
| 1276 | frame = p_frame; |
| 1277 | } |
| 1278 | |
| 1279 | _calc_frame_speed_scale(); |
| 1280 | frame_progress = p_progress; |
| 1281 | |
| 1282 | if (!is_changed) { |
| 1283 | return; // No change, don't redraw. |
| 1284 | } |
| 1285 | _queue_redraw(); |
| 1286 | emit_signal(SceneStringName(frame_changed)); |
| 1287 | } |
| 1288 | |
| 1289 | void AnimatedSprite3D::set_speed_scale(float p_speed_scale) { |
| 1290 | speed_scale = p_speed_scale; |
| 1291 | } |
| 1292 | |
| 1293 | float AnimatedSprite3D::get_speed_scale() const { |
| 1294 | return speed_scale; |
| 1295 | } |
| 1296 | |
| 1297 | float AnimatedSprite3D::get_playing_speed() const { |
| 1298 | if (!playing) { |
| 1299 | return 0; |
| 1300 | } |
| 1301 | return speed_scale * custom_speed_scale; |
| 1302 | } |
| 1303 | |
| 1304 | Rect2 AnimatedSprite3D::get_item_rect() const { |
| 1305 | if (frames.is_null() || !frames->has_animation(animation)) { |
| 1306 | return Rect2(0, 0, 1, 1); |
| 1307 | } |
| 1308 | if (frame < 0 || frame >= frames->get_frame_count(animation)) { |
| 1309 | return Rect2(0, 0, 1, 1); |
| 1310 | } |
| 1311 | |
| 1312 | Ref<Texture2D> t; |
| 1313 | if (animation) { |
| 1314 | t = frames->get_frame_texture(animation, frame); |
| 1315 | } |
| 1316 | if (t.is_null()) { |
| 1317 | return Rect2(0, 0, 1, 1); |
| 1318 | } |
| 1319 | Size2 s = t->get_size(); |
| 1320 | |
| 1321 | Point2 ofs = get_offset(); |
| 1322 | if (is_centered()) { |
| 1323 | ofs -= s / 2; |
| 1324 | } |
| 1325 | |
| 1326 | if (s == Size2(0, 0)) { |
| 1327 | s = Size2(1, 1); |
| 1328 | } |
| 1329 | |
| 1330 | return Rect2(ofs, s); |
| 1331 | } |
| 1332 | |
| 1333 | void AnimatedSprite3D::_res_changed() { |
| 1334 | set_frame_and_progress(frame, frame_progress); |
| 1335 | _queue_redraw(); |
| 1336 | notify_property_list_changed(); |
| 1337 | } |
| 1338 | |
| 1339 | bool AnimatedSprite3D::is_playing() const { |
| 1340 | return playing; |
| 1341 | } |
| 1342 | |
| 1343 | void AnimatedSprite3D::set_autoplay(const String &p_name) { |
| 1344 | if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) { |
| 1345 | WARN_PRINT("Setting autoplay after the node has been added to the scene has no effect."); |
| 1346 | } |
| 1347 | |
| 1348 | autoplay = p_name; |
| 1349 | } |
| 1350 | |
| 1351 | String AnimatedSprite3D::get_autoplay() const { |
| 1352 | return autoplay; |
| 1353 | } |
| 1354 | |
| 1355 | void AnimatedSprite3D::play(const StringName &p_name, float p_custom_scale, bool p_from_end) { |
| 1356 | StringName name = p_name; |
| 1357 | |
| 1358 | if (name == StringName()) { |
| 1359 | name = animation; |
| 1360 | } |
| 1361 | |
| 1362 | ERR_FAIL_COND_MSG(frames.is_null(), vformat("There is no animation with name '%s'.", name)); |
| 1363 | ERR_FAIL_COND_MSG(!frames->get_animation_names().has(name), vformat("There is no animation with name '%s'.", name)); |
| 1364 | |
| 1365 | if (frames->get_frame_count(name) == 0) { |
| 1366 | return; |
| 1367 | } |
| 1368 | |
| 1369 | playing = true; |
| 1370 | custom_speed_scale = p_custom_scale; |
| 1371 | |
| 1372 | if (name != animation) { |
| 1373 | animation = name; |
| 1374 | int end_frame = MAX(0, frames->get_frame_count(animation) - 1); |
| 1375 | |
| 1376 | if (p_from_end) { |
| 1377 | set_frame_and_progress(end_frame, 1.0); |
| 1378 | } else { |
| 1379 | set_frame_and_progress(0, 0.0); |
| 1380 | } |
| 1381 | emit_signal(SceneStringName(animation_changed)); |
| 1382 | } else { |
| 1383 | int end_frame = MAX(0, frames->get_frame_count(animation) - 1); |
| 1384 | bool is_backward = std::signbit(speed_scale * custom_speed_scale); |
| 1385 | |
| 1386 | if (p_from_end && is_backward && frame == 0 && frame_progress <= 0.0) { |
| 1387 | set_frame_and_progress(end_frame, 1.0); |
| 1388 | } else if (!p_from_end && !is_backward && frame == end_frame && frame_progress >= 1.0) { |
| 1389 | set_frame_and_progress(0, 0.0); |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | set_process_internal(true); |
| 1394 | notify_property_list_changed(); |
| 1395 | _queue_redraw(); |
| 1396 | } |
| 1397 | |
| 1398 | void AnimatedSprite3D::play_backwards(const StringName &p_name) { |
| 1399 | play(p_name, -1, true); |
| 1400 | } |
| 1401 | |
| 1402 | void AnimatedSprite3D::_stop_internal(bool p_reset) { |
| 1403 | playing = false; |
| 1404 | if (p_reset) { |
| 1405 | custom_speed_scale = 1.0; |
| 1406 | set_frame_and_progress(0, 0.0); |
| 1407 | } |
| 1408 | notify_property_list_changed(); |
| 1409 | set_process_internal(false); |
| 1410 | } |
| 1411 | |
| 1412 | void AnimatedSprite3D::pause() { |
| 1413 | _stop_internal(false); |
| 1414 | } |
| 1415 | |
| 1416 | void AnimatedSprite3D::stop() { |
| 1417 | _stop_internal(true); |
| 1418 | } |
| 1419 | |
| 1420 | double AnimatedSprite3D::_get_frame_duration() { |
| 1421 | if (frames.is_valid() && frames->has_animation(animation)) { |
| 1422 | return frames->get_frame_duration(animation, frame); |
| 1423 | } |
| 1424 | return 1.0; |
| 1425 | } |
| 1426 | |
| 1427 | void AnimatedSprite3D::_calc_frame_speed_scale() { |
| 1428 | frame_speed_scale = 1.0 / _get_frame_duration(); |
| 1429 | } |
| 1430 | |
| 1431 | void AnimatedSprite3D::set_animation(const StringName &p_name) { |
| 1432 | if (animation == p_name) { |
| 1433 | return; |
| 1434 | } |
| 1435 | |
| 1436 | animation = p_name; |
| 1437 | |
| 1438 | emit_signal(SceneStringName(animation_changed)); |
| 1439 | |
| 1440 | if (frames.is_null()) { |
| 1441 | animation = StringName(); |
| 1442 | stop(); |
| 1443 | ERR_FAIL_MSG(vformat("There is no animation with name '%s'.", p_name)); |
| 1444 | } |
| 1445 | |
| 1446 | if (animation == StringName() || frames->get_frame_count(animation) == 0) { |
| 1447 | stop(); |
| 1448 | return; |
| 1449 | } else if (!frames->get_animation_names().has(animation)) { |
| 1450 | animation = StringName(); |
| 1451 | stop(); |
| 1452 | ERR_FAIL_MSG(vformat("There is no animation with name '%s'.", p_name)); |
| 1453 | } |
| 1454 | |
| 1455 | if (std::signbit(get_playing_speed())) { |
| 1456 | set_frame_and_progress(frames->get_frame_count(animation) - 1, 1.0); |
| 1457 | } else { |
| 1458 | set_frame_and_progress(0, 0.0); |
| 1459 | } |
| 1460 | |
| 1461 | notify_property_list_changed(); |
| 1462 | _queue_redraw(); |
| 1463 | } |
| 1464 | |
| 1465 | StringName AnimatedSprite3D::get_animation() const { |
| 1466 | return animation; |
| 1467 | } |
| 1468 | |
| 1469 | PackedStringArray AnimatedSprite3D::get_configuration_warnings() const { |
| 1470 | PackedStringArray warnings = SpriteBase3D::get_configuration_warnings(); |
| 1471 | if (frames.is_null()) { |
| 1472 | warnings.push_back(RTR("A SpriteFrames resource must be created or set in the \"Sprite Frames\" property in order for AnimatedSprite3D to display frames.")); |
| 1473 | } |
| 1474 | return warnings; |
| 1475 | } |
| 1476 | |
| 1477 | #ifdef TOOLS_ENABLED |
| 1478 | void AnimatedSprite3D::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const { |
| 1479 | const String pf = p_function; |
| 1480 | if (p_idx == 0 && frames.is_valid()) { |
| 1481 | if (pf == "play" || pf == "play_backwards" || pf == "set_animation" || pf == "set_autoplay") { |
| 1482 | List<StringName> al; |
| 1483 | frames->get_animation_list(&al); |
| 1484 | for (const StringName &name : al) { |
| 1485 | r_options->push_back(String(name).quote()); |
| 1486 | } |
| 1487 | } |
| 1488 | } |
| 1489 | SpriteBase3D::get_argument_options(p_function, p_idx, r_options); |
| 1490 | } |
| 1491 | #endif |
| 1492 | |
| 1493 | #ifndef DISABLE_DEPRECATED |
| 1494 | bool AnimatedSprite3D::_set(const StringName &p_name, const Variant &p_value) { |
| 1495 | if ((p_name == SNAME("frames"))) { |
| 1496 | set_sprite_frames(p_value); |
| 1497 | return true; |
| 1498 | } |
| 1499 | return false; |
| 1500 | } |
| 1501 | #endif |
| 1502 | void AnimatedSprite3D::_bind_methods() { |
| 1503 | ClassDB::bind_method(D_METHOD("set_sprite_frames", "sprite_frames"), &AnimatedSprite3D::set_sprite_frames); |
| 1504 | ClassDB::bind_method(D_METHOD("get_sprite_frames"), &AnimatedSprite3D::get_sprite_frames); |
| 1505 | |
| 1506 | ClassDB::bind_method(D_METHOD("set_animation", "name"), &AnimatedSprite3D::set_animation); |
| 1507 | ClassDB::bind_method(D_METHOD("get_animation"), &AnimatedSprite3D::get_animation); |
| 1508 | |
| 1509 | ClassDB::bind_method(D_METHOD("set_autoplay", "name"), &AnimatedSprite3D::set_autoplay); |
| 1510 | ClassDB::bind_method(D_METHOD("get_autoplay"), &AnimatedSprite3D::get_autoplay); |
| 1511 | |
| 1512 | ClassDB::bind_method(D_METHOD("is_playing"), &AnimatedSprite3D::is_playing); |
| 1513 | |
| 1514 | ClassDB::bind_method(D_METHOD("play", "name", "custom_speed", "from_end"), &AnimatedSprite3D::play, DEFVAL(StringName()), DEFVAL(1.0), DEFVAL(false)); |
| 1515 | ClassDB::bind_method(D_METHOD("play_backwards", "name"), &AnimatedSprite3D::play_backwards, DEFVAL(StringName())); |
| 1516 | ClassDB::bind_method(D_METHOD("pause"), &AnimatedSprite3D::pause); |
| 1517 | ClassDB::bind_method(D_METHOD("stop"), &AnimatedSprite3D::stop); |
| 1518 | |
| 1519 | ClassDB::bind_method(D_METHOD("set_frame", "frame"), &AnimatedSprite3D::set_frame); |
| 1520 | ClassDB::bind_method(D_METHOD("get_frame"), &AnimatedSprite3D::get_frame); |
| 1521 | |
| 1522 | ClassDB::bind_method(D_METHOD("set_frame_progress", "progress"), &AnimatedSprite3D::set_frame_progress); |
| 1523 | ClassDB::bind_method(D_METHOD("get_frame_progress"), &AnimatedSprite3D::get_frame_progress); |
| 1524 | |
| 1525 | ClassDB::bind_method(D_METHOD("set_frame_and_progress", "frame", "progress"), &AnimatedSprite3D::set_frame_and_progress); |
| 1526 | |
| 1527 | ClassDB::bind_method(D_METHOD("set_speed_scale", "speed_scale"), &AnimatedSprite3D::set_speed_scale); |
| 1528 | ClassDB::bind_method(D_METHOD("get_speed_scale"), &AnimatedSprite3D::get_speed_scale); |
| 1529 | ClassDB::bind_method(D_METHOD("get_playing_speed"), &AnimatedSprite3D::get_playing_speed); |
| 1530 | |
| 1531 | ClassDB::bind_method(D_METHOD("_res_changed"), &AnimatedSprite3D::_res_changed); |
| 1532 | |
| 1533 | ADD_SIGNAL(MethodInfo("sprite_frames_changed")); |
| 1534 | ADD_SIGNAL(MethodInfo("animation_changed")); |
| 1535 | ADD_SIGNAL(MethodInfo("frame_changed")); |
| 1536 | ADD_SIGNAL(MethodInfo("animation_looped")); |
| 1537 | ADD_SIGNAL(MethodInfo("animation_finished")); |
| 1538 | |
| 1539 | ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "sprite_frames", PROPERTY_HINT_RESOURCE_TYPE, "SpriteFrames"), "set_sprite_frames", "get_sprite_frames"); |
| 1540 | ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "animation", PROPERTY_HINT_ENUM, ""), "set_animation", "get_animation"); |
| 1541 | ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "autoplay", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_autoplay", "get_autoplay"); |
| 1542 | ADD_PROPERTY(PropertyInfo(Variant::INT, "frame"), "set_frame", "get_frame"); |
| 1543 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "frame_progress", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_frame_progress", "get_frame_progress"); |
| 1544 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale"), "set_speed_scale", "get_speed_scale"); |
| 1545 | } |
| 1546 | |
| 1547 | AnimatedSprite3D::AnimatedSprite3D() { |
| 1548 | } |