three.js Vector2

2023-02-16 17:47 更新

表示2D vector(二維向量)的類。 一個二維向量是一對有順序的數(shù)字(標記為x和y),可用來表示很多事物,例如:

  • 一個位于二維空間中的點(例如一個在平面上的點)。
  • 一個在平面上的方向與長度的定義。在three.js中,長度總是從(0, 0)到(x, y)的 Euclidean distance(歐幾里德距離,即直線距離), 方向也是從(0, 0)到(x, y)的方向。
  • 任意的、有順序的一對數(shù)字。

其他的一些事物也可以使用二維向量進行表示,比如說動量矢量、復數(shù)等等;但以上這些是它在three.js中的常用用途。

對 Vector2 實例進行遍歷將按相應的順序生成它的分量 (x, y)。

代碼示例

const a = new THREE.Vector2( 0, 1 );

//no arguments; will be initialised to (0, 0)
const b = new THREE.Vector2( );

const d = a.distanceTo( b );

構造函數(shù)

Vector2( x : Float, y : Float )

x - 向量的x值,默認為0
y - 向量的y值,默認為0

創(chuàng)建一個新的Vector2。

屬性

.height : Float

y的別名。

.isVector2 : Boolean

用于檢查給定對象是否為 Vector2 類型的只讀標志。

.width : Float

x的別名。

.x : Float

.y : Float

方法

.add ( v : Vector2 ) : this

將傳入的向量v和這個向量相加。

.addScalar ( s : Float ) : this

將傳入的標量s和這個向量的x值、y值相加。

.addScaledVector ( v : Vector2, s : Float ) : this

將所傳入的v與s相乘所得乘積和這個向量相加。

.addVectors ( a : Vector2, b : Vector2 ) : this

將該向量設置為 a + b。

.angle () : Float

計算該向量相對于x軸正方向的弧度角度。

.applyMatrix3 ( m : Matrix3 ) : this

將該向量乘以三階矩陣m(第三個值隱式地為1)。

.ceil () : this

向量中的x分量和y分量向上取整為最接近的整數(shù)值。

.clamp ( min : Vector2, max : Vector2 ) : this

min - 在限制范圍內(nèi),x和y的最小值。
max - 在限制范圍內(nèi),x和y的最大值。

如果該向量的x值或y值大于限制范圍內(nèi)最大x值或y值,則該值將會被所對應的值取代。

如果該向量的x值或y值小于限制范圍內(nèi)最小x值或y值,則該值將會被所對應的值取代。

.clampLength ( min : Float, max : Float ) : this

min - 長度將被限制為的最小值
max - 長度將被限制為的最大值

如果向量長度大于最大值,則它將會被最大值所取代。

如果向量長度小于最小值,則它將會被最小值所取代。

.clampScalar ( min : Float, max : Float ) : this

min - 分量將被限制為的最小值

max - 分量將被限制為的最大值

如果該向量的x值或y值大于最大值,則它們將被最大值所取代。

如果該向量的x值或y值小于最小值,則它們將被最小值所取代。

.clone () : Vector2

返回一個新的Vector2,其具有和當前這個向量相同的x和y。

.copy ( v : Vector2 ) : this

將所傳入Vector2的x和y屬性復制給這一Vector2。

.distanceTo ( v : Vector2 ) : Float

計算該vector到傳入的v的距離。

.manhattanDistanceTo ( v : Vector2 ) : Float

計算該vector到傳入的v的曼哈頓距離(Manhattan distance)。

.distanceToSquared ( v : Vector2 ) : Float

計算該向量到傳入的v的平方距離。 如果你只是將該距離和另一個距離進行比較,則應當比較的是距離的平方, 因為它的計算效率會更高一些。

.divide ( v : Vector2 ) : this

將該向量除以向量v。

.divideScalar ( s : Float ) : this

將該向量除以標量s。

.dot ( v : Vector2 ) : Float

計算該vector和所傳入v的點積(dot product)。

.cross ( v : Vector2 ) : Float

計算該vector和所傳入v的叉積(cross product)。 請注意,“叉積”在2D中并沒有被明確定義。該函數(shù)計算的是2D圖形中經(jīng)常使用的幾何叉積。

.equals ( v : Vector2 ) : Boolean

檢查該向量和v的嚴格相等性。

.floor () : this

向量中的x分量和y分量向下取整為最接近的整數(shù)值。

.fromArray ( array : Array, offset : Integer ) : this

array - 來源的數(shù)組。
offset - (可選)在數(shù)組中的元素偏移量,默認值為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值。

.length () : Float

計算從(0, 0)到(x, y)的歐幾里得長度 (Euclidean length,即直線長度)。

.manhattanLength () : Float

計算該向量的曼哈頓長度(Manhattan length)。

.lengthSq () : Float

計算從(0, 0)到(x, y)的歐幾里得長度 (Euclidean length,即直線長度)的平方。 如果你正在比較向量的長度,應當比較的是長度的平方,因為它的計算效率更高一些。

.lerp ( v : Vector2, alpha : Float ) : this

v - 朝著進行插值的Vector2。
alpha - 插值因數(shù),其范圍通常在[0, 1]閉區(qū)間。

在該向量與傳入的向量v之間的線性插值,alpha是沿著線的長度的百分比 —— alpha = 0 時表示的是當前向量,alpha = 1 時表示的是所傳入的向量v。

.lerpVectors ( v1 : Vector2, v2 : Vector2, alpha : Float ) : this

v1 - 起始的Vector2。
v2 - 朝著進行插值的Vector2。
alpha - 插值因數(shù),其范圍通常在[0, 1]閉區(qū)間。

將此向量設置為在v1和v2之間進行線性插值的向量, 其中alpha為兩個向量之間連線的長度的百分比 —— alpha = 0 時表示的是v1,alpha = 1 時表示的是v2。

.negate () : this

向量取反,即: x = -x , y = -y。

.normalize () : this

將該向量轉換為單位向量(unit vector), 也就是說,將該向量的方向設置為和原向量相同,但是其長度(length)為1。

.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

center - 將被圍繞旋轉的點。
angle - 將要旋轉的角度,以弧度來表示。

將向量圍繞著center旋轉angle弧度。

.round () : this

向量中的x分量和y分量四舍五入取整為最接近的整數(shù)值。

.roundToZero () : this

向量中的分量朝向0取整數(shù)(若分量為負數(shù)則向上取整,若為正數(shù)則向下取整)。

.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 - (可選)被用于存儲向量的數(shù)組。如果這個值沒有傳入,則將創(chuàng)建一個新的數(shù)組。
offset - (可選) 數(shù)組中元素的偏移量。

返回一個數(shù)組[x, y],或者將x和y復制到所傳入的array中。

.random () : this

將該向量的每個分量(x、y)設置為介于 0 和 1 之間的偽隨機數(shù),不包括 1。

源代碼

src/math/Vector2.js


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號