From df9d48d9b552c30b9b42940a964ccc4271e6905b Mon Sep 17 00:00:00 2001 From: Biliogadafr Date: Sun, 24 May 2015 21:18:52 +0300 Subject: Replace color phases with color ramp for Particles2D. --- scene/2d/particles_2d.cpp | 168 ++++++++++++++++++++++++++++++---------------- scene/2d/particles_2d.h | 15 +++-- 2 files changed, 119 insertions(+), 64 deletions(-) (limited to 'scene/2d') diff --git a/scene/2d/particles_2d.cpp b/scene/2d/particles_2d.cpp index 39d747c436..6effd4f31d 100644 --- a/scene/2d/particles_2d.cpp +++ b/scene/2d/particles_2d.cpp @@ -503,19 +503,6 @@ void Particles2D::_notification(int p_what) { if (!local_space) invxform=get_global_transform().affine_inverse(); - int col_count=0; - float last=-1; - ColorPhase cphase[MAX_COLOR_PHASES]; - - for(int i=0;i& color_points = color_ramp->get_points(); - while(cpos ptime) - break; - cpos++; - } + while(cpos ptime) + break; + cpos++; + } - Color color; - //could be faster.. - if (cpos==-1) - color=Color(1,1,1,1); - else { - if (cpos==col_count-1) - color=cphase[cpos].color; - else { - float diff = (cphase[cpos+1].pos-cphase[cpos].pos); - if (diff>0) - color=cphase[cpos].color.linear_interpolate(cphase[cpos+1].color, (ptime - cphase[cpos].pos) / diff ); + cpos--; + //could be faster.. + if (cpos==-1) + if(color_points.size()) + color=color_points[0].color;//Extend color to the beginning else - color=cphase[cpos+1].color; + color=Color(1,1,1,1);//If no points just use white. + else { + if (cpos==color_points.size()-1) + color=color_points[cpos].color; + else { + float diff = (color_points[cpos+1].offset-color_points[cpos].offset); + if (diff>0) + color=color_points[cpos].color.linear_interpolate(color_points[cpos+1].color, (ptime - color_points[cpos].offset) / diff ); + else + color=color_points[cpos+1].color; + } } + } else + { + color = default_color; } @@ -813,6 +810,27 @@ Ref Particles2D::get_texture() const { return texture; } +void Particles2D::set_color(const Color& p_color) { + + default_color = p_color; +} + +Color Particles2D::get_color() const { + + return default_color; +} + + +void Particles2D::set_color_ramp(const Ref& p_color_ramp) { + + color_ramp=p_color_ramp; +} + +Ref Particles2D::get_color_ramp() const { + + return color_ramp; +} + void Particles2D::set_emissor_offset(const Point2& p_offset) { emissor_offset=p_offset; @@ -834,40 +852,76 @@ bool Particles2D::is_using_local_space() const { return local_space; } - +//Deprecated. Converts color phases to color ramp void Particles2D::set_color_phases(int p_phases) { - ERR_FAIL_INDEX(p_phases,MAX_COLOR_PHASES+1); - color_phase_count=p_phases; + //Create color ramp if we have 2 or more phases. + //Otherwise first phase phase will be assigned to default color. + if(p_phases > 1 && color_ramp.is_null()) + { + color_ramp = Ref(memnew (ColorRamp())); + } + if(color_ramp.is_valid()) + { + color_ramp->get_points().resize(p_phases); + } } +//Deprecated. int Particles2D::get_color_phases() const { - return color_phase_count; + if(color_ramp.is_valid()) + { + return color_ramp->get_points_count(); + } + return 0; } +//Deprecated. Converts color phases to color ramp void Particles2D::set_color_phase_color(int p_phase,const Color& p_color) { ERR_FAIL_INDEX(p_phase,MAX_COLOR_PHASES); - color_phases[p_phase].color=p_color; - + if(color_ramp.is_valid()) + { + if(color_ramp->get_points_count() > p_phase) + color_ramp->set_color(p_phase, p_color); + } else + { + if(p_phase == 0) + default_color = p_color; + } } + +//Deprecated. Color Particles2D::get_color_phase_color(int p_phase) const { ERR_FAIL_INDEX_V(p_phase,MAX_COLOR_PHASES,Color()); - return color_phases[p_phase].color; + if(color_ramp.is_valid()) + { + return color_ramp->get_color(p_phase); + } + return Color(0,0,0,1); } +//Deprecated. Converts color phases to color ramp void Particles2D::set_color_phase_pos(int p_phase,float p_pos) { ERR_FAIL_INDEX(p_phase,MAX_COLOR_PHASES); ERR_FAIL_COND(p_pos<0.0 || p_pos>1.0); - color_phases[p_phase].pos=p_pos; - + if(color_ramp.is_valid() && color_ramp->get_points_count() > p_phase) + { + return color_ramp->set_offset(p_phase, p_pos); + } } + +//Deprecated. float Particles2D::get_color_phase_pos(int p_phase) const { ERR_FAIL_INDEX_V(p_phase,MAX_COLOR_PHASES,0); - return color_phases[p_phase].pos; + if(color_ramp.is_valid()) + { + return color_ramp->get_offset(p_phase); + } + return 0; } void Particles2D::set_emission_half_extents(const Vector2& p_extents) { @@ -997,6 +1051,12 @@ void Particles2D::_bind_methods() { ObjectTypeDB::bind_method(_MD("set_texture:Texture","texture"),&Particles2D::set_texture); ObjectTypeDB::bind_method(_MD("get_texture:Texture"),&Particles2D::get_texture); + ObjectTypeDB::bind_method(_MD("set_color","color"),&Particles2D::set_color); + ObjectTypeDB::bind_method(_MD("get_color"),&Particles2D::get_color); + + ObjectTypeDB::bind_method(_MD("set_color_ramp:ColorRamp","color_ramp"),&Particles2D::set_color_ramp); + ObjectTypeDB::bind_method(_MD("get_color_ramp:ColorRamp"),&Particles2D::get_color_ramp); + ObjectTypeDB::bind_method(_MD("set_emissor_offset","offset"),&Particles2D::set_emissor_offset); ObjectTypeDB::bind_method(_MD("get_emissor_offset"),&Particles2D::get_emissor_offset); @@ -1055,7 +1115,6 @@ void Particles2D::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::INT,"config/v_frames",PROPERTY_HINT_RANGE,"1,512,1"),_SCS("set_v_frames"),_SCS("get_v_frames")); - for(int i=0;i particles; - int color_phase_count; - struct ColorPhase { - Color color; - float pos; - } color_phases[MAX_COLOR_PHASES]; struct AttractorCache { @@ -161,6 +157,9 @@ private: Ref texture; + //If no color ramp is set then default color is used. Created as simple alternative to color_ramp. + Color default_color; + Ref color_ramp; void testee(int a, int b, int c, int d, int e); void _process_particles(float p_delta); @@ -230,6 +229,12 @@ public: void set_texture(const Ref& p_texture); Ref get_texture() const; + void set_color(const Color& p_color); + Color get_color() const; + + void set_color_ramp(const Ref& p_texture); + Ref get_color_ramp() const; + void set_emissor_offset(const Point2& p_offset); Point2 get_emissor_offset() const; -- cgit v1.2.3