Quaternion
Gói: @aspose/3d (v24.12.0)
Quaternion được sử dụng xuyên suốt trong @aspose/3d API để biểu diễn các phép quay mà không gặp hiện tượng gimbal lock. Đây là kiểu được trả về bởi Transform.rotation và GlobalTransform.rotation. Quaternion đồng nhất (1, 0, 0, 0) đại diện cho không có phép quay. Quaternion được xuất ra từ @aspose/3d/utilities đường dẫn phụ.
export class Quaternionimport { Quaternion } from '@aspose/3d/utilities';Enumerations
Xoay một nút 90° quanh trục Y và sau đó nội suy mượt mà trở lại trạng thái đơn vị.
import { Scene } from '@aspose/3d';
import { Quaternion } from '@aspose/3d/utilities';
const scene = new Scene();
const node = scene.rootNode.createChildNode('spinner');
// 90° around Y
const q90 = Quaternion.fromEulerAngle(0, Math.PI / 2, 0);
node.transform.rotation = q90;
// Slerp halfway back to identity
const identity = Quaternion.IDENTITY;
const half = Quaternion.slerp(0.5, q90, identity);
console.log(`Half rotation W: ${half.w.toFixed(4)}`);
// Half rotation W: 0.9239
Enumerations
| Name | Type | Access | Description |
|---|---|---|---|
w | number | Read/Write | Gets the w. |
x | number | Read/Write | Gets the x. |
y | number | Read/Write | Gets the y. |
z | number | Read/Write | Gets the z. |
length | number | Read | Gets the length. |
Enumerations
| Signature | Description |
|---|---|
constructor() | Initializes a quaternion with the given w, x, y, and z components |
constructor(w: number, x: number, y: number, z: number) | |
constructor(w: number, x: number, y: number, z: number) | |
IDENTITY() → Quaternion | Returns the identity quaternion representing no rotation |
normalize() → Quaternion | Returns a unit‑length version of this quaternion |
conjugate() → Quaternion | Returns the conjugate of the quaternion |
inverse() → Quaternion | Returns the multiplicative inverse of the quaternion |
dot(q: Quaternion) → number | Computes the dot product with another quaternion |
concat(rhs: Quaternion) → Quaternion | Returns the concatenation (multiplication) of this quaternion with another |
eulerAngles() → Vector3 | Returns a Vector3 of Euler angles representing the rotation |
fromEulerAngle(pitch: number, yaw: number, roll: number) → Quaternion | Overload accepts a Vector3 or numeric pitch with yaw and roll |
fromEulerAngle(vec: Vector3) → Quaternion | |
| `fromEulerAngle(pitch: Vector3 | number, yaw: number, roll: number) → Quaternion` |
fromAngleAxis(a: number, axis: Vector3) → Quaternion | Creates a quaternion from a rotation angle and axis vector |
fromRotation(orig: Vector3, dest: Vector3) → Quaternion | Builds a quaternion that rotates from one direction vector to another |
interpolate(t: number, fromQ: Quaternion, toQ: Quaternion) → Quaternion | Linearly interpolates between two quaternions using parameter t |
slerp(t: number, v1: Quaternion, v2: Quaternion) → Quaternion | Performs spherical linear interpolation between two quaternions at t |
toString() → string | Returns a string representation of the quaternion components |
equals(other: Quaternion) → boolean | Checks whether another quaternion has identical component values |
toMatrix() → Matrix4 | Converts the quaternion into a 4×4 rotation matrix |
Thuộc tính tĩnh
| Enumerations | Enumerations | Enumerations |
|---|---|---|
Quaternion.IDENTITY | Quaternion | Trả về một quaternion đơn vị mới (1, 0, 0, 0). |
Phương thức tĩnh
Quaternion.fromEulerAngle(pitch, yaw, roll)
Tạo một quaternion từ các góc Euler tính bằng radian.
static fromEulerAngle(pitch: number, yaw: number, roll: number): Quaternion
static fromEulerAngle(vec: Vector3): QuaternionEnumerations
pitch number — Quay quanh trục X tính bằng radian.
yaw number — Quay quanh trục Y tính bằng radian.
roll number — Quay quanh trục Z tính bằng radian.
Hoặc, truyền một giá trị duy nhất Vector3 của x, y, z chứa pitch, yaw và roll.
Enumerations
const q = Quaternion.fromEulerAngle(0, Math.PI / 4, 0); // 45° around Y
console.log(W: ${q.w.toFixed(4)}); // W: 0.9239
---
### Quaternion.fromAngleAxis(angle, axis)
Tạo một quaternion đại diện cho một phép quay của `angle` radian quanh `axis`.
```typescript
static fromAngleAxis(angle: number, axis: Vector3): QuaternionEnumerations
angle number — Góc quay tính bằng radian.
axis Vector3 — Trục quay. Không cần phải chuẩn hoá.
Enumerations
import { Quaternion, Vector3 } from '@aspose/3d/utilities';
const axis = new Vector3(0, 1, 0);
const q = Quaternion.fromAngleAxis(Math.PI / 2, axis);
console.log(`Rotating 90° around Y — W: ${q.w.toFixed(4)}`);
// Rotating 90° around Y — W: 0.7071
Quaternion.fromRotation(orig, dest)
Tạo quaternion cung ngắn nhất quay vector hướng orig đến dest.
static fromRotation(orig: Vector3, dest: Vector3): QuaternionQuaternion.slerp(t, v1, v2)
Thực hiện spherical linear interpolation giữa v1 và v2 tại tham số t (0.0 = v1, 1.0 = v2).
static slerp(t: number, v1: Quaternion, v2: Quaternion): QuaternionEnumerations
const start = Quaternion.fromEulerAngle(0, 0, 0);
const end = Quaternion.fromEulerAngle(0, Math.PI, 0);
const mid = Quaternion.slerp(0.5, start, end);
console.log(Mid W: ${mid.w.toFixed(4)}); // Mid W: 0.7071
---
### Quaternion.interpolate(t, fromQ, toQ)
Bí danh cho `slerp`. Thực hiện nội suy tuyến tính hình cầu.
```typescript
static interpolate(t: number, fromQ: Quaternion, toQ: Quaternion): QuaternionPhương thức đối tượng
normalize()
Trả về một quaternion mới được chuẩn hoá về độ dài đơn vị.
normalize(): Quaternionconjugate()
Trả về phần liên hợp (w, -x, -y, -z).
conjugate(): Quaternioninverse()
Trả về quaternion nghịch đảo. Ném lỗi nếu quaternion có độ dài bằng 0.
inverse(): Quaterniondot(q)
Trả về tích vô hướng với quaternion q.
dot(q: Quaternion): numberconcat(rhs)
Trả về tích quaternion this * rhs, đại diện cho phép quay kết hợp khi đầu tiên áp dụng this và sau đó rhs.
concat(rhs: Quaternion): QuaternioneulerAngles()
Trả về phép quay được biểu diễn dưới dạng các góc Euler tính bằng radian như một Vector3(pitch, yaw, roll).
eulerAngles(): Vector3Enumerations
const q = Quaternion.fromEulerAngle(0, Math.PI / 3, 0);
const euler = q.eulerAngles();
console.log(Yaw: ${euler.y.toFixed(4)});
// Yaw: 1.0472 (= π/3)
---
### toMatrix()
Chuyển đổi quaternion này thành một `Matrix4` ma trận quay.
```typescript
toMatrix(): Matrix4equals(other)
Enumerations true nếu tất cả bốn thành phần đều hoàn toàn bằng nhau.
equals(other: Quaternion): booleantoString()
Trả về một chuỗi ở dạng Quaternion(w, x, y, z).
toString(): string