diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2022-08-30 14:47:19 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-30 14:47:19 +0200 |
commit | 733d04f72780eb474f6b52add57a635bba34530f (patch) | |
tree | 35f4a9448421f81056d3ae438c4227da144072f5 /scene/2d | |
parent | 0bf3f7915780f30fab4db323e843b35bf9210548 (diff) | |
parent | 73bbcaecb97ab1ab6c8658a320198e2f3f7c1bee (diff) |
Merge pull request #64701 from Geometror/fix-atlastexture-gpuparticles-2d
Fix GPUParticles2D ignoring AtlasTexture region
Diffstat (limited to 'scene/2d')
-rw-r--r-- | scene/2d/gpu_particles_2d.cpp | 38 | ||||
-rw-r--r-- | scene/2d/gpu_particles_2d.h | 2 |
2 files changed, 34 insertions, 6 deletions
diff --git a/scene/2d/gpu_particles_2d.cpp b/scene/2d/gpu_particles_2d.cpp index 65ead7afbc..6f9d429a66 100644 --- a/scene/2d/gpu_particles_2d.cpp +++ b/scene/2d/gpu_particles_2d.cpp @@ -30,6 +30,7 @@ #include "gpu_particles_2d.h" +#include "core/core_string_names.h" #include "scene/resources/particle_process_material.h" #ifdef TOOLS_ENABLED @@ -331,7 +332,15 @@ Rect2 GPUParticles2D::capture_rect() const { } void GPUParticles2D::set_texture(const Ref<Texture2D> &p_texture) { + if (texture.is_valid()) { + texture->disconnect(CoreStringNames::get_singleton()->changed, callable_mp(this, &GPUParticles2D::_texture_changed)); + } + texture = p_texture; + + if (texture.is_valid()) { + texture->connect(CoreStringNames::get_singleton()->changed, callable_mp(this, &GPUParticles2D::_texture_changed)); + } _update_collision_size(); update(); } @@ -363,6 +372,14 @@ void GPUParticles2D::_attach_sub_emitter() { } } +void GPUParticles2D::_texture_changed() { + // Changes to the texture need to trigger an update to make + // the editor redraw the sprite with the updated texture. + if (texture.is_valid()) { + update(); + } +} + void GPUParticles2D::set_sub_emitter(const NodePath &p_path) { if (is_inside_tree()) { RS::get_singleton()->particles_set_subemitter(particles, RID()); @@ -480,12 +497,21 @@ void GPUParticles2D::_notification(int p_what) { Vector2(-size.x / 2.0, size.y / 2.0) }; - Vector<Vector2> uvs = { - Vector2(0, 0), - Vector2(1, 0), - Vector2(1, 1), - Vector2(0, 1) - }; + Vector<Vector2> uvs; + AtlasTexture *atlas_texure = Object::cast_to<AtlasTexture>(*texture); + if (atlas_texure && atlas_texure->get_atlas().is_valid()) { + Rect2 region_rect = atlas_texure->get_region(); + Size2 atlas_size = atlas_texure->get_atlas()->get_size(); + uvs.push_back(Vector2(region_rect.position.x / atlas_size.x, region_rect.position.y / atlas_size.y)); + uvs.push_back(Vector2((region_rect.position.x + region_rect.size.x) / atlas_size.x, region_rect.position.y / atlas_size.y)); + uvs.push_back(Vector2((region_rect.position.x + region_rect.size.x) / atlas_size.x, (region_rect.position.y + region_rect.size.y) / atlas_size.y)); + uvs.push_back(Vector2(region_rect.position.x / atlas_size.x, (region_rect.position.y + region_rect.size.y) / atlas_size.y)); + } else { + uvs.push_back(Vector2(0, 0)); + uvs.push_back(Vector2(1, 0)); + uvs.push_back(Vector2(1, 1)); + uvs.push_back(Vector2(0, 1)); + } Vector<int> indices = { 0, 1, 2, 0, 2, 3 }; diff --git a/scene/2d/gpu_particles_2d.h b/scene/2d/gpu_particles_2d.h index 7eece32898..10ae91775f 100644 --- a/scene/2d/gpu_particles_2d.h +++ b/scene/2d/gpu_particles_2d.h @@ -82,6 +82,8 @@ private: void _attach_sub_emitter(); + void _texture_changed(); + protected: static void _bind_methods(); void _validate_property(PropertyInfo &p_property) const; |