THREE.Vector2是 Three.js 中用来表示二维向量的类,它可以用于处理 2D 空间中的坐标、方向、速度等。它提供了许多用于操作向量的方法,例如加减法、缩放、插值、点积等。
Vector2 有五个属性四十八个方法
Vector2( x : Float, y : Float ) x - 向量的x值,默认为0。 y - 向量的y值,默认为0。
属性
- height : Float y的别名。
- isVector2 : Boolean 判断是二维向量
- width : Float x的别名。
- x : Float x 值
- y : Float y 值
方法
- add ( v : Vector2 ) : this add(v: Vector2): this 是 THREE.Vector2 类中的一个方法,用于将另一个 Vector2 向量添加到当前向量上,并返回当前向量本身。这意味着该方法会修改当前对象,而不是创建新的向量。
const v1 = new THREE.Vector2(1, 2);
const v2 = new THREE.Vector2(3, 4);
// 将 v2 加到 v1 上
v1.add(v2);
console.log(v1); // 输出 {x: 4, y: 6}
- addScalar ( s : Float ) : this 将传入的标量s和这个向量的x值、y值相加。
- addScaledVector ( v : Vector2, s : Float ) : this addScaledVector(v: Vector2, s: Float): this 是 THREE.Vector2 类中的一个方法,用于将向量 v 按比例 s 缩放后,添加到当前向量上,并返回当前向量本身。这个方法允许你在进行缩放的同时将结果向量叠加到当前向量上,常用于一些物理模拟或动画中的矢量运算。
const v1 = new THREE.Vector2(1, 1);
const v2 = new THREE.Vector2(2, 3);
// 将 v2 缩放 2 倍后加到 v1 上
v1.addScaledVector(v2, 2);
console.log(v1); // 输出 {x: 5, y: 7}
- addVectors ( a : Vector2, b : Vector2 ) : this 将该向量设置为 a + b。
- angle () : Float angle(): Float 是 THREE.Vector2 类中的一个方法,用于计算当前二维向量与 X 轴正方向之间的夹角(以弧度为单位)。它返回的值在 [-π, π] 范围内,表示当前向量从 X 轴旋转到该向量所需的角度。
const v1 = new THREE.Vector2(1, 0); // 与 X 轴正方向重合
console.log(v1.angle()); // 输出 0
const v2 = new THREE.Vector2(0, 1); // 与 Y 轴正方向重合
console.log(v2.angle()); // 输出 π/2
const v3 = new THREE.Vector2(-1, 0); // 与 X 轴负方向重合
console.log(v3.angle()); // 输出 π 或 -π
const v4 = new THREE.Vector2(0, -1); // 与 Y 轴负方向重合
console.log(v4.angle()); // 输出 -π/2
- angleTo ( v : Vector2 ) : Float angeTo(v: Vector2): Float 是 THREE.Vector2 类中的一个方法,用于计算当前二维向量与另一个向量 v 之间的夹角(以弧度为单位)。这个夹角始终是正值,范围为 [0, π],表示两个向量之间的相对角度。中心点默认为 0,0
const v1 = new THREE.Vector2(1, 0); // 向量 (1, 0)
const v2 = new THREE.Vector2(2, 0); // 与 v1 平行的向量
// 计算 v1 和 v2 之间的夹角
console.log(v1.angleTo(v2)); // 输出 0,夹角为 0 度
const v3 = new THREE.Vector2(-1, 0); // 与 v1 方向相反的向量
// 计算 v1 和 v3 之间的夹角
console.log(v1.angleTo(v3)); // 输出 π(3.141592653589793),180度
- applyMatrix3 ( m : Matrix3 ) : this applyMatrix3(m: Matrix3): this 是 THREE.Vector2 类中的一个方法,用于将一个 3x3 矩阵 Matrix3 作用到当前的二维向量 Vector2 上,并返回修改后的向量。这种操作允许你通过矩阵来变换向量,例如执行旋转、缩放、平移等几何操作。
const v = new THREE.Vector2(1, 2);
const m = new THREE.Matrix3().set(
1, 0, 1, // 第一行:用于平移 (1, 0)
0, 1, 2, // 第二行:用于平移 (0, 2)
0, 0, 1 // 第三行:齐次坐标
);
// 将矩阵 m 作用于向量 v
v.applyMatrix3(m);
console.log(v); // 输出 {x: 2, y: 4},向量 (1, 2) 被平移变为 (2, 4)
- ceil () : this 向量中的x分量和y分量向上取整为最接近的整数值。
- clamp ( min : Vector2, max : Vector2 ) : this min - 在限制范围内,x和y的最小值。 max - 在限制范围内,x和y的最大值。 如果该向量的x值或y值大于限制范围内最大x值或y值,则该值将会被所对应的值取代。 如果该向量的x值或y值小于限制范围内最小x值或y值,则该值将会被所对应的值取代。
- clampLength ( min : Float, max : Float ) : this min - 长度将被限制为的最小值 max - 长度将被限制为的最大值 如果向量长度大于最大值,则它将会被最大值所取代。 如果向量长度小于最小值,则它将会被最小值所取代。
const v = new THREE.Vector2(1, 1);
// 限制向量 v 的长度在 2 到 5 之间
v.clampLength(2, 5);
console.log(v); // 输出 {x: 1.414, y: 1.414},这是长度为 2 的单位向量
- clampScalar ( min : Float, max : Float ) : this min - 分量将被限制为的最小值 max - 分量将被限制为的最大值 如果该向量的x值或y值大于最大值,则它们将被最大值所取代。 如果该向量的x值或y值小于最小值,则它们将被最小值所取代。
const v = new THREE.Vector2(5, -3);
// 限制向量 v 的分量在 -1 到 3 之间
v.clampScalar(-1, 3);
console.log(v); // 输出 {x: 3, y: -1}
- clone () : Vector2 返回一个新的Vector2,其具有和当前这个向量相同的x和y。
- copy ( v : Vector2 ) : this 将所传入Vector2的x和y属性复制当前对Vector2
- distanceTo ( v : Vector2 ) : Float 计算该vector到传入的v的距离。
// 创建两个 Vector2 实例
const a = new THREE.Vector2(3, 4);
const b = new THREE.Vector2(1, 1);
// 计算欧几里得距离
const distance = a.distanceTo(b);
console.log(distance); // 输出: 3.605551275463989
- manhattanDistanceTo ( v : Vector2 ) : Float 计算该vector到传入的v的曼哈顿距离(Manhattan distance)。
// 创建两个 Vector2 实例
const a = new THREE.Vector2(3, 4);
const b = new THREE.Vector2(1, 1);
// 计算曼哈顿距离
const distance = a.manhattanDistanceTo(b);
console.log(distance); // 输出: 5
- distanceToSquared ( v : Vector2 ) : Float 是
THREE.Vector2类中的一个方法,用于计算当前向量与另一个二维向量之间的平方距离。平方距离是指两个点之间的欧几里得距离的平方,常用于避免计算平方根,从而提高性能,尤其是在频繁调用距离计算的情况下。
// 创建两个 Vector2 实例
const a = new THREE.Vector2(3, 4);
const b = new THREE.Vector2(1, 1);
// 计算平方距离
const squaredDistance = a.distanceToSquared(b);
console.log(squaredDistance); // 输出: 13
- divide ( v : Vector2 ) : this divide(v: Vector2): this 是 THREE.Vector2 类中的一个方法,用于将当前向量的每个分量分别除以另一个二维向量的对应分量。这个操作通常用于调整或缩放一个向量。
// 创建两个 Vector2 实例
const a = new THREE.Vector2(4, 8);
const b = new THREE.Vector2(2, 4);
// 将 a 除以 b
a.divide(b);
console.log(a); // 输出: Vector2 { x: 2, y: 2 }
- divideScalar ( s : Float ) : this 将该向量除以标量s。
- dot ( v : Vector2 ) : Float dot(v: Vector2): Float 是 THREE.Vector2 类中的一个方法,用于计算当前向量与另一个二维向量之间的点积(也称为内积)。点积是向量运算中常用的一个操作,可以用于判断两个向量之间的夹角以及计算投影等。
// 创建两个 Vector2 实例
const a = new THREE.Vector2(3, 4);
const b = new THREE.Vector2(1, 2);
// 计算点积
const dotProduct = a.dot(b);
console.log(dotProduct); // 输出: 11
- cross ( v : Vector2 ) : Float 是
THREE.Vector2类中的一个方法,用于计算当前二维向量与另一个二维向量之间的叉积(cross product)。在二维空间中,叉积的结果实际上是一个标量值,表示两个向量所形成的平行四边形的面积的符号(方向),并且可以用于判断两个向量的旋转方向。
// 创建两个 Vector2 实例
const a = new THREE.Vector2(3, 4);
const b = new THREE.Vector2(1, 2);
// 计算叉积
const crossProduct = a.cross(b);
console.log(crossProduct); // 输出: 2
- equals ( v : Vector2 ) : Boolean 检查该向量和v的严格相等性。
- floor () : this 向量中的x分量和y分量向下取整为最接近的整数值。
- fromArray ( array : Array, offset : Integer ) : this array - 来源的数组。 offset - (可选)在数组中的元素偏移量,默认值为0。 设置向量中的x值为array[ offset ],y值为array[ offset + 1 ]。
- fromBufferAttribute ( attribute : BufferAttribute, index : Integer ) : this attribute - 来源的attribute。 index - 在attribute中的索引。 从attribute中设置向量的x值和y值。
- getComponent ( index : Integer ) : Float index - 0 或 1 如果index值为0则返回x值。 如果index值为1则返回y值。
// 创建一个 Vector2 实例
const a = new THREE.Vector2(3, 4);
// 获取 x 分量
const xComponent = a.getComponent(0);
console.log(xComponent); // 输出: 3
// 获取 y 分量
const yComponent = a.getComponent(1);
console.log(yComponent); // 输出: 4
- length () : Float 计算从(0, 0)到(x, y)的欧几里得长度 (Euclidean length,即直线长度)。
- manhattanLength () : Float manhattanLength(): Float 是 THREE.Vector2 类中的一个方法,用于计算当前二维向量的曼哈顿长度(Manhattan Length),也称为城市街区距离(Taxicab Distance)。曼哈顿长度是指在一个网格状的城市中,从一个点到另一个点所需的总水平和垂直距离之和。
// 创建一个 Vector2 实例
const v = new THREE.Vector2(3, 4);
// 计算曼哈顿长度
const manhattanLength = v.manhattanLength();
console.log(manhattanLength); // 输出: 7
- lengthSq () : Float lengthSq(): Float 是 THREE.Vector2 类中的一个方法,用于计算当前二维向量的平方长度(平方模)。平方长度是向量的长度的平方,可以避免使用平方根运算,从而提高性能,特别是在不需要实际长度值的情况下。
// 创建一个 Vector2 实例
const v = new THREE.Vector2(3, 4);
// 计算平方长度
const lengthSquared = v.lengthSq();
console.log(lengthSquared); // 输出: 25 x平方+ y平方
- lerp ( v : Vector2, alpha : Float ) : this lerp(v: Vector2, alpha: Float): this 是 THREE.Vector2 类中的一个方法,用于在当前向量和另一个向量之间进行线性插值(Linear Interpolation)。这个方法可以在两个向量之间生成一个新的向量,依据插值因子 alpha 的值来决定结果向量的位置。
- lerpVectors ( v1 : Vector2, v2 : Vector2, alpha : Float ) : this 是 THREE.Vector2 类中的一个静态方法,用于在两个给定的二维向量之间进行线性插值(Linear Interpolation)。与 lerp 方法不同,lerpVectors 直接返回一个新的插值结果,而不改变当前向量。
// 创建两个 Vector2 实例
const v1 = new THREE.Vector2(1, 2);
const v2 = new THREE.Vector2(4, 6);
// 创建一个新的 Vector2 实例用于存储结果
const result = new THREE.Vector2().lerpVectors(v1, v2, 0.5);
console.log(result); // 输出: Vector2 { x: 2.5, y: 4 }
- negate () : this 向量取反,即: x = -x , y = -y。
- normalize () : this 将该向量转换为单位向量(unit vector), 也就是说,将该向量的方向设置为和原向量相同,但是其长度(length)为1。
// 创建一个 Vector2 实例
const v = new THREE.Vector2(3, 4);
// 对向量进行归一化
v.normalize();
console.log(v); // 输出: Vector2 { x: 0.6, y: 0.8 }
- max ( v : Vector2 ) : this 如果该向量的x值或y值小于所传入v的x值或y值,则将该值替换为对应的最大值。
- min ( v : Vector2 ) : this 如果该向量的x值或y值大于所传入v的x值或y值,则将该值替换为对应的最小值。
- multiply ( v : Vector2 ) : this 将该向量与所传入的向量v进行相乘。
- multiplyScalar ( s : Float ) : this 将该向量与所传入的标量s进行相乘。
- rotateAround ( center : Vector2, angle : Float ) : this 是 THREE.Vector2 类中的一个方法,用于将当前向量绕指定中心点旋转一个给定的角度。这个方法可以用于实现旋转效果,特别是在图形和物理模拟中。
const v = new THREE.Vector2(2, 3);
const center = new THREE.Vector2(1, 1);
const angle = Math.PI / 2; // 90 degrees in radians
// 对向量进行旋转
v.rotateAround(center, angle);
console.log(v); // { x: 0, y: 2 }
- round () : this 向量中的x分量和y分量四舍五入取整为最接近的整数值。
- roundToZero () : this 向量中的分量朝向0取整数(若分量为负数则向上取整,若为正数则向下取整)。
- set ( x : Float, y : Float ) : this 设置该向量的x和y分量。
- setComponent ( index : Integer, value : Float ) : this index - 0 或 1 value - Float 如果index值为0则将x值设置为value。 如果index值为1则将y值设置为value。
- setLength ( l : Float ) : this 将该向量的方向设置为和原向量相同,但是长度(length)为l。
- setScalar ( scalar : Float ) : this 将该向量的x、y值同时设置为等于传入的scalar。
- setX ( x : Float ) : this 将向量中的x值替换为x。
- setY ( y : Float ) : this 将向量中的y值替换为y。
- sub ( v : Vector2 ) : this 从该向量减去向量v。
- subScalar ( s : Float ) : this 从该向量的x和y中减去标量s。
- subVectors ( a : Vector2, b : Vector2 ) : this 将该向量设置为a - b。
- toArray ( array : Array, offset : Integer ) : Array array - (可选)被用于存储向量的数组。如果这个值没有传入,则将创建一个新的数组。 offset - (可选) 数组中元素的偏移量。 返回一个数组[x, y],或者将x和y复制到所传入的array中。
- random () : this 设置XY为0-1的随机数