three.js Quaternion

2023-02-16 17:46 更新

該類實(shí)現(xiàn)了 quaternion 。

四元數(shù)在three.js中用于表示 rotation (旋轉(zhuǎn))。

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

代碼示例

const quaternion = new THREE.Quaternion();
quaternion.setFromAxisAngle( new THREE.Vector3( 0, 1, 0 ), Math.PI / 2 );

const vector = new THREE.Vector3( 1, 0, 0 );
vector.applyQuaternion( quaternion );

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

Quaternion( x : Float, y : Float, z : Float, w : Float )

x - x 坐標(biāo)
y - y 坐標(biāo)
z - z 坐標(biāo)
w - w 坐標(biāo)

屬性

.isQuaternion : Boolean

只讀標(biāo)志,用于檢查給定對(duì)象是否為四元數(shù)類型。

.x : Float

.y : Float

.z : Float

.w : Float

方法

.angleTo ( q : Quaternion ) : Float

以弧度返回該四元數(shù)與四元數(shù) q 之間的夾角。

.clone () : Quaternion

創(chuàng)建一個(gè)與該四元數(shù)具有相同x、y、z和w 屬性的四元數(shù)。

.conjugate () : this

返回該四元數(shù)的旋轉(zhuǎn)共軛。 四元數(shù)的共軛表示的是,圍繞旋轉(zhuǎn)軸在相反方向上的相同旋轉(zhuǎn)。

.copy ( q : Quaternion ) : this

復(fù)制四元數(shù) q 的 x、y、z 和 w 屬性到該四元數(shù)中。

.equals ( v : Quaternion ) : Boolean

v - 用于進(jìn)行比較的四元數(shù)。

將四元數(shù) v 的 x、 y、 z 和 w 的屬性 與當(dāng)前四元數(shù)的對(duì)應(yīng)屬性相比較,以確定它們是否表示相同的旋轉(zhuǎn)。

.dot ( v : Quaternion ) : Float

計(jì)算四元數(shù) v 與當(dāng)前四元數(shù)的dot product(點(diǎn)積)。

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

array - 用于構(gòu)造四元數(shù)的形如(x, y, z, w)的數(shù)組。
offset - (可選)數(shù)組的偏移量。(譯者注:使用數(shù)組中從第offset元素算起的四個(gè)元素)

從一個(gè)數(shù)組來設(shè)置四元數(shù)的 x、 y、z 和 w 的屬性。

.identity () : this

設(shè)置該四元數(shù)為 identity 四元數(shù),即表示“不旋轉(zhuǎn)”的四元數(shù)。

.invert () : this

翻轉(zhuǎn)該四元數(shù) —— 計(jì)算 conjugate 。假定該四元數(shù)具有單位長(zhǎng)度。

.length () : Float

計(jì)算四元數(shù)的 Euclidean length (歐幾里得長(zhǎng)度,直線長(zhǎng)度),視為一個(gè)四維向量。

.lengthSq () : Float

計(jì)算四元數(shù) Euclidean length (歐幾里得長(zhǎng)度,直線長(zhǎng)度)的平方,視為一個(gè)四維向量。 如果要比較兩個(gè)四元數(shù)的長(zhǎng)度,這可能會(huì)十分有用, 因?yàn)檫@比 length() 的效率稍高一些。

.normalize () : this

Normalizes(歸一化)四元數(shù) —— 即計(jì)算與該四元數(shù)具有相同旋轉(zhuǎn)、但長(zhǎng)度為1的四元數(shù)。

.multiply ( q : Quaternion ) : this

將該四元數(shù)與q相乘。

.multiplyQuaternions ( a : Quaternion, b : Quaternion ) : this

將該四元數(shù)設(shè)為 a x b 。
改編自 here 所概述的方法。

.premultiply ( q : Quaternion ) : this

使用 q 乘以該四元數(shù)。

.rotateTowards ( q : Quaternion, step : Float ) : this

q - 目標(biāo)四元數(shù)
step - 以弧度為單位的角度步長(zhǎng)

