Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
Curve2D¶
继承: Resource < RefCounted < Object
描述 2D 空间的贝塞尔曲线。
描述¶
该类描述了 2D 空间中的贝塞尔曲线。它主要用于给 Path2D 一个形状,但也可以手动采样用于其他目的。
它保留了沿曲线的预计算点的缓存,以加快进一步的计算。
属性¶
|
||
|
方法¶
void |
add_point ( Vector2 position, Vector2 in=Vector2(0, 0), Vector2 out=Vector2(0, 0), int index=-1 ) |
void |
clear_points ( ) |
get_baked_length ( ) const |
|
get_baked_points ( ) const |
|
get_closest_offset ( Vector2 to_point ) const |
|
get_closest_point ( Vector2 to_point ) const |
|
get_point_in ( int idx ) const |
|
get_point_out ( int idx ) const |
|
get_point_position ( int idx ) const |
|
void |
remove_point ( int idx ) |
sample_baked ( float offset=0.0, bool cubic=false ) const |
|
sample_baked_with_rotation ( float offset=0.0, bool cubic=false ) const |
|
void |
set_point_in ( int idx, Vector2 position ) |
void |
set_point_out ( int idx, Vector2 position ) |
void |
set_point_position ( int idx, Vector2 position ) |
tessellate ( int max_stages=5, float tolerance_degrees=4 ) const |
|
tessellate_even_length ( int max_stages=5, float tolerance_length=20.0 ) const |
属性说明¶
float bake_interval = 5.0
相邻两个缓存点之间的距离,以像素为单位。改变它将迫使缓存在下次调用 get_baked_points 或 get_baked_length 函数时重新计算。距离越小,缓存中的点越多,占用的内存也越多,所以使用时要注意。
int point_count = 0
描述该曲线的点的数量。
方法说明¶
void add_point ( Vector2 position, Vector2 in=Vector2(0, 0), Vector2 out=Vector2(0, 0), int index=-1 )
添加一个具有相对于曲线自身位置的指定 position
,且带有控制点 in
和 out
的点。在点列表的末尾追加该新点。
如果给定了 index
,则将新点插入到由索引 index
标识的已有点之前。从 index
开始的每个已有点,都会在点列表中进一步向下移动。索引必须大于或等于 0
,并且不得超过线段中已有点的数量。参见 point_count。
void clear_points ( )
从曲线中移除所有点。
float get_baked_length ( ) const
根据缓存的点,返回曲线的总长度。给予足够的密度(见 bake_interval),它应该是足够近似的。
PackedVector2Array get_baked_points ( ) const
返回缓存的点,形式为 PackedVector2Array。
float get_closest_offset ( Vector2 to_point ) const
返回最接近 to_point
的偏移量。该偏移量被用于 sample_baked。
to_point
必须在该曲线的局部空间中。
Vector2 get_closest_point ( Vector2 to_point ) const
返回已烘焙的线段上最接近 to_point
的点(在曲线的局部空间中)。
to_point
必须在该曲线的局部空间中。
Vector2 get_point_in ( int idx ) const
返回指向顶点 idx
的控制点的位置。返回的位置是相对于顶点 idx
的。如果索引越界,则该函数将向控制台发送一个错误,并返回 (0, 0)
。
Vector2 get_point_out ( int idx ) const
返回离向顶点 idx
的控制点的位置。返回的位置是相对于顶点 idx
的。如果索引越界,则该函数将向控制台发送一个错误,并返回 (0, 0)
。
Vector2 get_point_position ( int idx ) const
返回顶点的位置 idx
。如果索引越界,则该函数将向控制台发送一个错误,并返回 (0, 0)
。
void remove_point ( int idx )
从曲线上删除点 idx
。如果 idx
越界,则会向控制台发送错误信息。
Vector2 sample ( int idx, float t ) const
返回顶点 idx
和顶点 idx + 1
之间的位置,其中 t
控制该点是否为第一个顶点(t = 0.0
)、最后一个顶点(t = 1.0
)、或介于两者之间。超出范围(0.0 >= t <=1
)的 t
的值会给出奇怪但可预测的结果。
如果 idx
越界,它将被截断到第一个或最后一个顶点,而 t
将被忽略。如果曲线没有点,则该函数将向控制台发送一个错误,并返回 (0, 0)
。
Vector2 sample_baked ( float offset=0.0, bool cubic=false ) const
返回曲线内位于 offset
位置的一个点,其中 offset
为沿曲线的像素测量距离。
为此,它会找到 offset
位于其中的两个缓存点,然后对值进行插值。如果 cubic
被设置为 true
,则该插值是立方插值;如果被设置为 false
,则该插值是线性插值。
立方插值往往能更好地跟随曲线,但线性插值速度更快(而且通常足够精确)。
Transform2D sample_baked_with_rotation ( float offset=0.0, bool cubic=false ) const
与 sample_baked 类似,但返回的是 Transform2D,包含沿曲线进行的旋转,Transform2D.origin 为点的位置,Transform2D.x 为侧面向量,Transform2D.y 为前进方向的向量。如果曲线长度为 0
则返回的是一个空变换。
var baked = curve.sample_baked_with_rotation(offset)
# 旋转并放置节点,让原来的上方向指向曲线的方向。
position = baked.get_origin()
rotation = baked.get_rotation()
# 也可以这样,但是不保留缩放。
transform = baked * Transform2D.FLIP_Y
# 匹配 PathFollow2D 的旋转,但是不保留缩放。
transform = Transform2D(baked.y, baked.x, baked.origin)
Vector2 samplef ( float fofs ) const
返回顶点 fofs
的位置。该函数使用 fofs
的整数部分作为 idx
,其小数部分作为 t
,调用 sample。
void set_point_in ( int idx, Vector2 position )
设置通往顶点 idx
的控制点位置。如果索引超出范围,函数会向控制台发送错误信息。位置相对于顶点。
void set_point_out ( int idx, Vector2 position )
设置从顶点 idx
引出的控制点位置。如果索引超出范围,函数会向控制台发送错误信息。位置相对于顶点。
void set_point_position ( int idx, Vector2 position )
设置顶点 idx
的位置。如果索引超出范围,函数会向控制台发送错误信息。
PackedVector2Array tessellate ( int max_stages=5, float tolerance_degrees=4 ) const
返回沿曲线的点的列表,点的密度由曲率控制。也就是说,弯曲的部分比直的部分有更多的点。
这种近似会在每个点之间制作直段,然后将这些直段细分,直到得到的形状足够相似。
max_stages
控制曲线段在被认为足够近似之前可能会面临多少次细分。每次细分会将曲线段分成两半,因此默认的 5 个阶段可能意味着每个曲线段最多得到 32 个细分。请谨慎增加!
tolerance_degrees
控制曲线段在其中点偏离真实曲线的多少度会被细分。
PackedVector2Array tessellate_even_length ( int max_stages=5, float tolerance_length=20.0 ) const
返回沿曲线的点列表,具有几乎均匀的密度。max_stages
控制曲线段在被认为足够近似之前可能面临多少次细分。每次细分将段分成两半,因此默认的 5 个阶段可能意味着每个曲线段最多 32 个细分。请谨慎增加!
tolerance_length
控制在必须细分线段之前两个相邻点之间的最大距离。