数学函数(MathUtils)

优质
小牛编辑
126浏览
2023-12-01

具有多个数学实用函数的对象。

函数(Functions)

.clamp ( value : Float, min : Float, max : Float ) : Float

value — 需要clamp处理的值。
min — 最小值。
max — 最大值。

限制数值value处于最小值min和最大值max之间。

.degToRad ( degrees : Float ) : Float

将度转化为弧度。

.euclideanModulo ( n : Integer, m : Integer ) : Integer

n, m - 整型

计算 m % n 的欧几里得模:

( ( n % m ) + m ) % m

.generateUUID ( ) : UUID

创建一个全局唯一标识符 UUID

.isPowerOfTwo ( n : Number ) : Boolean

如果 n 是2的幂,返回true。

.inverseLerp ( x : Float, y : Float, value : Float ) : Float

x - Start point.
y - End point.
value - A value between start and end.

Returns the percentage in the closed interval [0, 1] of the given value between the start and end point.

.lerp ( x : Float, y : Float, t : Float ) : Float

x - 起始点。
y - 终点。
t - 闭区间 [0,1] 内的插值因子。

返回给定区间的线性插值linearly interpolated结果 - t = 0 将会返回 x 如果 t = 1 将会返回 y.

.damp ( x : Float, y : Float, lambda : Float, dt : Float ) : Float

x - Current point.
y - Target point.
lambda - A higher lambda value will make the movement more sudden, and a lower value will make the movement more gradual.
dt - Delta time in seconds.

Smoothly interpolate a number from x toward y in a spring-like manner using the dt to maintain frame rate independent movement. For details, see Frame rate independent damping using lerp.

.mapLinear ( x : Float, a1 : Float, a2 : Float, b1 : Float, b2 : Float ) : Float

x — 用于映射的值。
a1 — A区间最小值。
a2 — A区间最大值。
b1 — B区间最小值。
b2 — A区间最大值。

x从范围[a1, a2] 到范围[b1, b2]的线性映射。

.pingpong ( x : Float, length : Float ) : Float

x — The value to pingpong.
length — The positive value the function will pingpong to. Default is 1.

Returns a value that alternates between 0 and length : Float.

.ceilPowerOfTwo ( n : Number ) : Integer

返回大于等于 n 的2的最小次幂。

.floorPowerOfTwo ( n : Number ) : Integer

返回小于等于 n 的2的最大幂。

.radToDeg ( radians : Float ) : Float

将弧度转换为角度。

.randFloat ( low : Float, high : Float ) : Float

在区间 [low, high] 内随机一个浮点数。

.randFloatSpread ( range : Float ) : Float

在区间 [- range / 2, range / 2] 内随机一个浮点数。

.randInt ( low : Integer, high : Integer ) : Integer

在区间 [low, high] 内随机一个整数。

.seededRandom ( seed : Integer ) : Float

在区间 [0, 1] 中生成确定性的伪随机浮点数。 整数种子是可选的。

.smoothstep ( x : Float, min : Float, max : Float ) : Float

x - 根据其在最小值和最大值之间的位置来计算的值。
min - 任何x比最小值还小会返回0.
max - 任何x比最大值还大会返回1.

返回0-1之间的值,该值表示x在最小值和最大值之间移动的百分比,但是当x接近最小值和最大值时,变化程度会平滑或减慢。

查看更多详情请移步到 Smoothstep

.smootherstep ( x : Float, min : Float, max : Float ) : Float

x - 根据其在最小值和最大值之间的位置来计算的值。
min - 任何x比最小值还小会返回0.
max - 任何x比最大值还大会返回0.

返回一个0-1之间的值。它和smoothstep相同,但变动更平缓。variation on smoothstep 在x=0和x=1处有0阶和二阶导数。

.setQuaternionFromProperEuler ( q : Quaternion, a : Float, b : Float, c : Float, order : String ) : null

q - the quaternion to be set
a - the rotation applied to the first axis, in radians
b - the rotation applied to the second axis, in radians
c - the rotation applied to the third axis, in radians
order - a string specifying the axes order: 'XYX', 'XZX', 'YXY', 'YZY', 'ZXZ', or 'ZYZ'

Sets quaternion q from the intrinsic Proper Euler Angles defined by angles a, b, and c, and order order.
Rotations are applied to the axes in the order specified by order: rotation by angle a is applied first, then by angle b, then by angle c. Angles are in radians.

Source

src/math/MathUtils.js