Fixes broken CPUParticles2D AtlasTextures usage

(cherry picked from commit 36d9906d6a)
This commit is contained in:
bitstopper 2019-10-02 17:49:52 +02:00 committed by Rémi Verschelde
parent e10b0c9eaa
commit 7cb3a20418
2 changed files with 35 additions and 5 deletions

View file

@ -29,7 +29,7 @@
/*************************************************************************/ /*************************************************************************/
#include "cpu_particles_2d.h" #include "cpu_particles_2d.h"
#include "core/core_string_names.h"
#include "scene/2d/canvas_item.h" #include "scene/2d/canvas_item.h"
#include "scene/2d/particles_2d.h" #include "scene/2d/particles_2d.h"
#include "scene/resources/particles_material.h" #include "scene/resources/particles_material.h"
@ -169,10 +169,20 @@ void CPUParticles2D::_update_mesh_texture() {
vertices.push_back(-tex_size * 0.5 + Vector2(tex_size.x, tex_size.y)); vertices.push_back(-tex_size * 0.5 + Vector2(tex_size.x, tex_size.y));
vertices.push_back(-tex_size * 0.5 + Vector2(0, tex_size.y)); vertices.push_back(-tex_size * 0.5 + Vector2(0, tex_size.y));
PoolVector<Vector2> uvs; PoolVector<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(0, 0));
uvs.push_back(Vector2(1, 0)); uvs.push_back(Vector2(1, 0));
uvs.push_back(Vector2(1, 1)); uvs.push_back(Vector2(1, 1));
uvs.push_back(Vector2(0, 1)); uvs.push_back(Vector2(0, 1));
}
PoolVector<Color> colors; PoolVector<Color> colors;
colors.push_back(Color(1, 1, 1, 1)); colors.push_back(Color(1, 1, 1, 1));
colors.push_back(Color(1, 1, 1, 1)); colors.push_back(Color(1, 1, 1, 1));
@ -198,12 +208,29 @@ void CPUParticles2D::_update_mesh_texture() {
} }
void CPUParticles2D::set_texture(const Ref<Texture> &p_texture) { void CPUParticles2D::set_texture(const Ref<Texture> &p_texture) {
if (p_texture == texture)
return;
if (texture.is_valid())
texture->disconnect(CoreStringNames::get_singleton()->changed, this, "_texture_changed");
texture = p_texture; texture = p_texture;
if (texture.is_valid())
texture->connect(CoreStringNames::get_singleton()->changed, this, "_texture_changed");
update(); update();
_update_mesh_texture(); _update_mesh_texture();
} }
void CPUParticles2D::_texture_changed() {
if (texture.is_valid()) {
update();
_update_mesh_texture();
}
}
Ref<Texture> CPUParticles2D::get_texture() const { Ref<Texture> CPUParticles2D::get_texture() const {
return texture; return texture;
@ -1315,6 +1342,7 @@ void CPUParticles2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("convert_from_particles", "particles"), &CPUParticles2D::convert_from_particles); ClassDB::bind_method(D_METHOD("convert_from_particles", "particles"), &CPUParticles2D::convert_from_particles);
ClassDB::bind_method(D_METHOD("_update_render_thread"), &CPUParticles2D::_update_render_thread); ClassDB::bind_method(D_METHOD("_update_render_thread"), &CPUParticles2D::_update_render_thread);
ClassDB::bind_method(D_METHOD("_texture_changed"), &CPUParticles2D::_texture_changed);
ADD_GROUP("Emission Shape", "emission_"); ADD_GROUP("Emission Shape", "emission_");
ADD_PROPERTY(PropertyInfo(Variant::INT, "emission_shape", PROPERTY_HINT_ENUM, "Point,Sphere,Box,Points,Directed Points"), "set_emission_shape", "get_emission_shape"); ADD_PROPERTY(PropertyInfo(Variant::INT, "emission_shape", PROPERTY_HINT_ENUM, "Point,Sphere,Box,Points,Directed Points"), "set_emission_shape", "get_emission_shape");

View file

@ -186,6 +186,8 @@ private:
void _set_redraw(bool p_redraw); void _set_redraw(bool p_redraw);
void _texture_changed();
protected: protected:
static void _bind_methods(); static void _bind_methods();
void _notification(int p_what); void _notification(int p_what);