Curve3D
Describes a Bézier curve in 3D space.
Curve3D.swift:14class Curve3D
This class describes a Bézier curve in 3D space. It is mainly used to give a shape to a Path3D
, but can be manually sampled for other purposes.
It keeps a cache of precalculated points along the curve, to speed up further calculations.
Superclasses
class Resource
Base class for serializable objects.
Citizens in SwiftGodot
Conformances
protocol CustomStringConvertible
A type with a customized textual representation.
protocol Equatable
A type that can be compared for value equality.
protocol Hashable
A type that can be hashed into a
Hasher
to produce an integer hash value.protocol Identifiable<ID>
A class of types whose instances hold the value of an entity with stable identity.
protocol VariantRepresentable
Types that conform to VariantRepresentable can be stored directly in
Variant
with no conversion. These include all of the Variant types from Godot (for exampleGString
,Rect
,Plane
), Godot objects (those that subclass SwiftGodot.Object) as well as the built-in Swift types UInt8, Int64 and Double.protocol VariantStorable
Types that conform to VariantStorable can be stored in a Variant and can be extracted back out of a Variant.
Type members
Instance members
var bakeInterval: Double
The distance in meters between two adjacent cached points. Changing it forces the cache to be recomputed the next time the
getBakedPoints
orgetBakedLength
function is called. The smaller the distance, the more points in the cache and the more memory it will consume, so use with care.var pointCount: Int32
The number of points describing the curve.
var upVectorEnabled: Bool
If
true
, the curve will bake up vectors used for orientation. This is used whenrotationMode
is set toPathFollow3D/RotationMode/rotationOriented
. Changing it forces the cache to be recomputed.func addPoint(position: Vector3, in: Vector3, out: Vector3, index: Int32
) Adds a point with the specified
position
relative to the curve’s own position, with control pointsin
andout
. Appends the new point at the end of the point list.func clearPoints(
) Removes all points from the curve.
func getBakedLength(
) -> Double Returns the total length of the curve, based on the cached points. Given enough density (see
bakeInterval
), it should be approximate enough.func getBakedPoints(
) -> PackedVector3Array Returns the cache of points as a
PackedVector3Array
.func getBakedTilts(
) -> PackedFloat32Array Returns the cache of tilts as a
PackedFloat32Array
.func getBakedUpVectors(
) -> PackedVector3Array Returns the cache of up vectors as a
PackedVector3Array
.func getClosestOffset(toPoint: Vector3
) -> Double Returns the closest offset to
toPoint
. This offset is meant to be used insampleBaked(offset:cubic:)
orsampleBakedUpVector(offset:applyTilt:)
.func getClosestPoint(toPoint: Vector3
) -> Vector3 Returns the closest point on baked segments (in curve’s local space) to
toPoint
.func getPointIn(idx: Int32
) -> Vector3 Returns the position of the control point leading to the vertex
idx
. The returned position is relative to the vertexidx
. If the index is out of bounds, the function sends an error to the console, and returns(0, 0, 0)
.func getPointOut(idx: Int32
) -> Vector3 Returns the position of the control point leading out of the vertex
idx
. The returned position is relative to the vertexidx
. If the index is out of bounds, the function sends an error to the console, and returns(0, 0, 0)
.func getPointPosition(idx: Int32
) -> Vector3 Returns the position of the vertex
idx
. If the index is out of bounds, the function sends an error to the console, and returns(0, 0, 0)
.func getPointTilt(idx: Int32
) -> Double Returns the tilt angle in radians for the point
idx
. If the index is out of bounds, the function sends an error to the console, and returns0
.func removePoint(idx: Int32
) Deletes the point
idx
from the curve. Sends an error to the console ifidx
is out of bounds.func sample(idx: Int32, t: Double
) -> Vector3 Returns the position between the vertex
idx
and the vertexidx + 1
, wheret
controls if the point is the first vertex (t = 0.0
), the last vertex (t = 1.0
), or in between. Values oft
outside the range (0.0 >= t <=1
) give strange, but predictable results.func sampleBaked(offset: Double, cubic: Bool
) -> Vector3 Returns a point within the curve at position
offset
, whereoffset
is measured as a distance in 3D units along the curve. To do that, it finds the two cached points where theoffset
lies between, then interpolates the values. This interpolation is cubic ifcubic
is set totrue
, or linear if set tofalse
.func sampleBakedUpVector(offset: Double, applyTilt: Bool
) -> Vector3 Returns an up vector within the curve at position
offset
, whereoffset
is measured as a distance in 3D units along the curve. To do that, it finds the two cached up vectors where theoffset
lies between, then interpolates the values. IfapplyTilt
istrue
, an interpolated tilt is applied to the interpolated up vector.func sampleBakedWithRotation(offset: Double, cubic: Bool, applyTilt: Bool
) -> Transform3D Returns a
Transform3D
withorigin
as point position,basis.x
as sideway vector,basis.y
as up vector,basis.z
as forward vector. When the curve length is 0, there is no reasonable way to calculate the rotation, all vectors aligned with global space axes. See alsosampleBaked(offset:cubic:)
.func samplef(fofs: Double
) -> Vector3 Returns the position at the vertex
fofs
. It callssample(idx:t:)
using the integer part offofs
asidx
, and its fractional part ast
.func setPointIn(idx: Int32, position: Vector3
) Sets the position of the control point leading to the vertex
idx
. If the index is out of bounds, the function sends an error to the console. The position is relative to the vertex.func setPointOut(idx: Int32, position: Vector3
) Sets the position of the control point leading out of the vertex
idx
. If the index is out of bounds, the function sends an error to the console. The position is relative to the vertex.func setPointPosition(idx: Int32, position: Vector3
) Sets the position for the vertex
idx
. If the index is out of bounds, the function sends an error to the console.func setPointTilt(idx: Int32, tilt: Double
) Sets the tilt angle in radians for the point
idx
. If the index is out of bounds, the function sends an error to the console.func tessellate(maxStages: Int32, toleranceDegrees: Double
) -> PackedVector3Array Returns a list of points along the curve, with a curvature controlled point density. That is, the curvier parts will have more points than the straighter parts.
func tessellateEvenLength(maxStages: Int32, toleranceLength: Double
) -> PackedVector3Array Returns a list of points along the curve, with almost uniform density.
maxStages
controls how many subdivisions a curve segment may face before it is considered approximate enough. Each subdivision splits the segment in half, so the default 5 stages may mean up to 32 subdivisions per curve segment. Increase with care!