three.js Vector3

2023-02-16 17:47 更新

該類表示的是一個(gè)三維向量(3D vector)。 一個(gè)三維向量表示的是一個(gè)有順序的、三個(gè)為一組的數(shù)字組合(標(biāo)記為x、y和z), 可被用來表示很多事物,例如:

  • 一個(gè)位于三維空間中的點(diǎn)。
  • 一個(gè)在三維空間中的方向與長度的定義。在three.js中,長度總是從(0, 0, 0)到(x, y, z)的 Euclidean distance(歐幾里德距離,即直線距離), 方向也是從(0, 0, 0)到(x, y, z)的方向。
  • 任意的、有順序的、三個(gè)為一組的數(shù)字組合。

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

對 Vector3 實(shí)例進(jìn)行遍歷將按相應(yīng)的順序生成它的分量 (x, y, z)。

代碼示例

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

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

const d = a.distanceTo( b );

構(gòu)造函數(shù)

Vector3( x : Float, y : Float, z : Float )

x - 向量的x值,默認(rèn)為0。
y - 向量的y值,默認(rèn)為0。
z - 向量的z值,默認(rèn)為0。

創(chuàng)建一個(gè)新的Vector3。

屬性

.isVector3 : Boolean

用于檢查給定對象是否為 Vector3 類型的只讀標(biāo)志。

.x : Float

.y : Float

.z : Float

方法

.add ( v : Vector3 ) : this

將傳入的向量v和這個(gè)向量相加。

.addScalar ( s : Float ) : this

將傳入的標(biāo)量s和這個(gè)向量的x值、y值以及z值相加。

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

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

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

將該向量設(shè)置為a + b。

.applyAxisAngle ( axis : Vector3, angle : Float ) : this

axis - 一個(gè)被歸一化的Vector3。
angle - 以弧度表示的角度。

將軸和角度所指定的旋轉(zhuǎn)應(yīng)用到該向量上。

.applyEuler ( euler : Euler ) : this

通過將Euler(歐拉)對象轉(zhuǎn)換為Quaternion(四元數(shù))并應(yīng)用, 將歐拉變換應(yīng)用到這一向量上。

.applyMatrix3 ( m : Matrix3 ) : this

將該向量乘以三階矩陣m。

.applyMatrix4 ( m : Matrix4 ) : this

將該向量乘以四階矩陣m(第四個(gè)維度隱式地為1),并按角度進(jìn)行劃分。

.applyNormalMatrix ( m : Matrix3 ) : this

將該向量乘以正規(guī)矩陣 m,并將結(jié)果進(jìn)行歸一化。

.applyQuaternion ( quaternion : Quaternion ) : this

將Quaternion變換應(yīng)用到該向量。

.angleTo ( v : Vector3 ) : Float

以弧度返回該向量與向量v之間的角度。

.ceil () : this

將該向量x分量、 y分量以及z分量向上取整為最接近的整數(shù)。

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

.clone () : Vector3

返回一個(gè)新的Vector3,其具有和當(dāng)前這個(gè)向量相同的x、y和z。

.copy ( v : Vector3 ) : this

將所傳入Vector3的x、y和z屬性復(fù)制給這一Vector3。

.cross ( v : Vector3 ) : this

將該向量設(shè)置為它本身與傳入的v的叉積(cross product)。

.crossVectors ( a : Vector3, b : Vector3 ) : this

將該向量設(shè)置為傳入的a與b的叉積(cross product)。

.distanceTo ( v : Vector3 ) : Float

計(jì)算該向量到所傳入的v間的距離。

.manhattanDistanceTo ( v : Vector3 ) : Float

計(jì)算該向量到所傳入的v之間的曼哈頓距離(Manhattan distance)。

.distanceToSquared ( v : Vector3 ) : Float

計(jì)算該向量到傳入的v的平方距離。 如果你只是將該距離和另一個(gè)距離進(jìn)行比較,則應(yīng)當(dāng)比較的是距離的平方, 因?yàn)樗挠?jì)算效率會更高一些。

.divide ( v : Vector3 ) : this

將該向量除以向量v。

.divideScalar ( s : Float ) : this

將該向量除以標(biāo)量s。

.dot ( v : Vector3 ) : Float

計(jì)算該vector和所傳入v的點(diǎn)積(dot product)。

.equals ( v : Vector3 ) : Boolean

檢查該向量和v的嚴(yán)格相等性。

.floor () : this

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

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

array - 來源矩陣。
offset - (可選)在數(shù)組中的元素偏移量,默認(rèn)值為0。

設(shè)置向量中的x值為array[ offset + 0 ],y值為array[ offset + 1 ], z值為array[ offset + 2 ]。

.fromBufferAttribute ( attribute : BufferAttribute, index : Integer ) : this

attribute - 來源的attribute。

index - 在attribute中的索引。

從attribute中設(shè)置向量的x值、y值和z值。

.getComponent ( index : Integer ) : Float

index - 0, 1 or 2.

如果index值為0返回x值。
如果index值為1返回y值。
如果index值為2返回z值。

.length () : Float

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

.manhattanLength () : Float

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

.lengthSq () : Float

