diff options
Diffstat (limited to 'scene')
-rw-r--r-- | scene/2d/node_2d.cpp | 6 | ||||
-rw-r--r-- | scene/2d/path_2d.cpp | 270 | ||||
-rw-r--r-- | scene/2d/path_2d.h | 59 | ||||
-rw-r--r-- | scene/register_scene_types.cpp | 1 | ||||
-rw-r--r-- | scene/resources/curve.cpp | 587 | ||||
-rw-r--r-- | scene/resources/curve.h | 75 | ||||
-rw-r--r-- | scene/scene_string_names.cpp | 1 | ||||
-rw-r--r-- | scene/scene_string_names.h | 1 |
8 files changed, 822 insertions, 178 deletions
diff --git a/scene/2d/node_2d.cpp b/scene/2d/node_2d.cpp index 85adfbbbde..9c28212529 100644 --- a/scene/2d/node_2d.cpp +++ b/scene/2d/node_2d.cpp @@ -51,9 +51,9 @@ bool Node2D::edit_has_pivot() const { Variant Node2D::edit_get_state() const { Array state; - state.push_back(pos); - state.push_back(angle); - state.push_back(scale); + state.push_back(get_pos()); + state.push_back(get_rot()); + state.push_back(get_scale()); return state; diff --git a/scene/2d/path_2d.cpp b/scene/2d/path_2d.cpp index 22d56609ee..febec54124 100644 --- a/scene/2d/path_2d.cpp +++ b/scene/2d/path_2d.cpp @@ -27,7 +27,7 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /*************************************************************************/ #include "path_2d.h" - +#include "scene/scene_string_names.h" void Path2D::_notification(int p_what) { @@ -90,3 +90,271 @@ Path2D::Path2D() { set_curve(Ref<Curve2D>( memnew( Curve2D ))); //create one by default } + +///////////////////////////////////////////////////////////////////////////////// + + +void PathFollow2D::_update_transform() { + + + if (!path) + return; + + Ref<Curve2D> c =path->get_curve(); + if (!c.is_valid()) + return; + + + float o = offset; + if (loop) + o=Math::fposmod(o,c->get_baked_length()); + + Vector2 pos = c->interpolate_baked(o,cubic); + + if (rotate) { + + Vector2 n = (c->interpolate_baked(o+lookahead,cubic)-pos).normalized(); + Vector2 t = -n.tangent(); + pos+=n*h_offset; + pos+=t*v_offset; + + set_rot(t.atan2()); + + } else { + + pos.x+=h_offset; + pos.y+=v_offset; + } + + set_pos(pos); + +} + +void PathFollow2D::_notification(int p_what) { + + + switch(p_what) { + + case NOTIFICATION_ENTER_SCENE: { + + Node *parent=get_parent(); + if (parent) { + + path=parent->cast_to<Path2D>(); + if (path) { + _update_transform(); + } + } + + } break; + case NOTIFICATION_EXIT_SCENE: { + + + path=NULL; + } break; + } + +} + +void PathFollow2D::set_cubic_interpolation(bool p_enable) { + + cubic=p_enable; +} + +bool PathFollow2D::get_cubic_interpolation() const { + + return cubic; +} + + +bool PathFollow2D::_set(const StringName& p_name, const Variant& p_value) { + + if (p_name==SceneStringNames::get_singleton()->offset) { + set_offset(p_value); + } else if (p_name==SceneStringNames::get_singleton()->unit_offset) { + set_unit_offset(p_value); + } else if (p_name==SceneStringNames::get_singleton()->rotate) { + set_rotate(p_value); + } else if (p_name==SceneStringNames::get_singleton()->v_offset) { + set_v_offset(p_value); + } else if (p_name==SceneStringNames::get_singleton()->h_offset) { + set_h_offset(p_value); + } else if (String(p_name)=="cubic_interp") { + set_cubic_interpolation(p_value); + } else if (String(p_name)=="loop") { + set_loop(p_value); + } else if (String(p_name)=="lookahead") { + set_lookahead(p_value); + } else + return false; + + return true; +} + +bool PathFollow2D::_get(const StringName& p_name,Variant &r_ret) const{ + + if (p_name==SceneStringNames::get_singleton()->offset) { + r_ret=get_offset(); + } else if (p_name==SceneStringNames::get_singleton()->unit_offset) { + r_ret=get_unit_offset(); + } else if (p_name==SceneStringNames::get_singleton()->rotate) { + r_ret=is_rotating(); + } else if (p_name==SceneStringNames::get_singleton()->v_offset) { + r_ret=get_v_offset(); + } else if (p_name==SceneStringNames::get_singleton()->h_offset) { + r_ret=get_h_offset(); + } else if (String(p_name)=="cubic_interp") { + r_ret=cubic; + } else if (String(p_name)=="loop") { + r_ret=loop; + } else if (String(p_name)=="lookahead") { + r_ret=lookahead; + } else + return false; + + return true; + +} +void PathFollow2D::_get_property_list( List<PropertyInfo> *p_list) const{ + + float max=10000; + if (path && path->get_curve().is_valid()) + max=path->get_curve()->get_baked_length(); + p_list->push_back( PropertyInfo( Variant::REAL, "offset", PROPERTY_HINT_RANGE,"0,"+rtos(max)+",0.01")); + p_list->push_back( PropertyInfo( Variant::REAL, "unit_offset", PROPERTY_HINT_RANGE,"0,1,0.0001",PROPERTY_USAGE_EDITOR)); + p_list->push_back( PropertyInfo( Variant::REAL, "h_offset") ); + p_list->push_back( PropertyInfo( Variant::REAL, "v_offset") ); + p_list->push_back( PropertyInfo( Variant::BOOL, "rotate") ); + p_list->push_back( PropertyInfo( Variant::BOOL, "cubic_interp")); + p_list->push_back( PropertyInfo( Variant::BOOL, "loop")); + p_list->push_back( PropertyInfo( Variant::REAL, "lookahead",PROPERTY_HINT_RANGE,"0.001,1024.0,0.001")); +} + + +void PathFollow2D::_bind_methods() { + + ObjectTypeDB::bind_method(_MD("set_offset","offset"),&PathFollow2D::set_offset); + ObjectTypeDB::bind_method(_MD("get_offset"),&PathFollow2D::get_offset); + + ObjectTypeDB::bind_method(_MD("set_h_offset","h_offset"),&PathFollow2D::set_h_offset); + ObjectTypeDB::bind_method(_MD("get_h_offset"),&PathFollow2D::get_h_offset); + + ObjectTypeDB::bind_method(_MD("set_v_offset","v_offset"),&PathFollow2D::set_v_offset); + ObjectTypeDB::bind_method(_MD("get_v_offset"),&PathFollow2D::get_v_offset); + + ObjectTypeDB::bind_method(_MD("set_unit_offset","unit_offset"),&PathFollow2D::set_unit_offset); + ObjectTypeDB::bind_method(_MD("get_unit_offset"),&PathFollow2D::get_unit_offset); + + ObjectTypeDB::bind_method(_MD("set_rotate","enable"),&PathFollow2D::set_rotate); + ObjectTypeDB::bind_method(_MD("is_rotating"),&PathFollow2D::is_rotating); + + ObjectTypeDB::bind_method(_MD("set_cubic_interpolation","enable"),&PathFollow2D::set_cubic_interpolation); + ObjectTypeDB::bind_method(_MD("get_cubic_interpolation"),&PathFollow2D::get_cubic_interpolation); + + ObjectTypeDB::bind_method(_MD("set_loop","loop"),&PathFollow2D::set_loop); + ObjectTypeDB::bind_method(_MD("has_loop"),&PathFollow2D::has_loop); + + +} + +void PathFollow2D::set_offset(float p_offset) { + + offset=p_offset; + if (path) + _update_transform(); + _change_notify("offset"); + _change_notify("unit_offset"); + +} + +void PathFollow2D::set_h_offset(float p_h_offset) { + + h_offset=p_h_offset; + if (path) + _update_transform(); + +} + +float PathFollow2D::get_h_offset() const { + + return h_offset; +} + +void PathFollow2D::set_v_offset(float p_v_offset) { + + v_offset=p_v_offset; + if (path) + _update_transform(); + +} + +float PathFollow2D::get_v_offset() const { + + return v_offset; +} + + +float PathFollow2D::get_offset() const{ + + return offset; +} + +void PathFollow2D::set_unit_offset(float p_unit_offset) { + + if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length()) + set_offset(p_unit_offset*path->get_curve()->get_baked_length()); + +} + +float PathFollow2D::get_unit_offset() const{ + + if (path && path->get_curve().is_valid() && path->get_curve()->get_baked_length()) + return get_offset()/path->get_curve()->get_baked_length(); + else + return 0; +} + +void PathFollow2D::set_lookahead(float p_lookahead) { + + lookahead=p_lookahead; + +} + +float PathFollow2D::get_lookahead() const{ + + return lookahead; +} + +void PathFollow2D::set_rotate(bool p_rotate) { + + rotate=p_rotate; + _update_transform(); +} + +bool PathFollow2D::is_rotating() const { + + return rotate; +} + +void PathFollow2D::set_loop(bool p_loop) { + + loop=p_loop; +} + +bool PathFollow2D::has_loop() const{ + + return loop; +} + + +PathFollow2D::PathFollow2D() { + + offset=0; + h_offset=0; + v_offset=0; + path=NULL; + rotate=true; + cubic=true; + loop=true; + lookahead=4; +} diff --git a/scene/2d/path_2d.h b/scene/2d/path_2d.h index f401f9da4c..90f57c8eac 100644 --- a/scene/2d/path_2d.h +++ b/scene/2d/path_2d.h @@ -54,4 +54,63 @@ public: Path2D(); }; + + +class PathFollow2D : public Node2D { + + OBJ_TYPE(PathFollow2D,Node2D); +public: + + +private: + Path2D *path; + real_t offset; + real_t h_offset; + real_t v_offset; + real_t lookahead; + bool cubic; + bool loop; + bool rotate; + + void _update_transform(); + + +protected: + + bool _set(const StringName& p_name, const Variant& p_value); + bool _get(const StringName& p_name,Variant &r_ret) const; + void _get_property_list( List<PropertyInfo> *p_list) const; + + void _notification(int p_what); + static void _bind_methods(); +public: + + void set_offset(float p_offset); + float get_offset() const; + + void set_h_offset(float p_h_offset); + float get_h_offset() const; + + void set_v_offset(float p_v_offset); + float get_v_offset() const; + + void set_unit_offset(float p_unit_offset); + float get_unit_offset() const; + + void set_lookahead(float p_lookahead); + float get_lookahead() const; + + void set_loop(bool p_loop); + bool has_loop() const; + + void set_rotate(bool p_enabled); + bool is_rotating() const; + + void set_cubic_interpolation(bool p_enable); + bool get_cubic_interpolation() const; + + PathFollow2D(); +}; + + #endif // PATH_2D_H diff --git a/scene/register_scene_types.cpp b/scene/register_scene_types.cpp index 9a6454e416..ff8660a387 100644 --- a/scene/register_scene_types.cpp +++ b/scene/register_scene_types.cpp @@ -562,6 +562,7 @@ void register_scene_types() { ObjectTypeDB::register_type<ConcavePolygonShape2D>(); ObjectTypeDB::register_type<Curve2D>(); ObjectTypeDB::register_type<Path2D>(); + ObjectTypeDB::register_type<PathFollow2D>(); OS::get_singleton()->yield(); //may take time to init diff --git a/scene/resources/curve.cpp b/scene/resources/curve.cpp index f39bbf4c4e..ae2c07ff56 100644 --- a/scene/resources/curve.cpp +++ b/scene/resources/curve.cpp @@ -44,6 +44,7 @@ static _FORCE_INLINE_ T _bezier_interp(real_t t, T start, T control_1, T control + end * t3; } +#if 0 int Curve2D::get_point_count() const { @@ -379,21 +380,16 @@ Curve2D::Curve2D() { } +#endif -/***********************************************************************************/ -/***********************************************************************************/ -/***********************************************************************************/ -/***********************************************************************************/ -/***********************************************************************************/ -/***********************************************************************************/ -int Curve3D::get_point_count() const { +int Curve2D::get_point_count() const { return points.size(); } -void Curve3D::add_point(const Vector3& p_pos, const Vector3& p_in, const Vector3& p_out,int p_atpos) { +void Curve2D::add_point(const Vector2& p_pos, const Vector2& p_in, const Vector2& p_out,int p_atpos) { Point n; n.pos=p_pos; @@ -408,7 +404,8 @@ void Curve3D::add_point(const Vector3& p_pos, const Vector3& p_in, const Vector3 baked_cache_dirty=true; emit_signal(CoreStringNames::get_singleton()->changed); } -void Curve3D::set_point_pos(int p_index, const Vector3& p_pos) { + +void Curve2D::set_point_pos(int p_index, const Vector2& p_pos) { ERR_FAIL_INDEX(p_index,points.size()); @@ -417,31 +414,15 @@ void Curve3D::set_point_pos(int p_index, const Vector3& p_pos) { emit_signal(CoreStringNames::get_singleton()->changed); } -Vector3 Curve3D::get_point_pos(int p_index) const { +Vector2 Curve2D::get_point_pos(int p_index) const { - ERR_FAIL_INDEX_V(p_index,points.size(),Vector3()); + ERR_FAIL_INDEX_V(p_index,points.size(),Vector2()); return points[p_index].pos; } -void Curve3D::set_point_tilt(int p_index, float p_tilt) { - - ERR_FAIL_INDEX(p_index,points.size()); - - points[p_index].tilt=p_tilt; - baked_cache_dirty=true; - emit_signal(CoreStringNames::get_singleton()->changed); - -} -float Curve3D::get_point_tilt(int p_index) const { - - ERR_FAIL_INDEX_V(p_index,points.size(),0); - return points[p_index].tilt; -} - - -void Curve3D::set_point_in(int p_index, const Vector3& p_in) { +void Curve2D::set_point_in(int p_index, const Vector2& p_in) { ERR_FAIL_INDEX(p_index,points.size()); @@ -450,14 +431,14 @@ void Curve3D::set_point_in(int p_index, const Vector3& p_in) { emit_signal(CoreStringNames::get_singleton()->changed); } -Vector3 Curve3D::get_point_in(int p_index) const { +Vector2 Curve2D::get_point_in(int p_index) const { - ERR_FAIL_INDEX_V(p_index,points.size(),Vector3()); + ERR_FAIL_INDEX_V(p_index,points.size(),Vector2()); return points[p_index].in; } -void Curve3D::set_point_out(int p_index, const Vector3& p_out) { +void Curve2D::set_point_out(int p_index, const Vector2& p_out) { ERR_FAIL_INDEX(p_index,points.size()); @@ -467,15 +448,15 @@ void Curve3D::set_point_out(int p_index, const Vector3& p_out) { } -Vector3 Curve3D::get_point_out(int p_index) const { +Vector2 Curve2D::get_point_out(int p_index) const { - ERR_FAIL_INDEX_V(p_index,points.size(),Vector3()); + ERR_FAIL_INDEX_V(p_index,points.size(),Vector2()); return points[p_index].out; } -void Curve3D::remove_point(int p_index) { +void Curve2D::remove_point(int p_index) { ERR_FAIL_INDEX(p_index,points.size()); points.remove(p_index); @@ -483,25 +464,25 @@ void Curve3D::remove_point(int p_index) { emit_signal(CoreStringNames::get_singleton()->changed); } -Vector3 Curve3D::interpolate(int p_index, float p_offset) const { +Vector2 Curve2D::interpolate(int p_index, float p_offset) const { int pc = points.size(); - ERR_FAIL_COND_V(pc==0,Vector3()); + ERR_FAIL_COND_V(pc==0,Vector2()); if (p_index >= pc-1) return points[pc-1].pos; else if (p_index<0) return points[0].pos; - Vector3 p0 = points[p_index].pos; - Vector3 p1 = p0+points[p_index].out; - Vector3 p3 = points[p_index+1].pos; - Vector3 p2 = p3+points[p_index+1].in; + Vector2 p0 = points[p_index].pos; + Vector2 p1 = p0+points[p_index].out; + Vector2 p3 = points[p_index+1].pos; + Vector2 p2 = p3+points[p_index+1].in; return _bezier_interp(p_offset,p0,p1,p2,p3); } -Vector3 Curve3D::interpolatef(real_t p_findex) const { +Vector2 Curve2D::interpolatef(real_t p_findex) const { if (p_findex>0) @@ -513,216 +494,476 @@ Vector3 Curve3D::interpolatef(real_t p_findex) const { } -#if 0 -DVector<Point3> Curve3D::bake(int p_subdivs) const { - int pc = points.size(); +void Curve2D::_bake_segment2d(Map<float,Vector2>& r_bake, float p_begin, float p_end,const Vector2& p_a,const Vector2& p_out,const Vector2& p_b, const Vector2& p_in,int p_depth,int p_max_depth,float p_tol) const { - DVector<Point3> ret; - if (pc<3) - return ret; + float mp = p_begin+(p_end-p_begin)*0.5; + Vector2 beg = _bezier_interp(p_begin,p_a,p_a+p_out,p_b+p_in,p_b); + Vector2 mid = _bezier_interp(mp,p_a,p_a+p_out,p_b+p_in,p_b); + Vector2 end = _bezier_interp(p_end,p_a,p_a+p_out,p_b+p_in,p_b); - ret.resize((pc-1)*p_subdivs+1); + Vector2 na = (mid-beg).normalized(); + Vector2 nb = (end-mid).normalized(); + float dp = na.dot(nb); - DVector<Point3>::Write w = ret.write(); - const Point *r = points.ptr(); + if (dp<Math::cos(Math::deg2rad(p_tol))) { - for(int i=0;i<pc;i++) { + r_bake[mp]=mid; + } - int ofs = pc*p_subdivs; + if (p_depth<p_max_depth) { + _bake_segment2d(r_bake,p_begin,mp,p_a,p_out,p_b,p_in,p_depth+1,p_max_depth,p_tol); + _bake_segment2d(r_bake,mp,p_end,p_a,p_out,p_b,p_in,p_depth+1,p_max_depth,p_tol); + } +} - int limit=(i==pc-1)?p_subdivs+1:p_subdivs; - for(int j=0;j<limit;j++) { - Vector3 p0 = r[i].pos; - Vector3 p1 = p0+r[i].out; - Vector3 p3 = r[i].pos; - Vector3 p3 = p3+r[i].in; - real_t t = j/(real_t)p_subdivs; +void Curve2D::_bake() const { - w[ofs+j]=_bezier_interp(t,p0,p1,p3,p3); + if (!baked_cache_dirty) + return; - } + baked_max_ofs=0; + baked_cache_dirty=false; + + if (points.size()==0) { + baked_point_cache.resize(0); + return; } - w = DVector<Point3>::Write(); + if (points.size()==1) { - return ret; -} + baked_point_cache.resize(1); + baked_point_cache.set(0,points[0].pos); + return; + } + Vector2 pos=points[0].pos; + int point=0; + float ofs=0; + List<Vector2> pointlist; -void Curve3D::advance(real_t p_distance,int &r_index, real_t &r_pos) const { - int pc = points.size(); - ERR_FAIL_COND(pc<3); - if (r_index<0 || r_index>=(pc-1)) - return; + for(int i=0;i<points.size()-1;i++) { - Vector3 pos = interpolate(r_index,r_pos); + float slen=points[i].pos.distance_to(points[i+1].pos); + float divs = slen / bake_interval; + if (divs>1) + divs=1; - float sign=p_distance<0 ? -1 : 1; - p_distance=Math::abs(p_distance); + float step = divs*0.1; // 10 substeps ought to be enough? + float p = 0; - real_t base = r_index+r_pos; - real_t top = 0.1; //a tenth is in theory representative - int iterations=33; + while(p<1.0) { + float np=p+step; + if (np>1.0) + np=1.0; - for(int i=0;i<iterations;i++) { + Vector2 npp = _bezier_interp(np, points[i].pos,points[i].pos+points[i].out,points[i+1].pos+points[i+1].in,points[i+1].pos); + float d = pos.distance_to(npp); + if (d>bake_interval) { + // OK! between P and NP there _has_ to be Something, let's go searching! + + int iterations = 10; //lots of detail! + + float low = p; + float hi = np; + float mid = low+(hi-low)*0.5; + + for(int j=0;j<iterations;j++) { + + + npp = _bezier_interp(mid, points[i].pos,points[i].pos+points[i].out,points[i+1].pos+points[i+1].in,points[i+1].pos); + d = pos.distance_to(npp); + + if (bake_interval < d) + hi=mid; + else + low=mid; + mid = low+(hi-low)*0.5; + + } + + pos=npp; + p=mid; + pointlist.push_back(pos); + } else { + + p=np; + } - real_t o=base+top*sign; - if (sign>0 && o >=pc) { - top=pc-base; - break; - } else if (sign<0 && o <0) { - top=-base; - break; } + } - Vector3 new_d = interpolatef(o); + Vector2 lastpos = points[points.size()-1].pos; - if (new_d.distance_to(pos) > p_distance) - break; - top*=3.0; + + float rem = pos.distance_to(lastpos); + baked_max_ofs=(pointlist.size()-1)*bake_interval+rem; + pointlist.push_back(lastpos); + + baked_point_cache.resize(pointlist.size()); + Vector2Array::Write w = baked_point_cache.write(); + int idx=0; + + + for(List<Vector2>::Element *E=pointlist.front();E;E=E->next()) { + + w[idx]=E->get(); + idx++; } +} - real_t bottom = 0.0; - iterations=8; - real_t final_offset; +float Curve2D::get_baked_length() const { + if (baked_cache_dirty) + _bake(); - for(int i=0;i<iterations;i++) { + return baked_max_ofs; +} +Vector2 Curve2D::interpolate_baked(float p_offset,bool p_cubic) const{ - real_t middle = (bottom+top)*0.5; - real_t o=base+middle*sign; - Vector3 new_d = interpolatef(o); + if (baked_cache_dirty) + _bake(); - if (new_d.distance_to(pos) > p_distance) { - bottom=middle; - } else { - top=middle; - } - final_offset=o; + //validate// + int pc = baked_point_cache.size(); + if (pc==0) { + ERR_EXPLAIN("No points in Curve2D"); + ERR_FAIL_COND_V(pc==0,Vector2()); } - r_index=(int)final_offset; - r_pos=Math::fmod(final_offset,1.0); + if (pc==1) + return baked_point_cache.get(0); + + int bpc=baked_point_cache.size(); + Vector2Array::Read r = baked_point_cache.read(); + + if (p_offset<0) + return r[0]; + if (p_offset>=baked_max_ofs) + return r[bpc-1]; + + int idx = Math::floor(p_offset/bake_interval); + float frac = Math::fmod(p_offset,bake_interval); + + if (idx>=bpc-1) { + return r[bpc-1]; + } else if (idx==bpc-2) { + frac/=Math::fmod(baked_max_ofs,bake_interval); + } else { + frac/=bake_interval; + } + + if (p_cubic) { + + Vector2 pre = idx>0? r[idx-1] : r[idx]; + Vector2 post = (idx<(bpc-2))? r[idx+2] : r[idx+1]; + return r[idx].cubic_interpolate(r[idx+1],pre,post,frac); + } else { + return r[idx].linear_interpolate(r[idx+1],frac); + } +} + +Vector2Array Curve2D::get_baked_points() const { + + if (baked_cache_dirty) + _bake(); + + return baked_point_cache; } -void Curve3D::get_approx_position_from_offset(real_t p_offset,int &r_index, real_t &r_pos,int p_subdivs) const { +void Curve2D::set_bake_interval(float p_tolerance){ + + bake_interval=p_tolerance; + baked_cache_dirty=true; + emit_signal(CoreStringNames::get_singleton()->changed); + +} - ERR_FAIL_COND(points.size()<3); +float Curve2D::get_bake_interval() const{ - real_t accum=0; + return bake_interval; +} +Dictionary Curve2D::_get_data() const { + + Dictionary dc; + + Vector2Array d; + d.resize(points.size()*3); + Vector2Array::Write w = d.write(); for(int i=0;i<points.size();i++) { - Vector3 prev_p=interpolate(i,0); + w[i*3+0]=points[i].in; + w[i*3+1]=points[i].out; + w[i*3+2]=points[i].pos; + } - for(int j=1;j<=p_subdivs;j++) { + w=Vector2Array::Write(); - real_t frac = j/(real_t)p_subdivs; - Vector3 p = interpolate(i,frac); - real_t d = p.distance_to(prev_p); + dc["points"]=d; - accum+=d; - if (accum>p_offset) { + return dc; +} +void Curve2D::_set_data(const Dictionary& p_data){ - r_index=j-1; - if (d>0) { - real_t mf = (p_offset-(accum-d)) / d; - r_pos=frac-(1.0-mf); - } else { - r_pos=frac; - } + ERR_FAIL_COND(!p_data.has("points")); - return; - } + Vector2Array rp=p_data["points"]; + int pc = rp.size(); + ERR_FAIL_COND(pc%3!=0); + points.resize(pc/3); + Vector2Array::Read r = rp.read(); - prev_p=p; + for(int i=0;i<points.size();i++) { + + points[i].in=r[i*3+0]; + points[i].out=r[i*3+1]; + points[i].pos=r[i*3+2]; + } + + baked_cache_dirty=true; + +} + + +Vector2Array Curve2D::tesselate(int p_max_stages,float p_tolerance) const { + + Vector2Array tess; + + + if (points.size()==0) { + return tess; + } + Vector< Map<float,Vector2> > midpoints; + + midpoints.resize(points.size()-1); + + int pc=1; + for(int i=0;i<points.size()-1;i++) { + + _bake_segment2d(midpoints[i],0,1,points[i].pos,points[i].out,points[i+1].pos,points[i+1].in,0,p_max_stages,p_tolerance); + pc++; + pc+=midpoints[i].size(); + + } + + tess.resize(pc); + Vector2Array::Write bpw=tess.write(); + bpw[0]=points[0].pos; + int pidx=0; + + for(int i=0;i<points.size()-1;i++) { + + for(Map<float,Vector2>::Element *E=midpoints[i].front();E;E=E->next()) { + + pidx++; + bpw[pidx] = E->get(); } + + pidx++; + bpw[pidx] = points[i+1].pos; + } - r_index=points.size()-1; - r_pos=1.0; + bpw=Vector2Array::Write (); + return tess; } +void Curve2D::_bind_methods() { -void Curve3D::set_points_in(const Vector3Array& p_points) { + ObjectTypeDB::bind_method(_MD("get_point_count"),&Curve2D::get_point_count); + ObjectTypeDB::bind_method(_MD("add_point","pos","in","out","atpos"),&Curve2D::add_point,DEFVAL(Vector2()),DEFVAL(Vector2()),DEFVAL(-1)); + ObjectTypeDB::bind_method(_MD("set_point_pos","idx","pos"),&Curve2D::set_point_pos); + ObjectTypeDB::bind_method(_MD("get_point_pos","idx"),&Curve2D::get_point_pos); + ObjectTypeDB::bind_method(_MD("set_point_in","idx","pos"),&Curve2D::set_point_in); + ObjectTypeDB::bind_method(_MD("get_point_in","idx"),&Curve2D::get_point_in); + ObjectTypeDB::bind_method(_MD("set_point_out","idx","pos"),&Curve2D::set_point_out); + ObjectTypeDB::bind_method(_MD("get_point_out","idx"),&Curve2D::get_point_out); + ObjectTypeDB::bind_method(_MD("remove_point","idx"),&Curve2D::remove_point); + ObjectTypeDB::bind_method(_MD("interpolate","idx","t"),&Curve2D::interpolate); + ObjectTypeDB::bind_method(_MD("interpolatef","fofs"),&Curve2D::interpolatef); + //ObjectTypeDB::bind_method(_MD("bake","subdivs"),&Curve2D::bake,DEFVAL(10)); + ObjectTypeDB::bind_method(_MD("set_bake_interval","distance"),&Curve2D::set_bake_interval); + ObjectTypeDB::bind_method(_MD("get_bake_interval"),&Curve2D::get_bake_interval); - points.resize(p_points.size()); - for (int i=0; i<p_points.size(); i++) { + ObjectTypeDB::bind_method(_MD("get_baked_length"),&Curve2D::get_baked_length); + ObjectTypeDB::bind_method(_MD("interpolate_baked","offset","cubic"),&Curve2D::interpolate_baked,DEFVAL(false)); + ObjectTypeDB::bind_method(_MD("get_baked_points"),&Curve2D::get_baked_points); - Point p = points[i]; - p.in = p_points[i]; - points[i] = p; - }; -}; + ObjectTypeDB::bind_method(_MD("_get_data"),&Curve2D::_get_data); + ObjectTypeDB::bind_method(_MD("_set_data"),&Curve2D::_set_data); -void Curve3D::set_points_out(const Vector3Array& p_points) { - points.resize(p_points.size()); - for (int i=0; i<p_points.size(); i++) { + ADD_PROPERTY( PropertyInfo( Variant::REAL, "bake_interval",PROPERTY_HINT_RANGE,"0.01,512,0.01"), _SCS("set_bake_interval"),_SCS("get_bake_interval")); + ADD_PROPERTY( PropertyInfo( Variant::INT, "_data",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR), _SCS("_set_data"),_SCS("_get_data")); + /*ADD_PROPERTY( PropertyInfo( Variant::VECTOR3_ARRAY, "points_out"), _SCS("set_points_out"),_SCS("get_points_out")); + ADD_PROPERTY( PropertyInfo( Variant::VECTOR3_ARRAY, "points_pos"), _SCS("set_points_pos"),_SCS("get_points_pos")); +*/ +} - Point p = points[i]; - p.out = p_points[i]; - points[i] = p; - }; -}; -void Curve3D::set_points_pos(const Vector3Array& p_points) { - points.resize(p_points.size()); - for (int i=0; i<p_points.size(); i++) { - Point p = points[i]; - p.pos = p_points[i]; - points[i] = p; - }; -}; +Curve2D::Curve2D() +{ + baked_cache_dirty=false; + baked_max_ofs=0; +/* add_point(Vector2(-1,0,0)); + add_point(Vector2(0,2,0)); + add_point(Vector2(0,3,5));*/ + bake_interval=5; -Vector3Array Curve3D::get_points_in() const { - Vector3Array ret; - ret.resize(points.size()); - for (int i=0; i<points.size(); i++) { - ret.set(i, points[i].in); - }; - return ret; -}; +} -Vector3Array Curve3D::get_points_out() const { - Vector3Array ret; - ret.resize(points.size()); - for (int i=0; i<points.size(); i++) { - ret.set(i, points[i].out); - }; - return ret; -}; -Vector3Array Curve3D::get_points_pos() const { - Vector3Array ret; - ret.resize(points.size()); - for (int i=0; i<points.size(); i++) { - ret.set(i, points[i].pos); - }; - return ret; -}; -#endif +/***********************************************************************************/ +/***********************************************************************************/ +/***********************************************************************************/ +/***********************************************************************************/ +/***********************************************************************************/ +/***********************************************************************************/ + +int Curve3D::get_point_count() const { + + return points.size(); +} +void Curve3D::add_point(const Vector3& p_pos, const Vector3& p_in, const Vector3& p_out,int p_atpos) { + + Point n; + n.pos=p_pos; + n.in=p_in; + n.out=p_out; + if (p_atpos>=0 && p_atpos<points.size()) + points.insert(p_atpos,n); + else + points.push_back(n); + + + baked_cache_dirty=true; + emit_signal(CoreStringNames::get_singleton()->changed); +} +void Curve3D::set_point_pos(int p_index, const Vector3& p_pos) { + + ERR_FAIL_INDEX(p_index,points.size()); + + points[p_index].pos=p_pos; + baked_cache_dirty=true; + emit_signal(CoreStringNames::get_singleton()->changed); + +} +Vector3 Curve3D::get_point_pos(int p_index) const { + + ERR_FAIL_INDEX_V(p_index,points.size(),Vector3()); + return points[p_index].pos; + +} + +void Curve3D::set_point_tilt(int p_index, float p_tilt) { + + ERR_FAIL_INDEX(p_index,points.size()); + + points[p_index].tilt=p_tilt; + baked_cache_dirty=true; + emit_signal(CoreStringNames::get_singleton()->changed); + +} +float Curve3D::get_point_tilt(int p_index) const { + + ERR_FAIL_INDEX_V(p_index,points.size(),0); + return points[p_index].tilt; + +} + + +void Curve3D::set_point_in(int p_index, const Vector3& p_in) { + + ERR_FAIL_INDEX(p_index,points.size()); + + points[p_index].in=p_in; + baked_cache_dirty=true; + emit_signal(CoreStringNames::get_singleton()->changed); + +} +Vector3 Curve3D::get_point_in(int p_index) const { + + ERR_FAIL_INDEX_V(p_index,points.size(),Vector3()); + return points[p_index].in; + +} + +void Curve3D::set_point_out(int p_index, const Vector3& p_out) { + + ERR_FAIL_INDEX(p_index,points.size()); + + points[p_index].out=p_out; + baked_cache_dirty=true; + emit_signal(CoreStringNames::get_singleton()->changed); + +} + +Vector3 Curve3D::get_point_out(int p_index) const { + + ERR_FAIL_INDEX_V(p_index,points.size(),Vector3()); + return points[p_index].out; + +} + + +void Curve3D::remove_point(int p_index) { + + ERR_FAIL_INDEX(p_index,points.size()); + points.remove(p_index); + baked_cache_dirty=true; + emit_signal(CoreStringNames::get_singleton()->changed); +} + +Vector3 Curve3D::interpolate(int p_index, float p_offset) const { + + int pc = points.size(); + ERR_FAIL_COND_V(pc==0,Vector3()); + + if (p_index >= pc-1) + return points[pc-1].pos; + else if (p_index<0) + return points[0].pos; + + Vector3 p0 = points[p_index].pos; + Vector3 p1 = p0+points[p_index].out; + Vector3 p3 = points[p_index+1].pos; + Vector3 p2 = p3+points[p_index+1].in; + + return _bezier_interp(p_offset,p0,p1,p2,p3); +} + +Vector3 Curve3D::interpolatef(real_t p_findex) const { + + + if (p_findex>0) + p_findex=0; + else if (p_findex>=points.size()) + p_findex=points.size(); + + return interpolate((int)p_findex,Math::fmod(p_findex,1.0)); + +} void Curve3D::_bake_segment3d(Map<float,Vector3>& r_bake, float p_begin, float p_end,const Vector3& p_a,const Vector3& p_out,const Vector3& p_b, const Vector3& p_in,int p_depth,int p_max_depth,float p_tol) const { diff --git a/scene/resources/curve.h b/scene/resources/curve.h index 046359b9b6..e361604ce3 100644 --- a/scene/resources/curve.h +++ b/scene/resources/curve.h @@ -30,7 +30,7 @@ #define CURVE_H #include "resource.h" - +#if 0 class Curve2D : public Resource { OBJ_TYPE(Curve2D,Resource); @@ -79,6 +79,79 @@ public: Curve2D(); }; +#endif + + +class Curve2D : public Resource { + + OBJ_TYPE(Curve2D,Resource); + + struct Point { + + Vector2 in; + Vector2 out; + Vector2 pos; + }; + + + Vector<Point> points; + + struct BakedPoint { + + float ofs; + Vector2 point; + }; + + mutable bool baked_cache_dirty; + mutable Vector2Array baked_point_cache; + mutable float baked_max_ofs; + + + void _bake() const; + + float bake_interval; + + void _bake_segment2d(Map<float,Vector2>& r_bake, float p_begin, float p_end,const Vector2& p_a,const Vector2& p_out,const Vector2& p_b, const Vector2& p_in,int p_depth,int p_max_depth,float p_tol) const; + Dictionary _get_data() const; + void _set_data(const Dictionary &p_data); + +protected: + + static void _bind_methods(); + + + +public: + + + int get_point_count() const; + void add_point(const Vector2& p_pos, const Vector2& p_in=Vector2(), const Vector2& p_out=Vector2(),int p_atpos=-1); + void set_point_pos(int p_index, const Vector2& p_pos); + Vector2 get_point_pos(int p_index) const; + void set_point_in(int p_index, const Vector2& p_in); + Vector2 get_point_in(int p_index) const; + void set_point_out(int p_index, const Vector2& p_out); + Vector2 get_point_out(int p_index) const; + void remove_point(int p_index); + + Vector2 interpolate(int p_index, float p_offset) const; + Vector2 interpolatef(real_t p_findex) const; + + + void set_bake_interval(float p_distance); + float get_bake_interval() const; + + + float get_baked_length() const; + Vector2 interpolate_baked(float p_offset,bool p_cubic=false) const; + Vector2Array get_baked_points() const; //useful for going thru + + Vector2Array tesselate(int p_max_stages=5,float p_tolerance=4) const; //useful for display + + + Curve2D(); +}; + class Curve3D : public Resource { diff --git a/scene/scene_string_names.cpp b/scene/scene_string_names.cpp index 40792f5139..0576d5a5b1 100644 --- a/scene/scene_string_names.cpp +++ b/scene/scene_string_names.cpp @@ -112,6 +112,7 @@ SceneStringNames::SceneStringNames() { offset=StaticCString::create("offset"); unit_offset=StaticCString::create("unit_offset"); rotation_mode=StaticCString::create("rotation_mode"); + rotate=StaticCString::create("rotate"); h_offset=StaticCString::create("h_offset"); v_offset=StaticCString::create("v_offset"); diff --git a/scene/scene_string_names.h b/scene/scene_string_names.h index 866c0ea387..d4de0555ed 100644 --- a/scene/scene_string_names.h +++ b/scene/scene_string_names.h @@ -128,6 +128,7 @@ public: StringName offset; StringName unit_offset; StringName rotation_mode; + StringName rotate; StringName v_offset; StringName h_offset; |