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(position: Vector2, in: Vector2 = Vector2(0, 0), out: Vector2 = Vector2(0, 0), index: int = -1) |
void |
|
get_baked_length() const |
|
get_baked_points() const |
|
get_closest_offset(to_point: Vector2) const |
|
get_closest_point(to_point: Vector2) const |
|
get_point_in(idx: int) const |
|
get_point_out(idx: int) const |
|
get_point_position(idx: int) const |
|
void |
remove_point(idx: int) |
sample_baked(offset: float = 0.0, cubic: bool = false) const |
|
sample_baked_with_rotation(offset: float = 0.0, cubic: bool = false) const |
|
void |
set_point_in(idx: int, position: Vector2) |
void |
set_point_out(idx: int, position: Vector2) |
void |
set_point_position(idx: int, position: Vector2) |
tessellate(max_stages: int = 5, tolerance_degrees: float = 4) const |
|
tessellate_even_length(max_stages: int = 5, tolerance_length: float = 20.0) const |
屬性說明
相鄰兩個快取點之間的距離,以圖元為單位。改變它將迫使快取在下次呼叫 get_baked_points() 或 get_baked_length() 函式時重新計算。距離越小,快取中的點越多,佔用的記憶體也越多,所以使用時要注意。
描述該曲線的點的數量。
Vector2 point_{index}/in = Vector2(0, 0) 🔗
The position of the control point leading to the vertex at index.
Note: index is a value in the 0 .. point_count - 1 range.
Vector2 point_{index}/out = Vector2(0, 0) 🔗
The position of the control point leading out of the vertex at index.
Note: index is a value in the 0 .. point_count - 1 range.
Vector2 point_{index}/position = Vector2(0, 0) 🔗
The position of for the vertex at index.
Note: index is a value in the 0 .. point_count - 1 range.
方法說明
void add_point(position: Vector2, in: Vector2 = Vector2(0, 0), out: Vector2 = Vector2(0, 0), index: int = -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(to_point: Vector2) const 🔗
返回最接近 to_point 的偏移量。該偏移量被用於 sample_baked()。
to_point 必須在該曲線的局部空間中。
Vector2 get_closest_point(to_point: Vector2) const 🔗
返回已烘焙的線段上最接近 to_point 的點(在曲線的局部空間中)。
to_point 必須在該曲線的局部空間中。
Vector2 get_point_in(idx: int) const 🔗
返回指向頂點 idx 的控制點的位置。返回的位置是相對於頂點 idx 的。如果索引越界,則該函式將向控制台發送一個錯誤,並返回 (0, 0)。
Vector2 get_point_out(idx: int) const 🔗
返回離向頂點 idx 的控制點的位置。返回的位置是相對於頂點 idx 的。如果索引越界,則該函式將向控制台發送一個錯誤,並返回 (0, 0)。
Vector2 get_point_position(idx: int) const 🔗
返回頂點的位置 idx。如果索引越界,則該函式將向控制台發送一個錯誤,並返回 (0, 0)。
從曲線上刪除點 idx。如果 idx 越界,則會向控制台發送錯誤資訊。
Vector2 sample(idx: int, t: float) const 🔗
Returns the position between the vertex idx and the vertex idx + 1, where t controls if the point is the first vertex (t = 0.0), the last vertex (t = 1.0), or in between. Values of t outside the range (0.0 <= t <= 1.0) give strange, but predictable results.
If idx is out of bounds it is truncated to the first or last vertex, and t is ignored. If the curve has no points, the function sends an error to the console, and returns (0, 0).
Vector2 sample_baked(offset: float = 0.0, cubic: bool = false) const 🔗
返回曲線內位於 offset 位置的一個點,其中 offset 為沿曲線的像素測量距離。
為此,它會找到 offset 位於其中的兩個快取點,然後對值進行插值。如果 cubic 被設定為 true,則該插值是立方插值;如果被設定為 false,則該插值是線性插值。
立方插值往往能更好地跟隨曲線,但線性插值速度更快(而且通常足夠精確)。
Transform2D sample_baked_with_rotation(offset: float = 0.0, cubic: bool = false) const 🔗
Similar to sample_baked(), but returns Transform2D that includes a rotation along the curve, with Transform2D.origin as the point position and the Transform2D.x vector pointing in the direction of the path at that point. Returns an empty transform if the length of the curve is 0.
var baked = curve.sample_baked_with_rotation(offset)
# The returned Transform2D can be set directly.
transform = baked
# You can also read the origin and rotation separately from the returned Transform2D.
position = baked.get_origin()
rotation = baked.get_rotation()
Vector2 samplef(fofs: float) const 🔗
返回頂點 fofs 的位置。該函式使用 fofs 的整數部分作為 idx,其小數部分作為 t,呼叫 sample()。
void set_point_in(idx: int, position: Vector2) 🔗
設定通往頂點 idx 的控制點位置。如果索引超出範圍,函式會向控制台發送錯誤信息。位置相對於頂點。
void set_point_out(idx: int, position: Vector2) 🔗
設定從頂點 idx 引出的控制點位置。如果索引超出範圍,函式會向控制台發送錯誤資訊。位置相對於頂點。
void set_point_position(idx: int, position: Vector2) 🔗
設定頂點 idx 的位置。如果索引超出範圍,函式會向控制台發送錯誤資訊。
PackedVector2Array tessellate(max_stages: int = 5, tolerance_degrees: float = 4) const 🔗
返回沿曲線的點的列表,點的密度由曲率控制。也就是說,彎曲的部分比直的部分有更多的點。
這種近似會在每個點之間製作直段,然後將這些直段細分,直到得到的形狀足夠相似。
max_stages 控制曲線段在被認為足夠近似之前可能會面臨多少次細分。每次細分會將曲線段分成兩半,因此預設的 5 個階段可能意味著每個曲線段最多得到 32 個細分。請謹慎增加!
tolerance_degrees 控制曲線段在其中點偏離真實曲線的多少度會被細分。
PackedVector2Array tessellate_even_length(max_stages: int = 5, tolerance_length: float = 20.0) const 🔗
返回沿曲線的點列表,具有幾乎均勻的密度。max_stages 控制曲線段在被認為足夠近似之前可能面臨多少次細分。每次細分將段分成兩半,因此預設的 5 個階段可能意味著每個曲線段最多 32 個細分。請謹慎增加!
tolerance_length 控制在必須細分線段之前兩個相鄰點之間的最大距離。