計(jì)算從(0, 0, 0)到(x, y, z)的歐幾里得長度 (Euclidean length,即直線長度)的平方。 如果你正在比較向量的長度,應(yīng)當(dāng)比較的是長度的平方,因?yàn)樗挠?jì)算效率更高一些。

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

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

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

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

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

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

.max ( v : Vector3 ) : this

如果該向量的x值、y值或z值小于所傳入v的x值、y值或z值, 則將該值替換為對應(yīng)的最大值。

.min ( v : Vector3 ) : this

如果該向量的x值、y值或z值大于所傳入v的x值、y值或z值, 則將該值替換為對應(yīng)的最小值。

.multiply ( v : Vector3 ) : this

將該向量與所傳入的向量v進(jìn)行相乘。

.multiplyScalar ( s : Float ) : this

將該向量與所傳入的標(biāo)量s進(jìn)行相乘。

.multiplyVectors ( a : Vector3, b : Vector3 ) : this

按照分量順序,將該向量設(shè)置為和a * b相等。

.negate () : this

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

.normalize () : this

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

.project ( camera : Camera ) : this

camera — 在投影中使用的攝像機(jī)。

將此向量(坐標(biāo))從世界空間投影到相機(jī)的標(biāo)準(zhǔn)化設(shè)備坐標(biāo) (NDC) 空間。

.projectOnPlane ( planeNormal : Vector3 ) : this

planeNormal - 表示平面法線的向量

Projects 通過從該向量減去投影到平面法線上的向量,將該向量投影到平面上。

.projectOnVector ( v : Vector3 ) : this

投影(Projects)該向量到向量v上。

.reflect ( normal : Vector3 ) : this

normal - 反射面法線

將該向量設(shè)置為對指定 normal 法線的表面的反射向量。假設(shè)法線具有單位長度。

.round () : this

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

.roundToZero () : this

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

.set ( x : Float, y : Float, z : Float ) : this

設(shè)置該向量的x、y 和 z 分量。

.setComponent ( index : Integer, value : Float ) : this

index - 0、1 或 2。

value - Float

若index為 0 則設(shè)置 x 值為 value。

若index為 1 則設(shè)置 y 值為 value。

若index為 2 則設(shè)置 z 值為 value。

.setFromCylindrical ( c : Cylindrical ) : this

從圓柱坐標(biāo)c中設(shè)置該向量。

.setFromCylindricalCoords ( radius : Float, theta : Float, y : Float ) : this

從圓柱坐標(biāo)中的radius、theta和y設(shè)置該向量。

.setFromEuler ( euler : Euler ) : this

根據(jù)指定的Euler Angle的x、y、z分量來設(shè)置該向量的x、y、z分量。

.setFromMatrixColumn ( matrix : Matrix4, index : Integer ) : this

從傳入的四階矩陣matrix由index指定的列中, 設(shè)置該向量的x值、y值和z值。

.setFromMatrix3Column ( matrix : Matrix3, index : Integer ) : this

從傳入的三階矩陣 matrix 由 index 指定的列中,設(shè)置該向量的 x 值、y 值和 z 值。

.setFromMatrixPosition ( m : Matrix4 ) : this

從變換矩陣(transformation matrix)m中, 設(shè)置該向量為其中與位置相關(guān)的元素。

.setFromMatrixScale ( m : Matrix4 ) : this

從變換矩陣(transformation matrix)m中, 設(shè)置該向量為其中與縮放相關(guān)的元素。

.setFromSpherical ( s : Spherical ) : this

從球坐標(biāo)s中設(shè)置該向量。

.setFromSphericalCoords ( radius : Float, phi : Float, theta : Float ) : this

從球坐標(biāo)中的radius、phi和theta設(shè)置該向量。

.setLength ( l : Float ) : this

將該向量的方向設(shè)置為和原向量相同,但是長度(length)為l。

.setScalar ( scalar : Float ) : this

將該向量的x、y和z值同時(shí)設(shè)置為等于傳入的scalar。

.setX ( x : Float ) : this

將向量中的x值替換為x。

.setY ( y : Float ) : this

將向量中的y值替換為y。

.setZ ( z : Float ) : this

將向量中的z值替換為z。

.sub ( v : Vector3 ) : this

從該向量減去向量v。

.subScalar ( s : Float ) : this

從該向量的x、y和z中減去標(biāo)量s。

.subVectors ( a : Vector3, b : Vector3 ) : this

將該向量設(shè)置為a - b。

.toArray ( array : Array, offset : Integer ) : Array

array - (可選)被用于存儲向量的數(shù)組。如果這個(gè)值沒有傳入,則將創(chuàng)建一個(gè)新的數(shù)組。
offset - (可選) 數(shù)組中元素的偏移量。

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

.transformDirection ( m : Matrix4 ) : this

通過傳入的矩陣(m的左上角3 x 3子矩陣)變換向量的方向, 并將結(jié)果進(jìn)行normalizes(歸一化)。

.unproject ( camera : Camera ) : this

camera — 在投影中使用的攝像機(jī)。

將此向量(坐標(biāo))從相機(jī)的標(biāo)準(zhǔn)化設(shè)備坐標(biāo) (NDC) 空間投影到世界空間。

.random () : this

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

源代碼

src/math/Vector3.js


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

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號