將該四元數(shù)按照步長(zhǎng) step 向目標(biāo) q 進(jìn)行旋轉(zhuǎn)。該方法確保最終的四元數(shù)不會(huì)超過 q。

.slerp ( qb : Quaternion, t : Float ) : this

qb - 另一個(gè)四元數(shù)旋轉(zhuǎn)
t - 閉區(qū)間 [0, 1] 中的插值因子

處理四元數(shù)之間的球面線性插值。t 表示該四元數(shù)(其中 t 為 0) 和 qb (其中 t 為1) 之間的旋轉(zhuǎn)量。 該四元數(shù)會(huì)被設(shè)置為上述計(jì)算的結(jié)果。另請(qǐng)參閱下面 slerp 的靜態(tài)版本。

// rotate a mesh towards a target quaternion
	mesh.quaternion.slerp( endQuaternion, 0.01 );

.slerpQuaternions ( qa : Quaternion, qb : Quaternion, t : Float ) : this

在給定的四元數(shù)之間執(zhí)行球面線性插值,并將結(jié)果存儲(chǔ)在這個(gè)四元數(shù)中

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

設(shè)置該四元數(shù)的 x、y、z和w屬性。

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

從由 axis(軸) 和 angle(角度)所給定的旋轉(zhuǎn)來設(shè)置該四元數(shù)。
改編自 here 所述的方法。
假定Axis已被歸一化,angle以弧度來表示。

.setFromEuler ( euler : Euler ) : this

從由 Euler 角所給定的旋轉(zhuǎn)來設(shè)置該四元數(shù)。

.setFromRotationMatrix ( m : Matrix4 ) : this

從m的旋轉(zhuǎn)分量中來設(shè)置該四元數(shù)。
改編自 here 所概述的方法。

.setFromUnitVectors ( vFrom : Vector3, vTo : Vector3 ) : this

將該四元數(shù)設(shè)置為從方向向量 vFrom 旋轉(zhuǎn)到方向向量 vTo 所需的旋轉(zhuǎn)。
改編自方法 here。
假設(shè) vFrom 和 vTo 都已歸一化。

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

array - (可選)存儲(chǔ)該四元數(shù)的數(shù)組。若未指定該參數(shù),則將創(chuàng)建一個(gè)新數(shù)組。
offset - (可選)若指定了該值,結(jié)果將會(huì)被拷貝到該 Array。

在形如[x, y, z, w]的數(shù)組中,返回四元數(shù)中的數(shù)字元素。

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

attribute - 源 attribute。
index - attribute 中的索引。

從 attribute 中設(shè)置該四元數(shù)的x、 y、 z、 w屬性。

靜態(tài)方法

.slerpFlat ( dst : Array, dstOffset : Integer, src0 : Array, srcOffset0 : Integer, src1 : Array, srcOffset1 : Integer, t : Float ) : undefined

dst - 輸出數(shù)組
dstOffset - 輸出數(shù)組的偏移量
src0 - 起始四元數(shù)的源數(shù)組
srcOffset0 - 數(shù)組 src0 的偏移量
src1 - 目標(biāo)四元數(shù)的源數(shù)組
srcOffset1 - 數(shù)組 src1 的偏移量
t - 歸一化插值因子(介于 0 和 1 之間)

此 SLERP 實(shí)施假定四元數(shù)數(shù)據(jù)在平面數(shù)組中進(jìn)行管理。

.multiplyQuaternionsFlat ( dst : Array, dstOffset : Integer, src0 : Array, srcOffset0 : Integer, src1 : Array, srcOffset1 : Integer ) : Array

dst - 輸出數(shù)組。

dstOffset - 輸出數(shù)組的偏移量。

src0 - 起始四元數(shù)的源數(shù)組。

srcOffset0 - 數(shù)組 src0 的偏移量。

src1 - 目標(biāo)四元數(shù)的源數(shù)組。

srcOffset1 - 數(shù)組 src1 的偏移量。

此乘法實(shí)現(xiàn)假定四元數(shù)數(shù)據(jù)在平面數(shù)組中進(jìn)行管理。

源碼

src/math/Quaternion.js


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

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)